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
Verify Bedrock Model Access.
STEP 1: Navigate the Amazon Bedrock and get started.
STEP 2: Select the model access.
STEP 3: Scroll Down to Stability AI model and verify the model access is granted.
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.
STEP 5:Click the Notebook and Select the Notebook instance.
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.
STEP 7:Wait for the notebook instance status to update to ‘InService’, which may take approximately 5 minutes.
- Click on Open jupyter
STEP 8 : You will see this page.
- Click on new button and seclect the conda_python3.
STEP 9:Click on Files and rename button Enter the rename.
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()
STEP 11: Click on Run button.
- successful execution, you will receive the output message “Image generated successfully.”
STEP 12 :Return to the root folder, where you will find a newly created folder named ‘output’.”
STEP 13 :Click on Output and select the generate.
STEP 14 :Click on the image to view it.
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