AWS BedRock - Boto3 Demo - Stability AI

Search for a command to run...

No comments yet. Be the first to comment.
This series will feature a blog with code snippets. Spend 5-10 minutes in your environment to explore AWS Serverless Generative AI Platform - Bedrock.
An generative AI-based system that enables to dynamically adapt its fraud detection models to evolving fraud patterns
A field guide for anyone shipping agentic AI into a regulated lending or servicing environment — why the model is the smallest part of the system, and what to engineer around it instead. The Skill Ha

Somewhere in your organisation right now, a dashboard is showing a healthy deployment frequency, a stable change failure rate, and a throughput line that ticks gently upward. Leadership is satisfied.

Every organization with procedure-driven back-office operations faces the same invisible problem: knowledge lives in three disconnected silos — training videos, static documents, and SME expertise — w

Part 2 of 2 | DataOps Labs Series: Living Knowledge Systems on AWS

You've tuned your embedding model. You've benchmarked retrieval algorithms. You've swapped LLMs. And your RAG system still gets the wrong answer. Here's what nobody tells you upfront: the bottleneck i

SDXL, a high-quality image generation model, excels in open photorealism, allowing diverse art styles without imposing a specific 'feel.' SDXL 1.0 emphasizes vibrant, accurate colors with improved contrast, lighting, and shadows in native 1024x1024 resolution. Notably, it tackles challenging concepts like hands, text, and spatial arrangements. Developed by Stability AI, a leading open-source generative AI company, SDXL finds applications in artwork generation, creative tooling, and education. This diffusion-based text-to-image model, version 1.0, showcases stability and proficiency, generating detailed images based on text descriptions, and supporting tasks like inpainting, outpainting, and image-to-image translations with a maximum token limit of 77.
Blog 1: https://www.dataopslabs.com/p/aws-bedrock-learning-series-blog
Blog 2: https://www.dataopslabs.com/p/family-of-titan-text-models-cli-demo
Blog 3: https://www.dataopslabs.com/p/family-of-titan-text-models-boto3
Blog 4: https://blog.dataopslabs.com/aws-bedrock-boto3-demo-anthropic-claude
Blog 5: https://blog.dataopslabs.com/aws-bedrock-boto3-demo-ai21-labs
Blog 6: https://blog.dataopslabs.com/aws-bedrock-boto3-cohere-model
Blog 7: https://blog.dataopslabs.com/aws-bedrock-boto3-demo-llama2-model
https://github.com/jayyanar/learning-aws-bedrock/blob/main/blog8/Bedrock_stability_Boto3.ipynb
I am using vscode local environment with AWS Credential configured.
! python --version
Python 3.11.5
! pip install --upgrade pip
! pip install --no-build-isolation --force-reinstall \
"boto3>=1.33.6" \
"awscli>=1.31.6" \
"botocore>=1.33.6"
import json
import os
import sys
import boto3
import botocore
bedrock = boto3.client(service_name="bedrock")
bedrock_runtime = boto3.client(service_name="bedrock-runtime")
stability_image_prompt = "Skiing on Alps Mountain with my Golden Retreiver Dog"
negative_prompts = [
"poorly rendered",
"poor background details"
]
style_preset = "photographic" # (e.g. photographic, digital-art, cinematic, ...)
clip_guidance_preset = "FAST_GREEN" # (e.g. FAST_BLUE FAST_GREEN NONE SIMPLE SLOW SLOWER SLOWEST)
sampler = "K_DPMPP_2S_ANCESTRAL" # (e.g. DDIM, DDPM, K_DPMPP_SDE, K_DPMPP_2M, K_DPMPP_2S_ANCESTRAL, K_DPM_2, K_DPM_2_ANCESTRAL, K_EULER, K_EULER_ANCESTRAL, K_HEUN, K_LMS)
width = 768
request = json.dumps({
"text_prompts": (
[{"text": stability_image_prompt, "weight": 1.0}]
+ [{"text": negprompt, "weight": -1.0} for negprompt in negative_prompts]
),
"cfg_scale": 5,
"seed": 452345,
"steps": 60,
"style_preset": style_preset,
"clip_guidance_preset": clip_guidance_preset,
"sampler": sampler,
"width": width,
})
response = bedrock_runtime.invoke_model(
body=request,
modelId="stability.stable-diffusion-xl-v1",
)
response_body = json.loads(response.get("body").read())
print(response_body["result"])
base_64_img_str = response_body["artifacts"][0].get("base64")
print(f"{base_64_img_str[0:80]}...")
# Python Built-Ins:
import base64
import io
import json
import os
import sys
# Ensure the "data" directory exists, or create it if it doesn't
os.makedirs("data", exist_ok=True)
# Assuming `base_64_img_str` contains a base64-encoded image string
# Decode the base64 image string and create a PIL Image
stability_imageout = Image.open(io.BytesIO(base64.decodebytes(bytes(base_64_img_str, "utf-8"))))
# Save the PIL Image as a PNG file in the "data" directory
stability_imageout.save("data/alps_img.png")
# Display the saved image (optional, not required for saving)
stability_imageout

DreamStudio introduces a user-friendly interface designed for crafting images through the cutting-edge Stable Diffusion image generation model. This model, known for its speed and efficiency, seamlessly translates text into captivating images, demonstrating a profound understanding of the intricate relationships between words and visual elements. In this tutorial notebook, we will guide you through the process of setting up the Stability SDK package, providing you with the tools to harness the power of our API for fundamental inference calls. Explore the potential of image creation and unleash your creativity with DreamStudio and the Stable Diffusion model.
Prerequisites - https://github.com/jayyanar/genai-apps/blob/main/prerequisites.md#4-create-dreamstudio-account
Code to Play
https://github.com/jayyanar/genai-apps/blob/main/lab3_gen_images.ipynb