In this section, we will explore how to use the Amazon Bedrock API to generate images using Amazon Nova Canvas. We’ll complete three coding exercises to build image generation applications with the help of Amazon Q Developer.
Amazon Nova is the next generation of advanced foundation models (FMs) that offer cutting-edge intelligence and industry-leading price-performance, exclusively available on Amazon Bedrock.
You can use Amazon Nova to reduce cost and latency for nearly any generative AI task. It supports complex document and video analysis, diagram understanding, compelling video content creation, and sophisticated AI agents optimized for enterprise workloads.
Whether you’re developing document-processing applications that handle text and images, generating marketing content at scale, or building AI assistants that can understand and process visual information, Amazon Nova delivers the intelligence and flexibility you need through two model types:
Amazon Nova Canvas is a modern image-generation model that creates professional images from user-provided text or images. It also includes easy-to-use tools for image editing through text instructions, as well as controls to adjust color schemes and layouts.
This model includes built-in safety features for responsible AI usage, such as watermarking and content moderation.
We will begin by creating a Streamlit app that generates images from user prompts and predefined styles. Streamlit allows you to easily build interactive web apps in Python.
Build an app that allows users to:
The final app will look like this: Gen Image App
In your Visual Studio Code IDE, open the file:image_examples/image_gen_st.py
This file contains code for calling different models but is missing the interactive UI components. This is where Amazon Q Developer can help turn our ideas into executable code.
Since we already know the intended functionality of the app, ask Amazon Q to update the application with the following:
Can you add the following to my application:
1. A text input box to capture user prompts.
2. A standalone function that can turn a base64 string into an image.
3. A button to call `generate_image_nova`
4. The image return will be a base64 string; use the standalone function to convert it to an image that can be displayed in Streamlit.
Amazon Q Developer will process the request and provide updated code for the file.
To understand code functionality:
Q Developer will return a detailed explanation of the code.
To test your script:
streamlit run image_examples/image_gen_st.py
Click Open in Browser (or use the external URL).
If successful, you will see the application. Now create the image by entering the prompt, for example then click on the Button
a cat jumping into water
The complete code is available at: full_code/image_gen_st_full.py
For the next exercise, we will edit images using a prompt.
You can save one of the images you’ve created or use the image below for practice.
In this exercise, we will build an app that can update an image based on a user prompt.
️ First, open the file:
image_examples/image_to_image_st.py
This file already has some code to do image-to-image generation, but it’s missing some interactive components to make it fully functional.
This is where we can take advantage of inline code suggestions image_to_image_st.py
from Amazon Q Developer to generate the necessary code.
In image_to_image_st.py
, your task is to add comments at # TODO insert your comments
(line 148) that will help generate the following functionality:
Add a file uploader to upload an image
Get a user prompt to modify the image
Option + C
Alt + C
Need help? Try suggestions like those above.
Full reference code available at:
full_code/image_to_image_st_full.py
When you’re ready to test, run this command in your Terminal:
streamlit run image_examples/image_to_image_st.py
Stop it any time using CTRL + C
add a yellow mane to the cat
Make sure to click the button you created. Try experimenting with different prompts!
In this part, we’ll explore how to use Claude 3.5 Sonnet, Anthropic’s multimodal AI model, to:
We’ll also explore how to use the /dev feature from Amazon Q Developer to add new functionality.
️ Open the file:
image_examples/image_understanding_st.py
This file is missing a few interactive components. You need to:
/dev
in Amazon Q DeveloperIn the Amazon Q Developer chat window, type:
/dev
Once /dev
is bolded, request:
Can you update image_understanding_st.py to show the image in column 1, and then add a button to describe the image.
Amazon Q Developer will:
Full reference code available at:
full_code/image_understanding_st_full.py
In Terminal, run:
streamlit run image_examples/image_understanding_st.py
Stop with CTRL + C
Click the button you created and try with different images!
Now you know how to use Amazon Bedrock to generate and edit images.
Next, we’ll explore how to use this tool for text generation tasks!