File size: 2,819 Bytes
013874c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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()