Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from numpy import where as np_where
|
4 |
+
import pandas as pd
|
5 |
+
import gspread
|
6 |
+
import plotly.express as px
|
7 |
+
import scipy.stats as stats
|
8 |
+
from pymongo import MongoClient
|
9 |
+
st.set_page_config(layout="wide")
|
10 |
+
|
11 |
+
@st.cache_resource
|
12 |
+
def init_conn():
|
13 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
14 |
+
|
15 |
+
credentials = {
|
16 |
+
"type": "service_account",
|
17 |
+
"project_id": "model-sheets-connect",
|
18 |
+
"private_key_id": st.secrets['model_sheets_connect_pk'],
|
19 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w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Lm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
|
20 |
+
"client_email": "[email protected]",
|
21 |
+
"client_id": "100369174533302798535",
|
22 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
23 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
24 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
25 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
|
26 |
+
}
|
27 |
+
|
28 |
+
credentials2 = {
|
29 |
+
"type": "service_account",
|
30 |
+
"project_id": "sheets-api-connect-378620",
|
31 |
+
"private_key_id": st.secrets['sheets_api_connect_pk'],
|
32 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnb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ocsBd3Woy9\n+hXXecJHPOKV4oUJlJgvAZqe5HGBqEoTEK0wyPNLSQlO/9ypd+0fEnArwFHO7CMF\nycQprAKHJXM1eOOFFuZeQCaInqdPZy1UcV5Szla4UmUZWkk1m24blHzXAoGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
|
33 |
+
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
|
34 |
+
"client_id": "106625872877651920064",
|
35 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
36 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
37 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
38 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
39 |
+
}
|
40 |
+
|
41 |
+
NFL_Data = st.secrets['NFL_Data']
|
42 |
+
|
43 |
+
uri = st.secrets['mongo_uri']
|
44 |
+
client = MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=100000)
|
45 |
+
dfs_db = client["NFL_Database"]
|
46 |
+
props_db = client["Props_DB"]
|
47 |
+
|
48 |
+
gc = gspread.service_account_from_dict(credentials)
|
49 |
+
gc2 = gspread.service_account_from_dict(credentials2)
|
50 |
+
|
51 |
+
return gc, gc2, NFL_Data, props_db, dfs_db
|
52 |
+
|
53 |
+
gcservice_account, gcservice_account2, NFL_Data, props_db, dfs_db = init_conn()
|
54 |
+
|
55 |
+
game_format = {'Win%': '{:.2%}', 'Vegas': '{:.2%}', 'Win% Diff': '{:.2%}'}
|
56 |
+
american_format = {'First Inning Lead Percentage': '{:.2%}', 'Fifth Inning Lead Percentage': '{:.2%}'}
|
57 |
+
|
58 |
+
@st.cache_resource(ttl=600)
|
59 |
+
def init_baselines():
|
60 |
+
collection = dfs_db["Game_Betting_Model"]
|
61 |
+
cursor = collection.find()
|
62 |
+
raw_display = pd.DataFrame(list(cursor))
|
63 |
+
game_model = raw_display[['Team', 'Opp', 'Win%', 'Vegas', 'Win% Diff', 'Win Line', 'Vegas Line', 'Line Diff', 'PD Spread', 'Vegas Spread', 'Spread Diff']]
|
64 |
+
|
65 |
+
collection = dfs_db["Player_Stats"]
|
66 |
+
cursor = collection.find()
|
67 |
+
raw_display = pd.DataFrame(list(cursor))
|
68 |
+
overall_stats = raw_display[['Player', 'Position', 'Team', 'Opp', 'rush_att', 'rec', 'dropbacks', 'rush_yards', 'rush_tds', 'rec_yards', 'rec_tds', 'pass_att', 'pass_yards', 'pass_tds', 'PPR', 'Half_PPR']]
|
69 |
+
|
70 |
+
collection = dfs_db["Prop_Trends"]
|
71 |
+
cursor = collection.find()
|
72 |
+
raw_display = pd.DataFrame(list(cursor))
|
73 |
+
prop_trends = raw_display[['Player', 'over_prop', 'over_line', 'under_prop', 'under_line', 'book', 'prop_type', 'No Vig', 'Team', 'L3 Success', 'L6_Success', 'L10_success', 'L6 Avg', 'Projection',
|
74 |
+
'Proj Diff', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge']]
|
75 |
+
|
76 |
+
collection = dfs_db["DK_NFL_ROO"]
|
77 |
+
cursor = collection.find()
|
78 |
+
|
79 |
+
raw_display = pd.DataFrame(list(cursor))
|
80 |
+
raw_display = raw_display[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%',
|
81 |
+
'Own', 'Small_Field_Own', 'Large_Field_Own', 'Cash_Field_Own', 'CPT_Own', 'LevX', 'version', 'slate', 'timestamp', 'player_id', 'site']]
|
82 |
+
load_display = raw_display[raw_display['Position'] != 'K']
|
83 |
+
timestamp = load_display['timestamp'][0]
|
84 |
+
|
85 |
+
collection = dfs_db["Prop_Trends"]
|
86 |
+
cursor = collection.find()
|
87 |
+
raw_display = pd.DataFrame(list(cursor))
|
88 |
+
prop_frame = raw_display[['Player', 'over_prop', 'over_line', 'under_prop', 'under_line', 'book', 'prop_type', 'No Vig', 'Team', 'L3 Success', 'L6_Success', 'L10_success', 'L6 Avg', 'Projection',
|
89 |
+
'Proj Diff', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge']]
|
90 |
+
|
91 |
+
collection = dfs_db['Pick6_Trends']
|
92 |
+
cursor = collection.find()
|
93 |
+
raw_display = pd.DataFrame(list(cursor))
|
94 |
+
pick_frame = raw_display[['Player', 'over_prop', 'over_line', 'under_prop', 'under_line', 'book', 'prop_type', 'No Vig', 'Team', 'L3 Success', 'L6_Success', 'L10_success', 'L6 Avg', 'Projection',
|
95 |
+
'Proj Diff', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge', 'last_name', 'P6_name', 'Full_name']]
|
96 |
+
|
97 |
+
collection = props_db["NFL_Props"]
|
98 |
+
cursor = collection.find()
|
99 |
+
|
100 |
+
raw_display = pd.DataFrame(list(cursor))
|
101 |
+
market_props = raw_display[['Name', 'Position', 'Projection', 'PropType', 'OddsType', 'over_pay', 'under_pay']]
|
102 |
+
market_props['over_prop'] = market_props['Projection']
|
103 |
+
market_props['over_line'] = market_props['over_pay'].apply(lambda x: (x - 1) * 100 if x >= 2.0 else -100 / (x - 1))
|
104 |
+
market_props['under_prop'] = market_props['Projection']
|
105 |
+
market_props['under_line'] = market_props['under_pay'].apply(lambda x: (x - 1) * 100 if x >= 2.0 else -100 / (x - 1))
|
106 |
+
|
107 |
+
return game_model, overall_stats, timestamp, prop_frame, prop_trends, pick_frame, market_props
|
108 |
+
|
109 |
+
def convert_df_to_csv(df):
|
110 |
+
return df.to_csv().encode('utf-8')
|
111 |
+
|
112 |
+
with tab1:
|
113 |
+
st.info(t_stamp)
|
114 |
+
if st.button("Reset Data", key='reset1'):
|
115 |
+
st.cache_data.clear()
|
116 |
+
game_model, overall_stats, timestamp, prop_frame, prop_trends, pick_frame, market_props = init_baselines()
|
117 |
+
qb_stats = overall_stats[overall_stats['Position'] == 'QB']
|
118 |
+
qb_stats = qb_stats.drop_duplicates(subset=['Player', 'Position'])
|
119 |
+
non_qb_stats = overall_stats[overall_stats['Position'] != 'QB']
|
120 |
+
non_qb_stats = non_qb_stats.drop_duplicates(subset=['Player', 'Position'])
|
121 |
+
team_dict = dict(zip(prop_frame['Player'], prop_frame['Team']))
|
122 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
123 |
+
line_var1 = st.radio('How would you like to display odds?', options = ['Percentage', 'American'], key='line_var1')
|
124 |
+
team_frame = game_model
|
125 |
+
if line_var1 == 'Percentage':
|
126 |
+
team_frame = team_frame[['Team', 'Opp', 'Win%', 'Vegas', 'Win% Diff', 'PD Spread', 'Vegas Spread', 'Spread Diff']]
|
127 |
+
team_frame = team_frame.set_index('Team')
|
128 |
+
try:
|
129 |
+
st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), use_container_width = True)
|
130 |
+
except:
|
131 |
+
st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
132 |
+
if line_var1 == 'American':
|
133 |
+
team_frame = team_frame[['Team', 'Opp', 'Win Line', 'Vegas Line', 'Line Diff', 'PD Spread', 'Vegas Spread', 'Spread Diff']]
|
134 |
+
team_frame = team_frame.set_index('Team')
|
135 |
+
st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 1000, use_container_width = True)
|
136 |
+
|
137 |
+
st.download_button(
|
138 |
+
label="Export Team Model",
|
139 |
+
data=convert_df_to_csv(team_frame),
|
140 |
+
file_name='NFL_team_betting_export.csv',
|
141 |
+
mime='text/csv',
|
142 |
+
key='team_export',
|
143 |
+
)
|