tmarks-adobe's picture
Upload app.py
e96ec88
raw
history blame contribute delete
No virus
4.14 kB
import gradio as gr
import cfbd
import numpy as np
import pandas as pd
from fastai.tabular import *
from fastai.tabular.all import *
configuration = cfbd.Configuration()
configuration.api_key["Authorization"] = "be81N7cB5mpl4z+BdHBuD5wUACNkh5YSAhO8uaC3tKCRAgC9WMhJjoHrO3Qx3TFp"
configuration.api_key_prefix["Authorization"] = "Bearer"
api_config = cfbd.ApiClient(configuration)
teams_api = cfbd.TeamsApi(api_config)
ratings_api = cfbd.RatingsApi(api_config)
games_api = cfbd.GamesApi(api_config)
betting_api = cfbd.BettingApi(api_config)
all_teams = teams_api.get_teams()
def greet(name):
return "Hello " + name + "!!"
def normalize_teams():
teams = teams_api.get_fbs_teams()
team_names = []
for team in teams:
team_names.append(team.school)
return team_names
def home_team(year, team):
elo = find_most_recent_elo(year, team)
team_details = filter_team(team)
return dict(
team=team,
conference=team_details.conference,
elo=elo
)
def away_team(year, team):
elo = find_most_recent_elo(year, team)
team_details = filter_team(team)
return dict(
team=team,
conference=team_details.conference,
elo=elo
)
def filter_team(team_name):
for team in all_teams:
if team.school == team_name:
return team
def enable_teams():
return gr.update(choices=teams, value=None, interactive=True), gr.update(choices=teams, value=None,
interactive=True)
def predict(hteam, ateam):
learn = load_learner('talking_tech_neural_net.pkl')
game_details = dict(
neutral_site=False,
home_team=hteam['team'],
home_conference=hteam['conference'],
home_elo=hteam['elo'],
away_team=ateam['team'],
away_conference=ateam['conference'],
away_elo=ateam['elo'],
)
pdf = pd.DataFrame([game_details])
dl = learn.dls.test_dl(pdf)
pdf["predicted"] = learn.get_preds(dl=dl)[0].numpy()
return pdf
def find_most_recent_elo(year, team):
games = games_api.get_games(team=team, year=year)
reversed = sorted(games, key=lambda x: x.week, reverse=True)
elo = null
if reversed[0].home_team == team:
if reversed[0].home_postgame_elo != None:
elo = reversed[0].home_postgame_elo
else:
elo = reversed[0].home_pregame_elo
else:
if reversed[0].away_postgame_elo != None:
elo = reversed[0].away_postgame_elo
else:
elo = reversed[0].away_pregame_elo
return elo
teams = normalize_teams()
with gr.Blocks(theme=gr.themes.Default()) as app:
with gr.Row():
with gr.Column(scale=1):
year = gr.Dropdown([2023, 2022, 2021, 2020, 2019, 2018, 2017], label="Year")
with gr.Row():
with gr.Column(scale=1):
team_one = gr.Dropdown(choices=teams, label="Home Team", interactive=False)
with gr.Column(scale=1):
team_two = gr.Dropdown(choices=teams, label="Away Team", interactive=False)
with gr.Row():
with gr.Column(scale=1):
team_one_label = gr.JSON(label='Home Team Details')
with gr.Column(scale=1):
team_two_label = gr.JSON(label='Away Team Details')
with gr.Row():
predict_btn = gr.Button('Predict Spread')
with gr.Row():
data_table = gr.Dataframe(
label="Prediction Details",
headers=["Neutral Site", "Home Team", "Home Conference", "Home ELO", "Home Team", "Home Conference",
"Home ELO", "Predicted Spread"],
datatype=["bool", "str", "str", "number", "str", "str", "number", "number"],
row_count=1,
col_count=(8, "dynamic")
)
year.select(enable_teams, outputs=[team_one, team_two])
team_one.select(home_team, inputs=[year, team_one], outputs=[team_one_label])
team_two.select(away_team, inputs=[year, team_two], outputs=[team_two_label])
predict_btn.click(predict, inputs=[team_one_label, team_two_label], outputs=[data_table])
app.launch()