Download data_synthetic_extended/scripts/publish.py from WMTH-100d2exam/hackathon-data: direct link, hf CLI and curl.
- Browser
- Download file 5.44 kB
-
https://huggingface.co/datasets/WMTH-100d2exam/hackathon-data/resolve/main/data_synthetic_extended/scripts/publish.py
- Command line
-
hf download hf://datasets/WMTH-100d2exam/hackathon-data/data_synthetic_extended/scripts/publish.py
-
curl -L -o publish.py https://huggingface.co/datasets/WMTH-100d2exam/hackathon-data/resolve/main/data_synthetic_extended/scripts/publish.py
5.44 kB
| #!/usr/bin/env python3 | |
| """Assemble the repo folder data_synthetic_extended/ (data part) from runs/ -> publish/. | |
| python publish.py # variants/, generated/, wiki_check/, inventory/ (+ qa.py writes stats.json, QA_REPORT.md) | |
| - variants/<epoch>/<type>_<k>.jsonl one file per data_synthetic chunk, same order as the originals, id <parent>-w1 | |
| - generated/<epoch>/<kind>_<k>.jsonl kind = mcq | tf | open | essay, 40 tasks per file (20 for essays), | |
| 'Zadanie N.' renumbered 1..40 inside each file | |
| - wiki_check/<epoch>.jsonl fact-check of every original task against Wikipedia (last record per id) | |
| - inventory/<epoch>.jsonl requirement -> Wikipedia articles (+ passages) and essay foci per chapter | |
| Internal fields (_file, pid) are dropped. | |
| """ | |
| import re | |
| from collections import defaultdict | |
| from common import EPOCHS, PUBLISH, RUNS, load_originals, read_jsonl, write_jsonl | |
| HDR = re.compile(r"^Zadanie \d+\.") | |
| TF_I = re.compile(r"Oceń prawdziwość|Zaznacz P|Wybierz P,|jeśli zdanie jest prawdziwe|prawdziwe, a które fałszywe", re.I) | |
| STD_TF = {"Oceń prawdziwość zdań. Zaznacz P, jeśli zdanie jest prawdziwe, albo F – jeśli jest fałszywe.", | |
| "Na podstawie tekstu i własnej wiedzy oceń prawdziwość zdań. Zaznacz P, jeśli zdanie jest prawdziwe, albo F – " | |
| "jeśli jest fałszywe."} | |
| def fix_tf(r): | |
| """Variants written before the intro check: the intro already is an instruction -> drop the standard one.""" | |
| if r.get("task_type") != "true_false": | |
| return r | |
| lines = r["question"].split("\n") | |
| if sum(1 for l in lines if TF_I.search(l)) >= 2: | |
| for i in range(len(lines) - 1, 0, -1): | |
| if lines[i] in STD_TF: | |
| del lines[i] | |
| if i < len(lines) and i > 0 and not lines[i - 1].strip() and not lines[i].strip(): | |
| del lines[i] | |
| break | |
| r = {**r, "question": "\n".join(lines)} | |
| return r | |
| def last_by(rows, key): | |
| d = {} | |
| for r in rows: | |
| d[r[key]] = r | |
| return d | |
| def clean(r): | |
| return {k: v for k, v in r.items() if not k.startswith("_")} | |
| def main(): | |
| order = {r["id"]: (r["_file"], i) for i, r in enumerate(load_originals())} | |
| # variants | |
| var = last_by(read_jsonl(RUNS / "variants" / "variants.jsonl"), "parent_id") | |
| files = defaultdict(list) | |
| for pid, v in var.items(): | |
| f, i = order[pid] | |
| files[f].append((i, fix_tf(clean(v)))) | |
| for d in (PUBLISH / "variants").glob("*/*.jsonl"): | |
| d.unlink() | |
| for f, rows in files.items(): | |
| ep, name = f.split("/") | |
| write_jsonl(PUBLISH / "variants" / ep / name.replace(".verified", ""), [r for _, r in sorted(rows, key=lambda x: x[0])]) | |
| # wiki_check | |
| wc = last_by(read_jsonl(RUNS / "variants" / "wiki_check.jsonl"), "id") | |
| for ep in EPOCHS: | |
| rows = [wc[k] for k in sorted(wc, key=lambda k: order.get(k, ("", 0))) if k.startswith(f"syn-{ep}-")] | |
| if rows: | |
| write_jsonl(PUBLISH / "wiki_check" / f"{ep}.jsonl", rows) | |
| # generated | |
| rows = read_jsonl(RUNS / "generate" / "generated.jsonl") | |
| for r in rows: # early essay ids lacked the chapter (collisions): rebuild ids from seed.unit | |
| kind = r["id"].split("-")[2] | |
| r["id"] = f"wik-{r['epoch']}-{kind}-{'-'.join(r['seed']['unit'].split('-')[2:])}-{r['id'].rsplit('-', 1)[-1]}" | |
| gen = last_by(rows, "id") | |
| by = defaultdict(list) | |
| for r in gen.values(): | |
| kind = r["id"].split("-")[2] | |
| by[(r["epoch"], kind)].append(r) | |
| for d in (PUBLISH / "generated").glob("*/*.jsonl"): | |
| d.unlink() | |
| n_gen = 0 | |
| for (ep, kind), rows in sorted(by.items()): | |
| rows.sort(key=lambda r: r["id"]) | |
| size = 20 if kind == "essay" else 40 | |
| for k in range(0, len(rows), size): | |
| chunk = [] | |
| for j, r in enumerate(rows[k:k + size]): | |
| r = dict(r) | |
| r["question"] = HDR.sub(f"Zadanie {j + 1}.", r["question"], count=1) | |
| chunk.append(r) | |
| write_jsonl(PUBLISH / "generated" / ep / f"{kind}_{k // size}.jsonl", chunk) | |
| n_gen += len(chunk) | |
| # inventory | |
| reqs = read_jsonl(RUNS / "inventory" / "reqs.jsonl") | |
| foci = read_jsonl(RUNS / "inventory" / "foci.jsonl") | |
| for ep in EPOCHS: | |
| rows = [] | |
| for r in (x for x in reqs if x["epoch"] == ep): | |
| rows.append({"type": "requirement", **{k: r[k] for k in ("req", "chapter", "chapter_title", "text", "issues")}, | |
| "articles": [{k: a.get(k) for k in ("title", "url", "snap", "kind", "purpose", "passages")} | |
| for a in r["articles"]]}) | |
| for f in (x for x in foci if x["epoch"] == ep): | |
| rows.append({"type": "essay_focus", **{k: f.get(k) for k in ("focus_id", "chapter", "chapter_title", "focus", | |
| "period", "requirements", "angles")}, | |
| "articles": [{k: a.get(k) for k in ("title", "url", "snap", "passages")} for a in f["articles"]]}) | |
| if rows: | |
| write_jsonl(PUBLISH / "inventory" / f"{ep}.jsonl", rows) | |
| print(f"published: variants {len(var)} in {len(files)} files, wiki_check {len(wc)}, generated {n_gen}, " | |
| f"inventory {len(reqs)} requirements + {len(foci)} foci -> {PUBLISH}") | |
| if __name__ == "__main__": | |
| main() | |