-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_question_answering_openai.py
More file actions
40 lines (34 loc) · 1.07 KB
/
image_question_answering_openai.py
File metadata and controls
40 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
from openai import OpenAI
# Set up the OpenAI API client
client = OpenAI()
def visual_question_answering(image_url, question):
# Prepare the messages for the API call
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image_url",
"image_url": {
"url": image_url
}
}
]
}
]
# Make the API call
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
max_tokens=300
)
# Return the model's answer
return response.choices[0].message.content
# Example usage
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
question = "What's in this image?"
answer = visual_question_answering(image_url, question)
print(f"Question: {question}")
print(f"Answer: {answer}")