Download alea/gen_hf4.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 6.15 kB
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/e67c4d92ae8411576b2bd589cb64bb1d7c40b218/alea/gen_hf4.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs@e67c4d92ae8411576b2bd589cb64bb1d7c40b218/alea/gen_hf4.py
-
curl -L -o gen_hf4.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/e67c4d92ae8411576b2bd589cb64bb1d7c40b218/alea/gen_hf4.py
6.15 kB
| """gen_hf4: third wave — science/commonsense MCQ + math word problems.""" | |
| import argparse, json, random | |
| from datasets import load_dataset | |
| rng = random.Random(77) | |
| def choice_row(state, instructions, options_texts, correct_idx, family, | |
| meta=None): | |
| n = len(options_texts) | |
| keys = ["opt_%d" % i for i in range(n)] | |
| seen, fo, fk, ni = {}, [], [], None | |
| for i, t in enumerate(options_texts): | |
| t = str(t).strip() | |
| if not t: | |
| t = "(empty %d)" % i | |
| if t in seen: | |
| if i == correct_idx: | |
| ni = seen[t] | |
| continue | |
| seen[t] = len(fo); fo.append(t); fk.append(keys[i]) | |
| if i == correct_idx: | |
| ni = len(fo) - 1 | |
| if ni is None or len(fo) < 2: | |
| return None | |
| tgt = [0.0] * len(fo); tgt[ni] = 1.0 | |
| return {"state": str(state)[:20000], | |
| "questions": {"q0": {"type": "choice", "instructions": instructions, | |
| "criteria": dict(zip(fk, fo))}}, | |
| "targets": {"q0": tgt}, "family": family, "meta": meta or {}} | |
| def gen_sciq(n, rng): | |
| ds = load_dataset("allenai/sciq", split="train") | |
| out = [] | |
| for r in ds: | |
| if len(out) >= n: | |
| break | |
| opts = [r["correct_answer"], r["distractor1"], r["distractor2"], | |
| r["distractor3"]] | |
| order = list(range(4)); rng.shuffle(order) | |
| shuffled = [opts[j] for j in order] | |
| ci = shuffled.index(r["correct_answer"]) | |
| st = "Question: %s\n(Support: %s)" % (r["question"], | |
| (r.get("support") or "")[:800]) | |
| row = choice_row(st, "Choose the correct answer.", shuffled, ci, | |
| "hf_sciq", meta={"src": "sciq"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| def gen_qasc(n, rng): | |
| ds = load_dataset("allenai/qasc", split="train") | |
| out = [] | |
| for r in ds: | |
| if len(out) >= n: | |
| break | |
| ch = r["choices"] | |
| opts, ci = ch["text"], ch["label"].index(r["answerKey"]) | |
| row = choice_row("Question: %s" % r["question"], | |
| "Choose the correct answer.", opts, ci, "hf_qasc", | |
| meta={"src": "qasc"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| def gen_arc(n, rng): | |
| ds = load_dataset("allenai/ai2_arc", "ARC-Challenge", split="train") | |
| out = [] | |
| for r in ds: | |
| if len(out) >= n: | |
| break | |
| ch = r["choices"] | |
| labels = ch["label"] | |
| opts = ch["text"] | |
| if r["answerKey"] not in labels: | |
| continue | |
| ci = labels.index(r["answerKey"]) | |
| row = choice_row("Question: %s" % r["question"], | |
| "Choose the correct answer.", opts, ci, "hf_arc", | |
| meta={"src": "arc_c"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| def gen_csqa(n, rng): | |
| ds = load_dataset("tau/commonsense_qa", split="train") | |
| out = [] | |
| for r in ds: | |
| if len(out) >= n: | |
| break | |
| ch = r["choices"] | |
| labels = ch["label"]; opts = ch["text"] | |
| if r["answerKey"] not in labels: | |
| continue | |
| ci = labels.index(r["answerKey"]) | |
| st = ("Context: %s\n\nQuestion: %s" % | |
| (r.get("question_concept", ""), r["question"])) | |
| row = choice_row(st, "Choose the most sensible answer.", opts, ci, | |
| "hf_csqa", meta={"src": "csqa"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| def gen_obqa(n, rng): | |
| ds = load_dataset("allenai/openbookqa", "main", split="train") | |
| out = [] | |
| for r in ds: | |
| if len(out) >= n: | |
| break | |
| ch = r["choices"]; opts = ch["text"] | |
| if r["answerKey"] not in ch["label"]: | |
| continue | |
| ci = ch["label"].index(r["answerKey"]) | |
| row = choice_row("Question: %s" % r["question_stem"], | |
| "Choose the correct answer.", opts, ci, "hf_obqa", | |
| meta={"src": "obqa"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| def gen_orca(n, rng): | |
| """Orca math word problems -> choice over perturbed numeric answers.""" | |
| ds = load_dataset("microsoft/orca-math-word-problems-200k", split="train") | |
| out = [] | |
| idxs = list(range(len(ds))); rng.shuffle(idxs) | |
| import re | |
| for i in idxs: | |
| if len(out) >= n: | |
| break | |
| r = ds[i] | |
| ans_txt = r["answer"] | |
| m = re.findall(r"-?\d[\d,]*\.?\d*", ans_txt.split("\n")[-1]) | |
| if not m: | |
| continue | |
| try: | |
| a = float(m[-1].replace(",", "")) | |
| except ValueError: | |
| continue | |
| cands = {a * f for f in (0.5, 0.8, 1.25, 1.5, 2.0)} | {a + d for d in (-2, -1, 1, 2, 10)} | |
| cands = [c for c in cands if abs(c - a) > max(0.4, 0.03 * abs(a))] | |
| rng.shuffle(cands) | |
| fmt = lambda x: ("%g" % x) | |
| opts = [fmt(x) for x in ([a] + cands[:3])] | |
| order = list(range(4)); rng.shuffle(order) | |
| shuffled = [opts[j] for j in order] | |
| ci = shuffled.index(fmt(a)) | |
| st = "Problem:\n%s" % r["question"].strip() | |
| row = choice_row(st, "Solve the problem and choose the exact answer.", | |
| shuffled, ci, "hf_orca", meta={"src": "orca_math"}) | |
| if row: | |
| out.append(row) | |
| return out | |
| GENS = {"sciq": gen_sciq, "qasc": gen_qasc, "arc": gen_arc, | |
| "csqa": gen_csqa, "obqa": gen_obqa, "orca": gen_orca} | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", default="data/hf4.jsonl") | |
| ap.add_argument("--per", type=int, default=6000) | |
| args = ap.parse_args() | |
| rows = [] | |
| for name, fn in GENS.items(): | |
| try: | |
| got = fn(args.per, rng) | |
| except Exception as e: | |
| print(name, "FAILED", type(e).__name__, str(e)[:120]); continue | |
| print(name, len(got)); rows += got | |
| rng.shuffle(rows) | |
| with open(args.out, "w") as f: | |
| for r in rows: | |
| f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| print("TOTAL", len(rows), "->", args.out) | |
| if __name__ == "__main__": | |
| main() | |