Download backend/testing_pipeline/cli.py from interactAi-inc/Pit-Lane-Project: direct link, hf CLI and curl.
- Browser
- Download file 5.27 kB
-
https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/testing_pipeline/cli.py
- Command line
-
hf download hf://interactAi-inc/Pit-Lane-Project/backend/testing_pipeline/cli.py
-
curl -L -o cli.py https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/testing_pipeline/cli.py
5.27 kB
| from __future__ import annotations | |
| import argparse | |
| import asyncio | |
| import sys | |
| import traceback | |
| from pathlib import Path | |
| from typing import Any | |
| from app.config import get_settings | |
| from app.services.driver_registry import DriverRegistry | |
| from app.services.feature_engineer import LiveFeatureEngineer | |
| from app.services.inference_gateway import InferenceGateway | |
| from app.services.live_compressor import LiveLapCompressor | |
| from app.services.live_pipeline import LivePipeline | |
| from app.services.live_state import LiveRaceState | |
| from app.services.model_service import ModelService | |
| from app.services.prediction_service import PredictionService | |
| from app.services.raw_normalizer import RawNormalizer | |
| from app.services.remote_model_client import RemoteModelClient | |
| from testing_pipeline.fastf1_replay import FastF1Replay, ReplayConfig, ReplayResult | |
| from testing_pipeline.report import print_report | |
| def build_parser() -> argparse.ArgumentParser: | |
| parser = argparse.ArgumentParser( | |
| description=( | |
| "Replay a historical FastF1 race through the unchanged production " | |
| "normalization, lap, feature, and model pipeline." | |
| ) | |
| ) | |
| parser.add_argument("--year", type=int, default=2024) | |
| parser.add_argument("--gp", default="Bahrain", help="Grand Prix name or round") | |
| parser.add_argument("--session", default="R", help="FastF1 session code; default R") | |
| parser.add_argument( | |
| "--speed", | |
| type=float, | |
| default=0.0, | |
| help="0 runs instantly; positive values replay at that speed multiplier", | |
| ) | |
| parser.add_argument( | |
| "--max-laps", | |
| type=int, | |
| default=None, | |
| help="Optional bounded smoke test; omitted means the complete race", | |
| ) | |
| parser.add_argument( | |
| "--cache-dir", | |
| type=Path, | |
| default=Path("testing_pipeline/.cache/fastf1"), | |
| ) | |
| parser.add_argument( | |
| "--traceback", | |
| action="store_true", | |
| help="Print a Python traceback when setup or replay fails", | |
| ) | |
| return parser | |
| def main(argv: list[str] | None = None) -> int: | |
| args = build_parser().parse_args(argv) | |
| if args.speed < 0: | |
| print("ERROR: --speed cannot be negative", file=sys.stderr) | |
| return 2 | |
| if args.max_laps is not None and args.max_laps < 1: | |
| print("ERROR: --max-laps must be at least 1", file=sys.stderr) | |
| return 2 | |
| try: | |
| return asyncio.run(_run(args)) | |
| except KeyboardInterrupt: | |
| print("\nReplay cancelled by user.") | |
| return 130 | |
| except Exception as exc: | |
| print("\n" + "=" * 72, file=sys.stderr) | |
| print("F1 BACKEND REPLAY FAILED", file=sys.stderr) | |
| print(f"{type(exc).__name__}: {exc}", file=sys.stderr) | |
| if args.traceback: | |
| traceback.print_exc() | |
| else: | |
| print("Re-run with --traceback for technical details.", file=sys.stderr) | |
| print("=" * 72, file=sys.stderr) | |
| return 1 | |
| async def _run(args: argparse.Namespace) -> int: | |
| # The replay is deterministic and self-contained. It always exercises the | |
| # saved local production artifact, regardless of deployment-only remote URLs. | |
| settings = get_settings().model_copy(update={"model_backend": "local"}) | |
| model = ModelService(settings) | |
| gateway = InferenceGateway( | |
| settings=settings, | |
| local_model=model, | |
| remote_model=RemoteModelClient(settings), | |
| ) | |
| await gateway.startup() | |
| predictions = PredictionService( | |
| compressor=LiveLapCompressor(), | |
| feature_engineer=LiveFeatureEngineer(settings), | |
| gateway=gateway, | |
| ) | |
| pipeline = LivePipeline( | |
| normalizer=RawNormalizer(), | |
| registry=DriverRegistry(), | |
| state=LiveRaceState(), | |
| predictions=predictions, | |
| ) | |
| replay = FastF1Replay( | |
| ReplayConfig( | |
| year=args.year, | |
| grand_prix=str(args.gp), | |
| session=str(args.session), | |
| replay_speed=args.speed, | |
| max_laps=args.max_laps, | |
| cache_dir=args.cache_dir, | |
| ) | |
| ) | |
| print( | |
| f"Loading FastF1 data for {args.year} {args.gp} ({args.session}). " | |
| "The first run can take several minutes..." | |
| ) | |
| checkpoints: dict[int, list[dict[str, Any]]] = {} | |
| async def capture_progress( | |
| race_lap: int, | |
| total_laps: int, | |
| leaderboard: list[dict[str, Any]], | |
| ) -> None: | |
| targets = { | |
| max(1, round(total_laps * fraction)) | |
| for fraction in (0.25, 0.50, 0.75, 1.0) | |
| } | |
| if race_lap in targets and race_lap not in checkpoints: | |
| checkpoints[race_lap] = [dict(row) for row in leaderboard[:3]] | |
| leader = leaderboard[0]["driver"] if leaderboard else "waiting" | |
| print(f" lap {race_lap:>2}/{total_laps}: leader={leader}") | |
| result: ReplayResult = await replay.run( | |
| pipeline, | |
| predictions, | |
| on_progress=capture_progress, | |
| ) | |
| result.prediction_checkpoints = checkpoints | |
| leaderboard = await predictions.leaderboard() | |
| passed = print_report(result, pipeline, predictions, model, leaderboard) | |
| return 0 if passed else 1 | |