Z User
dpo-resplit: held-out DPO eval split added, aligned BY SURAH with the SFT eval set — train training/dpo.jsonl 153 rows (surah 2: 55, 19: 98), eval training/eval/dpo_eval.jsonl 32 rows (surah 6: 17, 9: 1, 14: 1, 60: 13); rows moved verbatim (verse_key multiset asserted), split_key preserved; scripts/resplit_dpo_by_surah.py + flatten_training.py extended (dpo_eval_flat 32); README: +dpo_eval +dpo_eval_flat configs (9 total), file table updated, stale judge-pending wording fixed (sweep complete, coverage full 185/185); verify G1-G6 PASS, validate_manifest PASS
0ea247b Download scripts/flatten_training.py from bravesigma/quran-md-data: direct link, hf CLI and curl.
- Browser
- Download file 3.36 kB
-
https://huggingface.co/datasets/bravesigma/quran-md-data/resolve/main/scripts/flatten_training.py
- Command line
-
hf download hf://datasets/bravesigma/quran-md-data/scripts/flatten_training.py
-
curl -L -o flatten_training.py https://huggingface.co/datasets/bravesigma/quran-md-data/resolve/main/scripts/flatten_training.py
3.36 kB
| #!/usr/bin/env python3 | |
| """ | |
| flatten_training.py — derive flat (string-only) variants of the training files | |
| for trainers that expect prompt/response strings instead of nested JSON objects. | |
| Nested objects remain canonical (training/*.jsonl); these are DERIVED artifacts: | |
| training/flat/sft_flat.jsonl {"prompt": instruction, "response": <compact JSON string of output>, "verse_key", "split_key"} | |
| training/flat/sft_eval_flat.jsonl same shape from training/eval/sft_eval.jsonl | |
| training/flat/dpo_flat.jsonl {"prompt": prompt, "chosen": <compact JSON string>, "rejected": <compact JSON string>, "verse_key", "split_key"} | |
| training/flat/dpo_eval_flat.jsonl same shape from training/eval/dpo_eval.jsonl (present when the eval split exists) | |
| Deterministic: compact separators, field order preserved, UTF-8, one row per line. | |
| Usage: python3 scripts/flatten_training.py | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent.parent | |
| FLAT = ROOT / "training/flat" | |
| def compact(obj) -> str: | |
| return json.dumps(obj, ensure_ascii=False, separators=(",", ":")) | |
| def load(p): | |
| return [json.loads(l) for l in p.open(encoding="utf-8") if l.strip()] | |
| def write(p, rows): | |
| p.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in rows), encoding="utf-8") | |
| def main(): | |
| FLAT.mkdir(exist_ok=True) | |
| sft = load(ROOT / "training/sft.jsonl") | |
| sev = load(ROOT / "training/eval/sft_eval.jsonl") | |
| dpo = load(ROOT / "training/dpo.jsonl") | |
| dpo_eval_p = ROOT / "training/eval/dpo_eval.jsonl" | |
| dpo_eval = load(dpo_eval_p) if dpo_eval_p.exists() else [] | |
| sft_flat = [] | |
| for r in sft: | |
| out = r["output"] | |
| sft_flat.append({ | |
| "prompt": r["instruction"], | |
| "response": compact(out), | |
| "verse_key": out.get("verse_key", ""), | |
| "split_key": r["split_key"], | |
| "grounded": r.get("grounded", True), | |
| }) | |
| sev_flat = [] | |
| for r in sev: | |
| out = r["output"] | |
| sev_flat.append({ | |
| "prompt": r["instruction"], | |
| "response": compact(out), | |
| "verse_key": out.get("verse_key", ""), | |
| "split_key": r["split_key"], | |
| "grounded": r.get("grounded", True), | |
| }) | |
| def dpo_to_flat(rows): | |
| out = [] | |
| for r in rows: | |
| ch = r["chosen"] | |
| if r.get("rejected") is None: | |
| sys.exit("dpo rejected side is not filled — run scripts/generate_dpo_negatives.py apply first") | |
| out.append({ | |
| "prompt": r["prompt"], | |
| "chosen": compact(ch), | |
| "rejected": compact(r["rejected"]), | |
| "failure_mode": r.get("failure_mode"), | |
| "verse_key": ch.get("verse_key", ""), | |
| "split_key": r["split_key"], | |
| }) | |
| return out | |
| dpo_flat = dpo_to_flat(dpo) | |
| dpo_eval_flat = dpo_to_flat(dpo_eval) | |
| write(FLAT / "sft_flat.jsonl", sft_flat) | |
| write(FLAT / "sft_eval_flat.jsonl", sev_flat) | |
| write(FLAT / "dpo_flat.jsonl", dpo_flat) | |
| if dpo_eval_flat: | |
| write(FLAT / "dpo_eval_flat.jsonl", dpo_eval_flat) | |
| print(f"flat written: sft_flat={len(sft_flat)} sft_eval_flat={len(sev_flat)} " | |
| f"dpo_flat={len(dpo_flat)} dpo_eval_flat={len(dpo_eval_flat)}") | |
| if __name__ == "__main__": | |
| main() | |