31 KiB
31 KiB
In [ ]:
from IPython.display import Image
Image(filename='./prompting_images/uh_oh.png') In [ ]:
# opens the image file in "read binary" mode
with open("./prompting_images/uh_oh.png", "rb") as image_file:
#reads the contents of the image as a bytes object
binary_data = image_file.read()
In [ ]:
binary_dataIn [ ]:
message = {
"role": "user",
"content": [
{
"image": {
"format": 'png',
"source": {
"bytes": binary_data
}
}
}
]
}In [ ]:
import boto3
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [
{
"image": {
"format": 'png',
"source": {
"bytes": binary_data
}
}
}
]
}]
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)In [ ]:
responseIn [ ]:
messages = [
{
"role": "user",
"content": [
{
"image": {
"format": 'png',
"source": {
"bytes": binary_data
}
}
},
{
"text": "What could this person have done to prevent this?"
},
]
}
]
In [ ]:
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
import mimetypes
def create_image_message(image_path):
# Open the image file in "read binary" mode
with open(image_path, "rb") as image_file:
# Read the contents of the image as a bytes object
binary_data = image_file.read()
# Get the MIME type of the image based on its file extension
mime_type, _ = mimetypes.guess_type(image_path)
sub_type = mime_type.split("/")[-1]
# Create the image block
image_block = {
"image": {
"format": sub_type,
"source": {
"bytes": binary_data
}
}
}
return image_blockIn [ ]:
Image("./prompting_images/animal1.png")In [ ]:
messages = [
{
"role": "user",
"content": [
create_image_message("./prompting_images/animal1.png")
]
}
]
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
messages = [
{
"role": "user",
"content": [
create_image_message("./prompting_images/animal1.png"),
{"text": "Where might I find this animal in the world?"}
]
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
from IPython.display import display
display(Image("./prompting_images/animal1.png", width=300))In [ ]:
display(Image("./prompting_images/animal2.png", width=300))In [ ]:
display(Image("./prompting_images/animal3.png", width=300))In [ ]:
messages = [
{
"role": "user",
"content": [
create_image_message('./prompting_images/animal1.png'),
create_image_message('./prompting_images/animal2.png'),
create_image_message('./prompting_images/animal3.png'),
{"text": "what are these animals?"}
]
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
import httpx
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Church_of_light.jpg/1599px-Church_of_light.jpg"
image_media_type = "jpeg"
image_data = httpx.get(image_url).content
messages=[
{
"role": "user",
"content": [
{
"image": {
"format": image_media_type,
"source": {
"bytes": image_data
}
}
},
],
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
response
In [ ]:
def get_image_dict_from_url(image_url):
# Send a GET request to the image URL and retrieve the content
response = httpx.get(image_url)
image_content = response.content
# Determine the media type of the image based on the URL extension
# This is not a foolproof approach, but it generally works
image_extension = image_url.split(".")[-1].lower()
if image_extension == "jpg" or image_extension == "jpeg":
image_media_type = "jpeg"
elif image_extension == "png":
image_media_type = "png"
elif image_extension == "gif":
image_media_type = "gif"
else:
raise ValueError("Unsupported image format")
# Encode the image content using base6
# Create the dictionary in the proper image block shape:
image_dict = {
"image": {
"format": image_media_type,
"source": {
"bytes": image_content
}
}
}
return image_dict
In [ ]:
url1 = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Rincon_fire_truck.png/1600px-Rincon_fire_truck.png"
url2 = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Ornge_C-GYNP.jpg/1600px-Ornge_C-GYNP.jpg"
messages=[
{
"role": "user",
"content": [
{"text": "Image 1:"},
get_image_dict_from_url(url1),
{"text": "Image 2:"},
get_image_dict_from_url(url2),
{"text": "What do these images have in common?"}
],
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
from IPython.display import Image
Image(filename='./prompting_images/people.png') In [ ]:
messages=[
{
"role": "user",
"content": [
create_image_message("./prompting_images/people.png"),
{"text": "How many people are in this image?"}
],
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
messages=[
{
"role": "user",
"content": [
create_image_message("./prompting_images/people.png"),
{"text": "You have perfect vision and pay great attention to detail which makes you an expert at counting objects in images. How many people are in this picture? Some of the people may be partially obscured or cut off in the image or may only have an arm visible. Please count people even if you can only see a single body part. Before providing the answer in <answer> tags, think step by step in <thinking> tags and analyze every part of the image."}
],
}
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
responseIn [ ]:
from IPython.display import display
display(Image("./prompting_images/slide1.png", width=800))In [ ]:
display(Image("./prompting_images/slide2.png", width=800))In [ ]:
display(Image("./prompting_images/slide3.png", width=800))In [ ]:
def generate_slide_json(image_path):
slide1_response = """{
"background": "#F2E0BD",
"title": "Haiku",
"body": "Our most powerful model, delivering state-of-the-art performance on highly complex tasks and demonstrating fluency and human-like understanding",
"image": "The image shows a simple line drawing of a human head in profile view, facing to the right. The head is depicted using thick black lines against a pale yellow background. Inside the outline of the head, there appears to be a white, spoked wheel or starburst pattern, suggesting a visualization of mental activity or thought processes. The overall style is minimalist and symbolic rather than realistic."
}"""
messages = [
{
"role": "user",
"content": [
create_image_message("./prompting_images/slide1.png"),
{"text": "Generate a JSON representation of this slide. It should include the background color, title, body text, and image description"}
],
},
{
"role": "assistant",
"content": slide1_response
},
{
"role": "user",
"content": [
create_image_message(image_path),
{"text": "Generate a JSON representation of this slide. It should include the background color, title, body text, and image description"}
],
},
]
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
)
response
In [ ]:
display(Image("./prompting_images/slide2.png", width=800))
generate_slide_json("./prompting_images/slide2.png")In [ ]:
display(Image("./prompting_images/slide3.png", width=800))
generate_slide_json("./prompting_images/slide3.png")In [ ]:
research_paper_pages = [
"./images/research_paper/page1.png",
"./images/research_paper/page2.png",
"./images/research_paper/page3.png",
"./images/research_paper/page4.png",
"./images/research_paper/page5.png"
]In [ ]:
Image(research_paper_pages[0])