Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ import plotly.graph_objects as go
|
|
| 9 |
|
| 10 |
df = pd.DataFrame()
|
| 11 |
bettype_list = []
|
| 12 |
-
betstatus_list = []
|
| 13 |
|
| 14 |
# Define a function to prepare default UI values after file upload
|
| 15 |
def prepare_ui(file):
|
|
@@ -23,7 +22,7 @@ def prepare_ui(file):
|
|
| 23 |
return min_date, max_date, gr.update(choices=bet_types, value="All")
|
| 24 |
|
| 25 |
def load_csv(file):
|
| 26 |
-
global df, bettype_list
|
| 27 |
if file is None:
|
| 28 |
return (
|
| 29 |
gr.update(choices=[], interactive=False),
|
|
@@ -37,13 +36,11 @@ def load_csv(file):
|
|
| 37 |
|
| 38 |
market_options = sorted(df['sourcemarkettypename2'].dropna().unique())
|
| 39 |
bettype_list = sorted(df['bet_type'].dropna().unique())
|
| 40 |
-
betstatus_list = sorted(df['status'].dropna().unique())
|
| 41 |
|
| 42 |
return (
|
| 43 |
gr.update(choices=market_options, value=None, interactive=True),
|
| 44 |
gr.update(choices=[], value=None, interactive=False),
|
| 45 |
-
gr.update(choices=bettype_list, value=
|
| 46 |
-
gr.update(choices=betstatus_list, value=betstatus_list, interactive=True)
|
| 47 |
)
|
| 48 |
|
| 49 |
def filter_table(start_date, end_date, selected_bet_type):
|
|
@@ -89,13 +86,27 @@ def filter_table(start_date, end_date, selected_bet_type):
|
|
| 89 |
)
|
| 90 |
|
| 91 |
top_selections = agg_df.sort_values("pot_risk", ascending=True).groupby("eventname").head(1)
|
|
|
|
| 92 |
top_10 = top_selections.nsmallest(10, "pot_risk")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
top_10["pot_risk"] = top_10["pot_risk"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 95 |
top_10["handle"] = top_10["handle"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 96 |
-
top_10["handle_no_value"] = top_10["handle_no_value"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 97 |
-
|
| 98 |
-
return top_10.sort_values("pot_risk", ascending=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def get_players(market_type):
|
| 101 |
if market_type is None or df.empty:
|
|
@@ -141,7 +152,7 @@ def generate_stacked_bar(data, x, y, category, title, y_label, normalize=False):
|
|
| 141 |
|
| 142 |
fig.add_annotation(
|
| 143 |
xref="paper", yref="paper",
|
| 144 |
-
x=0.
|
| 145 |
text="Over",
|
| 146 |
showarrow=False,
|
| 147 |
font=dict(size=12, color="green"),
|
|
@@ -150,7 +161,7 @@ def generate_stacked_bar(data, x, y, category, title, y_label, normalize=False):
|
|
| 150 |
|
| 151 |
fig.add_annotation(
|
| 152 |
xref="paper", yref="paper",
|
| 153 |
-
x=0.
|
| 154 |
text="Under",
|
| 155 |
showarrow=False,
|
| 156 |
font=dict(size=12, color="red"),
|
|
@@ -163,6 +174,7 @@ def generate_stacked_bar(data, x, y, category, title, y_label, normalize=False):
|
|
| 163 |
title=title,
|
| 164 |
xaxis_title=x,
|
| 165 |
yaxis_title=y_label + (" (%)" if normalize else ""),
|
|
|
|
| 166 |
xaxis_tickangle=-45,
|
| 167 |
bargap=0.25,
|
| 168 |
autosize=True,
|
|
@@ -179,7 +191,7 @@ def generate_stacked_bar(data, x, y, category, title, y_label, normalize=False):
|
|
| 179 |
|
| 180 |
return fig
|
| 181 |
|
| 182 |
-
def generate_charts(market_type, player_name, bettype_filters,
|
| 183 |
if not market_type or not player_name or df.empty:
|
| 184 |
return None, None
|
| 185 |
|
|
@@ -188,10 +200,8 @@ def generate_charts(market_type, player_name, bettype_filters, betstatus_filters
|
|
| 188 |
(df['player_name'] == player_name)
|
| 189 |
].copy()
|
| 190 |
|
| 191 |
-
if bettype_filters:
|
| 192 |
-
filtered = filtered[filtered['bet_type']
|
| 193 |
-
if betstatus_filters:
|
| 194 |
-
filtered = filtered[filtered['status'].isin(betstatus_filters)]
|
| 195 |
|
| 196 |
selection_parts = filtered['selectionname2'].str.extract(r'(?P<direction>Over|Under)\s+(?P<line>[\d\.]+)')
|
| 197 |
filtered['direction'] = selection_parts['direction']
|
|
@@ -215,8 +225,8 @@ def generate_charts(market_type, player_name, bettype_filters, betstatus_filters
|
|
| 215 |
lambda row: -row['bet_count'] if row['direction'] == 'Under' else row['bet_count'], axis=1
|
| 216 |
)
|
| 217 |
|
| 218 |
-
fig_risk = generate_stacked_bar(summary, 'line', 'pot_risk', 'customerbracket', f"Liability for {player_name} - {market_type}", f"Potential Risk")
|
| 219 |
-
fig_bets = generate_stacked_bar(summary, 'line', 'bet_count', 'customerbracket', f"Bet Count for {player_name} - {market_type}", f"Bet Count")
|
| 220 |
|
| 221 |
table_df = (
|
| 222 |
filtered
|
|
@@ -235,6 +245,8 @@ def generate_charts(market_type, player_name, bettype_filters, betstatus_filters
|
|
| 235 |
table_df['handle'] = table_df['handle'].round(0).astype(int).apply(lambda x: f"${x:,}")
|
| 236 |
table_df['potential_risk'] = table_df['potential_risk'].round(0).astype(int).apply(lambda x: f"${x:,}" if x >= 0 else f"-${abs(int(round(x))):,}")
|
| 237 |
|
|
|
|
|
|
|
| 238 |
net_lines = sorted(filtered['selection_num'].dropna().unique())
|
| 239 |
net_rows = []
|
| 240 |
|
|
@@ -273,6 +285,8 @@ def generate_charts(market_type, player_name, bettype_filters, betstatus_filters
|
|
| 273 |
|
| 274 |
net_liability_df = pd.DataFrame(net_rows)
|
| 275 |
|
|
|
|
|
|
|
| 276 |
return fig_risk, fig_bets, table_df, net_liability_df
|
| 277 |
|
| 278 |
with gr.Blocks(title="Top NFL Outright Markets by Liability") as demo:
|
|
@@ -285,7 +299,7 @@ with gr.Blocks(title="Top NFL Outright Markets by Liability") as demo:
|
|
| 285 |
end_input = gr.Textbox(label="End Date (YYYY-MM-DD)")
|
| 286 |
bet_type_input = gr.Dropdown(label="Bet Type", choices=["All"], value="All")
|
| 287 |
|
| 288 |
-
table = gr.Dataframe(label="Liability Summary", interactive=False)
|
| 289 |
|
| 290 |
file_input.change(fn=prepare_ui, inputs=file_input, outputs=[start_input, end_input, bet_type_input])
|
| 291 |
start_input.change(fn=filter_table, inputs=[start_input, end_input, bet_type_input], outputs=[table])
|
|
@@ -297,25 +311,24 @@ with gr.Blocks(title="Top NFL Outright Markets by Liability") as demo:
|
|
| 297 |
with gr.Row():
|
| 298 |
market_dropdown = gr.Dropdown(choices=[], label="Market Type", interactive=False)
|
| 299 |
player_dropdown = gr.Dropdown(choices=[], label="Player Name", interactive=False)
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
betstatus_checkbox = gr.CheckboxGroup(choices=[], label="Bet Status", interactive=False)
|
| 304 |
-
|
| 305 |
-
with gr.Row():
|
| 306 |
-
normalize_toggle = gr.Checkbox(label="Normalize bar height (percent scale)", value=False)
|
| 307 |
-
submit_button = gr.Button("Submit")
|
| 308 |
|
| 309 |
with gr.Row():
|
| 310 |
chart_risk = gr.Plot(label="Liability (pot_risk)")
|
| 311 |
chart_bets = gr.Plot(label="Bet Count")
|
| 312 |
|
|
|
|
|
|
|
| 313 |
with gr.Row():
|
| 314 |
-
data_table = gr.DataFrame(
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
with gr.Row():
|
| 317 |
net_liability_table = gr.DataFrame(
|
| 318 |
-
headers=["line", "under_net_liability", "over_net_liability"],
|
| 319 |
label="Net Liability by Line",
|
| 320 |
interactive=False
|
| 321 |
)
|
|
@@ -323,14 +336,20 @@ with gr.Blocks(title="Top NFL Outright Markets by Liability") as demo:
|
|
| 323 |
file_input.change(
|
| 324 |
fn=load_csv,
|
| 325 |
inputs=file_input,
|
| 326 |
-
outputs=[market_dropdown, player_dropdown,
|
| 327 |
)
|
| 328 |
|
| 329 |
market_dropdown.change(fn=update_players, inputs=market_dropdown, outputs=player_dropdown)
|
| 330 |
|
| 331 |
submit_button.click(
|
| 332 |
fn=generate_charts,
|
| 333 |
-
inputs=[market_dropdown, player_dropdown,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
outputs=[chart_risk, chart_bets, data_table, net_liability_table]
|
| 335 |
)
|
| 336 |
|
|
|
|
| 9 |
|
| 10 |
df = pd.DataFrame()
|
| 11 |
bettype_list = []
|
|
|
|
| 12 |
|
| 13 |
# Define a function to prepare default UI values after file upload
|
| 14 |
def prepare_ui(file):
|
|
|
|
| 22 |
return min_date, max_date, gr.update(choices=bet_types, value="All")
|
| 23 |
|
| 24 |
def load_csv(file):
|
| 25 |
+
global df, bettype_list
|
| 26 |
if file is None:
|
| 27 |
return (
|
| 28 |
gr.update(choices=[], interactive=False),
|
|
|
|
| 36 |
|
| 37 |
market_options = sorted(df['sourcemarkettypename2'].dropna().unique())
|
| 38 |
bettype_list = sorted(df['bet_type'].dropna().unique())
|
|
|
|
| 39 |
|
| 40 |
return (
|
| 41 |
gr.update(choices=market_options, value=None, interactive=True),
|
| 42 |
gr.update(choices=[], value=None, interactive=False),
|
| 43 |
+
gr.update(choices=["All"] + bettype_list, value="All", interactive=True),
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
def filter_table(start_date, end_date, selected_bet_type):
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
top_selections = agg_df.sort_values("pot_risk", ascending=True).groupby("eventname").head(1)
|
| 89 |
+
|
| 90 |
top_10 = top_selections.nsmallest(10, "pot_risk")
|
| 91 |
+
top_10["Sharp Handle"] = top_10.apply(
|
| 92 |
+
lambda row: f"${int(row['handle_no_value']):,} ({round((row['handle_no_value'] / row['handle']) * 100)}%)"
|
| 93 |
+
if row["handle_no_value"] > 0 else "$0 (0%)",
|
| 94 |
+
axis = 1
|
| 95 |
+
)
|
| 96 |
|
| 97 |
top_10["pot_risk"] = top_10["pot_risk"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 98 |
top_10["handle"] = top_10["handle"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 99 |
+
#top_10["handle_no_value"] = top_10["handle_no_value"].apply(lambda x: f"${int(x):,}" if x >= 0 else f"-${abs(int(x)):,}")
|
| 100 |
+
|
| 101 |
+
return top_10.sort_values("pot_risk", ascending=False).rename(columns={
|
| 102 |
+
"eventname": "Event Name",
|
| 103 |
+
"player_name": "Name",
|
| 104 |
+
"selectionname2": "Line",
|
| 105 |
+
"pot_risk": "Risk",
|
| 106 |
+
"bet_count": "Bets",
|
| 107 |
+
"handle": "Handle",
|
| 108 |
+
"Sharp Handle": "Sharp Handle"
|
| 109 |
+
}).drop(columns=["handle_no_value", "%_handle_no_value"])
|
| 110 |
|
| 111 |
def get_players(market_type):
|
| 112 |
if market_type is None or df.empty:
|
|
|
|
| 152 |
|
| 153 |
fig.add_annotation(
|
| 154 |
xref="paper", yref="paper",
|
| 155 |
+
x=-0.1, y=1.05,
|
| 156 |
text="Over",
|
| 157 |
showarrow=False,
|
| 158 |
font=dict(size=12, color="green"),
|
|
|
|
| 161 |
|
| 162 |
fig.add_annotation(
|
| 163 |
xref="paper", yref="paper",
|
| 164 |
+
x=-0.1, y=-0.05,
|
| 165 |
text="Under",
|
| 166 |
showarrow=False,
|
| 167 |
font=dict(size=12, color="red"),
|
|
|
|
| 174 |
title=title,
|
| 175 |
xaxis_title=x,
|
| 176 |
yaxis_title=y_label + (" (%)" if normalize else ""),
|
| 177 |
+
yaxis_tickprefix= ("%" if normalize else ""),
|
| 178 |
xaxis_tickangle=-45,
|
| 179 |
bargap=0.25,
|
| 180 |
autosize=True,
|
|
|
|
| 191 |
|
| 192 |
return fig
|
| 193 |
|
| 194 |
+
def generate_charts(market_type, player_name, bettype_filters, normalize):
|
| 195 |
if not market_type or not player_name or df.empty:
|
| 196 |
return None, None
|
| 197 |
|
|
|
|
| 200 |
(df['player_name'] == player_name)
|
| 201 |
].copy()
|
| 202 |
|
| 203 |
+
if bettype_filters and bettype_filters != "All":
|
| 204 |
+
filtered = filtered[filtered['bet_type'] == bettype_filters]
|
|
|
|
|
|
|
| 205 |
|
| 206 |
selection_parts = filtered['selectionname2'].str.extract(r'(?P<direction>Over|Under)\s+(?P<line>[\d\.]+)')
|
| 207 |
filtered['direction'] = selection_parts['direction']
|
|
|
|
| 225 |
lambda row: -row['bet_count'] if row['direction'] == 'Under' else row['bet_count'], axis=1
|
| 226 |
)
|
| 227 |
|
| 228 |
+
fig_risk = generate_stacked_bar(summary, 'line', 'pot_risk', 'customerbracket', f"Liability for {player_name} - {market_type}", f"Potential Risk", normalize)
|
| 229 |
+
fig_bets = generate_stacked_bar(summary, 'line', 'bet_count', 'customerbracket', f"Bet Count for {player_name} - {market_type}", f"Bet Count", normalize)
|
| 230 |
|
| 231 |
table_df = (
|
| 232 |
filtered
|
|
|
|
| 245 |
table_df['handle'] = table_df['handle'].round(0).astype(int).apply(lambda x: f"${x:,}")
|
| 246 |
table_df['potential_risk'] = table_df['potential_risk'].round(0).astype(int).apply(lambda x: f"${x:,}" if x >= 0 else f"-${abs(int(round(x))):,}")
|
| 247 |
|
| 248 |
+
table_df.columns = ["Line", "Selection", "Handle", "Risk", "Avg. Odds", "Bet Count"]
|
| 249 |
+
|
| 250 |
net_lines = sorted(filtered['selection_num'].dropna().unique())
|
| 251 |
net_rows = []
|
| 252 |
|
|
|
|
| 285 |
|
| 286 |
net_liability_df = pd.DataFrame(net_rows)
|
| 287 |
|
| 288 |
+
net_liability_df.columns = ["Line", "Under Net Risk", "Over Net Risk", "Interval Range", "Interval Exposure"]
|
| 289 |
+
|
| 290 |
return fig_risk, fig_bets, table_df, net_liability_df
|
| 291 |
|
| 292 |
with gr.Blocks(title="Top NFL Outright Markets by Liability") as demo:
|
|
|
|
| 299 |
end_input = gr.Textbox(label="End Date (YYYY-MM-DD)")
|
| 300 |
bet_type_input = gr.Dropdown(label="Bet Type", choices=["All"], value="All")
|
| 301 |
|
| 302 |
+
table = gr.Dataframe(label="Top 10 Liability Summary", interactive=False)
|
| 303 |
|
| 304 |
file_input.change(fn=prepare_ui, inputs=file_input, outputs=[start_input, end_input, bet_type_input])
|
| 305 |
start_input.change(fn=filter_table, inputs=[start_input, end_input, bet_type_input], outputs=[table])
|
|
|
|
| 311 |
with gr.Row():
|
| 312 |
market_dropdown = gr.Dropdown(choices=[], label="Market Type", interactive=False)
|
| 313 |
player_dropdown = gr.Dropdown(choices=[], label="Player Name", interactive=False)
|
| 314 |
+
bettype_dropdown = gr.Dropdown(choices=["All"], value="All", label="Bet Types", interactive=False)
|
| 315 |
+
|
| 316 |
+
submit_button = gr.Button("Submit", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
with gr.Row():
|
| 319 |
chart_risk = gr.Plot(label="Liability (pot_risk)")
|
| 320 |
chart_bets = gr.Plot(label="Bet Count")
|
| 321 |
|
| 322 |
+
normalize_toggle = gr.Checkbox(label="Normalize bar height (percent scale)", value=False)
|
| 323 |
+
|
| 324 |
with gr.Row():
|
| 325 |
+
data_table = gr.DataFrame(
|
| 326 |
+
label="Liability Summary",
|
| 327 |
+
interactive=False
|
| 328 |
+
)
|
| 329 |
|
| 330 |
with gr.Row():
|
| 331 |
net_liability_table = gr.DataFrame(
|
|
|
|
| 332 |
label="Net Liability by Line",
|
| 333 |
interactive=False
|
| 334 |
)
|
|
|
|
| 336 |
file_input.change(
|
| 337 |
fn=load_csv,
|
| 338 |
inputs=file_input,
|
| 339 |
+
outputs=[market_dropdown, player_dropdown, bettype_dropdown]
|
| 340 |
)
|
| 341 |
|
| 342 |
market_dropdown.change(fn=update_players, inputs=market_dropdown, outputs=player_dropdown)
|
| 343 |
|
| 344 |
submit_button.click(
|
| 345 |
fn=generate_charts,
|
| 346 |
+
inputs=[market_dropdown, player_dropdown, bettype_dropdown, normalize_toggle],
|
| 347 |
+
outputs=[chart_risk, chart_bets, data_table, net_liability_table]
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
normalize_toggle.change(
|
| 351 |
+
fn=generate_charts,
|
| 352 |
+
inputs=[market_dropdown, player_dropdown, bettype_dropdown, normalize_toggle],
|
| 353 |
outputs=[chart_risk, chart_bets, data_table, net_liability_table]
|
| 354 |
)
|
| 355 |
|