Introduction:
Generative Artificial Intelligence (AI) has revolutionized the way we approach creative tasks, allowing machines to generate novel and realistic content. One compelling use case of Generative AI is the synthesis of images and text, a technology that finds applications in diverse fields, from content creation to marketing and design.
Generative AI in AWS:
Amazon Web Services (AWS) provides a robust set of tools and services for implementing Generative AI solutions. Leveraging AWS’s scalable infrastructure and machine learning services, we can seamlessly create and deploy models for generating images and text. The integration of AI services like Amazon SageMaker and Bedrock enhances the capability to build sophisticated generative models.
Key Components:
Bedrock:
- Bedrock is an AI services platform on AWS that simplifies the deployment and management of AI models. With Bedrock, we can easily invoke pre-trained models, such as text-to-image generators, reducing the complexity of our implementation.
Create a Lambda layer that uses the latest Boto3 version
Open the AWS Cloudshell
- Create a lib folder:
- Install the library to LIB_DIR:
- Zip all the dependencies to /tmp/boto3-mylayer.zip:
- Publish the layer:
aws lambda publish-layer-version –layer-name boto3-mylayer –zip-file fileb:///tmp/boto3-mylayer.zip
Now i Created the Lambda function name lambda-bedrock-image where i choose the role lambda-s3-dynamodb
To generate an image using AWS Lambda, you typically need to create a Lambda function and upload the code along with any required dependencies. AWS Lambda has a maximum timeout of 15 minutes, so set the time 14 min 0 sec
import boto3
import json
import base64
def lambda_handler(event, context):
if ‘prompt’ in event:
prompt = event[‘prompt’]
try:
# Initialize Bedrock client
bedrock = boto3.client(
service_name=’bedrock’,
region_name=’us-east-1′
)
bedrock_runtime = boto3.client(
service_name=’bedrock-runtime’,
region_name=’us-east-1′
)
#print(“bedrock_chk:”, bedrock)
# Specify the model ID for the text-to-image model
#model_id = ‘amazon.titan-image-generator-v1’ # Replace with your actual model ID
#print(“Module_id:”, model_id)
# Prepare the input parameters based on the provided payload
body = {
“taskType”: “TEXT_IMAGE”,
“textToImageParams”: {
“text”: prompt,
#”negativeText”: event.get(“negative_prompts”, “yakub”) # Optional negative prompts
},
“imageGenerationConfig”: {
“numberOfImages”: 1,
“quality”: “standard”,
“height”: 1024,
“width”: 1024,
“cfgScale”: 7.5,
“seed”: 42
}
}
#print(“Request Body:”, json.dumps(body, indent=2))
# Convert input parameters to JSON
input_json = json.dumps(body)
# Invoke the model with the prepared input parameters
response = bedrock_runtime.invoke_model(
body=input_json,
modelId=”amazon.titan-image-generator-v1″,
accept=”application/json”,
contentType=”application/json”
)
#print(“response:”,response)
# Process the image
response_body = json.loads(response.get(“body”).read())
img1_b64 = response_body[‘images’][0]
# Debug
print(f”Output: {img1_b64[0:80]}…”)
return {
‘statusCode’: 200,
‘body’: json.dumps({‘prompt’: prompt, ‘image_data’: img1_b64, ‘status’: ‘Image generated successfully’}),
}
except Exception as e:
print(f”Error generating image: {e}”)
return {
‘statusCode’: 500,
‘body’: json.dumps({‘error’: ‘Failed to generate image’}),
}
else:
return {
‘statusCode’: 400,
‘body’: json.dumps({‘error’: ‘Missing prompt in event payload’}),
‘headers’: {‘Content-Type’: ‘application/json’}
}
- Added the Event Json for the Lambda function as shown below
Output:
Response
{
“statusCode”: 200,
“body”:
“{\”prompt\”: \”A beautiful Mountain images\”,
\”image_data\”:iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAEAAElEQVR4nHT9TbIuy64jiAGgx7opySwb…
\”status\”: \”Image generated
successfully\”}
}
Create S3 bucket or Prefix to store the image output:
- First Create the Bucket in my case I created the Bucket name e.g. bedrock-image-store.
- Gave Full access to my role lambda-s3-dynamodb to store the images in my bucket.
- Now go to the S3 bucket go to the Permission tab and Block public access (bucket settings) Edit it and make it Block all public access Off
- Now Edit the Bucket Policy Add the Policy as shown below
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Sid”: “PublicReadGetObject”,
“Effect”: “Allow”,
“Principal”: “*”,
“Action”: “s3:GetObject”,
“Resource”: “arn:aws:s3:::bedrock-image-store/*”
}
]
}
*Changed the bucket name according to the name of the Bucket
- Go the the Bucket list and check the the Bucket is publicly accessible or not Publicly accessible
- Now the Updated code for stored the generated image into my S3 bucket
import boto3
import json
import base64
import os
import io
def lambda_handler(event, context):
if ‘prompt’ in event:
prompt = event[‘prompt’]
try:
# Initialize Bedrock client
bedrock = boto3.client(
service_name=’bedrock’,
region_name=’us-east-1′
)
bedrock_runtime = boto3.client(
service_name=’bedrock-runtime’,
region_name=’us-east-1′
)
#print(“bedrock_chk:”, bedrock)
# Specify the model ID for the text-to-image model
#model_id = ‘amazon.titan-image-generator-v1’ # Replace with your actual model ID
#print(“Module_id:”, model_id)
# Prepare the input parameters based on the provided payload
body = {
“taskType”: “TEXT_IMAGE”,
“textToImageParams”: {
“text”: prompt,
#”negativeText”: event.get(“negative_prompts”, “yakub”) # Optional negative prompts
},
“imageGenerationConfig”: {
“numberOfImages”: 1,
“quality”: “standard”,
“height”: 1024,
“width”: 1024,
“cfgScale”: 7.5,
“seed”: 42
}
}
#print(“Request Body:”, json.dumps(body, indent=2))
# Convert input parameters to JSON
input_json = json.dumps(body)
# Invoke the model with the prepared input parameters
response = bedrock_runtime.invoke_model(
body=input_json,
modelId=”amazon.titan-image-generator-v1″,
accept=”application/json”,
contentType=”application/json”
)
#print(“response:”,response)
# Process the image
response_body = json.loads(response.get(“body”).read())
img1_b64 = response_body[‘images’][0]
# Decode the image
img_data = base64.b64decode(img1_b64)
# Upload the image to S3
s3_bucket_name = ‘bedrock-image-store’
s3_object_key = f’data/titan/image_{context.aws_request_id}.png’ # Use a unique key for each image
print(“obj_data:”,s3_object_key)
s3_client = boto3.client(‘s3’)
print(“s3client_data:”,s3_client)
s3_client.put_object(Bucket=s3_bucket_name, Key=s3_object_key, Body=img_data)
# Debug
print(f”Output: {img1_b64[0:80]}…”)
return {
‘statusCode’: 200,
‘body’: json.dumps({‘prompt’: prompt, ‘s3_object_key’: s3_object_key, ‘status’: ‘Image generated and uploaded successfully’}),
}
except Exception as e:
print(f”Error generating image: {e}”)
return {
‘statusCode’: 500,
‘body’: json.dumps({‘error’: ‘Failed to generate image’}),
}
else:
return {
‘statusCode’: 400,
‘body’: json.dumps({‘error’: ‘Missing prompt in event payload’}),
‘headers’: {‘Content-Type’: ‘application/json’}
}
- Deploy the Function
- Then Test the code and shows the generated image in target S3 prefix
- Now click on any stored images that go to the Bucket Properties tab
- Now click on the Object Url that downloads the image in our local folder
Conclusion:
Building an image and text generation use case with Generative AI on AWS opens up a world of possibilities for creative applications. AWS’s powerful services, such as SageMaker and Bedrock, streamline the development and deployment process, making it accessible to developers and businesses alike. By following the steps we can generate the image from the text.





