diff --git a/image_examples/image_gen_st.py b/image_examples/image_gen_st.py index d0a1881..a7944f6 100644 --- a/image_examples/image_gen_st.py +++ b/image_examples/image_gen_st.py @@ -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) diff --git a/image_examples/image_to_image_st.py b/image_examples/image_to_image_st.py index 1040a7b..e5ef62c 100644 --- a/image_examples/image_to_image_st.py +++ b/image_examples/image_to_image_st.py @@ -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: @@ -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) diff --git a/image_examples/image_understanding_st.py b/image_examples/image_understanding_st.py index 0fd53a4..d04d70f 100644 --- a/image_examples/image_understanding_st.py +++ b/image_examples/image_understanding_st.py @@ -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") diff --git a/image_examples/inpainting_st.py b/image_examples/inpainting_st.py index d978b99..c55f148 100644 --- a/image_examples/inpainting_st.py +++ b/image_examples/inpainting_st.py @@ -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): @@ -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") + + diff --git a/inpainted_image.png b/inpainted_image.png new file mode 100644 index 0000000..3542890 Binary files /dev/null and b/inpainted_image.png differ diff --git a/text_examples/gen_text.py b/text_examples/gen_text.py index a2197f6..7146747 100644 --- a/text_examples/gen_text.py +++ b/text_examples/gen_text.py @@ -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) @@ -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 @@ -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 diff --git a/updated_image.png b/updated_image.png new file mode 100644 index 0000000..371dc4b Binary files /dev/null and b/updated_image.png differ