In today’s rapidly evolving AI landscape, Amazon Bedrock is a quick, powerful, and easy to use platform for leveraging foundation models. This blog post will guide you through creating a simple web solution to demonstrate Stability AI’s image generation capabilities using Amazon Bedrock.
The goal is to build a simple website that accepts user prompts and generates corresponding images. This demo demonstrates the potential of AI-driven image creation and provides hands-on experience together with actual code. There is no reliance on PowerPoint decks 🙂 .
Infrastructure Setup
For this demonstration, I will be running the web server on an Amazon EC2 instance running Amazon Linux 2. The instance will be equipped with Python 3, Flask for web framework functionality, and boto3 for AWS SDK interactions.
A second windows EC2 instance located in the same VPC and subnet will be used to access the web page. The security group of the Linux server should allow connections from the windows server on port 5000.
Accessing Stability AI Model
Before executing the code, you must request access to the Stability AI model via the Amazon Bedrock console. This step allows the EC2 instance to execute the Stability AI model.
IAM Policy Configuration
To allow our EC2 instance to access the Stability AI model, we need to attach the below IAM policy:
{ "Effect": "Allow", "Action": [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream" ], "Resource": [ "arn:aws:bedrock:us-east-1::foundation-model/stability.stable-diffusion-xl-v1" ] }
This can be added as an inline policy to the IAM role used by your EC2 instance.
Upload the python code (see download link at bottom of page) to a file named my_ai_web.py
and start the web server with the command python ./my_ai_web.py
as shown in the below image.
Sign on to the windows server and navigate to the second ip displayed in the above image. The following web page should be displayed:
Enter a prompt and click on the “Generate Image” button as shown below:
At this point the code makes a call to bedrock to process the prompt and generate the image:
After the image is generated, it is displayed as shown below: