File size: 3,874 Bytes
dc5b036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f22e74d
 
dc5b036
 
f22e74d
dc5b036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f22e74d
dc5b036
 
f22e74d
dc5b036
f22e74d
 
dc5b036
 
f22e74d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc5b036
f22e74d
 
 
dc5b036
f22e74d
 
dc5b036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f22e74d
dc5b036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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()