Commit ·
cce426c
1
Parent(s): a0db73f
initial upload
Browse files- requirements.txt +3 -0
- schedule.json +0 -0
- ui.py +182 -0
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
hopsworks[python]
|
| 3 |
+
pandas
|
schedule.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
ui.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import hopsworks
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import os
|
| 7 |
+
import hopsworks
|
| 8 |
+
from hsfs.feature_store import FeatureStore
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def login(project="ID2223_Project") -> tuple[hopsworks.project.Project, FeatureStore]:
|
| 12 |
+
project = hopsworks.login(
|
| 13 |
+
api_key_value=os.environ["HOPSWORKS_API_KEY"],
|
| 14 |
+
project=project,
|
| 15 |
+
)
|
| 16 |
+
fs = project.get_feature_store()
|
| 17 |
+
|
| 18 |
+
return project, fs
|
| 19 |
+
|
| 20 |
+
def get_hist_roi():
|
| 21 |
+
project, fs = login()
|
| 22 |
+
# Initial bank balance, wager amount and which league
|
| 23 |
+
starting_bank = 0
|
| 24 |
+
wager = 1
|
| 25 |
+
league = "E0"
|
| 26 |
+
|
| 27 |
+
# Get feature groups
|
| 28 |
+
main_fg = fs.get_feature_group(
|
| 29 |
+
name=f"football_{league.lower()}",
|
| 30 |
+
version=1,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
pred_fg = fs.get_feature_group(
|
| 34 |
+
name=f"football_{league.lower()}_predictions",
|
| 35 |
+
version=1,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Query necessary features
|
| 39 |
+
query = pred_fg.select(["datetime", "predictions", "hometeam", "awayteam"]).join(
|
| 40 |
+
main_fg.select(["ftour", "avg_gt_2_5", "avg_lt_2_5"]),
|
| 41 |
+
on=["datetime", "hometeam", "awayteam"],
|
| 42 |
+
)
|
| 43 |
+
df = query.read()
|
| 44 |
+
|
| 45 |
+
df = df.sort_values(["datetime", "hometeam", "awayteam"], ignore_index=True)
|
| 46 |
+
|
| 47 |
+
# Encode the full time over/under results
|
| 48 |
+
df["ftour_encoded"] = df["ftour"].apply(
|
| 49 |
+
lambda x: int(x.lower() == "o") if not pd.isna(x) else pd.NA
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Determine the odds based on predictions
|
| 53 |
+
df["odds"] = df.apply(
|
| 54 |
+
lambda row: row["avg_gt_2_5"] if row["predictions"] == 1 else row["avg_lt_2_5"],
|
| 55 |
+
axis=1,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Calculate profit/loss for each game
|
| 59 |
+
df["profit"] = df.apply(
|
| 60 |
+
lambda row: (
|
| 61 |
+
wager * (row["odds"] - 1)
|
| 62 |
+
if row["predictions"] == row["ftour_encoded"]
|
| 63 |
+
else -wager
|
| 64 |
+
),
|
| 65 |
+
axis=1,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Calculate cumulative bank balance
|
| 69 |
+
df["bank_balance"] = df["profit"].cumsum() + starting_bank
|
| 70 |
+
df.drop(columns="odds", inplace=True)
|
| 71 |
+
|
| 72 |
+
df['date'] = df['datetime'].dt.date
|
| 73 |
+
daily_aggregated = df.groupby('date').agg({
|
| 74 |
+
'profit': 'sum', # Total profit
|
| 75 |
+
'bank_balance': 'last', # Last bank balance of the day
|
| 76 |
+
}).reset_index()
|
| 77 |
+
|
| 78 |
+
daily_aggregated['date'] = daily_aggregated['date'].astype(str)
|
| 79 |
+
|
| 80 |
+
total_best = len(df)
|
| 81 |
+
bets_won = (df['profit'] > 0).sum()
|
| 82 |
+
bets_lost = (df['profit'] < 0).sum()
|
| 83 |
+
current_balance = df.iloc[-1]['bank_balance']
|
| 84 |
+
return {"total_bets": total_best, "bets_won": bets_won, "bets_lost": bets_lost, "current_balance": current_balance, "data": daily_aggregated}
|
| 85 |
+
|
| 86 |
+
def logout():
|
| 87 |
+
hopsworks.logout()
|
| 88 |
+
|
| 89 |
+
def get_todays_predictions():
|
| 90 |
+
project, fs = login()
|
| 91 |
+
fg_pred = fs.get_feature_group('football_e0_predictions', version=1)
|
| 92 |
+
|
| 93 |
+
# Query the latest row from main_fg based
|
| 94 |
+
main_fg_query = fg_pred.select(
|
| 95 |
+
[
|
| 96 |
+
"datetime",
|
| 97 |
+
"hometeam",
|
| 98 |
+
"awayteam",
|
| 99 |
+
"predictions"
|
| 100 |
+
]
|
| 101 |
+
).filter(fg_pred.datetime >= datetime.today().strftime("%Y-%m-%d"))
|
| 102 |
+
main_df = main_fg_query.read(online=False)
|
| 103 |
+
|
| 104 |
+
main_df["predictions"] = main_df["predictions"].map(lambda x: "Under" if x == 0 else "Over")
|
| 105 |
+
return main_df
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def get_daily_predictions():
|
| 109 |
+
project, fs = login()
|
| 110 |
+
fg_pred = fs.get_feature_group('football_e0_predictions', version=1)
|
| 111 |
+
|
| 112 |
+
league = "E0"
|
| 113 |
+
# Get feature groups
|
| 114 |
+
main_fg = fs.get_feature_group(
|
| 115 |
+
name=f"football_{league.lower()}",
|
| 116 |
+
version=1,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
pred_fg = fs.get_feature_group(
|
| 120 |
+
name=f"football_{league.lower()}_predictions",
|
| 121 |
+
version=1,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Query necessary features
|
| 125 |
+
query = pred_fg.select(["datetime", "predictions", "hometeam", "awayteam"]).join(
|
| 126 |
+
main_fg.select(["ftour"]),
|
| 127 |
+
on=["datetime", "hometeam", "awayteam"],
|
| 128 |
+
)
|
| 129 |
+
main_df = query.read()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
main_df["predictions"] = main_df["predictions"].map(lambda x: "Under" if x == 0 else "Over")
|
| 134 |
+
main_df["ftour"] = main_df["ftour"].map(lambda x: "Under" if x == "U" else "Over")
|
| 135 |
+
|
| 136 |
+
main_df.rename(columns={"ftour": "Result"}, inplace=True)
|
| 137 |
+
return main_df.sort_values(by="datetime", ascending=False).head(10)
|
| 138 |
+
|
| 139 |
+
def get_schedule():
|
| 140 |
+
with open('./schedule.json', 'r') as handle:
|
| 141 |
+
parsed = json.load(handle)
|
| 142 |
+
return pd.DataFrame([{
|
| 143 |
+
"date": datetime.strptime(game['sport_event']['start_time'], "%Y-%m-%dT%H:%M:%S+00:00"),
|
| 144 |
+
"home_team": game['sport_event']['competitors'][0]['name'],
|
| 145 |
+
"away_team": game['sport_event']['competitors'][1]['name'],
|
| 146 |
+
}
|
| 147 |
+
for game in parsed["schedules"]
|
| 148 |
+
])
|
| 149 |
+
|
| 150 |
+
def get_next10games(schedule):
|
| 151 |
+
return schedule.loc[schedule['date'] > today].sort_values(by='date').head(10)
|
| 152 |
+
|
| 153 |
+
today = datetime.today()
|
| 154 |
+
|
| 155 |
+
roi = get_hist_roi()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
with gr.Blocks() as demo:
|
| 159 |
+
with gr.Row():
|
| 160 |
+
gr.Label(f"Total bets: {roi['total_bets']}")
|
| 161 |
+
gr.Label(f"Bets won: {roi['bets_won']}")
|
| 162 |
+
gr.Label(f"Bets lost: {roi['bets_lost']}")
|
| 163 |
+
gr.Label(f"Current balance: {roi['current_balance']}")
|
| 164 |
+
gr.LinePlot(roi["data"], x="date", y="bank_balance", title="Bank balance over time", y_title="Bank balance", x_title="Date")
|
| 165 |
+
gr.Label("Today's predictions")
|
| 166 |
+
gr.DataFrame(get_todays_predictions,
|
| 167 |
+
headers=["Date", "Home Team", "Away Team", "Prediction"],
|
| 168 |
+
every=7200) # 2hrs
|
| 169 |
+
gr.Label("Last 10 games")
|
| 170 |
+
gr.DataFrame(get_daily_predictions,
|
| 171 |
+
headers=["Date", "Home Team", "Away Team", "Prediction"],
|
| 172 |
+
every=7200) # 2hrs
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
gr.Label("Upcoming games")
|
| 176 |
+
next10 = gr.DataFrame(get_next10games(get_schedule()),
|
| 177 |
+
label=None,
|
| 178 |
+
headers=["Date", "Home Team", "Away Team"],
|
| 179 |
+
interactive=False,
|
| 180 |
+
every=7200) # 2hrs
|
| 181 |
+
|
| 182 |
+
demo.launch()
|