cfb-predictor / app.py
tmarks-adobe's picture
Upload app.py
f22e74d
raw
history blame contribute delete
No virus
3.87 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)
def predict(team, year):
matchups = get_game_details(year, team)
learn = load_learner("cfb-prediction.pkl")
pdf = pd.DataFrame(matchups)
dl = learn.dls.test_dl(pdf)
pdf["predicted"] = learn.get_preds(dl=dl)[0].numpy()
return pdf[['home_team','away_team','home_points','away_points','spread','margin','predicted']]
# def home_team_change(x):
# ratings = ratings_api.get_elo_ratings(team=x, year=2023)
# team_detail = ratings[0]
# return "Conference: " + team_detail.conference + ", ELO Rating: " + str(team_detail.elo)
def home_team_change(team, year):
games = games_api.get_games(team=team, year=year)
return games
def get_game_details(year, target):
games = games_api.get_games(team=target, year=year)
lines = betting_api.get_lines(team=target, year=year)
matchups = []
for game in games:
the_game = dict(
id=game.id,
year=game.season,
week=game.week,
neutral_site=game.neutral_site,
home_team=game.home_team,
home_conference=game.home_conference,
home_points=game.home_points,
home_elo=game.home_pregame_elo,
away_team=game.away_team,
away_conference=game.away_conference,
away_points=game.away_points,
away_elo=game.away_pregame_elo,
)
game_lines = [l for l in lines if l.id == the_game['id']]
if len(game_lines) > 0:
if len(game_lines[0].lines) > 0:
game_line = game_lines[0].lines[0]
the_game['spread'] = float(game_line.spread)
the_game['margin'] = the_game['away_points'] - the_game['home_points']
matchups.append(the_game)
return matchups
def lookup_teams(teams_api):
team_names = []
teams = teams_api.get_fbs_teams()
for team in teams:
team_names.append(team.school)
return team_names
def update_target(x):
print(x)
teams = lookup_teams(teams_api)
with gr.Blocks() as app:
with gr.Row():
with gr.Column(scale=1):
yearDd = gr.Dropdown(
[2023, 2022, 2021, 2020, 2019, 2018, 2017], label="Year"
)
with gr.Row():
with gr.Column(scale=1):
ddTeams1 = gr.Dropdown(choices=teams, label="Target Team")
with gr.Row():
submit = gr.Button("Predict")
with gr.Row():
data_table = gr.Dataframe(
headers=["Home Team", "Away Team", "Home Points", "Away Points", "Consensus Spread", "Margin", "Predicted Spread"],
datatype=["str", "str", "number", "number", "number", "number", "number"],
row_count=1,
col_count=(7, "dynamic")
)
submit.click(
predict, inputs=[ddTeams1, yearDd], outputs=[data_table]
)
app.launch()
# import gradio as gr
# languages = ['spanish', 'english']
# homeworks = {'spanish': ['hola', 'bien', 'gracias'], 'english': ['hello', 'good', 'thank you']}
# def rs_change(rs):
# return gr.update(choices=homeworks[rs], value=None)
# with gr.Blocks() as app:
# rs = gr.Dropdown(choices=languages, value='english')
# rs_hw = gr.Dropdown(choices=homeworks['english'], interactive=True)
# rs.change(fn=rs_change, inputs=[rs], outputs=[rs_hw])
# app.launch()