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
to this generated image by Amazon Bedrock.
Follow me for more
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
- Create an IAM User with Amazon Bedrock Invoke Model Policy
- Create User Access Keys
- Configure AWS CLI
- Enable Amazon Bedrock Model Access
- Create Text Prompt
- Invoke Amazon Bedrock's Stable Diffusion XL Model
- Conclusion
Create an IAM User with Amazon Bedrock Invoke Model Policy
- From AWS Management Console search IAM
- Go to user menu and click Create User
- Specify user detail then click Next
- Create Policy
- Specify Permission
- Review and Create Policy
Create User Access Keys
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.
- Check list Stability AI model and click save changes
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
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
}
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
- Amazon Bedrock will output the image in base64 format into
bedrock-response.json
{"result":"success","artifacts":[{"seed":0,"base64":"iVBORw0KGgoAAA.......","finishReason":"SUCCESS"}]}
- So we need to filter out the base64 part from
bedrock-response.json
, then output it into a filebase64.txt
cat bedrock-response.json | jq -r ".artifacts[0].base64" > base64.txt
- Last, Decode
base64.txt
file intoimage-result.png
cat base64.txt | base64 -d > image-result.png
- You should get
image-result.png
file with this image
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.
Top comments (0)