Add mathvision_adapt.py
Browse files- mathvision_adapt.py +40 -0
mathvision_adapt.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Helpers for scoring Misraj/MathVision_testmini with mm_scoring.py.
|
| 2 |
+
|
| 3 |
+
prepare_mathvision(row) -> dict of the extra fields mm_scoring.py needs
|
| 4 |
+
(question_type, answer_type, choices, answer_is_letter, precision, ...)
|
| 5 |
+
final_prompt(ex) -> the dataset `prompt` plus a short answer-format instruction
|
| 6 |
+
"""
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def prepare_mathvision(row):
|
| 11 |
+
opts = [str(o).strip() for o in (row.get("options") or []) if str(o).strip()]
|
| 12 |
+
gold = str(row["answer"]).strip()
|
| 13 |
+
is_mc = bool(opts)
|
| 14 |
+
letters = [chr(65 + i) for i in range(len(opts))]
|
| 15 |
+
bare = is_mc and all(re.fullmatch(r"\(?[A-Za-z]\)?\.?", o) for o in opts)
|
| 16 |
+
if is_mc:
|
| 17 |
+
qtype, atype, prec = "multi_choice", "text", 2
|
| 18 |
+
choices = [""] * len(opts) if bare else opts # bare letters: option values are in the image
|
| 19 |
+
else:
|
| 20 |
+
qtype, choices, g = "free_form", [], gold.replace(",", "")
|
| 21 |
+
if re.fullmatch(r"-?\d+", g):
|
| 22 |
+
atype, prec = "integer", 0
|
| 23 |
+
elif re.fullmatch(r"-?\d*\.\d+", g):
|
| 24 |
+
atype, prec = "float", len(g.split(".")[1])
|
| 25 |
+
else:
|
| 26 |
+
atype, prec = "text", 2
|
| 27 |
+
return {"question_type": qtype, "answer_type": atype, "choices": choices,
|
| 28 |
+
"answer": gold, "answer_raw": gold,
|
| 29 |
+
"answer_is_letter": is_mc and gold.upper() in letters,
|
| 30 |
+
"precision": prec, "unit": "",
|
| 31 |
+
"task": str(row.get("subject", "")), "category": str(row.get("subject", "")),
|
| 32 |
+
"context": "", "grade": str(row.get("level", "")), "skills": [str(row.get("subject", ""))]}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def final_prompt(ex):
|
| 36 |
+
if ex["question_type"] == "multi_choice":
|
| 37 |
+
tail = "\n\nThink step by step, then give the final answer as the option letter only, in the form \\boxed{A}."
|
| 38 |
+
else:
|
| 39 |
+
tail = "\n\nThink step by step, then give the final answer only, in the form \\boxed{...}."
|
| 40 |
+
return ex["prompt"] + tail
|