-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added vision examples for basic and multiple
- Loading branch information
1 parent
c8c7ee6
commit c57f705
Showing
10 changed files
with
133 additions
and
25 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from anthropic import Anthropic | ||
from dotenv import load_dotenv | ||
import base64 | ||
import os | ||
|
||
load_dotenv() | ||
|
||
anthropic_key = os.getenv('ANTHROPIC_API_KEY') | ||
|
||
client = Anthropic() | ||
|
||
|
||
with open('./Image/flip.png', "rb") as image_file: | ||
binary_data = image_file.read() | ||
|
||
base_64_encoded_data = base64.b64encode(binary_data) | ||
|
||
base64_string = base_64_encoded_data.decode('utf-8') | ||
|
||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type" : "image", "source" : { | ||
"type" : "base64", | ||
"media_type": "image/png", | ||
"data" : base64_string | ||
}}, | ||
{ | ||
"type" : "text", | ||
"text" : "Do you know what move she is performing?" | ||
} | ||
] | ||
}, | ||
] | ||
|
||
response = client.messages.create( | ||
model="claude-3-5-sonnet-20240620", | ||
max_tokens=2048, | ||
messages=messages, | ||
) | ||
|
||
print(response.content[0].text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from helpers.image_source import create_image_message | ||
|
||
from anthropic import Anthropic | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
|
||
anthropic_key = os.getenv('ANTHROPIC_API_KEY') | ||
|
||
client = Anthropic() | ||
|
||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": | ||
[ | ||
{"type": "text", "text": "Image 1:"}, # When working with lesser capable models, labelling images can be very helpful. | ||
create_image_message('./Image/flip.png'), | ||
{"type": "text", "text": "Image 2:"}, | ||
create_image_message('./Image/shot.png'), | ||
{"type": "text", "text": "You have perfect vision and pay great attention to detail in images. How many people flags are in this picture? Some of the flags may be partially obscured or cut off in the image or may be visible. Please count flags even if you can only see a single aspect. Before providing the answer in <answer> tags, think step by step in <thinking> tags and analyze every part of the image."} | ||
] | ||
}, | ||
] | ||
|
||
response = client.messages.create( | ||
model="claude-3-haiku-20240307", # Different models have different capabilities. | ||
max_tokens=2048, | ||
messages=messages, | ||
) | ||
|
||
print(response.content[0].text) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import base64 | ||
import mimetypes | ||
|
||
def create_image_message(image_path): | ||
with open(image_path, "rb") as image_file: | ||
binary_data = image_file.read() | ||
|
||
base64_encoded_data = base64.b64encode(binary_data) | ||
|
||
base64_string = base64_encoded_data.decode('utf-8') | ||
|
||
mime_type, _ = mimetypes.guess_type(image_path) | ||
|
||
image_block = { | ||
"type": "image", | ||
"source" : { | ||
"type" : "base64", | ||
"media_type" : mime_type, | ||
"data" : base64_string | ||
} | ||
} | ||
|
||
return image_block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from helpers.image_source import create_image_message | ||
|
||
from anthropic import Anthropic | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
|
||
anthropic_key = os.getenv('ANTHROPIC_API_KEY') | ||
|
||
client = Anthropic() | ||
|
||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": | ||
[ | ||
{"type": "text", "text": "Image 1:"}, # When working with lesser capable models, labelling images can be very helpful. | ||
create_image_message('./Image/flip.png'), | ||
{"type": "text", "text": "Image 2:"}, | ||
create_image_message('./Image/shot.png'), | ||
{"type": "text", "text": "You have perfect vision and pay great attention to detail in images. How many people flags are in this picture? Some of the flags may be partially obscured or cut off in the image or may be visible. Please count flags even if you can only see a single aspect. Before providing the answer in <answer> tags, think step by step in <thinking> tags and analyze every part of the image."} | ||
] | ||
}, | ||
] | ||
|
||
response = client.messages.create( | ||
model="claude-3-haiku-20240307", # Different models have different capabilities. | ||
max_tokens=2048, | ||
messages=messages, | ||
) | ||
|
||
print(response.content[0].text) |