tmarks-adobe commited on
Commit
f22e74d
1 Parent(s): dc5b036

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -47
app.py CHANGED
@@ -15,13 +15,13 @@ games_api = cfbd.GamesApi(api_config)
15
  betting_api = cfbd.BettingApi(api_config)
16
 
17
 
18
- def predict(game_details):
 
19
  learn = load_learner("cfb-prediction.pkl")
20
 
21
- pdf = pd.DataFrame([game_details])
22
  dl = learn.dls.test_dl(pdf)
23
  pdf["predicted"] = learn.get_preds(dl=dl)[0].numpy()
24
- pdf['margin'] = abs(game_details['home_points'] - game_details['away_points'])
25
 
26
  return pdf[['home_team','away_team','home_points','away_points','spread','margin','predicted']]
27
 
@@ -34,43 +34,43 @@ def predict(game_details):
34
 
35
  def home_team_change(team, year):
36
  games = games_api.get_games(team=team, year=year)
37
- names = []
38
- for game in games:
39
- if game.home_team == team:
40
- names.append(game.away_team)
41
- else:
42
- names.append(game.home_team)
43
 
44
- return gr.update(choices=names, value=None)
45
 
46
 
47
- def get_game_details(year, target, opponent):
48
  games = games_api.get_games(team=target, year=year)
49
- the_game = null
 
50
 
51
  for game in games:
52
- if game.home_team == opponent or game.away_team == opponent:
53
- the_game = dict(
54
- id=game.id,
55
- year=game.season,
56
- week=game.week,
57
- neutral_site=game.neutral_site,
58
- home_team=game.home_team,
59
- home_conference=game.home_conference,
60
- home_points=game.home_points,
61
- home_elo=game.home_pregame_elo,
62
- away_team=game.away_team,
63
- away_conference=game.away_conference,
64
- away_points=game.away_points,
65
- away_elo=game.away_pregame_elo,
66
- )
67
- break
68
-
69
- spread = betting_api.get_lines(game_id=the_game['id'],year=year)
 
 
70
 
71
- the_game['spread'] = spread[0].lines[0].spread
 
 
72
 
73
- return the_game
 
74
 
75
  def lookup_teams(teams_api):
76
  team_names = []
@@ -96,14 +96,6 @@ with gr.Blocks() as app:
96
  with gr.Row():
97
  with gr.Column(scale=1):
98
  ddTeams1 = gr.Dropdown(choices=teams, label="Target Team")
99
- with gr.Column(scale=1):
100
- oponents = gr.Dropdown(choices=[], label="Opponents", interactive=True)
101
- # with gr.Column(scale=1):
102
- # ddTeams2 = gr.Dropdown(choices=teams, label="Away Team")
103
- # label2 = (gr.Label(num_top_classes=4, label="Away Team Details"))
104
- # ddTeams2.select(home_team_change, inputs=ddTeams2, outputs=[label2])
105
- with gr.Row():
106
- game_details = gr.JSON({})
107
  with gr.Row():
108
  submit = gr.Button("Predict")
109
  with gr.Row():
@@ -114,14 +106,8 @@ with gr.Blocks() as app:
114
  col_count=(7, "dynamic")
115
  )
116
 
117
- ddTeams1.select(home_team_change, inputs=[ddTeams1, yearDd], outputs=[oponents])
118
- oponents.select(
119
- get_game_details,
120
- inputs=[yearDd, ddTeams1, oponents],
121
- outputs=[game_details]
122
- )
123
  submit.click(
124
- predict, inputs=[game_details], outputs=[data_table]
125
  )
126
  app.launch()
127
 
 
15
  betting_api = cfbd.BettingApi(api_config)
16
 
17
 
18
+ def predict(team, year):
19
+ matchups = get_game_details(year, team)
20
  learn = load_learner("cfb-prediction.pkl")
21
 
22
+ pdf = pd.DataFrame(matchups)
23
  dl = learn.dls.test_dl(pdf)
24
  pdf["predicted"] = learn.get_preds(dl=dl)[0].numpy()
 
25
 
26
  return pdf[['home_team','away_team','home_points','away_points','spread','margin','predicted']]
27
 
 
34
 
35
  def home_team_change(team, year):
36
  games = games_api.get_games(team=team, year=year)
 
 
 
 
 
 
37
 
38
+ return games
39
 
40
 
41
+ def get_game_details(year, target):
42
  games = games_api.get_games(team=target, year=year)
43
+ lines = betting_api.get_lines(team=target, year=year)
44
+ matchups = []
45
 
46
  for game in games:
47
+ the_game = dict(
48
+ id=game.id,
49
+ year=game.season,
50
+ week=game.week,
51
+ neutral_site=game.neutral_site,
52
+ home_team=game.home_team,
53
+ home_conference=game.home_conference,
54
+ home_points=game.home_points,
55
+ home_elo=game.home_pregame_elo,
56
+ away_team=game.away_team,
57
+ away_conference=game.away_conference,
58
+ away_points=game.away_points,
59
+ away_elo=game.away_pregame_elo,
60
+ )
61
+ game_lines = [l for l in lines if l.id == the_game['id']]
62
+
63
+ if len(game_lines) > 0:
64
+ if len(game_lines[0].lines) > 0:
65
+ game_line = game_lines[0].lines[0]
66
+ the_game['spread'] = float(game_line.spread)
67
 
68
+ the_game['margin'] = the_game['away_points'] - the_game['home_points']
69
+
70
+ matchups.append(the_game)
71
 
72
+
73
+ return matchups
74
 
75
  def lookup_teams(teams_api):
76
  team_names = []
 
96
  with gr.Row():
97
  with gr.Column(scale=1):
98
  ddTeams1 = gr.Dropdown(choices=teams, label="Target Team")
 
 
 
 
 
 
 
 
99
  with gr.Row():
100
  submit = gr.Button("Predict")
101
  with gr.Row():
 
106
  col_count=(7, "dynamic")
107
  )
108
 
 
 
 
 
 
 
109
  submit.click(
110
+ predict, inputs=[ddTeams1, yearDd], outputs=[data_table]
111
  )
112
  app.launch()
113