Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions image_examples/image_gen_st.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,38 @@ def generate_image_titan(text):


model = st.selectbox("Select model", ["Amazon Titan", "Stable Diffusion"])
prompt = st.text_input("Enter prompt")
# List of Stable Diffusion Preset Styles
sd_presets = [
"None", "3d-model", "analog-film", "anime", "cinematic", "comic-book",
"digital-art", "enhance", "fantasy-art", "isometric", "line-art",
"low-poly", "modeling-compound", "neon-punk", "origami", "photographic",
"pixel-art", "tile-texture"
]

# Select box for styles (only if Stable Diffusion is selected)
if model == "Stable Diffusion":
style = st.selectbox("Select Style", sd_presets)
else:
style = "None"
import base64
from PIL import Image
from io import BytesIO

def base64_to_pil(base64_str):
"""
Converts a base64 string to a PIL Image object.
"""
image_data = base64.b64decode(base64_str)
image = Image.open(BytesIO(image_data))
return image
if st.button("Generate Image"):
if model == "Amazon Titan":
image_base64 = generate_image_titan(prompt)
else:
image_base64 = generate_image_sd(prompt, style)
# Convert base64 to PIL Image
image = base64_to_pil(image_base64)

# Display the image
st.image(image)
18 changes: 12 additions & 6 deletions image_examples/image_to_image_st.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def base64_to_pil(base64_string):
return image


# Bedrock api call to stable diffusion
def sd_update_image(change_prompt, init_image_b64):
"""
Purpose:
Expand Down Expand Up @@ -143,15 +142,22 @@ def update_image_pipeline(user_image, change_prompt, model):

st.title("Building with Bedrock") # Title of the application
st.subheader("Image Generation Demo - Image to Image")
model = st.selectbox(
"Select model",
[
"Amazon Titan",
"Stable Diffusion",
],
)

model = st.selectbox("Select model", ["Amazon Titan", "Stable Diffusion"])

# TODO insert your comments

user_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
change_prompt = st.text_input("Enter prompt to change image")

col1, col2 = st.columns(2) # Column 1 for input image, Column 2 for output image
# Creat two columns, one to show the uploaded image, another for the new image
col1, col2 = st.columns(2)

# show user image
# Show the uploaded image
if user_image is not None:
user_image = Image.open(user_image)
col1.image(user_image)
Expand Down
6 changes: 6 additions & 0 deletions image_examples/image_understanding_st.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def pil_to_base64(image, format="png"):
if user_image is not None:
user_image = Image.open(user_image)
# TODO Finish App with Q
col1.image(user_image, use_column_width=True)

if col1.button("Get caption"):
base64_image = pil_to_base64(user_image)
caption = call_claude_sonnet(base64_image)
col2.write(caption)

else:
col2.write("No image uploaded")
15 changes: 11 additions & 4 deletions image_examples/inpainting_st.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def gen_mask_from_image(user_image):
return mask



def image_to_base64(img) -> str:
"""Convert a PIL Image or local image file path to a base64 string for Amazon Bedrock"""
if isinstance(img, str):
Expand Down Expand Up @@ -188,14 +189,20 @@ def inpaint_image_pipeline(user_image, change_prompt, mask, model):
# Create three columns, one to show the uploaded image, 2 for the mask, and 3 for the new image
col1, col2, col3 = st.columns(3)

# Show the uploaded image
if user_image is not None:
user_image = Image.open(user_image)
col1.image(user_image)

if model == "Stable Diffusion":
mask = Image.open("sd_mask.png")
mask = Image.open("sd_mask.png")
else:
mask = gen_mask_from_image(user_image)

# TODO Finish App with Q

col2.image(mask)
if st.button("Update Image"):
update_image = inpaint_image_pipeline(user_image, change_prompt, mask, model)
col3.image(update_image)
else:
col3.write("Please upload an image to get started")


Binary file added inpainted_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 28 additions & 5 deletions text_examples/gen_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ def summarize_text(text):
"""

model_id = "meta.llama3-70b-instruct-v1:0"
# Setup the system prompts and messages to send to the model.
system_prompts = [
{"text": "You are an app that creates summaries of text in 50 words or less."}
]
message_1 = {
messages = {
"role": "user",
"content": [{"text": f"Summarize the following text: {text}."}],
}

messages = [message_1]
messages = [messages]

result = generate_conversation(model_id, system_prompts, messages)

Expand All @@ -92,7 +91,18 @@ def sentiment_analysis(text):
Function to return a JSON object of sentiment from a given text.
"""
# TODO Can you fill in the function?
result = None
model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
system_prompts = [
{
"text": "You are a sentiment analysis bot that returns a JSON object of sentiment from a given text."
}
]
messages = [{
"role": "user",
"content": [{"text": f"Analyze the sentiment of the following text: {text}."}],
}]
result = generate_conversation(model_id, system_prompts, messages)
# result = None
return result


Expand All @@ -101,7 +111,20 @@ def perform_qa(question, text):
Function to perform a Q&A operation based on the provided text.
"""
# TODO Can you fill in the function?
result = None
model_id = "mistral.mistral-large-2402-v1:0"
system_prompts = [
{
"text": f"Please you following this text: {text}."
}
]
messages = [{
"role": "user",
"content": [
{"text": f"Question: {question}"}
],
}]
result = generate_conversation(model_id, system_prompts, messages)
# result = None
return result


Expand Down
Binary file added updated_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.