-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_gpt4o.py
148 lines (120 loc) · 4.53 KB
/
evaluate_gpt4o.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import re
import json
import argparse
from tqdm import tqdm
from datasets import load_dataset
import requests
import base64
import traceback
from utils import (
setup_seed,
reformat_option,
PROMPT_TEMPLATE,
IMAGE_PALCE_HOLODER,
)
api_key = "/your/api/key"
def generate_requests(model, content):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": content
}
],
"max_tokens": 1024
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return response.json()
def generate_reponse(model, content, max_try=100):
loop_num = 0
while(True):
try:
request = generate_requests(model, content)
except:
traceback.print_exc()
loop_num += 1
print(loop_num)
if loop_num > max_try:
break
continue
break
return request
def reformat_gpt4o_input(input_str, image_files):
images = [base64.b64encode(img_file["bytes"]).decode('utf-8') for img_file in image_files]
content_list = []
split_list = re.split(r'(<image>)', input_str)
for split_content in split_list:
if split_content == '<image>':
try:
img = images.pop(0)
except:
img = img
content_list.append({
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{img}"}
})
else:
content_list.append({
"type": "text",
"text": split_content
})
return content_list
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, default="gpt-4o")
parser.add_argument("--field", type=str, default="all", help="['all', 'science', 'engineering', 'healthcare']")
parser.add_argument("--lang", type=str, default="['en', 'de']", help="['all', 'en', 'zh', 'de']")
parser.add_argument("--seed", type=int, default=42, help="random seed")
parser.add_argument("--result_folder", type=str, default="./gpt4o", help="")
args = parser.parse_args()
setup_seed(args.seed)
os.makedirs(args.result_folder, exist_ok=True)
eval_fields = ['science', 'engineering', 'healthcare'] if args.field == "all" else args.field.split(",")
eval_langs = ['en', 'zh', 'de'] if args.lang == "all" else args.lang.split(",")
model = args.model
dataset = load_dataset("M4U-Benchmark/M4U")
for lang in eval_langs:
for field in eval_fields:
result = {
"model": model,
"total": 0,
"record": []
}
eval_split = "{}_{}".format(field, lang)
tgt_path = os.path.join(args.result_folder, eval_split + ".json")
if os.path.exists(tgt_path):
result = json.load(open(tgt_path, "r"))
idx_save, begin = 0, result['total']
for item in tqdm(dataset[eval_split]):
idx_save += 1
if idx_save <= begin:
continue
option_str = "\n".join(reformat_option(item["options"]))
input_str = PROMPT_TEMPLATE[lang].replace("{question}", item["question"]).replace("{options}", option_str)
image_tags = re.findall(r"<image_\d+>", input_str)
for idx, img_idx in enumerate(image_tags):
input_str = input_str.replace(img_idx, IMAGE_PALCE_HOLODER)
content_list = reformat_gpt4o_input(input_str, item["image_files"])
try:
response = generate_reponse(model, content_list)
outputs = response["choices"][0]['message']['content']
except:
outputs = 'error'
result["total"] += 1
result["record"].append({
"question": item["question"],
"options": item["options"],
"image_type": item["image_type"],
"cross_lingual": item["cross_lingual"],
"gt": item["answer"],
"predict": outputs,
})
with open(tgt_path, "w") as f:
json.dump(result, f, indent=4, ensure_ascii=False)