Spaces:
Running on T4
Running on T4
Download AI-coach-agent/evals/run_eval.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/evals/run_eval.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/evals/run_eval.py
-
curl -L -o run_eval.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/evals/run_eval.py
3.68 kB
| """Run the coach over a folder of session JSONs and report quality, cost and cache behaviour. | |
| python evals/run_eval.py --mode rules # offline, rule-based notes only (always works) | |
| python evals/run_eval.py --mode live # real Gemini calls (needs GOOGLE_API_KEY), fresh cache | |
| python evals/run_eval.py --mode live --repeat 2 # second pass shows response-cache hits | |
| Exit code 1 if any final note breaks a guardrail (it never should: the service falls back to rules). | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| import tempfile | |
| import time | |
| from pathlib import Path | |
| from ai_coach.config import Settings | |
| from ai_coach.guardrails import check_note | |
| from ai_coach.input_builder import build_coach_input | |
| from ai_coach.service import CoachService, render_note | |
| DEFAULT_DIR = Path(__file__).resolve().parents[1] / "examples" / "inputs" | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| parser.add_argument("--mode", choices=("rules", "live"), default="rules") | |
| parser.add_argument("--dir", type=Path, default=DEFAULT_DIR) | |
| parser.add_argument("--repeat", type=int, default=1, help="passes over the inputs (2+ shows cache hits)") | |
| parser.add_argument("--show", action="store_true", help="print every note") | |
| args = parser.parse_args(argv) | |
| cache_dir = Path(tempfile.mkdtemp(prefix="coach-eval-")) | |
| overrides = {"cache_path": cache_dir / "responses.sqlite3"} | |
| settings = Settings(**overrides) | |
| if args.mode == "rules": | |
| settings = settings.model_copy(update={"google_api_key": None}) | |
| elif not settings.llm_enabled: | |
| print("live mode needs GOOGLE_API_KEY (env or .env)", file=sys.stderr) | |
| return 2 | |
| service = CoachService(settings) | |
| rows, failures = [], 0 | |
| for _ in range(args.repeat): | |
| for path in sorted(args.dir.glob("*.json")): | |
| session = json.loads(path.read_text(encoding="utf-8")) | |
| started = time.perf_counter() | |
| result = service.coach(session) | |
| elapsed = time.perf_counter() - started | |
| problems = check_note(result.note, build_coach_input(session)) | |
| failures += bool(problems) | |
| rows.append((path.stem, result, elapsed, problems)) | |
| if args.show: | |
| print(f"\n=== {path.stem}\n{render_note(result.note)}") | |
| print(f"\n{'example':42} {'source':6} {'model':24} {'try':>3} {'cache':>5} {'cached_tok':>10} {'$':>9} {'sec':>5} notes") | |
| for name, r, secs, problems in rows: | |
| notes = r.fallback_reason or "" | |
| if r.violations: | |
| notes += f" first-try violations={len(r.violations)}" | |
| if problems: | |
| notes += f" FINAL VIOLATIONS: {[str(p) for p in problems]}" | |
| print( | |
| f"{name:42} {r.source:6} {r.model!s:24} {r.attempts:>3} {'hit' if r.cache_hit else '-':>5} " | |
| f"{r.usage.cached_tokens:>10} {r.usage.cost_usd:>9.5f} {secs:>5.1f} {notes}" | |
| ) | |
| llm_rows = [r for _, r, _, _ in rows if r.source == "llm" and not r.cache_hit] | |
| first_try = [r for r in llm_rows if r.attempts == 1] | |
| total_cost = sum(r.usage.cost_usd for _, r, _, _ in rows) | |
| print( | |
| f"\nnotes={len(rows)} llm_calls={len(llm_rows)} first_try_pass=" | |
| f"{len(first_try)}/{len(llm_rows) if llm_rows else 0} total_cost=${total_cost:.5f} " | |
| f"avg_cost_per_llm_note=${(total_cost / len(llm_rows)) if llm_rows else 0:.5f} " | |
| f"final_guardrail_failures={failures}" | |
| ) | |
| return 1 if failures else 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |