Spaces:
Running
Running
Download challenge_cli.py from kugguk/electrocheap: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/kugguk/electrocheap/resolve/main/challenge_cli.py
- Command line
-
hf download hf://spaces/kugguk/electrocheap/challenge_cli.py
-
curl -L -o challenge_cli.py https://huggingface.co/spaces/kugguk/electrocheap/resolve/main/challenge_cli.py
3.67 kB
| """Local export, scoring and callable adapter runner; no inference service by default. | |
| Adapter contract: predict(task: dict, system_prompt: str) -> list[{id, forecast}]. | |
| Use only a trusted local module. Credentials remain in the adapter's environment. | |
| """ | |
| import argparse | |
| import importlib | |
| import json | |
| from pathlib import Path | |
| import time | |
| from bundle import ensure_resources | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("operation", choices=["export", "score", "run"]) | |
| parser.add_argument("--country", choices=["PT", "DE", "AU"], default="PT") | |
| parser.add_argument("--input", type=Path) | |
| parser.add_argument("--output", required=True, type=Path) | |
| parser.add_argument("--adapter", help="Trusted local module:function; run only") | |
| parser.add_argument("--model-id") | |
| parser.add_argument("--revision") | |
| parser.add_argument("--adapter-revision", default="unrecorded") | |
| args = parser.parse_args() | |
| ensure_resources() | |
| from challenge import challenge_packet, challenge_score, challenge_export, digest | |
| if args.operation == "export": | |
| args.output.write_bytes(challenge_export(args.country)) | |
| return | |
| if args.operation == "score": | |
| if not args.input: | |
| parser.error("score requires --input predictions.json") | |
| report = challenge_score(json.loads(args.input.read_text())) | |
| args.output.write_text(json.dumps(report, indent=2, allow_nan=False)+"\n") | |
| return | |
| if not args.adapter or ":" not in args.adapter or not args.model_id or not args.revision: | |
| parser.error("run requires --adapter module:function --model-id ID --revision REVISION") | |
| module, function = args.adapter.split(":", 1) | |
| predict = getattr(importlib.import_module(module), function) | |
| packet = challenge_packet({"country": args.country}) | |
| submission = packet["response_template"] | |
| submission["model"] = {"id": args.model_id, "revision": args.revision, | |
| "adapter_revision": args.adapter_revision, "prompt_sha256": digest({"system": packet["system_prompt"], "tasks": packet["tasks"]})} | |
| predictions, attempts = [], [] | |
| for task in packet["tasks"]: | |
| started = time.monotonic() | |
| try: | |
| rows = predict(task, packet["system_prompt"]) | |
| expected = {t["id"] for t in task["targets"]} | |
| if not isinstance(rows, list) or len(rows) != len(expected) or any(not isinstance(r, dict) for r in rows) or {r.get("id") for r in rows} != expected: | |
| raise ValueError("Adapter must return every requested target exactly once.") | |
| # Exercise the scoring validator before accepting malformed adapter values. | |
| check = {**packet["demo_submission"], "predictions": [r for r in packet["demo_submission"]["predictions"] if r["id"] not in expected] + rows} | |
| challenge_score(check) | |
| predictions.extend(rows); status = "ok" | |
| except Exception: | |
| # Retain the whole attempted origin without leaking exception text/credentials. | |
| predictions.extend({"id": t["id"], "status": "error"} for t in task["targets"]) | |
| status = "error" | |
| attempts.append({"origin": task["origin"], "status": status, "latency_seconds": time.monotonic()-started}) | |
| submission["predictions"] = predictions | |
| args.output.write_text(json.dumps(submission, indent=2, allow_nan=False)+"\n") | |
| args.output.with_suffix(".run.json").write_text(json.dumps({"attempts": attempts, | |
| "submission_sha256": digest(submission), "cost": "unmeasured", "inference": "local adapter; caller-configured"}, indent=2)+"\n") | |
| if __name__ == "__main__": | |
| main() | |