Datasets:
Download klein_code_bench/cli.py from klein-ml/klein-code-bench: direct link, hf CLI and curl.
- Browser
- Download file 5.05 kB
-
https://huggingface.co/datasets/klein-ml/klein-code-bench/resolve/main/klein_code_bench/cli.py
- Command line
-
hf download hf://datasets/klein-ml/klein-code-bench/klein_code_bench/cli.py
-
curl -L -o cli.py https://huggingface.co/datasets/klein-ml/klein-code-bench/resolve/main/klein_code_bench/cli.py
5.05 kB
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| from .runner import evaluate, find_task, load_tasks | |
| def write_json(path, value): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text( | |
| json.dumps(value, indent=2, ensure_ascii=False) + "\n", encoding="utf-8" | |
| ) | |
| def main(argv=None): | |
| parser = argparse.ArgumentParser( | |
| prog="klein-bench", | |
| description="Klein-Code-Bench: 260 from-scratch Python projects. No Docker.", | |
| ) | |
| commands = parser.add_subparsers(dest="command", required=True) | |
| listing = commands.add_parser("list", help="List task IDs and titles") | |
| listing.add_argument("--subset", choices=["easy", "intermediate", "hard", "max"]) | |
| for name in ("prepare", "reference", "evaluate"): | |
| p = commands.add_parser(name) | |
| p.add_argument("task_id") | |
| if name == "evaluate": | |
| p.add_argument("--solution", type=Path, required=True) | |
| p.add_argument("--output", type=Path) | |
| p.add_argument("--fail-fast", action="store_true") | |
| p.add_argument( | |
| "--public-only", | |
| action="store_true", | |
| help="Smoke test only; never an official task pass", | |
| ) | |
| else: | |
| p.add_argument("--output", type=Path, required=True) | |
| validation = commands.add_parser( | |
| "validate", | |
| help="Validate manifest, all goldens, references and empty submissions", | |
| ) | |
| validation.add_argument("--subset", choices=["easy", "intermediate", "hard", "max"]) | |
| validation.add_argument( | |
| "--output", type=Path, default=Path("results/validation.json") | |
| ) | |
| commands.add_parser( | |
| "build", help="Rebuild dataset reproducibly from authored source" | |
| ) | |
| score = commands.add_parser( | |
| "score", help="Aggregate one JSON evaluation report per submitted task" | |
| ) | |
| score.add_argument("reports", nargs="+", type=Path) | |
| args = parser.parse_args(argv) | |
| try: | |
| if args.command == "list": | |
| for task in load_tasks(args.subset): | |
| print(task["task_id"] + "\t" + task["title"]) | |
| return 0 | |
| if args.command == "build": | |
| from .build import build | |
| print(json.dumps(build(), indent=2)) | |
| return 0 | |
| if args.command == "score": | |
| from .scoring import summarize | |
| print( | |
| json.dumps( | |
| summarize([json.loads(p.read_text()) for p in args.reports]), | |
| indent=2, | |
| ) | |
| ) | |
| return 0 | |
| if args.command == "validate": | |
| from .validation import validate | |
| report = validate(args.subset) | |
| write_json(args.output, report) | |
| print( | |
| json.dumps( | |
| {k: v for k, v in report.items() if k != "results"}, indent=2 | |
| ) | |
| ) | |
| return 0 if report["passed"] else 1 | |
| task = find_task(args.task_id) | |
| if args.command == "prepare": | |
| if args.output.exists() and any(args.output.iterdir()): | |
| raise ValueError("Prepare destination must be empty") | |
| args.output.mkdir(parents=True, exist_ok=True) | |
| (args.output / "project").mkdir() | |
| (args.output / "PROMPT.md").write_text( | |
| task["prompt"] + "\n", encoding="utf-8" | |
| ) | |
| public = {k: v for k, v in task.items() if k != "acceptance_tests"} | |
| write_json(args.output / "task.json", public) | |
| print(f"Empty project: {args.output / 'project'}") | |
| elif args.command == "reference": | |
| from .reference.export import export_reference | |
| print(export_reference(task["family"], task["subset"], args.output)) | |
| else: | |
| tests = None | |
| if args.public_only: | |
| examples = json.loads(task["public_tests"]) | |
| tests = [ | |
| { | |
| "name": "public-smoke-only", | |
| "sessions": [ | |
| { | |
| "requests": [e["request"] for e in examples], | |
| "expected": [e["response"] for e in examples], | |
| } | |
| ], | |
| } | |
| ] | |
| report = evaluate( | |
| task, args.solution, tests=tests, fail_fast=args.fail_fast | |
| ) | |
| report["evaluation_scope"] = "public" if args.public_only else "acceptance" | |
| if args.public_only: | |
| report["resolved"] = False | |
| if args.output: | |
| write_json(args.output, report) | |
| print(json.dumps(report, indent=2)) | |
| return 0 if report["passed"] else 1 | |
| return 0 | |
| except (OSError, ValueError) as error: | |
| print(f"klein-bench: {error}", file=sys.stderr) | |
| return 2 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |