DEV Community

Cover image for Using Stable Diffusion in AWS Bedrock to Generate Images
Muhammed Ashraf
Muhammed Ashraf

Posted on

Using Stable Diffusion in AWS Bedrock to Generate Images

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

Image description

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 *

Image description

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 *

Image description

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

Image description

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

Image description

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

Image description

Select View API Request you will get a sample request to interact with this model as below

Image description

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
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

Then you will get the input from prompt text from the API gateway

  input_text = event['prompt']
  print(input_text) # for troubleshooting
Enter fullscreen mode Exit fullscreen mode

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
    )
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
    }

Enter fullscreen mode Exit fullscreen mode
  • Now to let's move to API Gateway, we are going to create REST API with any related name you want

Image description

We will create a resource with get method

** Create Resource --> Create Method with GET**

Image description

Select your Lambda Function and make sure that the API gateway has permission to invoke the Lambda function

Image description

Make sure to modify the method request settings & URL query string parameters to be as below

Image description

The value here should be similar to the one in the code

Image description

  • Edit the mapping template in the integration request with the below values

Image description

  • Now you are ready to Deploy your API and test it

Image description

Image description

You should now receive a pre-signed returned to the API Gateway

Image description

The generated image should be something like this

Image description

Hope you like the demo 😊, Leave like to keep me motivated ❤

References

Rahul Trisal Udemy Course *Recommended *
SDXL Parameters
Boto3 Invoke Model

Top comments (0)