Skip to content

Commit

Permalink
feat(imagegeneration): renamed construct to contentgenerationappsyncl…
Browse files Browse the repository at this point in the history
…ambda
  • Loading branch information
Dinesh Sajwan committed Feb 21, 2024
1 parent 45865fb commit 53229d6
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 314 deletions.
4 changes: 2 additions & 2 deletions apidocs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
### Classes

- [ContainerImage](classes/ContainerImage.md)
- [ContentGenerationAppsyncLambda](classes/ContentGenerationAppsyncLambda.md)
- [CustomSageMakerEndpoint](classes/CustomSageMakerEndpoint.md)
- [DeepLearningContainerImage](classes/DeepLearningContainerImage.md)
- [HuggingFaceSageMakerEndpoint](classes/HuggingFaceSageMakerEndpoint.md)
- [ImageGenerationAppsyncLambda](classes/ImageGenerationAppsyncLambda.md)
- [JumpStartModel](classes/JumpStartModel.md)
- [JumpStartSageMakerEndpoint](classes/JumpStartSageMakerEndpoint.md)
- [LangchainCommonDepsLayer](classes/LangchainCommonDepsLayer.md)
Expand All @@ -31,12 +31,12 @@

- [AdapterProps](interfaces/AdapterProps.md)
- [ContainerImageConfig](interfaces/ContainerImageConfig.md)
- [ContentGenerationAppsyncLambdaProps](interfaces/ContentGenerationAppsyncLambdaProps.md)
- [CustomSageMakerEndpointProps](interfaces/CustomSageMakerEndpointProps.md)
- [HuggingFaceSageMakerEndpointProps](interfaces/HuggingFaceSageMakerEndpointProps.md)
- [IInstanceAliase](interfaces/IInstanceAliase.md)
- [IInstanceValiant](interfaces/IInstanceValiant.md)
- [IJumpStartModelSpec](interfaces/IJumpStartModelSpec.md)
- [ImageGenerationAppsyncLambdaProps](interfaces/ImageGenerationAppsyncLambdaProps.md)
- [JumpStartSageMakerEndpointProps](interfaces/JumpStartSageMakerEndpointProps.md)
- [LangchainLayerProps](interfaces/LangchainLayerProps.md)
- [LangchainProps](interfaces/LangchainProps.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
[@cdklabs/generative-ai-cdk-constructs](../README.md) / ImageGenerationAppsyncLambda
[@cdklabs/generative-ai-cdk-constructs](../README.md) / ContentGenerationAppsyncLambda

# Class: ImageGenerationAppsyncLambda
# Class: ContentGenerationAppsyncLambda

**`Summary`**

The QaAppsyncOpensearch class.
The ContentGenerationAppsyncLambda class.

## Hierarchy

- `Construct`

**`ImageGenerationAppsyncLambda`**
**`ContentGenerationAppsyncLambda`**

## Table of contents

### Constructors

- [constructor](ImageGenerationAppsyncLambda.md#constructor)
- [constructor](ContentGenerationAppsyncLambda.md#constructor)

### Properties

- [generatedImageBus](ImageGenerationAppsyncLambda.md#generatedimagebus)
- [graphqlApi](ImageGenerationAppsyncLambda.md#graphqlapi)
- [node](ImageGenerationAppsyncLambda.md#node)
- [s3GenerateAssetsBucket](ImageGenerationAppsyncLambda.md#s3generateassetsbucket)
- [s3GenerateAssetsBucketInterface](ImageGenerationAppsyncLambda.md#s3generateassetsbucketinterface)
- [securityGroup](ImageGenerationAppsyncLambda.md#securitygroup)
- [vpc](ImageGenerationAppsyncLambda.md#vpc)
- [generatedImageBus](ContentGenerationAppsyncLambda.md#generatedimagebus)
- [graphqlApi](ContentGenerationAppsyncLambda.md#graphqlapi)
- [node](ContentGenerationAppsyncLambda.md#node)
- [s3GenerateAssetsBucket](ContentGenerationAppsyncLambda.md#s3generateassetsbucket)
- [s3GenerateAssetsBucketInterface](ContentGenerationAppsyncLambda.md#s3generateassetsbucketinterface)
- [securityGroup](ContentGenerationAppsyncLambda.md#securitygroup)
- [vpc](ContentGenerationAppsyncLambda.md#vpc)

### Methods

- [toString](ImageGenerationAppsyncLambda.md#tostring)
- [isConstruct](ImageGenerationAppsyncLambda.md#isconstruct)
- [toString](ContentGenerationAppsyncLambda.md#tostring)
- [isConstruct](ContentGenerationAppsyncLambda.md#isconstruct)

## Constructors

### constructor

**new ImageGenerationAppsyncLambda**(`scope`, `id`, `props`): [`ImageGenerationAppsyncLambda`](ImageGenerationAppsyncLambda.md)
**new ContentGenerationAppsyncLambda**(`scope`, `id`, `props`): [`ContentGenerationAppsyncLambda`](ContentGenerationAppsyncLambda.md)

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `scope` | `Construct` | represents the scope for all the resources. |
| `id` | `string` | this is a a scope-unique id. |
| `props` | [`ImageGenerationAppsyncLambdaProps`](../interfaces/ImageGenerationAppsyncLambdaProps.md) | user provided props for the construct. |
| `props` | [`ContentGenerationAppsyncLambdaProps`](../interfaces/ContentGenerationAppsyncLambdaProps.md) | user provided props for the construct. |

#### Returns

[`ImageGenerationAppsyncLambda`](ImageGenerationAppsyncLambda.md)
[`ContentGenerationAppsyncLambda`](ContentGenerationAppsyncLambda.md)

**`Summary`**

Constructs a new instance of the ImageGenerationAppsyncLambda class.
Constructs a new instance of the ContentGenerationAppsyncLambda class.

**`Since`**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
[@cdklabs/generative-ai-cdk-constructs](../README.md) / ImageGenerationAppsyncLambdaProps
[@cdklabs/generative-ai-cdk-constructs](../README.md) / ContentGenerationAppsyncLambdaProps

# Interface: ImageGenerationAppsyncLambdaProps
# Interface: ContentGenerationAppsyncLambdaProps

The properties for the ImageGenerationAppsyncLambdaProps class.
The properties for the ContentGenerationAppsyncLambdaProps class.

## Table of contents

### Properties

- [cognitoUserPool](ImageGenerationAppsyncLambdaProps.md#cognitouserpool)
- [enableOperationalMetric](ImageGenerationAppsyncLambdaProps.md#enableoperationalmetric)
- [existingBusInterface](ImageGenerationAppsyncLambdaProps.md#existingbusinterface)
- [existingGeneratedAssetsBucketObj](ImageGenerationAppsyncLambdaProps.md#existinggeneratedassetsbucketobj)
- [existingMergedApi](ImageGenerationAppsyncLambdaProps.md#existingmergedapi)
- [existingSecurityGroup](ImageGenerationAppsyncLambdaProps.md#existingsecuritygroup)
- [existingVpc](ImageGenerationAppsyncLambdaProps.md#existingvpc)
- [generatedAssetsBucketProps](ImageGenerationAppsyncLambdaProps.md#generatedassetsbucketprops)
- [lambdaProvisionedConcurrency](ImageGenerationAppsyncLambdaProps.md#lambdaprovisionedconcurrency)
- [observability](ImageGenerationAppsyncLambdaProps.md#observability)
- [stage](ImageGenerationAppsyncLambdaProps.md#stage)
- [vpcProps](ImageGenerationAppsyncLambdaProps.md#vpcprops)
- [cognitoUserPool](ContentGenerationAppsyncLambdaProps.md#cognitouserpool)
- [enableOperationalMetric](ContentGenerationAppsyncLambdaProps.md#enableoperationalmetric)
- [existingBusInterface](ContentGenerationAppsyncLambdaProps.md#existingbusinterface)
- [existingGeneratedAssetsBucketObj](ContentGenerationAppsyncLambdaProps.md#existinggeneratedassetsbucketobj)
- [existingMergedApi](ContentGenerationAppsyncLambdaProps.md#existingmergedapi)
- [existingSecurityGroup](ContentGenerationAppsyncLambdaProps.md#existingsecuritygroup)
- [existingVpc](ContentGenerationAppsyncLambdaProps.md#existingvpc)
- [generatedAssetsBucketProps](ContentGenerationAppsyncLambdaProps.md#generatedassetsbucketprops)
- [lambdaProvisionedConcurrency](ContentGenerationAppsyncLambdaProps.md#lambdaprovisionedconcurrency)
- [observability](ContentGenerationAppsyncLambdaProps.md#observability)
- [stage](ContentGenerationAppsyncLambdaProps.md#stage)
- [vpcProps](ContentGenerationAppsyncLambdaProps.md#vpcprops)

## Properties

Expand Down
2 changes: 1 addition & 1 deletion docs/generative_ai_cdk_constructs.drawio
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
</root>
</mxGraphModel>
</diagram>
<diagram id="zwjm-m0tJrHEoboqtmw_" name="aws-imagegen-appsync-lambda">
<diagram id="zwjm-m0tJrHEoboqtmw_" name="aws-contentgen-appsync-lambda">
<mxGraphModel dx="3134" dy="795" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
metrics = Metrics(namespace="image_generation", service="IMAGE_GENERATION")


ENDPOINT_ARN=os.environ['COMPREHEND_ENDPOINT_ARN']

s3 = boto3.resource('s3')
aws_region = boto3.Session().region_name
credentials = boto3.Session().get_credentials()
aws_auth_appsync = AWS4Auth(
Expand All @@ -24,10 +23,10 @@
class image_generator():
"""Generate image ."""

def __init__(self,input_text,image_name, rekognition_client,comprehend_client,bedrock_client,bucket):
def __init__(self,input_text,file_name, rekognition_client,comprehend_client,bedrock_client,bucket):
"""Initialize with bucket , key and rekognition_client."""

self.image_name = image_name
self.file_name = file_name
self.rekognition_client = rekognition_client
self.comprehend_client = comprehend_client
self.input_text =input_text
Expand All @@ -41,16 +40,15 @@ def upload_file_to_s3(self,imgbase64encoded):

"""Upload generated file to S3 bucket"""

s3 = boto3.resource('s3')
logger.info(f"uploading file to s3 bucket: {self.bucket}, key: {self.image_name}")
logger.info(f"uploading file to s3 bucket: {self.bucket}, key: {self.file_name}")
try:
respImg= s3.Object(self.bucket, self.image_name).put(Body=base64.b64decode(imgbase64encoded))
respImg= s3.Object(self.bucket, self.file_name).put(Body=base64.b64decode(imgbase64encoded))

except Exception as e:
logger.error(f"Error occured :: {e}")
return False
return {
"image_name":self.image_name,
"file_name":self.file_name,
"bucket_name":self.bucket,
}

Expand All @@ -60,20 +58,26 @@ def text_moderation(self):
"""Check input text has any toxicity or not. The comprehend is trained
with specific set of data set for toxicity.
"""
textModerationClassesResponse = self.comprehend_client.classify_document(
Text= self.input_text,
EndpointArn=ENDPOINT_ARN
textModerationClassesResponse = self.comprehend_client.detect_toxic_content(
TextSegments= [{'Text': self.input_text}],
LanguageCode='en',

)
response={
"isToxic":False,
"confidence":0
}
logger.info(f"text moderation :: {textModerationClassesResponse}")
for label in textModerationClassesResponse["Classes"]:
if(label['Name']=='Toxic' and label['Score'] > 0.50):
response['isToxic']=True
response['confidence']=label['Score']
break

for label in textModerationClassesResponse["ResultList"]:
logger.info(f"label :: {label}")
if(label['Toxicity'] > 0.50):
logger.info(" toxicity score is greater than 0.50")
response['isToxic']=True
else:
logger.info(f"Toxicity :: {label['Toxicity']}")
logger.info(" No toxicity in entered text")

return response

@tracer.capture_method
Expand All @@ -90,7 +94,7 @@ def image_moderation(self):
Image={
'S3Object':{
'Bucket':self.bucket,
'Name':self.image_name}
'Name':self.file_name}
}
)
for label in rekognition_response['ModerationLabels']:
Expand All @@ -102,56 +106,53 @@ def image_moderation(self):

return response

@tracer.capture_method
def generate_image(self,input_params):

"""Generate image using Using bedrock with configured modelid"""


input_text=input_params['input_text']
input_text=self.input_text
model_id=input_params['model_config']['modelId']
cfg_scale=input_params['model_config']['model_kwargs']['cfg_scale']
cfg_scale=input_params['model_config']['model_kwargs']['cfg_scale']
seed=input_params['model_config']['model_kwargs']['seed']
steps=input_params['model_config']['model_kwargs']['steps']

promptTemplate="{\"text_prompts\":[{\"text\":\"$input_text\\n\"}],\"cfg_scale\":$cfg_scale,\"seed\":$seed,\"steps\":$steps}"

prompt=promptTemplate.replace("$input_text", input_text).replace("$cfg_scale", str(cfg_scale)).replace("$seed", str(seed)).replace("$steps", str(steps))

try:
return self.bedrock_client.invoke_model(
modelId= model_id,
contentType= "application/json",
accept= "application/json",
body=prompt
)
except Exception as e:
logger.error(f"Error occured during generating image:: {e}")
return ''

logger.info(f" prompt :: {prompt}")

return self.bedrock_client.invoke_model(
modelId= model_id,
contentType= "application/json",
accept= "application/json",
body=prompt
)

@tracer.capture_method
def send_job_status(self,variables):

logger.info(f"send status variables :: {variables}")
query ="""mutation updateGenerateImageStatus
{updateGenerateImageStatus
(status: \"$status\",
jobid: $jobid,
bucket :\"$bucket\",
filename: \"$filename\",
input_text: \"$input_text\",message: \"$message\" )
{
status,
query ="""
mutation updateGenerateImageStatus {
updateGenerateImageStatus (status: \"$status\", jobid: \"$jobid\", image_path: \"$image_path\", input_text: \"$input_text\", filename: \"$filename\", message: \"$message\")
{
status,
jobid,
bucket,
filename,input_text,
image_path,
filename,
input_text,
message
}
}
}
}
"""

query = query.replace("$status", variables['status'])
query = query.replace("$jobid", str(variables['jobid']))
query = query.replace("$bucket", str(variables['bucket']))
query = query.replace("$image_path", str(variables['image_path']))
query = query.replace("$filename", str(variables['filename']))
query = query.replace("$input_text", str(variables['input_text']))
query = query.replace("$message", str(variables['message']))
Expand Down
Loading

0 comments on commit 53229d6

Please sign in to comment.