AWS Bedrock: Easy 3 Step Image Generation App Guide
Hello Everyone! Today we will learn how to create an image generation Ai based application in just 3 simple steps.
Today we will be taking help of AWS Bedrock, Langchain, StreamLit and python based AWS Boto3 library to create the application.
Follow along and you will understand how easy to os to create such amazing application using the AWS Bedrock service.
So lets get started and learn some GenAI stuff.
Prerequisite
In this example we will using the AWS Bedrock service. We will be using the models provided by the StabilityAI Before getting stated with the example you must request access for the StabilityAI models.
Also since the bedrock service is not available in all the AWS regions we will be using the us-east-1 region for this example.
Action Items
Access for the StabilityAI models in the us-east-1 region.
Generate and configure access keys to execute the below python script. You will need the Bedrock service access.
Lets create the application
Step 1 - Create the Backend
Create an image_lib.py and paste the below code in it.
import boto3 #import aws sdk and supporting libraries
import json
import base64
from io import BytesIO
session = boto3.Session()
bedrock = session.client(service_name='bedrock-runtime') #creates a Bedrock client
bedrock_model_id = "stability.stable-diffusion-xl-v1" #use the Stable Diffusion model
def get_response_image_from_payload(response): #returns the image bytes from the model response payload
payload = json.loads(response.get('body').read()) #load the response body into a json object
images = payload.get('artifacts') #extract the image artifacts
image_data = base64.b64decode(images[0].get('base64')) #decode image
return BytesIO(image_data) #return a BytesIO object for client app consumption
def get_image_response(prompt_content): #text-to-text client function
request_body = json.dumps({"text_prompts":
[ {"text": prompt_content } ], #prompts to use
"cfg_scale": 9, #how closely the model tries to match the prompt
"steps": 50, }) #number of diffusion steps to perform
response = bedrock.invoke_model(body=request_body, modelId=bedrock_model_id) #call the Bedrock endpoint
output = get_response_image_from_payload(response) #convert the response payload to a BytesIO object for the client to consume
return output
Step 2 - Create the Frontend
We will be creating the UI of the application with the help of StreamLit library.
Create an image_apCreate an image generation app with AWS Bedrock using LangChain, streamlit with Boto3, and use Python for GenAI.p.py and paste the below code in it.
import streamlit as st #all streamlit commands will be available through the "st" alias
import image_lib as glib #reference to local lib script
st.set_page_config(layout="wide", page_title="Image Generation") #set the page width wider to accommodate columns
st.title("Image Generation") #page title
col1, col2 = st.columns(2) #create 2 columns
with col1: #everything in this with block will be placed in column 1
st.subheader("Image generation prompt") #subhead for this column
prompt_text = st.text_area("Prompt text", height=200, label_visibility="collapsed") #display a multiline text box with no label
process_button = st.button("Run", type="primary") #display a primary button
with col2: #everything in this with block will be placed in column 2
st.subheader("Result") #subhead for this column
if process_button: #code in this if block will be run when the button is clicked
with st.spinner("Drawing..."): #show a spinner while the code in this with block runs
generated_image = glib.get_image_response(prompt_content=prompt_text) #call the model through the supporting library
st.image(generated_image) #display the generated image
Step 3 - Run the application
Open the terminal and paste the below command. A server will be running at port 8080. You can access the application by visiting the url - http://localhost:8080
streamlit run image_app.py --server.port 8080
Conclusion
In conclusion, creating an image generation app with AWS Bedrock in just three simple steps is both efficient and powerful.
By integrating LangChain, streamlining processes with Boto3, and leveraging the flexibility of Python, you can harness the full potential of GenAI. This approach not only simplifies development but also ensures robust and scalable solutions for your image generation needs.
Start building today and experience the transformative power of these cutting-edge technologies.
Subscribe to my newsletter
Read articles from CloudGags directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by