Overview:
Amazon Bedrock is a fully managed service provided by AWS (Amazon Web Services) that allows developers to build and scale generative AI applications using foundation models (FMs) from leading AI companies. Launched in 2023, Amazon Bedrock gives developers access to a range of powerful foundation models, such as text generators, image creators, and more, from companies like Anthropic (Claude), Stability AI (Stable Diffusion), AI21 Labs (Jurassic-2), and Meta (Llama).
Diagram
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 1 Screenshot 2024 10 31 223129](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-223129.png)
Verify Bedrock Model Access.
STEP 1: Navigate the Amazon Bedrock and get started.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 2 Screenshot 2024 10 31 141856](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-141856.png)
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 3 Screenshot 2024 10 31 141913](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-141913.png)
STEP 2: Select the model access.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 4 Screenshot 2024 10 31 14193111](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-14193111.png)
STEP 3: Scroll Down to Stability AI model and verify the model access is granted.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 5 Screenshot 2024 10 31 142054](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-142054.png)
Create a SageMaker Notebook Instance
STEP 4: Make sure you are in the US East (N. Virginia) us-east-1 Region. From the Top Search bar search for Amazon SageMaker and navigate to SageMaker Service.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 6 Screenshot 2024 10 31 142146](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-142146.png)
STEP 5:Click the Notebook and Select the Notebook instance.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 7 Screenshot 2024 10 31 142223](https://jeevisoft.com/blogs/wp-content/uploads/2024/10/Screenshot-2024-10-31-142223.png)
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 8 Screenshot 2024 10 31 142249](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-142249.png)
STEP 6 : Enter the notebook instancename.
- Notebook instance type: ml.t2.medium
- Platform Identifier: Amazon Linux 2, Jupyter Lab 3
- For IAM role select: SageMakerInstanceRole
- Leave all the rest default.
- Click on Create Notebook instance button.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 9 Screenshot 2024 11 01 07533611](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-11-01-07533611.png)
STEP 7:Wait for the notebook instance status to update to ‘InService’, which may take approximately 5 minutes.
- Click on Open jupyter
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 10 Screenshot 2024 10 31 1429071111](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-1429071111.png)
STEP 8 : You will see this page.
- Click on new button and seclect the conda_python3.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 11 Screenshot 2024 10 31 143116](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143116.png)
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 12 Screenshot 2024 10 31 14312911](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-14312911.png)
STEP 9:Click on Files and rename button Enter the rename.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 13 Screenshot 2024 10 31 143351](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143351.png)
STEP 10: Paste the following code into the designated code cell in the Jupyter Notebook to generate an image using the Stable Diffusion model.
import base64
import os
import random
import boto3
import json
prompt_data = """
A high-red 4k HDR photo of a golden retriever puppy running on a beach.
Action shot, blue sky, white sand, and a big smile. Cinematic film quality.
"""
def main():
seed = random.randint(0, 100000)
generate_image(prompt=prompt_data, seed=seed, index=0)
def generate_image(prompt: str, seed: int, index: int):
payload = {
"text_prompts": [{"text": prompt}],
"cfg_scale": 12,
"seed": seed,
"steps": 80,
}
# Create the client and invoke the model.
bedrock = boto3.client(service_name="bedrock-runtime")
body = json.dumps(payload)
model_id = "stability.stable-diffusion-xl-v1"
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept="application/json",
contentType="application/json",
)
# Get the image from the response. It is base64 encoded.
response_body = json.loads(response.get("body").read())
artifact = response_body.get("artifacts")[0]
image_encoded = artifact.get("base64").encode("utf-8")
image_bytes = base64.b64decode(image_encoded)
# Save image to a file in the output directory.
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
file_name = f"{output_dir}/generated-{index}.png"
with open(file_name, "wb") as f:
f.write(image_bytes)
print("Image generated successfully")
if __name__ == "__main__":
main()
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 14 Screenshot 2024 10 31 143459](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143459.png)
STEP 11: Click on Run button.
- successful execution, you will receive the output message “Image generated successfully.”
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 15 Screenshot 2024 10 31 143605](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143605.png)
STEP 12 :Return to the root folder, where you will find a newly created folder named ‘output’.”
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 16 Screenshot 2024 10 31 14363811](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-14363811.png)
STEP 13 :Click on Output and select the generate.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 17 Screenshot 2024 10 31 143703](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143703.png)
STEP 14 :Click on the image to view it.
![Amazon Bedrock's New Text-to-Image Feature Powered by Stable Diffusion XL. 18 Screenshot 2024 10 31 143736](https://jeevisoft.com/blogs/wp-content/uploads/2024/11/Screenshot-2024-10-31-143736.png)
CONCLUSION
In conclusion, Amazon Bedrock’s integration of the Stable Diffusion XL model into its text-to-image capabilities marks a significant advancement in generative AI. This feature not only enhances creativity and content generation for developers and businesses but also showcases the potential for more sophisticated visual outputs. By leveraging cutting-edge technology, Amazon Bedrock empowers users to easily create high-quality images from text prompts, streamlining workflows and unlocking new possibilities in various industries.
Add a Comment