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
13 |
+
'H': 'ح',
|
14 |
+
'I': 'ط',
|
15 |
+
'J': 'ي',
|
16 |
+
'K': 'ك',
|
17 |
+
'L': 'ل',
|
18 |
+
'M': 'م',
|
19 |
+
'N': 'ن',
|
20 |
+
'O': 'ع',
|
21 |
+
'P': 'ف',
|
22 |
+
'Q': 'ص',
|
23 |
+
'R': 'ر',
|
24 |
+
'S': 'س',
|
25 |
+
'هـ': 'ه',
|
26 |
+
'ا': 'أ'
|
27 |
+
}
|
28 |
+
|
29 |
+
def translate_numbers(text: str) -> str:
|
30 |
+
english_to_arabic = {
|
31 |
+
'0': '٠', '1': '١', '2': '٢', '3': '٣', '4': '٤',
|
32 |
+
'5': '٥', '6': '٦', '7': '٧', '8': '٨', '9': '٩'
|
33 |
+
}
|
34 |
+
|
35 |
+
translation_table = str.maketrans(english_to_arabic)
|
36 |
+
return text.translate(translation_table)
|
37 |
+
|
38 |
+
|
39 |
+
def mcq_eval(pred: str, gt: str):
|
40 |
+
pred = pred.strip()
|
41 |
+
gt = gt.strip()
|
42 |
+
if len(pred) > 2 and pred[0] == '(' and pred[2] == ')':
|
43 |
+
pred = pred[1]
|
44 |
+
if len(gt) > 2 and gt[0] == '(' and gt[2] == ')':
|
45 |
+
gt = gt[1]
|
46 |
+
if len(pred) == 0: return False
|
47 |
+
pred = pred[0]
|
48 |
+
gt = gt[0]
|
49 |
+
pred = arabic_letters.get(pred, pred)
|
50 |
+
gt = arabic_letters.get(gt, gt)
|
51 |
+
return pred == gt
|
52 |
+
|
53 |
+
def create_options_prompt(row_data, option_candidate):
|
54 |
+
available_keys = set(row_data.keys()) & set(option_candidate)
|
55 |
+
options = {cand: row_data[cand] for cand in available_keys if row_data[cand]}
|
56 |
+
sorted_options = dict(sorted(options.items()))
|
57 |
+
options_prompt = f"هناك عدة خيارات:\n"
|
58 |
+
for key, item in sorted_options.items():
|
59 |
+
if pd.notna(item) and item != "nan":
|
60 |
+
arabic_key = arabic_letters[key]
|
61 |
+
options_prompt += f"{arabic_key}. {item}\n"
|
62 |
+
return options_prompt.rstrip("\n")
|
63 |
+
|
64 |
+
def mmbench_doc_to_text(doc):
|
65 |
+
option_candidate = ["A", "B", "C", "D", "E"]
|
66 |
+
options = create_options_prompt(doc, option_candidate)
|
67 |
+
question = f"{doc['hint']} {doc['question']} {options}" if pd.notna(doc["hint"]) and doc["hint"] != "nan" else f"{doc['question']} {options}"
|
68 |
+
return f"{question}\nأجب بحرف الخيار من الاختيارات المعطاة مباشرة."
|
69 |
+
|
70 |
+
def mmbench_eval(pred, gt):
|
71 |
+
return mcq_eval(pred, gt)
|
72 |
+
|
73 |
+
def mme_doc_to_text(doc):
|
74 |
+
question = doc["question"].strip()
|
75 |
+
return question
|
76 |
+
|
77 |
+
def mme_eval(pred: str, gt: str):
|
78 |
+
pred = pred.strip()
|
79 |
+
if pred == "صح":
|
80 |
+
pred = 'نعم'
|
81 |
+
return gt in pred
|
82 |
+
|
83 |
+
def default_eval(pred: str, gt: str):
|
84 |
+
pred = pred.strip()
|
85 |
+
gt = gt.strip()
|
86 |
+
return pred == gt
|
87 |
+
|
88 |
+
|
89 |
+
def iconqa_doc_to_text(doc):
|
90 |
+
return doc['question']
|
91 |
+
|
92 |
+
def iconqa_eval(pred, gt):
|
93 |
+
return mcq_eval(pred, gt)
|
94 |
+
|
95 |
+
def mmmu_parse_options(options):
|
96 |
+
option_letters = [arabic_letters[chr(ord("A") + i)] for i in range(len(options))]
|
97 |
+
choices_str = "\n".join([f"{option_letter}. {option}" for option_letter, option in zip(option_letters, options)])
|
98 |
+
return choices_str
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
MMMU_MULTI_CHOICE_PROMPT = "أجب بحرف الخيار من الاختيارات المعطاة مباشرة."
|
104 |
+
MMMU_OPEN_ENDED_PROMPT = "أجب عن السؤال باستخدام كلمة أو عبارة واحدة."
|
105 |
+
CONTEXT_KEYWORD = "السياق"
|
106 |
+
|
107 |
+
def mmmu_doc_to_text(doc):
|
108 |
+
question = doc["question"]
|
109 |
+
if doc["question_type"] == "multiple-choice":
|
110 |
+
# Weirdly, data["options"] is a string in MMMU Huggingface dataset
|
111 |
+
parsed_options = mmmu_parse_options(ast.literal_eval(doc["options"].replace("،", ",")))
|
112 |
+
question = f"{question}\n{parsed_options}\n\n{MMMU_MULTI_CHOICE_PROMPT}"
|
113 |
+
else:
|
114 |
+
question = f"{question}\n\n{MMMU_OPEN_ENDED_PROMPT}"
|
115 |
+
return question
|
116 |
+
|
117 |
+
def mmmu_eval(pred, gt):
|
118 |
+
return mcq_eval(pred, gt)
|
119 |
+
|
120 |
+
def gqa_doc_to_text(doc):
|
121 |
+
question = doc["question"]
|
122 |
+
post_prompt = "\nأجب عن السؤال باستخدام كلمة أو عبارة واحدة."
|
123 |
+
return f"{question}{post_prompt}"
|
124 |
+
|
125 |
+
def gqa_eval(pred, gt):
|
126 |
+
return gt in pred
|
127 |
+
|
128 |
+
def realworldqa_doc_to_text(doc):
|
129 |
+
question = doc["question"].strip()
|
130 |
+
pre_prompt = "المستخدم\nالسؤال: "
|
131 |
+
return f"{pre_prompt}{question}"
|
132 |
+
|
133 |
+
def realworldqa_eval(pred, gt):
|
134 |
+
return mcq_eval(pred, gt)
|
135 |
+
|
136 |
+
def vqav2_doc_to_text(doc):
|
137 |
+
post_prompt = "\nأجب على السؤال باستخدام كلمة أو عبارة واحدة."
|
138 |
+
return f"{doc['question']}{post_prompt}"
|
139 |
+
|
140 |
+
def vizwiz_doc_to_text(doc):
|
141 |
+
post_prompt = "\nعندما تكون المعلومات المقدمة غير كافية، أجب بـ 'لا يمكن الإجابة'.\nأجب عن السؤال باستخدام كلمة واحدة أو عبارة قصيرة."
|
142 |
+
text = f"{doc['question'].capitalize()}{post_prompt}"
|
143 |
+
return text
|
144 |
+
|
145 |
+
def vizwiz_eval(pred: str, gt: str):
|
146 |
+
try:
|
147 |
+
_ = ast.literal_eval(gt)
|
148 |
+
gt = gt.replace(" ", ", ")
|
149 |
+
gt = ast.literal_eval(gt)
|
150 |
+
except:
|
151 |
+
gt = gt.strip()
|
152 |
+
pred = pred.strip()
|
153 |
+
if pred == gt:
|
154 |
+
return True
|
155 |
+
for x in gt:
|
156 |
+
if x in pred:
|
157 |
+
return True
|
158 |
+
return False
|
159 |
+
|
160 |
+
def pope_doc_to_text(doc):
|
161 |
+
question = doc["question"].strip()
|
162 |
+
return f"{question}\nأجب عن السؤال باستخدام كلمة واحدة أو عبارة قصيرة."
|
163 |
+
|
164 |
+
def pope_eval(pred, gt):
|
165 |
+
pred = pred.strip()
|
166 |
+
gt = gt.strip()
|
167 |
+
return gt in pred
|
168 |
+
|
169 |
+
def countbench_doc_to_text(_):
|
170 |
+
return "كم عدد الأشياء الموجودة في الصورة؟\nأجب برقم فقط."
|
171 |
+
|
172 |
+
def countbench_eval(pred, gt):
|
173 |
+
pred = pred.strip()
|
174 |
+
gt = gt.strip()
|
175 |
+
return translate_numbers(pred) == translate_numbers(gt)
|
176 |
+
|
177 |
+
def diagramsMMMU_eval(pred, gt):
|
178 |
+
pred = pred.strip()
|
179 |
+
gt = gt.strip()
|
180 |
+
if len(pred) == 0: return False
|
181 |
+
if len(gt) == 1:
|
182 |
+
return pred[0] == gt
|
183 |
+
pred = translate_numbers(pred)
|
184 |
+
gt = translate_numbers(gt)
|
185 |
+
return gt in pred
|
186 |
+
|
187 |
+
def diagramsMMMU_doc_to_text(doc):
|
188 |
+
return mmmu_doc_to_text(doc)
|
189 |
+
|
190 |
+
def medicalMMMU_eval(pred, gt):
|
191 |
+
return mcq_eval(pred, gt)
|
192 |
+
|
193 |
+
def medicalMMMU_doc_to_text(doc):
|
194 |
+
return mmmu_doc_to_text(doc)
|
195 |
+
|
196 |
+
def medicalMMMUPro_eval(pred, gt):
|
197 |
+
return mcq_eval(pred, gt)
|
198 |
+
|
199 |
+
def medicalMMMUPro_parse_options(options):
|
200 |
+
option_letters = [arabic_letters[chr(ord("A") + i)] for i in range(len(options))]
|
201 |
+
choices_str = "\n".join([f"{option_letter}. {option}" for option_letter, option in zip(option_letters, options)])
|
202 |
+
return choices_str
|
203 |
+
|
204 |
+
|
205 |
+
def medicalMMMUPro_doc_to_text(doc):
|
206 |
+
post_prompt="أجب بحرف الخيار من الخيارات المعطاة مباشرة."
|
207 |
+
question = doc["question"]
|
208 |
+
# Weirdly, data["options"] is a string in MMMU Huggingface dataset
|
209 |
+
parsed_options = medicalMMMUPro_parse_options(ast.literal_eval(doc["options"].replace("،", ",")))
|
210 |
+
question = f"{question}\n{parsed_options}\n\n{post_prompt}"
|
211 |
+
return question
|
212 |
+
|
213 |
+
|
214 |
+
def mmt_doc_to_text(doc):
|
215 |
+
question_text = "سؤال: <image>\n" + doc["question"].strip()
|
216 |
+
|
217 |
+
options = []
|
218 |
+
for option in ["A", "B", "C", "D", "E", "F", "G", "H", "I"]:
|
219 |
+
option_text = doc.get(option)
|
220 |
+
if option_text and option_text.strip():
|
221 |
+
options.append(f"{arabic_letters[option]}: {option_text.strip()}")
|
222 |
+
|
223 |
+
options_text = "\n".join(options) if options else ""
|
224 |
+
|
225 |
+
formatted_question = f"{question_text}\n{options_text}"
|
226 |
+
post_prompt = "\nأجب عن السؤال باستخدام حرف واحد من الخيارات المعطاة."
|
227 |
+
formatted_question = f"{formatted_question}{post_prompt}"
|
228 |
+
|
229 |
+
return formatted_question
|
230 |
+
|
231 |
+
def medicalmmt_doc_to_text(doc):
|
232 |
+
return mmt_doc_to_text(doc)
|
233 |
+
|
234 |
+
def medicalmmt_eval(pred, gt):
|
235 |
+
return mcq_eval(pred, gt)
|
236 |
+
|
237 |
+
def seed_doc_to_text(doc):
|
238 |
+
question = doc["question"]
|
239 |
+
question += "\n" + f"أ. {doc['choice_a']}\n"
|
240 |
+
question += f"ب. {doc['choice_b']}\n"
|
241 |
+
question += f"ج. {doc['choice_c']}\n"
|
242 |
+
question += f"د. {doc['choice_d']}"
|
243 |
+
return f"{question}\nأجب بحرف الخيار من الاختيارات المعطاة مباشرة."
|
244 |
+
|
245 |
+
def seed_eval(pred, gt):
|
246 |
+
return mcq_eval(pred, gt)
|
247 |
+
|
248 |
+
def hallucinationmmt_doc_to_text(doc):
|
249 |
+
return mmt_doc_to_text(doc)
|
250 |
+
|
251 |
+
def hallucinationmmt_eval(pred, gt):
|
252 |
+
return mcq_eval(pred, gt)
|
253 |
+
|
254 |
+
def vqammt_doc_to_text(doc):
|
255 |
+
return mmt_doc_to_text(doc)
|
256 |
+
|
257 |
+
def vqammt_eval(pred, gt):
|
258 |
+
return mcq_eval(pred, gt)
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
def mutliimagemmt_doc_to_text(doc):
|
263 |
+
return mmt_doc_to_text(doc)
|
264 |
+
|
265 |
+
def mutliimagemmt_eval(pred, gt):
|
266 |
+
return mcq_eval(pred, gt)
|
267 |
+
|
268 |
+
|
269 |
+
def our_options_to_str(options):
|
270 |
+
option_prompt_str = ""
|
271 |
+
for i, option in enumerate(options):
|
272 |
+
option_choice = chr(ord("A") + i)
|
273 |
+
option_choice = arabic_letters[option_choice]
|
274 |
+
option_prompt_str += f"{option_choice}. {option}\n"
|
275 |
+
|
276 |
+
option_prompt_str = option_prompt_str.rstrip("\n")
|
277 |
+
return option_prompt_str
|
278 |
+
|
279 |
+
def our_doc_to_text(doc):
|
280 |
+
question_text = "سؤال:\n" + doc["question"].strip()
|
281 |
+
options = our_options_to_str(doc["options"])
|
282 |
+
options_text = options
|
283 |
+
formatted_question = f"{question_text}\n{options_text}"
|
284 |
+
post_prompt = "\nأجب عن السؤال باستخدام حرف واحد من الخيارات المعطاة."
|
285 |
+
formatted_question = f"{formatted_question}{post_prompt}"
|
286 |
+
return formatted_question
|
287 |
+
|
288 |
+
|
289 |
+
def isidocvqa_doc_to_text(doc):
|
290 |
+
return our_doc_to_text(doc)
|
291 |
+
|
292 |
+
def isidocvqa_eval(pred, gt):
|
293 |
+
return mcq_eval(pred, gt)
|
294 |
+
|
295 |
+
def patddocvqa_doc_to_text(doc):
|
296 |
+
return our_doc_to_text(doc)
|
297 |
+
|
298 |
+
def patddocvqa_eval(pred, gt):
|
299 |
+
return mcq_eval(pred, gt)
|
300 |
+
|
301 |
+
def celebvqa_doc_to_text(doc):
|
302 |
+
return our_doc_to_text(doc)
|
303 |
+
|
304 |
+
def celebvqa_eval(pred, gt):
|
305 |
+
return mcq_eval(pred, gt)
|
306 |
+
|
307 |
+
def countriesvqa_doc_to_text(doc):
|
308 |
+
return our_doc_to_text(doc)
|
309 |
+
|
310 |
+
def countriesvqa_eval(pred, gt):
|
311 |
+
return mcq_eval(pred, gt)
|
312 |
+
|
313 |
+
def foodvqa_doc_to_text(doc):
|
314 |
+
return our_doc_to_text(doc)
|
315 |
+
|
316 |
+
def foodvqa_eval(pred, gt):
|
317 |
+
return mcq_eval(pred, gt)
|
318 |
+
|
319 |
+
def objectcoco_doc_to_text(doc):
|
320 |
+
return doc['question']
|
321 |
+
|
322 |
+
def objectcoco_eval(pred, gt):
|
323 |
+
return mcq_eval(pred, gt)
|
324 |
+
|
325 |
+
def blink_doc_to_text(doc):
|
326 |
+
return doc['question']
|
327 |
+
|
328 |
+
def blink_eval(pred, gt):
|
329 |
+
return mcq_eval(pred, gt)
|
330 |
+
|
331 |
+
def examsv_doc_to_text(doc):
|
332 |
+
return doc['question']
|
333 |
+
|
334 |
+
def examsv_eval(pred, gt):
|
335 |
+
return mcq_eval(pred, gt)
|
336 |
+
|
337 |
+
def chartqa_doc_to_text(doc):
|
338 |
+
return doc['question']
|
339 |
+
|
340 |
+
def chartqa_eval(pred, gt):
|
341 |
+
return mcq_eval(pred, gt)
|
342 |
+
|
343 |
+
def mtvqa_doc_to_text(doc):
|
344 |
+
return doc['question']
|
345 |
+
|
346 |
+
def create_one_query(problem):
|
347 |
+
demo_prompt = ""
|
348 |
+
|
349 |
+
question = problem["question"]
|
350 |
+
unit = problem["unit"]
|
351 |
+
choices = problem["choices"]
|
352 |
+
precision = problem["precision"]
|
353 |
+
question_type = problem["question_type"]
|
354 |
+
answer_type = problem["answer_type"]
|
355 |
+
|
356 |
+
if question_type == "multi_choice":
|
357 |
+
assert answer_type == "text"
|
358 |
+
hint_text = f"تلميح: يرجى الإجابة على السؤال وتقديم حرف الخيار الصحيح، مثل أ أو ب أو ج أو د، في النهاية."
|
359 |
+
else:
|
360 |
+
assert answer_type in ["integer", "float", "list"]
|
361 |
+
if answer_type == "integer":
|
362 |
+
hint_text = f"تلميح: يرجى الإجابة على السؤال الذي يتطلب إجابة بعدد صحيح وتقديم القيمة النهائية، مثل 1 أو 2 أو 3، في النهاية."
|
363 |
+
|
364 |
+
elif answer_type == "float" and precision == 1:
|
365 |
+
hint_text = f"تلميح: يرجى الإجابة على السؤال الذي يتطلب رقمًا عشريًا بمنزلة عشرية واحدة وتقديم القيمة النهائية، مثل 1.2 أو 1.3 أو 1.4، في النهاية."
|
366 |
+
|
367 |
+
elif answer_type == "float" and precision == 2:
|
368 |
+
hint_text = f"تلميح: يرجى الإجابة على السؤال الذي يتطلب رقمًا عشريًا بمنزلتين عشريتين وتقديم القيمة النهائية، مثل 1.23 أو 1.34 أو 1.45، في النهاية."
|
369 |
+
|
370 |
+
elif answer_type == "list":
|
371 |
+
hint_text = f"تلميح: يرجى الإجابة على السؤال الذي يتطلب قائمة بايثون كإجابة وتقديم القائمة النهائية، مثل [1, 2, 3] أو [1.2, 1.3, 1.4]، في النهاية."
|
372 |
+
|
373 |
+
hint_text = translate_numbers(hint_text)
|
374 |
+
|
375 |
+
question_text = f"سؤال: {question}"
|
376 |
+
if unit:
|
377 |
+
question_text += f" (الوحدة: {unit})"
|
378 |
+
|
379 |
+
if choices and choices != 'None':
|
380 |
+
texts = ["الاختيارات:"]
|
381 |
+
choices = ast.literal_eval(choices.replace("' '", '", "'))
|
382 |
+
for i, choice in enumerate(choices):
|
383 |
+
texts.append(f"({arabic_letters[chr(ord('A')+i)]}) {choice}")
|
384 |
+
choices_text = "\n".join(texts)
|
385 |
+
else:
|
386 |
+
choices_text = ""
|
387 |
+
|
388 |
+
|
389 |
+
prompt = "الحل: "
|
390 |
+
|
391 |
+
elements = [question_text, choices_text, hint_text, prompt]
|
392 |
+
test_query = "\n".join([e for e in elements if e != ""])
|
393 |
+
|
394 |
+
query = demo_prompt + "\n\n" + test_query
|
395 |
+
query = query.strip()
|
396 |
+
return query
|
397 |
+
|
398 |
+
def mathvista_doc_to_text(doc):
|
399 |
+
problem = {
|
400 |
+
"question_type": doc["question_type"],
|
401 |
+
"answer_type": doc["answer_type"],
|
402 |
+
"question": doc["question"],
|
403 |
+
"unit": doc["unit"] if "unit" in doc else "",
|
404 |
+
"choices": doc["choices"],
|
405 |
+
"answer": doc["answer"] if "answer" in doc else None,
|
406 |
+
"precision": doc["precision"] if "precision" in doc else 0,
|
407 |
+
}
|
408 |
+
query_prompt = create_one_query(problem)
|
409 |
+
return query_prompt
|
410 |
+
|
411 |
+
|
412 |
+
def infographicsvqa_doc_to_text(doc):
|
413 |
+
return doc['question']
|
414 |
+
|
415 |
+
def infographicsvqa_eval(pred, gt):
|
416 |
+
return mcq_eval(pred, gt)
|
417 |
+
|
418 |
+
def agrovqa_doc_to_text(doc):
|
419 |
+
return doc['question']
|
420 |
+
|
421 |
+
def agrovqa_eval(pred, gt):
|
422 |
+
return mcq_eval(pred, gt)
|
423 |
+
|
424 |
+
def diagramsvqa_doc_to_text(doc):
|
425 |
+
return doc['question']
|
426 |
+
|
427 |
+
def diagramsvqa_eval(pred, gt):
|
428 |
+
return mcq_eval(pred, gt)
|
429 |
+
|
430 |
+
def tablesvqa_doc_to_text(doc):
|
431 |
+
return doc['question']
|
432 |
+
|
433 |
+
def tablesvqa_eval(pred, gt):
|
434 |
+
return mcq_eval(pred, gt)
|
435 |
+
|
436 |
+
def scienceqa_doc_to_text(doc):
|
437 |
+
context, question, choices = doc["hint"], doc["question"], doc["choices"]
|
438 |
+
choices = ast.literal_eval(choices)
|
439 |
+
len_choices = len(choices)
|
440 |
+
options = [arabic_letters[chr(ord("A") + i)] for i in range(len_choices)]
|
441 |
+
choices_str = "\n".join([f"{option}. {choice}" for option, choice in zip(options, choices)])
|
442 |
+
if context:
|
443 |
+
context = f"السياق: {context}\n"
|
444 |
+
|
445 |
+
post_prompt = "\n.أجب بحرف الخيار من الخيارات المعطاة مباشرة"
|
446 |
+
return f"{context}{question}\n{choices_str}{post_prompt}"
|
447 |
+
|
448 |
+
def scienceqa_eval(pred, gt):
|
449 |
+
gt = arabic_letters[chr(ord('A') + int(gt))]
|
450 |
+
return mcq_eval(pred, gt)
|
451 |
+
|
452 |
+
def ocrisi_doc_to_text(doc):
|
453 |
+
return doc['question']
|
454 |
+
|
455 |
+
def cer(pred, gt):
|
456 |
+
d = np.zeros((len(gt) + 1, len(pred) + 1))
|
457 |
+
for i in range(len(gt) + 1):
|
458 |
+
d[i, 0] = i
|
459 |
+
for j in range(len(pred) + 1):
|
460 |
+
d[0, j] = j
|
461 |
+
|
462 |
+
for i in range(1, len(gt) + 1):
|
463 |
+
for j in range(1, len(pred) + 1):
|
464 |
+
if gt[i-1] == pred[j-1]:
|
465 |
+
d[i, j] = d[i-1, j-1]
|
466 |
+
else:
|
467 |
+
substitution = d[i-1, j-1] + 1
|
468 |
+
insertion = d[i, j-1] + 1
|
469 |
+
deletion = d[i-1, j] + 1
|
470 |
+
d[i, j] = min(substitution, insertion, deletion)
|
471 |
+
|
472 |
+
error = d[len(gt), len(pred)]
|
473 |
+
total_chars = len(gt)
|
474 |
+
cer = error / total_chars
|
475 |
+
|
476 |
+
return cer
|
477 |
+
|
478 |
+
from fast_edit_distance import edit_distance
|
479 |
+
def fastcer(pred, gt):
|
480 |
+
ed = edit_distance(pred, gt, max_ed=1000)
|
481 |
+
if len(pred) > len(gt):
|
482 |
+
return ed / len(pred)
|
483 |
+
return ed /len(gt)
|
484 |
+
|
485 |
+
|
486 |
+
def ocrisi_eval(pred: str, gt: str):
|
487 |
+
pred = pred.strip()
|
488 |
+
gt = gt.strip()
|
489 |
+
return fastcer(pred, gt) <= 0.3
|
490 |
+
|
491 |
+
def evarest_doc_to_text(doc):
|
492 |
+
return doc['question']
|
493 |
+
|
494 |
+
def evarest_eval(pred: str, gt: str):
|
495 |
+
pred = pred.strip()
|
496 |
+
gt = gt.strip()
|
497 |
+
return fastcer(pred, gt) <= 0.5
|
498 |
+
|
499 |
+
def historicalbooks_doc_to_text(doc):
|
500 |
+
return doc['question']
|
501 |
+
|
502 |
+
def historicalbooks_eval(pred: str, gt: str):
|
503 |
+
pred = pred.strip()
|
504 |
+
gt = gt.strip()
|
505 |
+
return fastcer(pred, gt) <= 0.2
|
506 |
+
|
507 |
+
def khatt_doc_to_text(doc):
|
508 |
+
return doc['question']
|
509 |
+
|
510 |
+
def khatt_eval(pred: str, gt: str):
|
511 |
+
pred = pred.strip()
|
512 |
+
gt = gt.strip()
|
513 |
+
return fastcer(pred, gt) <= 0.3
|
514 |
+
|
515 |
+
def patsocr_doc_to_text(doc):
|
516 |
+
return doc['question']
|
517 |
+
|
518 |
+
def patsocr_eval(pred: str, gt: str):
|
519 |
+
pred = pred.strip()
|
520 |
+
gt = gt.strip()
|
521 |
+
return fastcer(pred, gt) <= 0.5
|
522 |
+
|
523 |
+
def arabicocr_doc_to_text(doc):
|
524 |
+
return doc['question']
|
525 |
+
|
526 |
+
def arabicocr_eval(pred: str, gt: str):
|
527 |
+
pred = pred.strip()
|
528 |
+
gt = gt.strip()
|
529 |
+
return fastcer(pred, gt) <= 0.4
|
530 |
+
|
531 |
+
|
532 |
+
def culturevideovqa_doc_to_text(doc):
|
533 |
+
return doc['question']
|
534 |
+
|
535 |
+
def culturevideovqa_eval(pred, gt):
|
536 |
+
return mcq_eval(pred, gt)
|
537 |
+
|
538 |
+
|
539 |
+
def videomme_doc_to_text(doc):
|
540 |
+
prompt = """الترجمات النصية لهذا الفيديو مدرجة أدناه:
|
541 |
+
{subtitles}
|
542 |
+
اختر أفضل إجابة للسؤال التالي متعدد الخيارات بناءً على الفيديو. أجب فقط بالحرف (أ، ب، ج، أو د) للخيار الصحيح.
|
543 |
+
{question}
|
544 |
+
{options}
|
545 |
+
أفضل إجابة هي:"""
|
546 |
+
subtitles = doc["subtitles"]
|
547 |
+
question = doc["question"]
|
548 |
+
options = doc["options"]
|
549 |
+
options_str = "\n".join([f"{option}" for i, option in enumerate(options)])
|
550 |
+
return prompt.format(subtitles=subtitles, question=question, options=options_str)
|
551 |
+
|
552 |
+
def videomme_eval(pred, gt):
|
553 |
+
return mcq_eval(pred, gt)
|
554 |
+
|
555 |
+
def geochat_doc_to_text(doc):
|
556 |
+
pre_prompt = "أجب على السؤال التالي بكلمة أو جملة.\n"
|
557 |
+
return pre_prompt + doc['question']
|
558 |
+
|
559 |
+
def muribench_doc_to_text(doc):
|
560 |
+
return doc['question']
|
561 |
+
|
562 |
+
def muribench_eval(pred, gt):
|
563 |
+
return mcq_eval(pred, gt)
|
564 |
+
|
565 |
+
def mmlu_doc_to_text(doc):
|
566 |
+
question = doc['Question']
|
567 |
+
options = []
|
568 |
+
for i in range(1, 6):
|
569 |
+
if doc[f'Option {i}'] is not None:
|
570 |
+
options.append(doc[f'Option {i}'])
|
571 |
+
parsed_options = mmmu_parse_options(options)
|
572 |
+
context = doc['Context']
|
573 |
+
if context:
|
574 |
+
return f"{question}\n{CONTEXT_KEYWORD}: {context}\n{parsed_options}\n{MMMU_MULTI_CHOICE_PROMPT}"
|
575 |
+
return f"{question}\n{parsed_options}\n{MMMU_MULTI_CHOICE_PROMPT}"
|
576 |
+
|
577 |
+
def mmlu_eval(pred, gt):
|
578 |
+
return mcq_eval(pred, gt)
|
579 |
+
|
580 |
+
def henna_doc_to_text(doc):
|
581 |
+
return doc['question']
|
582 |
+
|
583 |
+
|
584 |
+
mtvqa_user_prompt = """Here are three examples to guide your evaluation:
|
585 |
+
Example 1:
|
586 |
+
Question: "ما هي اللغة المستخدمة في النص؟"
|
587 |
+
Predicted Answer: "العربية"
|
588 |
+
Ground Truth: "اللغة العربية"
|
589 |
+
Score: 1
|
590 |
+
|
591 |
+
Example 2:
|
592 |
+
Question: "ما هو موضوع النص؟"
|
593 |
+
Predicted Answer: "إثنان"
|
594 |
+
Ground Truth: "الحب و الكراهية"
|
595 |
+
Score: 0
|
596 |
+
|
597 |
+
Example 3:
|
598 |
+
Question: "ما هو عدد صفحات الكتاب؟"
|
599 |
+
Predicted Answer: "الصورة لا تحتوي على عدد صفحات الكتاب."
|
600 |
+
Ground Truth: "غير معروف"
|
601 |
+
Score: 1
|
602 |
+
|
603 |
+
Now, for each new pair of sentences, analyze their semantic similarity and provide a score of 1 for similar meanings or 0 for different meanings. Always consider the context and potential variations in expressing the same concept.
|
604 |
+
Question: "{question}"
|
605 |
+
Predicted Answer: "{pred}"
|
606 |
+
Ground Truth: "{gt}"
|
607 |
+
Score: """
|
608 |
+
|
609 |
+
geochat_user_prompt = """Here are three examples to guide your evaluation:
|
610 |
+
Example 1:
|
611 |
+
Question: "أجب على السؤال التالي بكلمة أو جملة.\n<image>\n كم عدد قوارب الصيد الموجودة في الصورة؟"
|
612 |
+
Predicted Answer: "قارب صيد واحد"
|
613 |
+
Ground Truth: "يوجد قارب صيد واحد في الصورة."
|
614 |
+
Score: 1
|
615 |
+
|
616 |
+
Example 2:
|
617 |
+
Question: "أجب على السؤال التالي بكلمة أو جملة.\n<image>\nكم عدد السيارات الصغيرة الموجودة في الصورة؟"
|
618 |
+
Predicted Answer: "عدد السيارات الصغيرة هو 10."
|
619 |
+
Ground Truth: "هناك سيارة صغيرة واحدة في الصورة."
|
620 |
+
Score: 0
|
621 |
+
|
622 |
+
Example 3:
|
623 |
+
Question: "أجب على السؤال التالي بكلمة أو جملة.\n<image>\nهل يمكنك إخباري بعدد الأشجار الموجودة في أعلى الصورة؟"
|
624 |
+
Predicted Answer: "توجد عدة أشجار في أع��ى الصورة، ومن الصعب تحديد العدد الدقيق بسبب تداخلها."
|
625 |
+
Ground Truth: "يوجد بعض الأشجار في الأعلى"
|
626 |
+
Score: 1
|
627 |
+
|
628 |
+
Now, for each new pair of sentences, analyze their semantic similarity and provide a score of 1 for similar meanings or 0 for different meanings. Always consider the context and potential variations in expressing the same concept.
|
629 |
+
Question: "{question}"
|
630 |
+
Predicted Answer: "{pred}"
|
631 |
+
Ground Truth: "{gt}"
|
632 |
+
Score: """
|
633 |
+
|
634 |
+
mathvista_user_prompt = """Here are three examples to guide your evaluation:
|
635 |
+
Example 1:
|
636 |
+
Question: "سؤال: أي كائن سيتأثر أكثر إذا تم القضاء على الطحالب؟\nالاختيارات:\n(أ) البلطي\n(ب) برغوث الماء الشائع\n(ج) خنفساء الغوص العظيمة\n(د) الشرغوف\nتلميح: يرجى الإجابة على السؤال وتقديم حرف الخيار الصحيح، مثل أ أو ب أو ج أو د، في النهاية.\nالحل: "
|
637 |
+
Predicted Answer: "ب) برغوث الماء الشائع\nالحل: ب"
|
638 |
+
Ground Truth: "برغوث الماء الشائع"
|
639 |
+
Score: 1
|
640 |
+
|
641 |
+
Example 2:
|
642 |
+
Question: "سؤال: تتقاطع منصفات الزاويتين الداخليتين للمثلث △ABC في النقطة O، إذا كانت ∠A = 110°، فكم تساوي ∠BOC؟\nالاختيارات:\n(أ) ١٣٥°\n(ب) ١٤٠°\n(ج) ١٤٥°\n(د) ١٥٠°\nتلميح: يرجى الإجابة على السؤال وتقديم حرف الخيار الصحيح، مثل أ أو ب أو ج أو د، في النهاية.\nالحل: "
|
643 |
+
Predicted Answer: "لحساب ∠BOC، نستخدم حقيقة أن مجموع الزوايا في المثلث هو 180°. إذا كانت زاوية A = 110°، فإن مجموع الزاويتين B + C هو 70° (لأن 180° - 110° = 70°).<br>\nعندما تتقاطع منصفات الزاويتين B وC عند النقطة O، يمكن حساب ∠BOC باستخدام العلاقة الخاصة بزوايا المنصفات:\n2∠BOC = 180° + ∠A.<br>\nلذا:<br>\n2∠BOC = 180° + 110° = 290°<br>\n∠BOC = 290° / 2 = 145°<br>\nوبذلك، الخيار الصحيح هو (ج) ١٤٥°."
|
644 |
+
Ground Truth: "ب"
|
645 |
+
Score: 0
|
646 |
+
|
647 |
+
Example 3:
|
648 |
+
Question: "سؤال: مشتقة f(x) عند x=2 هي ____ عند x=5\nالاختيارات:\n(أ) أكبر من\n(ب) تساوي\n(ج) أصغر من\nتلميح: يرجى الإجابة على السؤال وتقديم حرف الخيار الصحيح، مثل أ أو ب أو ج أو د، في النهاية.\nالحل: "
|
649 |
+
Predicted Answer: "الوح بالاعتماد على توضيح الصورة، مشتقة الدالة f(x) = |2x - 3| + 1 عند نقاط مختلفة تعتمد على كيفية تغير ميل المنحنى. عند x=2 (يمين رأس الدالة)، الميل هو +2. عند x=5، الميل أيضًا +2 لأن هذا الجزء من الدالة خطي بزاوية صعودية مستمرة.\n\nلذا، مشتقة f(x) عند x=2 تساوي مشتقتها عند x=5.\n\nالإجابة الصحيحة هي (ب) تساوي."
|
650 |
+
Ground Truth: "تساوي"
|
651 |
+
Score: 1
|
652 |
+
|
653 |
+
Now, for each new pair of sentences, analyze their semantic similarity and provide a score of 1 for similar meanings or 0 for different meanings. Always consider the context and potential variations in expressing the same concept.
|
654 |
+
Question: "{question}"
|
655 |
+
Predicted Answer: "{pred}"
|
656 |
+
Ground Truth: "{gt}"
|
657 |
+
Score: """
|
658 |
+
|
659 |
+
vqav2_user_prompt = """Here are three examples to guide your evaluation:
|
660 |
+
Example 1:
|
661 |
+
Question: "ما لون الحوض؟\nأجب على السؤال باستخدام كلمة أو عبارة واحدة."
|
662 |
+
Predicted Answer: "أبيض"
|
663 |
+
Ground Truth: "أبيض"
|
664 |
+
Score: 1
|
665 |
+
|
666 |
+
Example 2:
|
667 |
+
Question: "كم عدد الأشخاص في الصورة؟\nأجب على السؤال باستخدام كلمة أو عبارة واحدة."
|
668 |
+
Predicted Answer: "اثنان"
|
669 |
+
Ground Truth: "٣"
|
670 |
+
Score: 0
|
671 |
+
|
672 |
+
Example 3:
|
673 |
+
Question: "ما هو شكل الطائرة الورقية؟\nأجب على السؤال باستخدام كلمة أو عبارة واحدة."
|
674 |
+
Predicted Answer: "سداسي"
|
675 |
+
Ground Truth: "سداسي الأضلاع"
|
676 |
+
Score: 1
|
677 |
+
|
678 |
+
Now, for each new pair of sentences, analyze their semantic similarity and provide a score of 1 for similar meanings or 0 for different meanings. Always consider the context and potential variations in expressing the same concept.
|
679 |
+
Question: "{question}"
|
680 |
+
Predicted Answer: "{pred}"
|
681 |
+
Ground Truth: "{gt}"
|
682 |
+
Score: """
|
683 |
+
|
684 |
+
|
685 |
+
|
686 |
+
henna_user_prompt = """Here are three examples to guide your evaluation:
|
687 |
+
Example 1:
|
688 |
+
Question: "هل تظهر علامات الحضارة البشرية مثل البناء أو الطرق في الصورة؟"
|
689 |
+
Predicted Answer: "لا"
|
690 |
+
Ground Truth: "لا، لا تظهر في الصورة علامات الحضارة البشرية مثل البناء أو الطرق، وتبدو المنطقة طبيعية وغير معبأة بالتطورات البشرية."
|
691 |
+
Score: 1
|
692 |
+
|
693 |
+
Example 2:
|
694 |
+
Question: "ما العناصر المعمارية المرئية على ��اب اليمن التي تدل على قدمه؟"
|
695 |
+
Predicted Answer: "أ"
|
696 |
+
Ground Truth: "العناصر المعمارية المرئية على باب اليمن والتي تدل على قدمه تشمل الأبراج الدفاعية المستديرة، الزخارف والنقوش على الجدران، والأقواس الضخمة للبوابة التي تعود إلى العمارة الإسلامية التقليدية."
|
697 |
+
Score: 0
|
698 |
+
|
699 |
+
Example 3:
|
700 |
+
Question: "ما هي المظاهر الثقافية أو الاجتماعية التي يمكن استنتاجها من الملابس التقليدية للأشخاص في الصورة؟"
|
701 |
+
Predicted Answer: "استناداً إلى الصورة، يمكن استنتاج عدة مظاهر ثقافية أو اجتماعية من الملابس التقليدية للأشخاص:\n\n1. الزي التقليدي: يرتدي العديد من الأشخاص في الصورة ملابس تقليدية، مما يشير إلى أن هذا قد يكون مهرجاناً أو احتفالاً ثقافياً أو ممارسة اجتماعية حيث يرتدي الناس ملابس تقليدية.\n\n2. التنوع في الملابس: تشير مجموعة الألوان والأنماط المختلفة للملابس إلى وجود تنوع ثقافي أو اجتماعي داخل المجتمع. قد يمثل هذا تنوعاً في الأصول أو الأعراق أو الممارسات الثقافية المختلفة.\n\n3. الأزياء الرسمية: تشير وجود أشخاص يرتدون ملابس رسمية، بما في ذلك الأوشحة، إلى أن هذا قد يكون حدثاً اجتماعياً أو ثقافياً رسمياً أو مهنياً.\n\n4. التجمعات الجماعية: يشير وجود العديد من الأشخاص الذين يرتدون ملابس تقليدية إلى أن"
|
702 |
+
Ground Truth: "الملابس التقليدية التي يرتديها الأشخاص تشير إلى التمسك بالتراث والثقافة اليمنية، والتي تعبر عن هوية المجتمع وتنوعه الثقافي."
|
703 |
+
Score: 1
|
704 |
+
|
705 |
+
Now, for each new pair of sentences, analyze their semantic similarity and provide a score of 1 for similar meanings or 0 for different meanings. Always consider the context and potential variations in expressing the same concept.
|
706 |
+
Question: "{question}"
|
707 |
+
Predicted Answer: "{pred}"
|
708 |
+
Ground Truth: "{gt}"
|
709 |
+
Score: """
|
710 |
+
|
711 |
+
fuzz_eval_system_prompt = "You are an expert in natural language understanding and semantic similarity. Your task is to evaluate the semantic similarity between two given sentences: a predicted answer and a ground truth answer. You should output a score of 1 if the sentences are semantically similar, and 0 if they are not."
|