DEV Community

Cover image for Generate Image from Text Using Amazon Bedrock's Stable Diffusion XL Model via AWS CLI
Jimmy for AWS Community Builders

Posted on • Edited on

Generate Image from Text Using Amazon Bedrock's Stable Diffusion XL Model via AWS CLI

In this article, we will learn another way to invoke Amazon Bedrock's Model using AWS Command Line Interface (CLI).

We are going to generate an image from a text prompt using Amazon Bedrock's Stable DIffusion XL Model via AWS CLI.

From this text prompt

the quick brown fox jumps over the lazy dog
Enter fullscreen mode Exit fullscreen mode

to this generated image by Amazon Bedrock.

the quick brown fox jumps over the lazy dog

Follow me for more

fullstacksaiyan; | Twitter, Instagram, Facebook, TikTok | Linktree

Share about Web, Mobile, Backend, Frontend, and Cloud Computing.

favicon linktr.ee

Amazon Bedrock

Amazon Bedrock is a fully managed service from Amazon Web Services that provides access to foundation models from Amazon and select third-party providers through a simple API. It allows customers to easily choose from a wide range of foundation models to find the one best suited for their use case. With Amazon Bedrock, customers can quickly get started with these models, privately customize them using their own data, and integrate them into applications. The service handles all infrastructure management so customers can focus on using the models without having to worry about deployment or maintenance. Amazon Bedrock also integrates with other Amazon Web Services like Amazon SageMaker for capabilities such as model testing and lifecycle management.

Prerequisites Tools

Table of Contents

  1. Create an IAM User with Amazon Bedrock Invoke Model Policy
  2. Create User Access Keys
  3. Configure AWS CLI
  4. Enable Amazon Bedrock Model Access
  5. Create Text Prompt
  6. Invoke Amazon Bedrock's Stable Diffusion XL Model
  7. Conclusion

Create an IAM User with Amazon Bedrock Invoke Model Policy

  • From AWS Management Console search IAM iam
  • Go to user menu and click Create User create user
  • Specify user detail then click Next user detail
  • Create Policy create policy
  • Specify Permission specify permission
  • Review and Create Policy create policy

Create User Access Keys

  • Go to Security Credentials
    security credentials

  • Choose the access key use case
    access key

  • Set Descriptions
    descriptions

  • Copy Access Key and Secret access key or you can download in a csv format then click done
    access key secret key

Enable Amazon Bedrock Model Access

  • Go to Amazon Bedrock in AWS Management Console (make sure you are in N.Virginia (us-east-1) region.
  • Go to Model access menu then click Edit.

model access

  • Check list Stability AI model and click save changes

check list stability AI model

Configure AWS CLI

Configure AWS CLI with the Access Key and Secret Access key that you created before using aws configure command in your terminal.

$ aws configure
AWS Access Key ID [****************T7JE]: xxxxxx
AWS Secret Access Key [****************153Y]: xxxxx
Default region name [ap-southeast-1]: us-east-1
Default output format [json]: json
Enter fullscreen mode Exit fullscreen mode

Create Text Prompt

We will create a file body.json that contains a text prompt and some model controls that we are going to use to invoke Amazon Bedrock Model.

{
  "text_prompts": [{ "text": "the quick brown fox jumps over the lazy dog" }],
  "cfg_scale": 10,
  "seed": 0,
  "steps": 50
}
Enter fullscreen mode Exit fullscreen mode

The Stability.ai Diffusion models support the following controls.

Prompt strength (cfg_scale) – Determines how much the final image portrays the prompt. Use a lower number to increase randomness in the generation.

Generation step (steps) – Generation step determines how many times the image is sampled. More steps can result in a more accurate result.

Seed (seed) – The seed determines the initial noise setting. Use the same seed and the same settings as a previous run to allow inference to create a similar image. If you don't set this value, it is set as a random number.

Invoke Amazon Bedrock's Stable Diffusion XL Model

  • Invoke Amazon Bedrock model using command below and then output it into bedrock-response.json
aws bedrock-runtime invoke-model \
--body "$(base64 -i body.json)" \
--model-id "stability.stable-diffusion-xl-v0" \
--region us-east-1
bedrock-response.json 
Enter fullscreen mode Exit fullscreen mode
  • Amazon Bedrock will output the image in base64 format into bedrock-response.json
{"result":"success","artifacts":[{"seed":0,"base64":"iVBORw0KGgoAAA.......","finishReason":"SUCCESS"}]}
Enter fullscreen mode Exit fullscreen mode
  • So we need to filter out the base64 part from bedrock-response.json, then output it into a file base64.txt
cat bedrock-response.json | jq -r ".artifacts[0].base64" > base64.txt
Enter fullscreen mode Exit fullscreen mode
  • Last, Decode base64.txt file into image-result.png
cat base64.txt | base64 -d > image-result.png
Enter fullscreen mode Exit fullscreen mode
  • You should get image-result.png file with this image the quick brown fox jumps over the lazy dog

Conclusion

From this article, we learn that we have the option to utilize the Amazon Bedrock model not only through the AWS-SDK but also directly via AWS-CLI from our terminal.

Check out my previous post

Top comments (0)