Spaces:
Configuration error
Configuration error
Download app.py from Brettapps/Trifecta-Lab: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/spaces/Brettapps/Trifecta-Lab/resolve/main/app.py
- Command line
-
hf download hf://spaces/Brettapps/Trifecta-Lab/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Brettapps/Trifecta-Lab/resolve/main/app.py
2.82 kB
| #!/usr/bin/env python3 | |
| """Trifecta-Bro Gradio app.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import gradio as gr | |
| from src.prediction_model import TrifectaPredictor | |
| from src.build_booklet import build_booklet | |
| DATA_DIR = Path(__file__).resolve().parents[0] / "data" | |
| PDF_DIR = DATA_DIR / "pdfs" | |
| PDF_DIR.mkdir(parents=True, exist_ok=True) | |
| PREDICTIONS_DIR = DATA_DIR / "predictions" | |
| PREDICTIONS_DIR.mkdir(parents=True, exist_ok=True) | |
| def get_predictions(date: str) -> dict: | |
| path = PREDICTIONS_DIR / f"predictions-{date}.json" | |
| if not path.exists(): | |
| return {"error": f"No predictions for {date}"} | |
| return json.loads(path.read_text()) | |
| def format_race_block(race: dict) -> str: | |
| pred = race.get("prediction", {}) | |
| lines = [ | |
| f"**Race {race.get('race_number')}: {race.get('race_name')}**", | |
| f"- Distance: {race.get('distance')} | Condition: {race.get('condition')} | Class: {race.get('race_class')}", | |
| f"- Prize: ${race.get('prize_money')} | Runners: {race.get('number_of_runners')}", | |
| f"- Primary: {pred.get('primary')} | Secondary: {pred.get('secondary') or '-'} | Value: {pred.get('value') or '-'}", | |
| ] | |
| return "\n".join(lines) | |
| def generate_booklet(date: str) -> tuple[str, str | None]: | |
| payload = get_predictions(date) | |
| if "error" in payload: | |
| return payload["error"], None | |
| races = payload.get("races", []) | |
| out_path = PDF_DIR / f"trifecta-bro-{date}.pdf" | |
| build_booklet(races, out_path) | |
| return f"Generated: {out_path.name}", str(out_path) | |
| def build_ui() -> gr.Blocks: | |
| with gr.Blocks(title="Trifecta-Bro") as demo: | |
| gr.Markdown("# Trifecta-Bro\nAustralian Gallops Trifecta Predictions") | |
| with gr.Row(): | |
| date_input = gr.Textbox(label="Date", value="2026-08-10") | |
| predict_btn = gr.Button("Predict") | |
| booklet_btn = gr.Button("Generate PDF Booklet") | |
| predictions_output = gr.Markdown(label="Predictions") | |
| pdf_output = gr.File(label="Download PDF") | |
| def on_predict(date: str): | |
| payload = get_predictions(date) | |
| if "error" in payload: | |
| return payload["error"] | |
| races = payload.get("races", []) | |
| blocks = [] | |
| for race in races: | |
| pred = race.get("prediction", {}) | |
| blocks.append(format_race_block({**race, "prediction": pred})) | |
| return "\n\n---\n\n".join(blocks) | |
| def on_booklet(date: str): | |
| msg, path = generate_booklet(date) | |
| return msg, path if path else None | |
| predict_btn.click(on_predict, inputs=date_input, outputs=predictions_output) | |
| booklet_btn.click(on_booklet, inputs=date_input, outputs=[predictions_output, pdf_output]) | |
| return demo | |
| app = build_ui() | |