canary-eval / scripts /flatten.py
NagaYu's picture
canary-eval: dev 306 / held-out test 217, provenance, evaluation script and results
b3991a8 verified
Raw History Blame Contribute Delete
2.04 kB
#!/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()