Download convert_tasksource.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/aa04508695a0cbda565c64c62fe416b8ab2334b7/convert_tasksource.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs@aa04508695a0cbda565c64c62fe416b8ab2334b7/convert_tasksource.py
-
curl -L -o convert_tasksource.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/aa04508695a0cbda565c64c62fe416b8ab2334b7/convert_tasksource.py
2.9 kB
| import ast, hashlib, json, random | |
| from collections import Counter, defaultdict | |
| from datasets import load_dataset | |
| random.seed(42) | |
| MAX_PER_SOURCE = 200 | |
| MAX_ROWS = 90000 | |
| MAX_OPT = 12 | |
| def load_side(ds, rows, per_source, group_ids, fingerprints): | |
| for r in ds: | |
| src = r["source"] | |
| if per_source[src] >= MAX_PER_SOURCE: | |
| continue | |
| try: | |
| opts = ast.literal_eval(r["options"]) if isinstance(r["options"], str) else r["options"] | |
| tgt = ast.literal_eval(r["target"]) if isinstance(r["target"], str) else r["target"] | |
| except Exception: | |
| continue | |
| if not isinstance(opts, list) or not (2 <= len(opts) <= MAX_OPT): | |
| continue | |
| if not isinstance(tgt, list) or len(tgt) != len(opts): | |
| continue | |
| if sum(1 for t in tgt if t and t > 0) != 1: | |
| continue | |
| try: | |
| tgt = [float(t) for t in tgt] | |
| except Exception: | |
| continue | |
| st = r["state"] if isinstance(r["state"], str) else json.dumps(r["state"], sort_keys=True) | |
| fp = hashlib.sha256(f"{st}|{r['question']}|{opts}".encode()).hexdigest() | |
| if fp in fingerprints: | |
| continue | |
| fingerprints.add(fp) | |
| group_ids.add(r["group_id"]) | |
| rows.append({"state": st, "id": r["id"], "kind": r["kind"], | |
| "options": [str(o) for o in opts], "target": tgt, | |
| "question": r["question"], "source": f"tasksource:{src}", | |
| "group_id": r["group_id"]}) | |
| per_source[src] += 1 | |
| if len(rows) >= MAX_ROWS: | |
| return True | |
| return False | |
| rows, per_source, group_ids, fps = [], Counter(), set(), set() | |
| print("streaming train...") | |
| done = load_side(load_dataset("tasksource/tasksource-jev-typed-decisions", split="train", streaming=True), | |
| rows, per_source, group_ids, fps) | |
| random.shuffle(rows) | |
| train = [r for r in rows if not r["source"].startswith("DEVMARK")] | |
| dev = [r for r in rows if r["source"].startswith("DEVMARK")] | |
| train, dev = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] != "0"], \ | |
| [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] == "0"] | |
| train, dev = train[:76000], dev[:6000] | |
| used_groups = {r["group_id"] for r in train} | |
| dev = [r for r in dev if r["group_id"] not in used_groups] | |
| with open("/root/alea/data/tasksource_train.jsonl", "w") as f: | |
| for r in train: | |
| f.write(json.dumps(r) + "\n") | |
| with open("/root/alea/data/tasksource_dev.jsonl", "w") as f: | |
| for r in dev: | |
| f.write(json.dumps(r) + "\n") | |
| rep = {"train": len(train), "dev": len(dev), "sources": len(per_source), | |
| "kinds": Counter(r["kind"] for r in train + dev), | |
| "top_sources": per_source.most_common(20)} | |
| open("/root/alea/data/tasksource_conversion_report.json", "w").write(json.dumps(rep, indent=2, default=str)) | |
| print(rep) | |