gnosisx commited on
Commit
7bdf0d0
·
verified ·
1 Parent(s): 11d140c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +173 -0
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Hugging Face Space for EPL Predictions
3
+ Deploy this as a Gradio app on HF Spaces
4
+ """
5
+ import gradio as gr
6
+ import numpy as np
7
+ import pandas as pd
8
+ from model_predictor import EPLPredictor
9
+ import json
10
+
11
+ # Initialize predictor
12
+ print("Loading models...")
13
+ predictor = EPLPredictor(use_local=False) # Will download from HF
14
+ print("Models loaded!")
15
+
16
+ def predict_match(home_team, away_team, home_odds, draw_odds, away_odds):
17
+ """Predict match outcome"""
18
+ try:
19
+ # Create odds dict
20
+ best_odds = {
21
+ 'H': {'odds': home_odds},
22
+ 'D': {'odds': draw_odds},
23
+ 'A': {'odds': away_odds}
24
+ }
25
+
26
+ # Get predictions
27
+ result = predictor.predict(
28
+ home_team=home_team,
29
+ away_team=away_team,
30
+ best_odds=best_odds
31
+ )
32
+
33
+ # Format output
34
+ output = f"""
35
+ ## Match Prediction: {home_team} vs {away_team}
36
+
37
+ ### Ensemble Probabilities:
38
+ - **Home Win**: {result['ensemble']['H']:.1%}
39
+ - **Draw**: {result['ensemble']['D']:.1%}
40
+ - **Away Win**: {result['ensemble']['A']:.1%}
41
+ - **Over 2.5 Goals**: {result['ensemble']['over25']:.1%}
42
+ - **BTTS**: {result['ensemble']['btts']:.1%}
43
+
44
+ ### Expected Goals:
45
+ - {home_team}: {result['expected_goals']['home']:.2f}
46
+ - {away_team}: {result['expected_goals']['away']:.2f}
47
+
48
+ ### Model Components:
49
+ **Poisson**: H:{result['poisson']['H']:.1%} D:{result['poisson']['D']:.1%} A:{result['poisson']['A']:.1%}
50
+ **XGBoost**: H:{result['xgboost']['H']:.1%} D:{result['xgboost']['D']:.1%} A:{result['xgboost']['A']:.1%}
51
+ """
52
+
53
+ # Check for value
54
+ value_analysis = ""
55
+ for market, prob in [('Home', result['ensemble']['H']),
56
+ ('Draw', result['ensemble']['D']),
57
+ ('Away', result['ensemble']['A'])]:
58
+ if market == 'Home':
59
+ odds = home_odds
60
+ elif market == 'Draw':
61
+ odds = draw_odds
62
+ else:
63
+ odds = away_odds
64
+
65
+ value = predictor.calculate_value(prob, odds)
66
+ if value['has_value']:
67
+ value_analysis += f"\n⚡ **VALUE BET**: {market} @ {odds:.2f} (Edge: {value['edge']:.1f}%)"
68
+
69
+ if value_analysis:
70
+ output += f"\n### Value Bets Found:{value_analysis}"
71
+ else:
72
+ output += "\n### No value bets found at these odds"
73
+
74
+ return output
75
+
76
+ except Exception as e:
77
+ return f"Error: {str(e)}"
78
+
79
+ def batch_predict(csv_text):
80
+ """Predict multiple matches from CSV"""
81
+ try:
82
+ # Parse CSV
83
+ lines = csv_text.strip().split('\n')
84
+ results = []
85
+
86
+ for line in lines[1:]: # Skip header
87
+ parts = line.split(',')
88
+ if len(parts) >= 5:
89
+ home, away = parts[0].strip(), parts[1].strip()
90
+ h_odds, d_odds, a_odds = float(parts[2]), float(parts[3]), float(parts[4])
91
+
92
+ pred = predict_match(home, away, h_odds, d_odds, a_odds)
93
+ results.append(pred)
94
+
95
+ return "\n---\n".join(results)
96
+ except Exception as e:
97
+ return f"Error processing CSV: {str(e)}"
98
+
99
+ # Create Gradio interface
100
+ with gr.Blocks(title="EPL Match Predictor") as app:
101
+ gr.Markdown("""
102
+ # ⚽ EPL Match Predictor
103
+
104
+ Powered by ensemble models (Poisson + XGBoost) trained on EPL data.
105
+
106
+ Models available at: [gnosisx/epl-ensemble-1x2](https://huggingface.co/gnosisx/epl-ensemble-1x2)
107
+ """)
108
+
109
+ with gr.Tab("Single Match"):
110
+ with gr.Row():
111
+ home_input = gr.Textbox(label="Home Team", value="Liverpool")
112
+ away_input = gr.Textbox(label="Away Team", value="Everton")
113
+
114
+ with gr.Row():
115
+ home_odds_input = gr.Number(label="Home Odds", value=1.48)
116
+ draw_odds_input = gr.Number(label="Draw Odds", value=5.0)
117
+ away_odds_input = gr.Number(label="Away Odds", value=8.0)
118
+
119
+ predict_btn = gr.Button("Predict", variant="primary")
120
+ output = gr.Markdown()
121
+
122
+ predict_btn.click(
123
+ predict_match,
124
+ inputs=[home_input, away_input, home_odds_input, draw_odds_input, away_odds_input],
125
+ outputs=output
126
+ )
127
+
128
+ with gr.Tab("Batch Prediction"):
129
+ gr.Markdown("Upload CSV with format: `Home,Away,HomeOdds,DrawOdds,AwayOdds`")
130
+
131
+ csv_input = gr.Textbox(
132
+ label="CSV Data",
133
+ lines=10,
134
+ value="Home,Away,H_Odds,D_Odds,A_Odds\nLiverpool,Everton,1.48,5.0,8.0\nArsenal,Chelsea,2.1,3.5,3.8\nMan City,Burnley,1.15,9.0,21.0"
135
+ )
136
+
137
+ batch_btn = gr.Button("Predict All", variant="primary")
138
+ batch_output = gr.Markdown()
139
+
140
+ batch_btn.click(
141
+ batch_predict,
142
+ inputs=csv_input,
143
+ outputs=batch_output
144
+ )
145
+
146
+ with gr.Tab("API"):
147
+ gr.Markdown("""
148
+ ## API Endpoint
149
+
150
+ You can also use this as an API:
151
+
152
+ ```python
153
+ import requests
154
+
155
+ response = requests.post(
156
+ "https://gnosisx-epl-predictor.hf.space/api/predict",
157
+ json={
158
+ "home_team": "Liverpool",
159
+ "away_team": "Everton",
160
+ "best_odds": {
161
+ "H": {"odds": 1.48},
162
+ "D": {"odds": 5.0},
163
+ "A": {"odds": 8.0}
164
+ }
165
+ }
166
+ )
167
+ print(response.json())
168
+ ```
169
+ """)
170
+
171
+ # Launch app
172
+ if __name__ == "__main__":
173
+ app.launch()