-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlisting1.py
More file actions
50 lines (40 loc) · 1.34 KB
/
Copy pathlisting1.py
File metadata and controls
50 lines (40 loc) · 1.34 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
41
42
43
44
45
46
47
48
49
50
'''
Created on Nov 9, 2023
@author: immanueltrummer
'''
import argparse
import openai
import time
client = openai.OpenAI()
def analyze_image(image_url, question):
""" Use language model to answer question about image.
Args:
image_url: URL leading to image.
question: question about image.
Returns:
Answer generated by the language model.
"""
for nr_retries in range(1, 4):
try:
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role':'user', 'content':[
{'type':'text', 'text':question},
{'type':'image_url', 'image_url':{
'url':image_url, 'detail':'low'
}
}]
}]
)
return response.choices[0].message.content
except:
time.sleep(nr_retries * 2)
raise Exception('Cannot query OpenAI model!')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('imageurl', type=str, help='URL to image')
parser.add_argument('question', type=str, help='Question about the image')
args = parser.parse_args()
answer = analyze_image(args.imageurl, args.question)
print(answer)