-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGPT_4V.py
More file actions
57 lines (47 loc) · 1.81 KB
/
GPT_4V.py
File metadata and controls
57 lines (47 loc) · 1.81 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
51
52
53
54
55
56
57
import openai
import base64
import os
import time
import json
def testOpenaiChatCompletions(img, content):
client = openai.AzureOpenAI(
azure_endpoint="azure_endpoint",
api_version="api_version",
api_key="api_key"
)
completion = client.chat.completions.create(
model="gptv",
messages=[
{
"role": "user",
"content": [content, {"image": img}]
}
]
)
return completion
if __name__ == '__main__':
category_list = ["artwork", "attribute_reason", "attribute_recognition", "biology", \
"calculation", "celebrity", "chemistry", "code", "color", "count", "cross_instance_reason", \
"landmark", "math", "ocr", "physics", "position", "poster", "scene", "translation"]
for category in category_list:
print("Eval on " + category)
testdata_root = "Conbench/%s"%category
json_file = os.path.join(testdata_root, 'output_w_caption_%s.json'%category)
infos = json.load(open(json_file, 'r'))
pred_answer_list = []
for info in infos:
img_file = os.path.join(testdata_root, info['image'])
image = open(img_file, "rb")
b64string = base64.b64encode(image.read()).decode("utf-8")
try:
resp = testOpenaiChatCompletions(b64string, qs)
res = resp.choices[0].message.content
except:
res = "None"
print(res)
print("-"*50)
time.sleep(1)
record = "%s"%info['image'] + "\t" + "%s"%info['question'] + "\t" + "%s"%info['answer'] + "\t" + "%s"%res.replace("\n", "") + "\n"
pred_answer_list.append(record)
with open("./Res/GPT-4/%s.txt"%category, "w") as file:
file.writelines(pred_answer_list)