SSamson commited on
Commit
d873353
1 Parent(s): cbb26dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +280 -72
app.py CHANGED
@@ -1,73 +1,281 @@
1
- import requests
 
2
  import pandas as pd
3
- from sklearn.model_selection import train_test_split
4
- from sklearn.ensemble import RandomForestRegressor
5
- from sklearn.metrics import mean_absolute_error
6
- from flask import Flask, request, jsonify
7
-
8
- # Step 1: Data Collection
9
- def fetch_data():
10
- api_key = 'itku7CjwJv5bfrwAvGlwR3nYv' # Replace with your SportsData.io API key
11
- response = requests.get(f"https://api.sportsdata.io/v3/nba/stats/json/PlayerSeasonStats/2023?key={api_key}")
12
-
13
- # Check if the response is valid
14
- if response.status_code != 200:
15
- raise ValueError(f"Error fetching data: {response.status_code}, {response.text}")
16
-
17
- data = response.json()
18
-
19
- # Check if the response is a list of dictionaries
20
- if not isinstance(data, list) or not all(isinstance(i, dict) for i in data):
21
- raise ValueError("API response is not in the expected format (list of dictionaries)")
22
-
23
- return pd.DataFrame(data)
24
-
25
- # Step 2: Data Preprocessing
26
- def preprocess_data(df):
27
- df = df.dropna() # Remove rows with missing values
28
- df = df[df['Minutes'] > 0] # Filter players with playing time
29
- df['PointsPerGame'] = df['Points'] / df['Games']
30
- return df
31
-
32
- # Step 3: Feature Engineering
33
- def engineer_features(df):
34
- df['RecentForm'] = df['PointsPerGame'].rolling(window=5).mean().fillna(0)
35
- df['HomeAdvantage'] = df['HomeGames'] / df['TotalGames']
36
- return df
37
-
38
- # Step 4: Model Training
39
- def train_model(df):
40
- X = df[['RecentForm', 'HomeAdvantage']]
41
- y = df['PointsPerGame']
42
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
43
-
44
- model = RandomForestRegressor(n_estimators=100, random_state=42)
45
- model.fit(X_train, y_train)
46
-
47
- y_pred = model.predict(X_test)
48
- mae = mean_absolute_error(y_test, y_pred)
49
- print(f"Mean Absolute Error: {mae}")
50
-
51
- return model
52
-
53
- # Step 5: Deployment with Flask
54
- app = Flask(__name__)
55
-
56
- @app.route('/predict', methods=['POST'])
57
- def predict():
58
- data = request.json
59
- input_features = [data['RecentForm'], data['HomeAdvantage']]
60
- prediction = model.predict([input_features])[0]
61
- return jsonify({'prediction': prediction})
62
-
63
- if __name__ == '__main__':
64
- # Fetch and preprocess data
65
- df = fetch_data()
66
- df = preprocess_data(df)
67
- df = engineer_features(df)
68
-
69
- # Train the model
70
- model = train_model(df)
71
-
72
- # Run the Flask app
73
- app.run(debug=True, host='0.0.0.0', port=5000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pulp
2
+ import numpy as np
3
  import pandas as pd
4
+ import streamlit as st
5
+ import gspread
6
+
7
+ scope = ['https://www.googleapis.com/auth/spreadsheets',
8
+ "https://www.googleapis.com/auth/drive"]
9
+
10
+ credentials = {
11
+ "type": "service_account",
12
+ "project_id": "sheets-api-connect-378620",
13
+ "private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
14
+ "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnbwoOfc66gW3547ja/UEyIGAF112dt/VqHprRafkKGmlg55jqJ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EoTEK0wyPNLSQlO/9ypd+0fEnArwFHO7CMF\nycQprAKHJXM1eOOFFuZeQCaInqdPZy1UcV5Szla4UmUZWkk1m24blHzXAoGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
15
+ "client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
16
+ "client_id": "106625872877651920064",
17
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
18
+ "token_uri": "https://oauth2.googleapis.com/token",
19
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
20
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
21
+ }
22
+
23
+ gc = gspread.service_account_from_dict(credentials)
24
+
25
+ st.set_page_config(layout="wide")
26
+
27
+ roo_format = {'Win%': '{:.2%}', 'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}',
28
+ '60+%': '{:.2%}','5x%': '{:.2%}','6x%': '{:.2%}','7x%': '{:.2%}','Own': '{:.2%}','LevX': '{:.2%}'}
29
+ stat_format = {'Odds%': '{:.2%}'}
30
+ table_format = {'Odds': '{:.2%}'}
31
+
32
+ csgo_overall = 'CSGO_Overall_Proj'
33
+ csgo_rpl = 'CSGO_RPL_Proj'
34
+ csgo_neutral = 'CSGO_Neutral_Proj'
35
+ csgo_wins = 'CSGO_Win_Proj'
36
+ csgo_losses = 'CSGO_Loss_Proj'
37
+ overall_odds = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
38
+ RPL_odds = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
39
+ csgo_bo1 = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
40
+ two_map = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
41
+ csgo_bo3 = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
42
+ csgo_bo5 = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
43
+ player_baselines = 'https://docs.google.com/spreadsheets/d/1aLVN4izjSuqZGRyz73Kip6U1q3rubh6v9GrckgEqbfs/edit?pli=1#gid=1545712013'
44
+
45
+ @st.cache_data
46
+ def load_roo_model(URL):
47
+ sh = gc.open(URL)
48
+ worksheet = sh.get_worksheet(0)
49
+ raw_display = pd.DataFrame(worksheet.get_all_records())
50
+ try:
51
+ raw_display["Salary"] = raw_display["Salary"].replace("$", "", regex=True).astype(float)
52
+ except:
53
+ pass
54
+ try:
55
+ raw_display['Win%'] = raw_display['Win%'].str.replace('%', '').astype(float)/100
56
+ except:
57
+ pass
58
+ try:
59
+ raw_display['Top_finish'] = raw_display['Top_finish'].str.replace('%', '').astype(float)/100
60
+ except:
61
+ pass
62
+ try:
63
+ raw_display['Top_5_finish'] = raw_display['Top_5_finish'].str.replace('%', '').astype(float)/100
64
+ except:
65
+ pass
66
+ try:
67
+ raw_display['Top_10_finish'] = raw_display['Top_10_finish'].str.replace('%', '').astype(float)/100
68
+ except:
69
+ pass
70
+ try:
71
+ raw_display['60+%'] = raw_display['60+%'].str.replace('%', '').astype(float)/100
72
+ except:
73
+ pass
74
+ try:
75
+ raw_display['5x%'] = raw_display['5x%'].str.replace('%', '').astype(float)/100
76
+ except:
77
+ pass
78
+ try:
79
+ raw_display['6x%'] = raw_display['6x%'].str.replace('%', '').astype(float)/100
80
+ except:
81
+ pass
82
+ try:
83
+ raw_display['7x%'] = raw_display['7x%'].str.replace('%', '').astype(float)/100
84
+ except:
85
+ pass
86
+ try:
87
+ raw_display['Own'] = raw_display['Own'].str.replace('%', '').astype(float)/100
88
+ except:
89
+ pass
90
+ try:
91
+ raw_display['LevX'] = raw_display['LevX'].str.replace('%', '').astype(float)/100
92
+ except:
93
+ pass
94
+
95
+ return raw_display
96
+
97
+ @st.cache_data
98
+ def load_overall_odds(URL):
99
+ sh = gc.open_by_url(URL)
100
+ worksheet = sh.worksheet('Overall_Vegas')
101
+ raw_display = pd.DataFrame(worksheet.get_all_records())
102
+ raw_display['Odds'] = raw_display['Odds'].str.replace('%', '').astype(float)/100
103
+
104
+ return raw_display
105
+
106
+ @st.cache_data
107
+ def load_rpl_odds(URL):
108
+ sh = gc.open_by_url(URL)
109
+ worksheet = sh.worksheet('RPL_Vegas')
110
+ raw_display = pd.DataFrame(worksheet.get_all_records())
111
+ raw_display['Odds'] = raw_display['Odds'].str.replace('%', '').astype(float)/100
112
+ raw_display['Vegas'] = raw_display['Vegas'].str.replace('%', '').astype(float)/100
113
+ raw_display = raw_display[['Team', 'Opponent', 'RPL', 'Opp_RPL', 'RPL_Diff', 'Vegas', 'Odds', 'P Rounds']]
114
+
115
+ return raw_display
116
+
117
+ @st.cache_data
118
+ def load_bo1_proj_model(URL):
119
+ sh = gc.open_by_url(URL)
120
+ worksheet = sh.worksheet('Overall_BO1_Projections')
121
+ raw_display = pd.DataFrame(worksheet.get_all_records())
122
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
123
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
124
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
125
+
126
+ return raw_display
127
+
128
+ @st.cache_data
129
+ def two_map_load(URL):
130
+ sh = gc.open_by_url(URL)
131
+ worksheet = sh.worksheet('2_map_projections')
132
+ raw_display = pd.DataFrame(worksheet.get_all_records())
133
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
134
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
135
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
136
+
137
+ return raw_display
138
+
139
+ @st.cache_data
140
+ def load_bo3_proj_model(URL):
141
+ sh = gc.open_by_url(URL)
142
+ worksheet = sh.worksheet('Overall_BO3_Projections')
143
+ raw_display = pd.DataFrame(worksheet.get_all_records())
144
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
145
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
146
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
147
+
148
+ return raw_display
149
+
150
+ @st.cache_data
151
+ def load_bo5_proj_model(URL):
152
+ sh = gc.open_by_url(URL)
153
+ worksheet = sh.worksheet('Overall_BO5_Projections')
154
+ raw_display = pd.DataFrame(worksheet.get_all_records())
155
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
156
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
157
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
158
+
159
+ return raw_display
160
+
161
+ @st.cache_data
162
+ def load_slate_baselines(URL):
163
+ sh = gc.open_by_url(URL)
164
+ worksheet = sh.worksheet('Player_Data')
165
+ raw_display = pd.DataFrame(worksheet.get_all_records())
166
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
167
+ raw_display = raw_display.sort_values(by='Kills/Round', ascending=False)
168
+
169
+ return raw_display
170
+
171
+ hold_display = load_roo_model(csgo_overall)
172
+
173
+ tab1, tab2, tab3, tab4, tab5 = st.tabs(["CSGO Odds Tables", "CSGO Range of Outcomes", "CSGO Player Stat Projections", "CSGO Slate Baselines", '2-map Projections'])
174
+
175
+ def convert_df_to_csv(df):
176
+ return df.to_csv().encode('utf-8')
177
+
178
+ with tab1:
179
+ if st.button("Reset Data", key='reset4'):
180
+ # Clear values from *all* all in-memory and on-disk data caches:
181
+ # i.e. clear values from both square and cube
182
+ st.cache_data.clear()
183
+ odds_choice = st.radio("What table would you like to display?", ('Overall', 'RPL'), key='odds_table')
184
+ if odds_choice == 'Overall':
185
+ hold_display = load_overall_odds(overall_odds)
186
+ elif odds_choice == 'RPL':
187
+ hold_display = load_rpl_odds(RPL_odds)
188
+ display = hold_display.set_index('Team')
189
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(table_format, precision=2), use_container_width = True)
190
+ st.download_button(
191
+ label="Export Tables",
192
+ data=convert_df_to_csv(display),
193
+ file_name='CSGO_Odds_Tables_export.csv',
194
+ mime='text/csv',
195
+ )
196
+
197
+ with tab2:
198
+ if st.button("Reset Data", key='reset1'):
199
+ # Clear values from *all* all in-memory and on-disk data caches:
200
+ # i.e. clear values from both square and cube
201
+ st.cache_data.clear()
202
+ model_choice = st.radio("What table would you like to display?", ('Overall', 'RPL', 'Neutral', 'Wins', 'Losses'), key='roo_table')
203
+ team_var1 = st.multiselect('View specific team?', options = hold_display['Team'].unique(), key = 'roo_teamvar')
204
+ if model_choice == 'Overall':
205
+ hold_display = load_roo_model(csgo_overall)
206
+ elif model_choice == 'RPL':
207
+ hold_display = load_roo_model(csgo_rpl)
208
+ elif model_choice == 'Neutral':
209
+ hold_display = load_roo_model(csgo_neutral)
210
+ elif model_choice == 'Wins':
211
+ hold_display = load_roo_model(csgo_wins)
212
+ elif model_choice == 'Losses':
213
+ hold_display = load_roo_model(csgo_losses)
214
+ hold_display['Own'] = hold_display['Own'] / 100
215
+ display = hold_display.set_index('Player')
216
+ export_display = display
217
+ export_display['Own'] = export_display['Own'] *100
218
+ export_display['Position'] = "FLEX"
219
+ if team_var1:
220
+ display = display[display['Team'].isin(team_var1)]
221
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(roo_format, precision=2), use_container_width = True)
222
+ st.download_button(
223
+ label="Export Range of Outcomes",
224
+ data=convert_df_to_csv(export_display),
225
+ file_name='CSGO_ROO_export.csv',
226
+ mime='text/csv',
227
+ )
228
+
229
+ with tab3:
230
+ if st.button("Reset Data", key='reset2'):
231
+ # Clear values from *all* all in-memory and on-disk data caches:
232
+ # i.e. clear values from both square and cube
233
+ st.cache_data.clear()
234
+ gametype_choice = st.radio("What format are the games being played?", ('Best of 1', 'Best of 3', 'Best of 5'), key='player_stats')
235
+ team_var2 = st.multiselect('View specific team?', options = hold_display['Team'].unique(), key = 'stat_teamvar')
236
+ if gametype_choice == 'Best of 1':
237
+ hold_display = load_bo1_proj_model(csgo_bo1)
238
+ elif gametype_choice == 'Best of 3':
239
+ hold_display = load_bo3_proj_model(csgo_bo3)
240
+ elif gametype_choice == 'Best of 5':
241
+ hold_display = load_bo5_proj_model(csgo_bo5)
242
+ display = hold_display.set_index('Player')
243
+ if team_var2:
244
+ display = display[display['Team'].isin(team_var2)]
245
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(stat_format, precision=2), use_container_width = True)
246
+ st.download_button(
247
+ label="Export Projections",
248
+ data=convert_df_to_csv(display),
249
+ file_name='CSGO_Projections_export.csv',
250
+ mime='text/csv',
251
+ )
252
+
253
+ with tab4:
254
+ if st.button("Reset Data", key='reset3'):
255
+ # Clear values from *all* all in-memory and on-disk data caches:
256
+ # i.e. clear values from both square and cube
257
+ st.cache_data.clear()
258
+ hold_display = load_slate_baselines(player_baselines)
259
+ display = hold_display.set_index('Player')
260
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
261
+ st.download_button(
262
+ label="Export Baselines",
263
+ data=convert_df_to_csv(display),
264
+ file_name='CSGO_Baselines_export.csv',
265
+ mime='text/csv',
266
+ )
267
+
268
+ with tab5:
269
+ if st.button("Reset Data", key='reset5'):
270
+ # Clear values from *all* all in-memory and on-disk data caches:
271
+ # i.e. clear values from both square and cube
272
+ st.cache_data.clear()
273
+ hold_display = two_map_load(two_map)
274
+ display = hold_display.set_index('Player')
275
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
276
+ st.download_button(
277
+ label="Export Baselines",
278
+ data=convert_df_to_csv(display),
279
+ file_name='CSGO_2_map_export.csv',
280
+ mime='text/csv',
281
+ )