Over 10 years we help companies reach their financial and branding goals. Engitech is a values-driven technology agency dedicated.

Gallery

Contacts

Bhubaneswar, India

info@krescitus.com

+1 -800-456-478-23

 

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:

Create a Lambda layer that uses the latest Boto3 version

Open the AWS Cloudshell

  1.   Create a lib folder:

 

  1.   Install the library to LIB_DIR:

 

  1.   Zip all the dependencies to /tmp/boto3-mylayer.zip:

 

  1.   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’}

}

 

 

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:

 

 

 

{

“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

 

 

 

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’}

}

 

 

 

 

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.