Spaces:
Running
Running
Download scripts/export_rollouts.py from ruhzi/foresightbench-rl: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/spaces/ruhzi/foresightbench-rl/resolve/main/scripts/export_rollouts.py
- Command line
-
hf download hf://spaces/ruhzi/foresightbench-rl/scripts/export_rollouts.py
-
curl -L -o export_rollouts.py https://huggingface.co/spaces/ruhzi/foresightbench-rl/resolve/main/scripts/export_rollouts.py
3.62 kB
| """Export Beam's 71 ForesightBench eval rollouts (plus headline numbers) for the static Space. | |
| python scripts/export_rollouts.py --trajectories ../data/trajectories \ | |
| --eval ../benchmark-release/eval_set.jsonl --out site/data/ | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import glob | |
| import json | |
| from pathlib import Path | |
| from statistics import mean | |
| from foresightbench_env.data import load_split | |
| from foresightbench_env.scoring import score_completion | |
| ROOT = Path(__file__).resolve().parents[1] | |
| PATTERN = "*_reflection_beam-1.0-501b-a23b_{sid}.json" | |
| PAPER_BEAM_ERROR = 0.292 | |
| PAPER_COPY_BASELINE_ERROR = 0.327 | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--trajectories", type=Path, required=True) | |
| ap.add_argument("--eval", type=Path, required=True) | |
| ap.add_argument("--out", type=Path, default=ROOT / "site" / "data") | |
| a = ap.parse_args() | |
| eval_rows = [json.loads(l) for l in a.eval.open(encoding="utf-8") if l.strip()] | |
| rl_eval_ids = {r["sample_id"] for r in load_split("eval")} | |
| n_train = len(load_split("train")) | |
| rollouts = [] | |
| for row in eval_rows: | |
| files = sorted(glob.glob(str(a.trajectories / PATTERN.format(sid=row["sample_id"])))) | |
| if not files: | |
| raise SystemExit(f"missing Beam trajectory for {row['sample_id']}") | |
| traj = json.loads(Path(files[-1]).read_text(encoding="utf-8")) | |
| msg = traj["response"]["choices"][0]["message"] | |
| content = msg.get("content") or "" | |
| s = score_completion(content, row) | |
| rollouts.append({ | |
| "sample_id": row["sample_id"], | |
| "arxiv_id": row["arxiv_id"], | |
| "title": row["title"], | |
| "area": row["meta"]["area"], | |
| "component": row["meta"].get("component"), | |
| "prompt": row["prompt"], | |
| "reasoning": msg.get("reasoning") or msg.get("reasoning_content") or "", | |
| "content": content, | |
| "asked": row["asked"], | |
| "predictions": s["values"], | |
| "truth": [t["method_value"] for t in row["target"]], | |
| "baseline": [t["baseline_value"] for t in row["target"]], | |
| "slot_errors": [round(e, 4) for e in s["slot_errors"]], | |
| "score": round(s["numeric_reward"], 4), | |
| "parse_ok": s["parse_ok"], | |
| "in_rl_eval": row["sample_id"] in rl_eval_ids, | |
| "usage": traj["response"].get("usage", {}), | |
| "trace_file": Path(files[-1]).name, | |
| }) | |
| beam_err = mean(1 - r["score"] for r in rollouts) | |
| if abs(beam_err - PAPER_BEAM_ERROR) > 0.005: | |
| raise SystemExit(f"recomputed Beam error {beam_err:.4f} is not within 0.005 of {PAPER_BEAM_ERROR}") | |
| headline = { | |
| "beam_eval_error": round(beam_err, 4), | |
| "copy_baseline_error": PAPER_COPY_BASELINE_ERROR, | |
| "n_eval": len(rollouts), | |
| "n_rl_train": n_train, | |
| "n_rl_eval": len(rl_eval_ids), | |
| "n_papers": len({r["arxiv_id"] for r in rollouts}), | |
| "beam_direction_right_pct": None, | |
| "by_area": { | |
| area: round(mean(1 - r["score"] for r in rollouts if r["area"] == area), 3) | |
| for area in sorted({r["area"] for r in rollouts}) | |
| }, | |
| } | |
| a.out.mkdir(parents=True, exist_ok=True) | |
| (a.out / "rollouts.json").write_text(json.dumps(rollouts, ensure_ascii=False), encoding="utf-8") | |
| (a.out / "headline.json").write_text(json.dumps(headline, indent=1), encoding="utf-8") | |
| print(json.dumps(headline, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |