Upload scripts/convert_reasoning.py with huggingface_hub
Browse files- scripts/convert_reasoning.py +123 -0
scripts/convert_reasoning.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Convert BIG-bench (tasksource mirror) + LogiQA + ReClor into Alea canonical rows.
|
| 2 |
+
All are MCQ with exact labels -> choice rows. Group-disjoint dev via hash split."""
|
| 3 |
+
import hashlib, json, random, re
|
| 4 |
+
from collections import Counter
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
|
| 7 |
+
random.seed(7)
|
| 8 |
+
D = "/root/alea/data/"
|
| 9 |
+
rows, per_src, fps = [], Counter(), set()
|
| 10 |
+
|
| 11 |
+
BB_CONFIGS = ["logical_deduction", "tracking_shuffled_objects", "web_of_lies",
|
| 12 |
+
"boolean_expressions", "date_understanding", "temporal_sequences",
|
| 13 |
+
"multistep_arithmetic_two", "navigate", "hyperbaton", "dyck_languages",
|
| 14 |
+
"reasoning_about_colored_objects", "penguins_in_a_table",
|
| 15 |
+
"formal_fallacies", "minute_mysteries_qa", "snarks",
|
| 16 |
+
"analytic_entailment", "general_knowledge", "disambiguation_qa"]
|
| 17 |
+
CAP = 2600
|
| 18 |
+
|
| 19 |
+
def gid(*xs):
|
| 20 |
+
return hashlib.sha256("|".join(xs).encode()).hexdigest()[:16]
|
| 21 |
+
|
| 22 |
+
for cfg in BB_CONFIGS:
|
| 23 |
+
n = 0
|
| 24 |
+
for split in ("train", "validation"):
|
| 25 |
+
try:
|
| 26 |
+
ds = load_dataset("tasksource/bigbench", cfg, split=split, streaming=True)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(cfg, split, "ERR", str(e)[:60]); continue
|
| 29 |
+
for r in ds:
|
| 30 |
+
if n >= CAP:
|
| 31 |
+
break
|
| 32 |
+
opts = [str(o).strip() for o in (r.get("multiple_choice_targets") or [])]
|
| 33 |
+
sc = r.get("multiple_choice_scores") or []
|
| 34 |
+
if len(opts) < 2 or len(opts) != len(sc) or len(opts) > 14:
|
| 35 |
+
# non-MCQ: fall back to noul verification on the text target
|
| 36 |
+
t = r.get("targets") or []
|
| 37 |
+
if not t:
|
| 38 |
+
continue
|
| 39 |
+
ans = str(t[0]).strip()
|
| 40 |
+
if len(ans) > 60 or not ans:
|
| 41 |
+
continue
|
| 42 |
+
correct = random.random() < 0.5
|
| 43 |
+
shown = ans if correct else (ans + "x" if ans else "x")
|
| 44 |
+
if not correct and shown == ans:
|
| 45 |
+
continue
|
| 46 |
+
state = {"task": cfg.replace("_", " "), "problem": str(r["inputs"]).strip()[:1800]}
|
| 47 |
+
qs = {"q": {"type": "noul",
|
| 48 |
+
"instructions": f"Is the proposed answer correct? Proposed answer: {shown}",
|
| 49 |
+
"criteria": {}}}
|
| 50 |
+
tg = {"q": float(correct)}
|
| 51 |
+
kind = "noul"
|
| 52 |
+
else:
|
| 53 |
+
ti = sc.index(max(sc))
|
| 54 |
+
state = {"task": cfg.replace("_", " "), "problem": str(r["inputs"]).strip()[:1800]}
|
| 55 |
+
crit = {chr(65 + i): o for i, o in enumerate(opts)}
|
| 56 |
+
qs = {"q": {"type": "choice",
|
| 57 |
+
"instructions": "Choose the correct answer.",
|
| 58 |
+
"criteria": crit}}
|
| 59 |
+
vec = [0.0] * len(opts); vec[ti] = 1.0
|
| 60 |
+
tg = {"q": vec}
|
| 61 |
+
g = f"bb:{cfg}:{r.get('idx', n)}"
|
| 62 |
+
fp = gid(state["problem"], json.dumps(qs, sort_keys=True))
|
| 63 |
+
if fp in fps:
|
| 64 |
+
continue
|
| 65 |
+
fps.add(fp)
|
| 66 |
+
rows.append({"state": state, "id": g, "questions": qs, "targets": tg,
|
| 67 |
+
"source": f"bigbench:{cfg}", "group_id": g,
|
| 68 |
+
"family": f"bigbench:{cfg}", "split0": split})
|
| 69 |
+
n += 1
|
| 70 |
+
per_src[cfg] += 1
|
| 71 |
+
print(cfg, per_src[cfg])
|
| 72 |
+
|
| 73 |
+
# LogiQA
|
| 74 |
+
try:
|
| 75 |
+
for r in load_dataset("lucasmccabe/logiqa", split="train", streaming=True):
|
| 76 |
+
opts = [str(o).strip() for o in r["options"]]
|
| 77 |
+
if len(opts) < 2 or len(opts) > 10:
|
| 78 |
+
continue
|
| 79 |
+
try:
|
| 80 |
+
ti = int(r["correct_option"])
|
| 81 |
+
except Exception:
|
| 82 |
+
continue
|
| 83 |
+
state = {"context": str(r["context"]).strip()[:1400],
|
| 84 |
+
"question": str(r["query"]).strip()}
|
| 85 |
+
crit = {chr(65 + i): o for i, o in enumerate(opts)}
|
| 86 |
+
vec = [0.0] * len(opts); vec[ti] = 1.0
|
| 87 |
+
g = f"logiqa:{r.get('id', len(rows))}"
|
| 88 |
+
rows.append({"state": state, "id": g,
|
| 89 |
+
"questions": {"q": {"type": "choice", "instructions": "Choose the logically correct answer.", "criteria": crit}},
|
| 90 |
+
"targets": {"q": vec}, "source": "logiqa", "group_id": g,
|
| 91 |
+
"family": "logiqa"})
|
| 92 |
+
per_src["logiqa"] += 1
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print("logiqa ERR", e)
|
| 95 |
+
|
| 96 |
+
# ReClor
|
| 97 |
+
try:
|
| 98 |
+
for r in load_dataset("metaeval/reclor", split="train", streaming=True):
|
| 99 |
+
opts = [str(o).strip() for o in r["answers"]]
|
| 100 |
+
lab = r.get("label")
|
| 101 |
+
if lab is None or lab < 0 or len(opts) != 4:
|
| 102 |
+
continue
|
| 103 |
+
state = {"context": str(r["context"]).strip()[:1400],
|
| 104 |
+
"question": str(r["question"]).strip()}
|
| 105 |
+
crit = {chr(65 + i): o for i, o in enumerate(opts)}
|
| 106 |
+
vec = [0.0] * 4; vec[lab] = 1.0
|
| 107 |
+
g = f"reclor:{r.get('id_string', len(rows))}"
|
| 108 |
+
rows.append({"state": state, "id": g,
|
| 109 |
+
"questions": {"q": {"type": "choice", "instructions": "Choose the logically correct answer.", "criteria": crit}},
|
| 110 |
+
"targets": {"q": vec}, "source": "reclor", "group_id": g,
|
| 111 |
+
"family": "reclor"})
|
| 112 |
+
per_src["reclor"] += 1
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print("reclor ERR", e)
|
| 115 |
+
|
| 116 |
+
random.shuffle(rows)
|
| 117 |
+
dev = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] == "0"]
|
| 118 |
+
train = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] != "0"]
|
| 119 |
+
tg = {r["group_id"] for r in train}
|
| 120 |
+
dev = [r for r in dev if r["group_id"] not in tg]
|
| 121 |
+
open(D + "reason_train.jsonl", "w").write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in train))
|
| 122 |
+
open(D + "reason_dev.jsonl", "w").write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in dev[:4000]))
|
| 123 |
+
print("train", len(train), "dev", min(4000, len(dev)), dict(per_src))
|