whyu commited on
Commit
9133cc3
1 Parent(s): f1592e2

initial commit

Browse files
Files changed (4) hide show
  1. app.py +319 -0
  2. mm-vet/bard_set.json +1 -0
  3. mm-vet/mm-vet.json +0 -0
  4. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import json
4
+ import os
5
+ from tqdm import tqdm
6
+ import pandas as pd
7
+ import numpy as np
8
+ from collections import Counter
9
+ import time
10
+ from zipfile import ZipFile
11
+
12
+ openai.api_key = os.environ.get("AZURE_OPENAI_KEY")
13
+ openai.api_base = os.environ.get("AZURE_OPENAI_ENDPOINT")
14
+ openai.api_type = 'azure'
15
+ openai.api_version = os.environ.get("AZURE_OPENAI_API_VERSION")
16
+ deployment_id = os.environ.get("AZURE_OPENAI_DEP_ID")
17
+ gpt_model = deployment_id
18
+
19
+ print(os.environ.get("AZURE_OPENAI_KEY"))
20
+ print(os.environ.get("AZURE_OPENAI_ENDPOINT"))
21
+ print(os.environ.get("AZURE_OPENAI_API_VERSION"))
22
+ print(gpt_model)
23
+
24
+ prompt = """Compare the ground truth and prediction from AI models, to give a correctness score for the prediction. <AND> in the ground truth means it is totally right only when all elements in the ground truth are present in the prediction, and <OR> means it is totally right when any one element in the ground truth is present in the prediction. The correctness score is 0.0 (totally wrong), 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, or 1.0 (totally right). Just complete the last space of the correctness score.
25
+
26
+ Question | Ground truth | Prediction | Correctness
27
+ --- | --- | --- | ---
28
+ What is x in the equation? | -1 <AND> -5 | x = 3 | 0.0
29
+ What is x in the equation? | -1 <AND> -5 | x = -1 | 0.5
30
+ What is x in the equation? | -1 <AND> -5 | x = -5 | 0.5
31
+ What is x in the equation? | -1 <AND> -5 | x = -5 or 5 | 0.5
32
+ What is x in the equation? | -1 <AND> -5 | x = -1 or x = -5 | 1.0
33
+ Can you explain this meme? | This meme is poking fun at the fact that the names of the countries Iceland and Greenland are misleading. Despite its name, Iceland is known for its beautiful green landscapes, while Greenland is mostly covered in ice and snow. The meme is saying that the person has trust issues because the names of these countries do not accurately represent their landscapes. | The meme talks about Iceland and Greenland. It's pointing out that despite their names, Iceland is not very icy and Greenland isn't very green. | 0.4
34
+ Can you explain this meme? | This meme is poking fun at the fact that the names of the countries Iceland and Greenland are misleading. Despite its name, Iceland is known for its beautiful green landscapes, while Greenland is mostly covered in ice and snow. The meme is saying that the person has trust issues because the names of these countries do not accurately represent their landscapes. | The meme is using humor to point out the misleading nature of Iceland's and Greenland's names. Iceland, despite its name, has lush green landscapes while Greenland is mostly covered in ice and snow. The text 'This is why I have trust issues' is a playful way to suggest that these contradictions can lead to distrust or confusion. The humor in this meme is derived from the unexpected contrast between the names of the countries and their actual physical characteristics. | 1.0
35
+ """
36
+
37
+
38
+
39
+ def grade(file_obj, progress=gr.Progress()):
40
+ # load metadata
41
+ # Download mm-vet.zip and `unzip mm-vet.zip` and change the path below
42
+ mmvet_path = "mm-vet"
43
+ use_sub_set = False
44
+ decimal_places = 1 # number of decimal places to round to
45
+
46
+
47
+ if use_sub_set:
48
+ bard_set_file = os.path.join(mmvet_path, "bard_set.json")
49
+ with open(bard_set_file, 'r') as f:
50
+ sub_set = json.load(f)
51
+ sub_set_name = 'bardset'
52
+ sub_set_name = sub_set_name + '_'
53
+ else:
54
+ sub_set = None
55
+ sub_set_name = ''
56
+
57
+ mmvet_metadata = os.path.join(mmvet_path, "mm-vet.json")
58
+ with open(mmvet_metadata, 'r') as f:
59
+ data = json.load(f)
60
+
61
+
62
+ counter = Counter()
63
+ cap_set_list = []
64
+ cap_set_counter = []
65
+ len_data = 0
66
+ for id, value in data.items():
67
+ if sub_set is not None and id not in sub_set:
68
+ continue
69
+ question = value["question"]
70
+ answer = value["answer"]
71
+ cap = value["capability"]
72
+ cap = set(cap)
73
+ counter.update(cap)
74
+ if cap not in cap_set_list:
75
+ cap_set_list.append(cap)
76
+ cap_set_counter.append(1)
77
+ else:
78
+ cap_set_counter[cap_set_list.index(cap)] += 1
79
+
80
+ len_data += 1
81
+
82
+ sorted_list = counter.most_common()
83
+ columns = [k for k, v in sorted_list]
84
+ columns.append("total")
85
+ columns.append("std")
86
+ columns.append('runs')
87
+ df = pd.DataFrame(columns=columns)
88
+
89
+
90
+ cap_set_sorted_indices = np.argsort(-np.array(cap_set_counter))
91
+ new_cap_set_list = []
92
+ new_cap_set_counter = []
93
+ for index in cap_set_sorted_indices:
94
+ new_cap_set_list.append(cap_set_list[index])
95
+ new_cap_set_counter.append(cap_set_counter[index])
96
+
97
+ cap_set_list = new_cap_set_list
98
+ cap_set_counter = new_cap_set_counter
99
+ cap_set_names = ["_".join(list(cap_set)) for cap_set in cap_set_list]
100
+
101
+ columns2 = cap_set_names
102
+ columns2.append("total")
103
+ columns2.append("std")
104
+ columns2.append('runs')
105
+ df2 = pd.DataFrame(columns=columns2)
106
+
107
+
108
+ ###### change your model name ######
109
+ model = file_obj.name.split("/")[-1][:-5]
110
+ # result_path = "results"
111
+ num_run = 1 # we set 5 in the paper
112
+ # model_results_file = os.path.join(result_path, f"{model}.json")
113
+ model_results_file = file_obj.name
114
+
115
+ # grade results for each sample to svae
116
+ grade_file = f'{model}_{gpt_model}-grade-{num_run}runs.json'
117
+ # grade_file = os.path.join(result_path, grade_file)
118
+
119
+ # score results regarding capabilities/capability integration to save
120
+ cap_score_file = f'{model}_{sub_set_name}{gpt_model}-cap-score-{num_run}runs.csv'
121
+ # cap_score_file = os.path.join(result_path, cap_score_file)
122
+ cap_int_score_file = f'{model}_{sub_set_name}{gpt_model}-cap-int-score-{num_run}runs.csv'
123
+ # cap_int_score_file = os.path.join(result_path, cap_int_score_file)
124
+
125
+
126
+
127
+ with open(model_results_file) as f:
128
+ results = json.load(f)
129
+ if os.path.exists(grade_file):
130
+ with open(grade_file, 'r') as f:
131
+ grade_results = json.load(f)
132
+ else:
133
+ grade_results = {}
134
+
135
+
136
+ def need_more_runs():
137
+ need_more_runs = False
138
+ if len(grade_results) > 0:
139
+ for k, v in grade_results.items():
140
+ if len(v['score']) < num_run:
141
+ need_more_runs = True
142
+ break
143
+ return need_more_runs or len(grade_results) < len_data
144
+
145
+
146
+ while need_more_runs():
147
+ for j in range(num_run):
148
+ print(f'eval run {j}')
149
+ for id, line in progress.tqdm(data.items(), desc="Grading"):
150
+ if sub_set is not None and id not in sub_set:
151
+ continue
152
+ if id in grade_results and len(grade_results[id]['score']) >= (j + 1):
153
+ continue
154
+
155
+ model_pred = results[id]
156
+
157
+ question = prompt + '\n' + ' | '.join([line['question'], line['answer'].replace("<AND>", " <AND> ").replace("<OR>", " <OR> "), model_pred, ""])
158
+ messages = [
159
+ {"role": "user", "content": question},
160
+ ]
161
+
162
+ if id not in grade_results:
163
+ sample_grade = {'model': [], 'content': [], 'score': []}
164
+ else:
165
+ sample_grade = grade_results[id]
166
+
167
+
168
+ grade_sample_run_complete = False
169
+ temperature = 0.0
170
+
171
+ # while not grade_sample_run_complete:
172
+ # try:
173
+ response = openai.ChatCompletion.create(
174
+ # model=gpt_model,
175
+ engine=gpt_model,
176
+ max_tokens=3,
177
+ temperature=temperature,
178
+ messages=messages)
179
+ content = response['choices'][0]['message']['content']
180
+ flag = True
181
+ try_time = 1
182
+ while flag:
183
+ try:
184
+ content = content.split(' ')[0].strip()
185
+ score = float(content)
186
+ if score > 1.0 or score < 0.0:
187
+ assert False
188
+ flag = False
189
+ except:
190
+ question = prompt + '\n' + ' | '.join([line['question'], line['answer'].replace("<AND>", " <AND> ").replace("<OR>", " <OR> "), model_pred, ""]) + "\nPredict the correctness of the answer (digit): "
191
+ messages = [
192
+ {"role": "user", "content": question},
193
+ ]
194
+ response = openai.ChatCompletion.create(
195
+ # model=gpt_model,
196
+ engine=gpt_model,
197
+ max_tokens=3,
198
+ temperature=temperature,
199
+ messages=messages)
200
+ content = response['choices'][0]['message']['content']
201
+ try_time += 1
202
+ temperature += 0.5
203
+ print(f"{id} try {try_time} times")
204
+ print(content)
205
+ if try_time > 5:
206
+ score = 0.0
207
+ flag = False
208
+ grade_sample_run_complete = True
209
+ # except:
210
+ # # gpt4 may have token rate limit
211
+ # print("sleep 30s")
212
+ # time.sleep(30)
213
+
214
+ if len(sample_grade['model']) >= j + 1:
215
+ sample_grade['model'][j] = response['model']
216
+ sample_grade['content'][j] = content
217
+ sample_grade['score'][j] = score
218
+ else:
219
+ sample_grade['model'].append(response['model'])
220
+ sample_grade['content'].append(content)
221
+ sample_grade['score'].append(score)
222
+ grade_results[id] = sample_grade
223
+
224
+ with open(grade_file, 'w') as f:
225
+ json.dump(grade_results, f, indent=4)
226
+
227
+
228
+ assert not need_more_runs()
229
+ cap_socres = {k: [0.0]*num_run for k in columns[:-2]}
230
+ counter['total'] = len_data
231
+
232
+ cap_socres2 = {k: [0.0]*num_run for k in columns2[:-2]}
233
+ counter2 = {columns2[i]:cap_set_counter[i] for i in range(len(cap_set_counter))}
234
+ counter2['total'] = len_data
235
+
236
+ for k, v in grade_results.items():
237
+ if sub_set is not None and k not in sub_set:
238
+ continue
239
+ for i in range(num_run):
240
+ score = v['score'][i]
241
+ caps = set(data[k]['capability'])
242
+ for c in caps:
243
+ cap_socres[c][i] += score
244
+
245
+ cap_socres['total'][i] += score
246
+
247
+ index = cap_set_list.index(caps)
248
+ cap_socres2[cap_set_names[index]][i] += score
249
+ cap_socres2['total'][i] += score
250
+
251
+ for k, v in cap_socres.items():
252
+ cap_socres[k] = np.array(v) / counter[k] *100
253
+
254
+
255
+ std = round(cap_socres['total'].std(), decimal_places)
256
+ total_copy = cap_socres['total'].copy()
257
+ runs = str(list(np.round(total_copy, decimal_places)))
258
+
259
+ for k, v in cap_socres.items():
260
+ cap_socres[k] = round(v.mean(), decimal_places)
261
+
262
+ cap_socres['std'] = std
263
+ cap_socres['runs'] = runs
264
+ df.loc[model] = cap_socres
265
+
266
+
267
+ for k, v in cap_socres2.items():
268
+ cap_socres2[k] = round(np.mean(np.array(v) / counter2[k] *100), decimal_places)
269
+ cap_socres2['std'] = std
270
+ cap_socres2['runs'] = runs
271
+ df2.loc[model] = cap_socres2
272
+
273
+ df.to_csv(cap_score_file)
274
+ df2.to_csv(cap_int_score_file)
275
+
276
+ files = [cap_score_file, cap_int_score_file, grade_file]
277
+ zip_file = f"results.zip"
278
+ with ZipFile(zip_file, "w") as zipObj:
279
+ for idx, file in enumerate(files):
280
+ zipObj.write(file, file)
281
+ for file in files:
282
+ os.remove(file)
283
+ return zip_file
284
+
285
+
286
+
287
+ # demo = gr.Interface(
288
+ # fn=grade,
289
+ # inputs=gr.File(file_types=[".json"]),
290
+ # outputs="file")
291
+
292
+
293
+ markdown = """
294
+ <p align="center">
295
+ <img src="https://github-production-user-asset-6210df.s3.amazonaws.com/49296856/258254299-29c00dae-8201-4128-b341-dad4663b544a.jpg" width="400"> <br>
296
+ </p>
297
+
298
+
299
+ # MM-Vet: Evaluating Large Multimodal Models for Integrated Capabilities
300
+
301
+ In this demo, we offer MM-Vet LLM-based (GPT-4) evaluator to grade open-ended outputs from your models.
302
+
303
+ Plese upload your json file of your model results containing `\{v1_0\: ..., v1_1\: ..., \}`.
304
+
305
+ The grading may last 5 minutes. Sine we only support 1 queue, the grading time may be longer when you need to wait for other users' grading to finish.
306
+
307
+ The grading results will be downloaded as a zip file.
308
+ """
309
+
310
+
311
+ with gr.Blocks() as demo:
312
+ gr.Markdown(markdown)
313
+ with gr.Row():
314
+ inp = gr.File(file_types=[".json"])
315
+ out = gr.File(file_types=[".zip"])
316
+ inp.change(grade, inp, out)
317
+
318
+ if __name__ == "__main__":
319
+ demo.queue().launch()
mm-vet/bard_set.json ADDED
@@ -0,0 +1 @@
 
 
1
+ ["v1_0", "v1_1", "v1_2", "v1_3", "v1_4", "v1_5", "v1_6", "v1_7", "v1_9", "v1_10", "v1_11", "v1_12", "v1_13", "v1_14", "v1_15", "v1_16", "v1_18", "v1_19", "v1_20", "v1_21", "v1_22", "v1_23", "v1_24", "v1_27", "v1_28", "v1_29", "v1_30", "v1_31", "v1_32", "v1_33", "v1_34", "v1_35", "v1_36", "v1_37", "v1_38", "v1_39", "v1_40", "v1_41", "v1_42", "v1_43", "v1_44", "v1_45", "v1_46", "v1_47", "v1_48", "v1_49", "v1_50", "v1_51", "v1_52", "v1_53", "v1_54", "v1_55", "v1_56", "v1_57", "v1_58", "v1_59", "v1_60", "v1_61", "v1_62", "v1_63", "v1_67", "v1_68", "v1_69", "v1_70", "v1_71", "v1_72", "v1_73", "v1_74", "v1_75", "v1_76", "v1_77", "v1_78", "v1_80", "v1_81", "v1_82", "v1_83", "v1_84", "v1_85", "v1_86", "v1_87", "v1_88", "v1_89", "v1_90", "v1_91", "v1_93", "v1_94", "v1_95", "v1_96", "v1_97", "v1_98", "v1_99", "v1_100", "v1_101", "v1_102", "v1_106", "v1_107", "v1_109", "v1_110", "v1_111", "v1_113", "v1_114", "v1_116", "v1_117", "v1_118", "v1_119", "v1_121", "v1_122", "v1_123", "v1_124", "v1_125", "v1_126", "v1_128", "v1_129", "v1_132", "v1_133", "v1_134", "v1_137", "v1_138", "v1_139", "v1_140", "v1_142", "v1_148", "v1_150", "v1_151", "v1_152", "v1_153", "v1_156", "v1_157", "v1_158", "v1_159", "v1_160", "v1_161", "v1_162", "v1_163", "v1_164", "v1_165", "v1_166", "v1_167", "v1_177", "v1_179", "v1_180", "v1_181", "v1_182", "v1_183", "v1_184", "v1_185", "v1_186", "v1_187", "v1_188", "v1_189", "v1_190", "v1_192", "v1_193", "v1_194", "v1_195", "v1_196", "v1_197", "v1_201", "v1_208", "v1_209", "v1_210", "v1_211", "v1_212", "v1_213", "v1_214", "v1_215", "v1_216", "v1_217"]
mm-vet/mm-vet.json ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai