Datasets:
File size: 2,040 Bytes
b3991a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/env python3
"""Convert the working JSONL files to the flat, viewer-friendly schema published on the Hub.
Every row gets the same columns and types:
id, split, contract_type, contract (JSON string), output (string), samples (list of strings),
inputs (JSON string), context, system_prompt, label, lang, category, difficulty, rationale, source
python hf/dataset/scripts/flatten.py hf/dataset/data/dev.jsonl hf/dataset/data/test.jsonl
"""
import json
import sys
from pathlib import Path
def flatten(row: dict, split: str) -> dict:
"""Returns one row in the published schema (the original row is not modified)."""
output = row["output"]
case = row.get("case") or {}
return {
"id": row["id"],
"split": split,
"contract_type": row["contract_type"],
"contract": json.dumps(row["contract"], ensure_ascii=False, sort_keys=True),
"output": output if isinstance(output, str) else "",
"samples": [str(s) for s in output] if isinstance(output, list) else [],
"inputs": json.dumps(case.get("inputs") or {}, ensure_ascii=False, sort_keys=True),
"context": case.get("context") or "",
"system_prompt": case.get("system_prompt") or "",
"label": row["label"],
"lang": row["lang"],
"category": row.get("category") or "regression-test",
"difficulty": row.get("difficulty") or "",
"rationale": row.get("rationale") or "",
"source": row.get("source") or "",
}
def main() -> None:
for name in sys.argv[1:]:
path = Path(name)
split = path.stem
rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()]
if rows and "split" in rows[0]:
print(f"{path}: already flat, skipped")
continue
path.write_text("".join(json.dumps(flatten(r, split), ensure_ascii=False) + "\n" for r in rows), encoding="utf-8")
print(f"{path}: {len(rows)} rows flattened")
if __name__ == "__main__":
main()
|