This content originally appeared on DEV Community and was authored by Muhammed Ashraf
No doubt that Generative AI is now being used by nearly everyone in the technology industry, it helps a lot of people daily with their work, life and almost everything,
Generative AI is type of AI that is used to generate contents such as texts, images and videos based on the data that the models trained on.
there are a lot of foundations models which they are already trained on data, these models are very powerful and trained on very big datasets, and they are available to use such as:
- Chat GPT
- Amazon Titan
- Stable Diffusion
- Claude
- Llama
Amazon Bedrock is a managed service that offers these foundation model to be used or to you can use some AWS bedrock to train these models with your own data and use it with other AWS services to build your application.
In this article we will building an API that invoke AWS Bedrock to generate images using SDXL 1.0 model
We will use the below services
- Lambda function which contain the logic and integrate with AWS Bedrock
- API Gateway which exposes the Lambda function
- AWS Bedrock with Stable Diffusion Model
- S3 bucket to store the images
High Level Design
Steps
- We will Create IAM role first for our lambda function with managed policy (S3FullAccess,BedrockFullAccess)
Go to IAM --> Select Roles --> Create Role and Select AWS Service and Lambda to be the use case --> Add the previous policies --> Lambda-execution-role-demo will be the name -->
Create
- We will create Lambda function with python runtime *Go to Lambda --> Create Function *
Make sure to modify the Lambda timeout since it will be 3 seconds by default
- Create S3 bucket with default configuration *Go to S3 Console --> Create Bucket --> Leave the default settings *
Make sure to enter a unique bucket name
This bucket will be used to store the generated images and will be used to generate a pre-signed URLs to have temporary access to these images
- Subscribe to Amazon Bedrock Stable Diffusion model Go to Bedrock Console --> Base Models under Foundation Models --> Request model access
Search for SDXL you should able to see it, mark it and press next then Submit
I am already subscribed to this model, so the configuration screen is different
You will have access after few minutes, after than you are ready to go
- Get the model ID since it will be used later in Lambda python code Go to Bedrock Console --> On the left select images under playground --> Select SDXL 1.0 and Press on the 3 dots
Select View API Request you will get a sample request to interact with this model as below
note down the model id stability.stable-diffusion-xl-v1
Code Breakdown
First you need to import the below packages
import json # for converting the payload into JSON
import boto3 # to interact with AWS services
import base64 # it will be used to encode the images
import datetime # will be used to interact with date and time functions
To interact with AWS services you will need to define client for the services
below we are going to interact with bedrock and S3 so we will have two clients
bedrock_client = boto3.client('bedrock-runtime')
s3_client = boto3.client('s3')
Then you will get the input from prompt text from the API gateway
input_text = event['prompt']
print(input_text) # for troubleshooting
Now you will need to configure the parameter for invoking the Stable Diffusion model, You can find the full parameters here
I used the below
payload = {
"text_prompts": [ #(Required) – An array of text prompts to use for generation.
{
"text": input_text # The prompt that you want to pass to the model.
}
],
"height": 1024, # (Optional) Height of the image to generate
"width": 1024, # (Optional) Width of the image to generate
"cfg_scale": 7, # (Optional) Determines how much the final image portrays the prompt.
"steps": 50, # (Optional) Generation step determines how many times the image is sampled
"style_preset": "photographic" # A style preset that guides the image model towards a particular style
}
# Now we are invoking the model
response = bedrock_client.invoke_model(
body=json.dumps(payload),
contentType='application/json', # The MIME type of the input data in the request.
accept='application/json', #The desired MIME type of the inference body in the response.
modelId='stability.stable-diffusion-xl-v1' #model ID
)
We will now work to extract the image from the response and we will use base64 to decode it
response_byte=json.loads(response['body'].read()) # store the response bytes
response_base64=response_byte['artifacts'][0]['base64'] #extract the artifacts into base 64
resposne_image=base64.b64decode(response_base64) # decode the image
image_name='images-'+ datetime.datetime.today().strftime('%Y-%M-%D-%H-%S') +'.jpg' # store the image name into date time format
Now we are going to put the generated image into S3 bucket and generate pre-signed URL
s3_response=s3_client.put_object(
Bucket='', #replace it with your bucket name
Body=resposne_image,
Key=image_name
)
generate_url=s3_client.generate_presigned_url('get_object', Params={'Bucket':'<add your bucket here>','Key':image_name}, ExpiresIn=3600)
return {
'statusCode': 200,
'body':generate_url
}
- Now to let's move to API Gateway, we are going to create REST API with any related name you want
We will create a resource with get method
** Create Resource --> Create Method with GET**
Select your Lambda Function and make sure that the API gateway has permission to invoke the Lambda function
Make sure to modify the method request settings & URL query string parameters to be as below
The value here should be similar to the one in the code
- Edit the mapping template in the integration request with the below values
- Now you are ready to Deploy your API and test it
You should now receive a pre-signed returned to the API Gateway
The generated image should be something like this
Hope you like the demo 😊, Leave like to keep me motivated ❤
References
Rahul Trisal Udemy Course *Recommended *
SDXL Parameters
Boto3 Invoke Model
This content originally appeared on DEV Community and was authored by Muhammed Ashraf
Muhammed Ashraf | Sciencx (2024-11-03T14:50:22+00:00) Using Stable Diffusion in AWS Bedrock to Generate Images. Retrieved from https://www.scien.cx/2024/11/03/using-stable-diffusion-in-aws-bedrock-to-generate-images/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.