mu-cai commited on
Commit
9b8cf79
·
1 Parent(s): f6fe943

Initial model commit

Browse files
Files changed (5) hide show
  1. README.md +5 -5
  2. app.py +325 -0
  3. mm-vet/bard_set.json +1 -0
  4. mm-vet/mm-vet.json +0 -0
  5. requirements.txt +1 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: ViP-Bench Evaluator
3
- emoji: 🌖
4
- colorFrom: indigo
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.9.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
1
  ---
2
+ title: MM-Vet Evaluator
3
+ emoji: 🐨
4
+ colorFrom: gray
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.39.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
app.py ADDED
@@ -0,0 +1,325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
20
+
21
+
22
+ 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.
23
+
24
+ Question | Ground truth | Prediction | Correctness
25
+ --- | --- | --- | ---
26
+ What is x in the equation? | -1 <AND> -5 | x = 3 | 0.0
27
+ What is x in the equation? | -1 <AND> -5 | x = -1 | 0.5
28
+ What is x in the equation? | -1 <AND> -5 | x = -5 | 0.5
29
+ What is x in the equation? | -1 <AND> -5 | x = -5 or 5 | 0.5
30
+ What is x in the equation? | -1 <AND> -5 | x = -1 or x = -5 | 1.0
31
+ 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
32
+ 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
33
+ """
34
+
35
+
36
+
37
+ def grade(file_obj, progress=gr.Progress()):
38
+ # load metadata
39
+ # Download mm-vet.zip and `unzip mm-vet.zip` and change the path below
40
+ mmvet_path = "mm-vet"
41
+ use_sub_set = False
42
+ decimal_places = 1 # number of decimal places to round to
43
+
44
+
45
+ if use_sub_set:
46
+ bard_set_file = os.path.join(mmvet_path, "bard_set.json")
47
+ with open(bard_set_file, 'r') as f:
48
+ sub_set = json.load(f)
49
+ sub_set_name = 'bardset'
50
+ sub_set_name = sub_set_name + '_'
51
+ else:
52
+ sub_set = None
53
+ sub_set_name = ''
54
+
55
+ mmvet_metadata = os.path.join(mmvet_path, "mm-vet.json")
56
+ with open(mmvet_metadata, 'r') as f:
57
+ data = json.load(f)
58
+
59
+
60
+ counter = Counter()
61
+ cap_set_list = []
62
+ cap_set_counter = []
63
+ len_data = 0
64
+ for id, value in data.items():
65
+ if sub_set is not None and id not in sub_set:
66
+ continue
67
+ question = value["question"]
68
+ answer = value["answer"]
69
+ cap = value["capability"]
70
+ cap = set(cap)
71
+ counter.update(cap)
72
+ if cap not in cap_set_list:
73
+ cap_set_list.append(cap)
74
+ cap_set_counter.append(1)
75
+ else:
76
+ cap_set_counter[cap_set_list.index(cap)] += 1
77
+
78
+ len_data += 1
79
+
80
+ sorted_list = counter.most_common()
81
+ columns = [k for k, v in sorted_list]
82
+ columns.append("total")
83
+ columns.append("std")
84
+ columns.append('runs')
85
+ df = pd.DataFrame(columns=columns)
86
+
87
+
88
+ cap_set_sorted_indices = np.argsort(-np.array(cap_set_counter))
89
+ new_cap_set_list = []
90
+ new_cap_set_counter = []
91
+ for index in cap_set_sorted_indices:
92
+ new_cap_set_list.append(cap_set_list[index])
93
+ new_cap_set_counter.append(cap_set_counter[index])
94
+
95
+ cap_set_list = new_cap_set_list
96
+ cap_set_counter = new_cap_set_counter
97
+ cap_set_names = ["_".join(list(cap_set)) for cap_set in cap_set_list]
98
+
99
+ columns2 = cap_set_names
100
+ columns2.append("total")
101
+ columns2.append("std")
102
+ columns2.append('runs')
103
+ df2 = pd.DataFrame(columns=columns2)
104
+
105
+
106
+ ###### change your model name ######
107
+ model = file_obj.name.split("/")[-1][:-5]
108
+ # result_path = "results"
109
+ num_run = 1 # we set 5 in the paper
110
+ # model_results_file = os.path.join(result_path, f"{model}.json")
111
+ model_results_file = file_obj.name
112
+
113
+ # grade results for each sample to svae
114
+ grade_file = f'{model}_{gpt_model}-grade-{num_run}runs.json'
115
+ # grade_file = os.path.join(result_path, grade_file)
116
+
117
+ # score results regarding capabilities/capability integration to save
118
+ cap_score_file = f'{model}_{sub_set_name}{gpt_model}-cap-score-{num_run}runs.csv'
119
+ # cap_score_file = os.path.join(result_path, cap_score_file)
120
+ cap_int_score_file = f'{model}_{sub_set_name}{gpt_model}-cap-int-score-{num_run}runs.csv'
121
+ # cap_int_score_file = os.path.join(result_path, cap_int_score_file)
122
+
123
+
124
+
125
+ with open(model_results_file) as f:
126
+ results = json.load(f)
127
+ if os.path.exists(grade_file):
128
+ with open(grade_file, 'r') as f:
129
+ grade_results = json.load(f)
130
+ else:
131
+ grade_results = {}
132
+
133
+
134
+ def need_more_runs():
135
+ need_more_runs = False
136
+ if len(grade_results) > 0:
137
+ for k, v in grade_results.items():
138
+ if len(v['score']) < num_run:
139
+ need_more_runs = True
140
+ break
141
+ return need_more_runs or len(grade_results) < len_data
142
+
143
+
144
+ while need_more_runs():
145
+ for j in range(num_run):
146
+ print(f'eval run {j}')
147
+ for id, line in progress.tqdm(data.items(), desc="Grading"):
148
+ if sub_set is not None and id not in sub_set:
149
+ continue
150
+ if id in grade_results and len(grade_results[id]['score']) >= (j + 1):
151
+ continue
152
+
153
+ model_pred = results[id]
154
+
155
+ question = prompt + '\n' + ' | '.join([line['question'], line['answer'].replace("<AND>", " <AND> ").replace("<OR>", " <OR> "), model_pred, ""])
156
+ messages = [
157
+ {"role": "user", "content": question},
158
+ ]
159
+
160
+ if id not in grade_results:
161
+ sample_grade = {'model': [], 'content': [], 'score': []}
162
+ else:
163
+ sample_grade = grade_results[id]
164
+
165
+
166
+ grade_sample_run_complete = False
167
+ temperature = 0.0
168
+
169
+ num_sleep = 0
170
+ while not grade_sample_run_complete:
171
+ try:
172
+ response = openai.ChatCompletion.create(
173
+ # model=gpt_model,
174
+ engine=gpt_model,
175
+ max_tokens=3,
176
+ temperature=temperature,
177
+ messages=messages)
178
+ content = response['choices'][0]['message']['content']
179
+ flag = True
180
+ try_time = 1
181
+ while flag:
182
+ try:
183
+ content = content.split(' ')[0].strip()
184
+ score = float(content)
185
+ if score > 1.0 or score < 0.0:
186
+ assert False
187
+ flag = False
188
+ except:
189
+ question = prompt + '\n' + ' | '.join([line['question'], line['answer'].replace("<AND>", " <AND> ").replace("<OR>", " <OR> "), model_pred, ""]) + "\nPredict the correctness of the answer (digit): "
190
+ messages = [
191
+ {"role": "user", "content": question},
192
+ ]
193
+ response = openai.ChatCompletion.create(
194
+ # model=gpt_model,
195
+ engine=gpt_model,
196
+ max_tokens=3,
197
+ temperature=temperature,
198
+ messages=messages)
199
+ content = response['choices'][0]['message']['content']
200
+ try_time += 1
201
+ temperature += 0.5
202
+ print(f"{id} try {try_time} times")
203
+ print(content)
204
+ if try_time > 5:
205
+ score = 0.0
206
+ flag = False
207
+ grade_sample_run_complete = True
208
+ except:
209
+ # gpt4 may have token rate limit
210
+ num_sleep += 1
211
+ if num_sleep > 12:
212
+ score = 0.0
213
+ grade_sample_run_complete = True
214
+ num_sleep = 0
215
+ continue
216
+ print("sleep 5s")
217
+ time.sleep(5)
218
+
219
+
220
+ if len(sample_grade['model']) >= j + 1:
221
+ sample_grade['model'][j] = response['model']
222
+ sample_grade['content'][j] = content
223
+ sample_grade['score'][j] = score
224
+ else:
225
+ sample_grade['model'].append(response['model'])
226
+ sample_grade['content'].append(content)
227
+ sample_grade['score'].append(score)
228
+ grade_results[id] = sample_grade
229
+
230
+ with open(grade_file, 'w') as f:
231
+ json.dump(grade_results, f, indent=4)
232
+
233
+
234
+ assert not need_more_runs()
235
+ cap_socres = {k: [0.0]*num_run for k in columns[:-2]}
236
+ counter['total'] = len_data
237
+
238
+ cap_socres2 = {k: [0.0]*num_run for k in columns2[:-2]}
239
+ counter2 = {columns2[i]:cap_set_counter[i] for i in range(len(cap_set_counter))}
240
+ counter2['total'] = len_data
241
+
242
+ for k, v in grade_results.items():
243
+ if sub_set is not None and k not in sub_set:
244
+ continue
245
+ for i in range(num_run):
246
+ score = v['score'][i]
247
+ caps = set(data[k]['capability'])
248
+ for c in caps:
249
+ cap_socres[c][i] += score
250
+
251
+ cap_socres['total'][i] += score
252
+
253
+ index = cap_set_list.index(caps)
254
+ cap_socres2[cap_set_names[index]][i] += score
255
+ cap_socres2['total'][i] += score
256
+
257
+ for k, v in cap_socres.items():
258
+ cap_socres[k] = np.array(v) / counter[k] *100
259
+
260
+
261
+ std = round(cap_socres['total'].std(), decimal_places)
262
+ total_copy = cap_socres['total'].copy()
263
+ runs = str(list(np.round(total_copy, decimal_places)))
264
+
265
+ for k, v in cap_socres.items():
266
+ cap_socres[k] = round(v.mean(), decimal_places)
267
+
268
+ cap_socres['std'] = std
269
+ cap_socres['runs'] = runs
270
+ df.loc[model] = cap_socres
271
+
272
+
273
+ for k, v in cap_socres2.items():
274
+ cap_socres2[k] = round(np.mean(np.array(v) / counter2[k] *100), decimal_places)
275
+ cap_socres2['std'] = std
276
+ cap_socres2['runs'] = runs
277
+ df2.loc[model] = cap_socres2
278
+
279
+ df.to_csv(cap_score_file)
280
+ df2.to_csv(cap_int_score_file)
281
+
282
+ files = [cap_score_file, cap_int_score_file, grade_file]
283
+ zip_file = f"results.zip"
284
+ with ZipFile(zip_file, "w") as zipObj:
285
+ for idx, file in enumerate(files):
286
+ zipObj.write(file, file)
287
+ for file in files:
288
+ os.remove(file)
289
+ return zip_file
290
+
291
+
292
+
293
+ # demo = gr.Interface(
294
+ # fn=grade,
295
+ # inputs=gr.File(file_types=[".json"]),
296
+ # outputs="file")
297
+
298
+
299
+ markdown = """
300
+ <p align="center">
301
+ <img src="https://github-production-user-asset-6210df.s3.amazonaws.com/49296856/258254299-29c00dae-8201-4128-b341-dad4663b544a.jpg" width="400"> <br>
302
+ </p>
303
+
304
+
305
+ # [MM-Vet: Evaluating Large Multimodal Models for Integrated Capabilities](https://github.com/yuweihao/MM-Vet)
306
+
307
+ In this demo, we offer MM-Vet LLM-based (GPT-4) evaluator to grade open-ended outputs from your models.
308
+
309
+ Plese upload your json file of your model results containing `{v1_0: ..., v1_1: ..., }`like [this json file](https://raw.githubusercontent.com/yuweihao/MM-Vet/main/results/llava_llama2_13b_chat.json).
310
+
311
+ 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.
312
+
313
+ The grading results will be downloaded as a zip file.
314
+ """
315
+
316
+
317
+ with gr.Blocks() as demo:
318
+ gr.Markdown(markdown)
319
+ with gr.Row():
320
+ inp = gr.File(file_types=[".json"])
321
+ out = gr.File(file_types=[".zip"])
322
+ inp.change(grade, inp, out)
323
+
324
+ if __name__ == "__main__":
325
+ 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