Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,48 +3,69 @@ import numpy as np
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
#
|
| 7 |
# -----------------------------
|
| 8 |
-
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
}
|
| 18 |
|
| 19 |
STAT_LABELS = {
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
|
|
|
|
| 27 |
}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
TIMEFRAMES = {
|
| 30 |
"Game": "",
|
| 31 |
"1H": "_1h",
|
| 32 |
"1Q": "_1q",
|
| 33 |
}
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# -----------------------------
|
| 36 |
# Odds helpers
|
| 37 |
# -----------------------------
|
| 38 |
def to_american_odds(prob: float) -> str:
|
| 39 |
-
prob = max(min(prob, 0.9999), 0.0001)
|
| 40 |
if prob >= 0.5:
|
| 41 |
-
return
|
| 42 |
else:
|
| 43 |
return f"+{int(100 * (1 - prob) / prob)}"
|
| 44 |
|
| 45 |
def apply_vig(prob: float, vig_pct: float) -> float:
|
| 46 |
-
|
| 47 |
-
return max(min(prob + vig_pct / 100.0, 0.9999), 0.0001)
|
| 48 |
|
| 49 |
# -----------------------------
|
| 50 |
# Global store after upload
|
|
@@ -57,48 +78,59 @@ df = None
|
|
| 57 |
def load_csv(file):
|
| 58 |
global df
|
| 59 |
if file is None:
|
| 60 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
tmp = pd.read_csv(file.name)
|
| 64 |
except Exception as e:
|
| 65 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
# Normalize column names
|
| 68 |
tmp.columns = [c.strip() for c in tmp.columns]
|
| 69 |
df = tmp
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
warn = ""
|
| 75 |
-
if missing:
|
| 76 |
-
warn = f"β οΈ Missing columns expected from the exporter: {missing}. Team tab requires 'player_team'."
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
return (
|
| 83 |
gr.update(choices=teams, value=(teams[0] if teams else None)),
|
| 84 |
gr.update(choices=players, value=[]),
|
| 85 |
-
(
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
# -----------------------------
|
| 89 |
# Core calculators
|
| 90 |
# -----------------------------
|
| 91 |
-
def
|
|
|
|
|
|
|
| 92 |
suffix = TIMEFRAMES.get(timeframe_key, "")
|
| 93 |
-
|
| 94 |
-
col = f"{base}{suffix}"
|
| 95 |
-
return col
|
| 96 |
|
| 97 |
def player_milestone_rows(pdata: pd.DataFrame, player_name: str, stat_col: str, milestones: list[int]) -> list[dict]:
|
| 98 |
rows = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
if stat_col not in pdata.columns:
|
| 100 |
-
# No
|
| 101 |
-
games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 102 |
for m in milestones:
|
| 103 |
rows.append({
|
| 104 |
"Player": player_name,
|
|
@@ -113,23 +145,18 @@ def player_milestone_rows(pdata: pd.DataFrame, player_name: str, stat_col: str,
|
|
| 113 |
return rows
|
| 114 |
|
| 115 |
series = pd.to_numeric(pdata[stat_col], errors="coerce").dropna()
|
| 116 |
-
games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(series)
|
| 117 |
mean = series.mean() if not series.empty else np.nan
|
| 118 |
|
| 119 |
for m in milestones:
|
| 120 |
-
if games
|
| 121 |
-
prob = 0.0
|
| 122 |
-
else:
|
| 123 |
-
prob = float((series >= m).sum()) / float(games)
|
| 124 |
-
odds = to_american_odds(prob) if games > 0 else "+10000"
|
| 125 |
rows.append({
|
| 126 |
"Player": player_name,
|
| 127 |
"Games": games,
|
| 128 |
"Milestone": f"{m}+",
|
| 129 |
"Hit %": round(prob * 100, 2),
|
| 130 |
"Prob": round(prob, 4),
|
| 131 |
-
"True Odds":
|
| 132 |
-
"Odds w/ Vig": to_american_odds(apply_vig(prob, 4.92)),
|
| 133 |
"Mean": round(mean, 2) if not np.isnan(mean) else np.nan,
|
| 134 |
})
|
| 135 |
return rows
|
|
@@ -137,39 +164,35 @@ def player_milestone_rows(pdata: pd.DataFrame, player_name: str, stat_col: str,
|
|
| 137 |
def compute_team_table(team: str, stat_key: str, timeframe_key: str, vig_pct: float, player_subset: list[str] | None):
|
| 138 |
if df is None:
|
| 139 |
return pd.DataFrame(), "Upload a CSV first."
|
| 140 |
-
|
| 141 |
if not team:
|
| 142 |
return pd.DataFrame(), "Pick a team."
|
| 143 |
-
|
| 144 |
if stat_key not in NFL_MILESTONES:
|
| 145 |
return pd.DataFrame(), f"Choose a stat. Unknown: {stat_key}"
|
| 146 |
|
| 147 |
-
stat_col =
|
| 148 |
milestones = NFL_MILESTONES[stat_key]
|
| 149 |
|
| 150 |
-
team_df = df[df
|
| 151 |
if player_subset:
|
| 152 |
-
team_df = team_df[team_df
|
| 153 |
|
| 154 |
if team_df.empty:
|
| 155 |
return pd.DataFrame(), f"No rows for team {team} with current filters."
|
| 156 |
|
| 157 |
rows = []
|
| 158 |
-
for player, pdata in team_df.groupby("player"):
|
| 159 |
rows.extend(player_milestone_rows(pdata, player, stat_col, milestones))
|
| 160 |
|
| 161 |
out = pd.DataFrame(rows)
|
| 162 |
-
|
| 163 |
-
# Update vig odds with UI vig value
|
| 164 |
out["Odds w/ Vig"] = out["Prob"].apply(lambda p: to_american_odds(apply_vig(float(p), float(vig_pct))))
|
| 165 |
|
| 166 |
-
#
|
| 167 |
def milestone_int(s):
|
| 168 |
try:
|
| 169 |
return int(str(s).replace("+", ""))
|
| 170 |
except:
|
| 171 |
return 0
|
| 172 |
-
|
| 173 |
out["_m"] = out["Milestone"].apply(milestone_int)
|
| 174 |
out = out.sort_values(by=["Prob", "Player", "_m"], ascending=[False, True, True]).drop(columns=["_m"])
|
| 175 |
return out, f"Showing {team} β’ {STAT_LABELS.get(stat_key, stat_key)} β’ {timeframe_key}"
|
|
@@ -182,30 +205,37 @@ def compute_player_table(players: list[str], stat_key: str, timeframe_key: str,
|
|
| 182 |
if stat_key not in NFL_MILESTONES:
|
| 183 |
return pd.DataFrame(), f"Choose a stat. Unknown: {stat_key}"
|
| 184 |
|
| 185 |
-
stat_col =
|
| 186 |
milestones = NFL_MILESTONES[stat_key]
|
| 187 |
|
| 188 |
-
|
| 189 |
-
work = df[df["player"].isin(players)].copy()
|
| 190 |
if work.empty:
|
| 191 |
return pd.DataFrame(), "No rows found for those players."
|
| 192 |
|
| 193 |
-
|
|
|
|
| 194 |
rows.extend(player_milestone_rows(pdata, player, stat_col, milestones))
|
| 195 |
|
| 196 |
out = pd.DataFrame(rows)
|
| 197 |
out["Odds w/ Vig"] = out["Prob"].apply(lambda p: to_american_odds(apply_vig(float(p), float(vig_pct))))
|
| 198 |
|
| 199 |
-
#
|
| 200 |
if "player_team" in work.columns:
|
| 201 |
-
team_map = (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
out = out.merge(team_map.rename("Team"), left_on="Player", right_index=True, how="left")
|
| 203 |
-
# Move Team next to Player
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
| 207 |
|
| 208 |
-
#
|
| 209 |
def milestone_int(s):
|
| 210 |
try:
|
| 211 |
return int(str(s).replace("+", ""))
|
|
@@ -221,8 +251,8 @@ def compute_player_table(players: list[str], stat_key: str, timeframe_key: str,
|
|
| 221 |
with gr.Blocks(title="π NFL Team & Player Milestones") as demo:
|
| 222 |
gr.Markdown("## π NFL Milestone Explorer")
|
| 223 |
gr.Markdown(
|
| 224 |
-
"Upload the `nfl_milestone_logs.csv`
|
| 225 |
-
"
|
| 226 |
)
|
| 227 |
|
| 228 |
file_in = gr.File(label="Upload nfl_milestone_logs.csv", file_types=[".csv"])
|
|
@@ -232,54 +262,33 @@ with gr.Blocks(title="π NFL Team & Player Milestones") as demo:
|
|
| 232 |
# -------- Team Tab --------
|
| 233 |
with gr.Tab("Team"):
|
| 234 |
with gr.Row():
|
| 235 |
-
team_dd = gr.Dropdown(label="Team (
|
| 236 |
-
stat_dd = gr.Dropdown(
|
| 237 |
-
label="Stat",
|
| 238 |
-
choices=list(STAT_LABELS.keys()),
|
| 239 |
-
value="rec_yds",
|
| 240 |
-
info="Choose the stat to analyze"
|
| 241 |
-
)
|
| 242 |
timeframe_radio = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
| 243 |
vig_num = gr.Number(label="Vig %", value=4.92, precision=2)
|
| 244 |
|
| 245 |
-
# Player filter within the selected team
|
| 246 |
team_players_ms = gr.Dropdown(choices=[], multiselect=True, label="Filter to Players (optional)")
|
| 247 |
|
| 248 |
team_btn = gr.Button("Compute Team Table")
|
| 249 |
team_out_title = gr.Markdown(visible=False)
|
| 250 |
-
team_table = gr.Dataframe(
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
visible=False,
|
| 254 |
-
wrap=True
|
| 255 |
-
)
|
| 256 |
-
|
| 257 |
-
# -------- Player Tab --------
|
| 258 |
with gr.Tab("Player"):
|
| 259 |
with gr.Row():
|
| 260 |
players_ms = gr.Dropdown(choices=[], multiselect=True, label="Players")
|
| 261 |
-
p_stat_dd = gr.Dropdown(
|
| 262 |
-
label="Stat",
|
| 263 |
-
choices=list(STAT_LABELS.keys()),
|
| 264 |
-
value="rec_yds"
|
| 265 |
-
)
|
| 266 |
p_timeframe_radio = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
| 267 |
p_vig_num = gr.Number(label="Vig %", value=4.92, precision=2)
|
| 268 |
|
| 269 |
player_btn = gr.Button("Compute Player Table")
|
| 270 |
player_out_title = gr.Markdown(visible=False)
|
| 271 |
-
player_table = gr.Dataframe(
|
| 272 |
-
label="Player Milestones",
|
| 273 |
-
interactive=False,
|
| 274 |
-
visible=False,
|
| 275 |
-
wrap=True
|
| 276 |
-
)
|
| 277 |
|
| 278 |
# ------------- Wiring -------------
|
| 279 |
def on_file(file):
|
| 280 |
-
team_update,
|
| 281 |
-
|
| 282 |
-
return team_update, player_update, player_update, msg
|
| 283 |
|
| 284 |
file_in.change(
|
| 285 |
on_file,
|
|
@@ -287,12 +296,15 @@ with gr.Blocks(title="π NFL Team & Player Milestones") as demo:
|
|
| 287 |
outputs=[team_dd, players_ms, team_players_ms, status]
|
| 288 |
)
|
| 289 |
|
| 290 |
-
#
|
| 291 |
def on_team_change(team):
|
| 292 |
if df is None or not team:
|
| 293 |
return gr.update(choices=[], value=[])
|
| 294 |
-
team_players =
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
team_dd.change(on_team_change, inputs=[team_dd], outputs=[team_players_ms])
|
| 298 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# UI Stat options -> CSV columns
|
| 7 |
# -----------------------------
|
| 8 |
+
STAT_MAP = {
|
| 9 |
+
"pass_att": "att",
|
| 10 |
+
"pass_yds": "pass_yds",
|
| 11 |
+
"pass_td": "pass_td",
|
| 12 |
+
"rush_att": "rush_att",
|
| 13 |
+
"rush_yds": "rush_yds",
|
| 14 |
+
"rush_td": "rush_td",
|
| 15 |
+
"receptions":"receptions",
|
| 16 |
+
"rec_yds": "rec_yds",
|
| 17 |
}
|
| 18 |
|
| 19 |
STAT_LABELS = {
|
| 20 |
+
"pass_att": "Pass Attempts",
|
| 21 |
+
"pass_yds": "Passing Yards",
|
| 22 |
+
"pass_td": "Passing TDs",
|
| 23 |
+
"rush_att": "Rush Attempts",
|
| 24 |
+
"rush_yds": "Rushing Yards",
|
| 25 |
+
"rush_td": "Rushing TDs",
|
| 26 |
+
"receptions":"Receptions",
|
| 27 |
+
"rec_yds": "Receiving Yards",
|
| 28 |
}
|
| 29 |
|
| 30 |
+
# These are the stats youβll be able to select in both tabs (in this order)
|
| 31 |
+
STAT_OPTIONS = [
|
| 32 |
+
"pass_att", "pass_yds", "pass_td",
|
| 33 |
+
"rush_att", "rush_yds", "rush_td",
|
| 34 |
+
"receptions", "rec_yds"
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
TIMEFRAMES = {
|
| 38 |
"Game": "",
|
| 39 |
"1H": "_1h",
|
| 40 |
"1Q": "_1q",
|
| 41 |
}
|
| 42 |
|
| 43 |
+
# -----------------------------
|
| 44 |
+
# Milestone definitions
|
| 45 |
+
# -----------------------------
|
| 46 |
+
NFL_MILESTONES = {
|
| 47 |
+
"pass_att": [15, 20, 25, 30, 35, 40, 45, 50],
|
| 48 |
+
"pass_yds": [150, 200, 225, 250, 275, 300, 325, 350, 400],
|
| 49 |
+
"pass_td": [1, 2, 3, 4, 5],
|
| 50 |
+
"rush_att": [8, 10, 12, 14, 16, 18, 20, 22],
|
| 51 |
+
"rush_yds": [25, 40, 50, 60, 75, 80, 90, 100, 110, 125, 150],
|
| 52 |
+
"rush_td": [1, 2, 3],
|
| 53 |
+
"receptions":[3, 4, 5, 6, 7, 8, 9, 10],
|
| 54 |
+
"rec_yds": [25, 40, 50, 60, 75, 80, 90, 100, 110, 125, 150],
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
# -----------------------------
|
| 58 |
# Odds helpers
|
| 59 |
# -----------------------------
|
| 60 |
def to_american_odds(prob: float) -> str:
|
| 61 |
+
prob = max(min(float(prob), 0.9999), 0.0001)
|
| 62 |
if prob >= 0.5:
|
| 63 |
+
return str(int(-100 * prob / (1 - prob)))
|
| 64 |
else:
|
| 65 |
return f"+{int(100 * (1 - prob) / prob)}"
|
| 66 |
|
| 67 |
def apply_vig(prob: float, vig_pct: float) -> float:
|
| 68 |
+
return max(min(float(prob) + float(vig_pct) / 100.0, 0.9999), 0.0001)
|
|
|
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
# Global store after upload
|
|
|
|
| 78 |
def load_csv(file):
|
| 79 |
global df
|
| 80 |
if file is None:
|
| 81 |
+
return (
|
| 82 |
+
gr.update(choices=[], value=None), # team_dd
|
| 83 |
+
gr.update(choices=[], value=[]), # players_ms
|
| 84 |
+
gr.update(choices=[], value=[]), # team_players_ms
|
| 85 |
+
"Please upload a CSV."
|
| 86 |
+
)
|
| 87 |
|
| 88 |
try:
|
| 89 |
tmp = pd.read_csv(file.name)
|
| 90 |
except Exception as e:
|
| 91 |
+
return (
|
| 92 |
+
gr.update(choices=[], value=None),
|
| 93 |
+
gr.update(choices=[], value=[]),
|
| 94 |
+
gr.update(choices=[], value=[]),
|
| 95 |
+
f"Failed to read CSV: {e}"
|
| 96 |
+
)
|
| 97 |
|
|
|
|
| 98 |
tmp.columns = [c.strip() for c in tmp.columns]
|
| 99 |
df = tmp
|
| 100 |
|
| 101 |
+
# Build lists
|
| 102 |
+
teams = sorted(df.get("player_team", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 103 |
+
players = sorted(df.get("player", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
warn_cols = [c for c in ["player", "player_id", "player_team"] if c not in df.columns]
|
| 106 |
+
msg = "β
File loaded."
|
| 107 |
+
if warn_cols:
|
| 108 |
+
msg = f"β οΈ Missing expected columns: {warn_cols}. Team tab needs 'player_team'."
|
| 109 |
|
| 110 |
return (
|
| 111 |
gr.update(choices=teams, value=(teams[0] if teams else None)),
|
| 112 |
gr.update(choices=players, value=[]),
|
| 113 |
+
gr.update(choices=[], value=[]), # team_players_ms (will fill after team pick)
|
| 114 |
+
msg
|
| 115 |
)
|
| 116 |
|
| 117 |
# -----------------------------
|
| 118 |
# Core calculators
|
| 119 |
# -----------------------------
|
| 120 |
+
def csv_col_for(stat_key: str, timeframe_key: str) -> str:
|
| 121 |
+
"""Map UI stat + timeframe to actual CSV column."""
|
| 122 |
+
base = STAT_MAP.get(stat_key, stat_key)
|
| 123 |
suffix = TIMEFRAMES.get(timeframe_key, "")
|
| 124 |
+
return f"{base}{suffix}"
|
|
|
|
|
|
|
| 125 |
|
| 126 |
def player_milestone_rows(pdata: pd.DataFrame, player_name: str, stat_col: str, milestones: list[int]) -> list[dict]:
|
| 127 |
rows = []
|
| 128 |
+
|
| 129 |
+
# Games played in the scope
|
| 130 |
+
games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 131 |
+
|
| 132 |
if stat_col not in pdata.columns:
|
| 133 |
+
# No such column β still show rows with zeros so the table never breaks
|
|
|
|
| 134 |
for m in milestones:
|
| 135 |
rows.append({
|
| 136 |
"Player": player_name,
|
|
|
|
| 145 |
return rows
|
| 146 |
|
| 147 |
series = pd.to_numeric(pdata[stat_col], errors="coerce").dropna()
|
|
|
|
| 148 |
mean = series.mean() if not series.empty else np.nan
|
| 149 |
|
| 150 |
for m in milestones:
|
| 151 |
+
prob = float((series >= m).sum()) / float(games) if games > 0 else 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
rows.append({
|
| 153 |
"Player": player_name,
|
| 154 |
"Games": games,
|
| 155 |
"Milestone": f"{m}+",
|
| 156 |
"Hit %": round(prob * 100, 2),
|
| 157 |
"Prob": round(prob, 4),
|
| 158 |
+
"True Odds": to_american_odds(prob) if games > 0 else "+10000",
|
| 159 |
+
"Odds w/ Vig": to_american_odds(apply_vig(prob, 4.92)),
|
| 160 |
"Mean": round(mean, 2) if not np.isnan(mean) else np.nan,
|
| 161 |
})
|
| 162 |
return rows
|
|
|
|
| 164 |
def compute_team_table(team: str, stat_key: str, timeframe_key: str, vig_pct: float, player_subset: list[str] | None):
|
| 165 |
if df is None:
|
| 166 |
return pd.DataFrame(), "Upload a CSV first."
|
|
|
|
| 167 |
if not team:
|
| 168 |
return pd.DataFrame(), "Pick a team."
|
|
|
|
| 169 |
if stat_key not in NFL_MILESTONES:
|
| 170 |
return pd.DataFrame(), f"Choose a stat. Unknown: {stat_key}"
|
| 171 |
|
| 172 |
+
stat_col = csv_col_for(stat_key, timeframe_key)
|
| 173 |
milestones = NFL_MILESTONES[stat_key]
|
| 174 |
|
| 175 |
+
team_df = df[df.get("player_team") == team].copy()
|
| 176 |
if player_subset:
|
| 177 |
+
team_df = team_df[team_df.get("player").isin(player_subset)]
|
| 178 |
|
| 179 |
if team_df.empty:
|
| 180 |
return pd.DataFrame(), f"No rows for team {team} with current filters."
|
| 181 |
|
| 182 |
rows = []
|
| 183 |
+
for player, pdata in team_df.groupby("player", dropna=True):
|
| 184 |
rows.extend(player_milestone_rows(pdata, player, stat_col, milestones))
|
| 185 |
|
| 186 |
out = pd.DataFrame(rows)
|
| 187 |
+
# apply UI vig
|
|
|
|
| 188 |
out["Odds w/ Vig"] = out["Prob"].apply(lambda p: to_american_odds(apply_vig(float(p), float(vig_pct))))
|
| 189 |
|
| 190 |
+
# sort: higher prob, then player, then numeric milestone
|
| 191 |
def milestone_int(s):
|
| 192 |
try:
|
| 193 |
return int(str(s).replace("+", ""))
|
| 194 |
except:
|
| 195 |
return 0
|
|
|
|
| 196 |
out["_m"] = out["Milestone"].apply(milestone_int)
|
| 197 |
out = out.sort_values(by=["Prob", "Player", "_m"], ascending=[False, True, True]).drop(columns=["_m"])
|
| 198 |
return out, f"Showing {team} β’ {STAT_LABELS.get(stat_key, stat_key)} β’ {timeframe_key}"
|
|
|
|
| 205 |
if stat_key not in NFL_MILESTONES:
|
| 206 |
return pd.DataFrame(), f"Choose a stat. Unknown: {stat_key}"
|
| 207 |
|
| 208 |
+
stat_col = csv_col_for(stat_key, timeframe_key)
|
| 209 |
milestones = NFL_MILESTONES[stat_key]
|
| 210 |
|
| 211 |
+
work = df[df.get("player").isin(players)].copy()
|
|
|
|
| 212 |
if work.empty:
|
| 213 |
return pd.DataFrame(), "No rows found for those players."
|
| 214 |
|
| 215 |
+
rows = []
|
| 216 |
+
for player, pdata in work.groupby("player", dropna=True):
|
| 217 |
rows.extend(player_milestone_rows(pdata, player, stat_col, milestones))
|
| 218 |
|
| 219 |
out = pd.DataFrame(rows)
|
| 220 |
out["Odds w/ Vig"] = out["Prob"].apply(lambda p: to_american_odds(apply_vig(float(p), float(vig_pct))))
|
| 221 |
|
| 222 |
+
# If player_team exists, add a Team column (safe)
|
| 223 |
if "player_team" in work.columns:
|
| 224 |
+
team_map = (
|
| 225 |
+
work[["player", "player_team"]]
|
| 226 |
+
.dropna()
|
| 227 |
+
.drop_duplicates()
|
| 228 |
+
.groupby("player")["player_team"]
|
| 229 |
+
.agg(lambda x: ", ".join(sorted(set(map(str, x)))))
|
| 230 |
+
)
|
| 231 |
out = out.merge(team_map.rename("Team"), left_on="Player", right_index=True, how="left")
|
| 232 |
+
# Move Team next to Player if present
|
| 233 |
+
if "Team" in out.columns:
|
| 234 |
+
cols = out.columns.tolist()
|
| 235 |
+
cols.insert(1, cols.pop(cols.index("Team")))
|
| 236 |
+
out = out[cols]
|
| 237 |
|
| 238 |
+
# sort by Player then milestone number
|
| 239 |
def milestone_int(s):
|
| 240 |
try:
|
| 241 |
return int(str(s).replace("+", ""))
|
|
|
|
| 251 |
with gr.Blocks(title="π NFL Team & Player Milestones") as demo:
|
| 252 |
gr.Markdown("## π NFL Milestone Explorer")
|
| 253 |
gr.Markdown(
|
| 254 |
+
"Upload the `nfl_milestone_logs.csv` from your exporter. "
|
| 255 |
+
"Team tab uses the **player_team** column so it only shows players on that team."
|
| 256 |
)
|
| 257 |
|
| 258 |
file_in = gr.File(label="Upload nfl_milestone_logs.csv", file_types=[".csv"])
|
|
|
|
| 262 |
# -------- Team Tab --------
|
| 263 |
with gr.Tab("Team"):
|
| 264 |
with gr.Row():
|
| 265 |
+
team_dd = gr.Dropdown(label="Team (player_team)", choices=[])
|
| 266 |
+
stat_dd = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
timeframe_radio = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
| 268 |
vig_num = gr.Number(label="Vig %", value=4.92, precision=2)
|
| 269 |
|
|
|
|
| 270 |
team_players_ms = gr.Dropdown(choices=[], multiselect=True, label="Filter to Players (optional)")
|
| 271 |
|
| 272 |
team_btn = gr.Button("Compute Team Table")
|
| 273 |
team_out_title = gr.Markdown(visible=False)
|
| 274 |
+
team_table = gr.Dataframe(label="Team Milestones", interactive=False, visible=False, wrap=True)
|
| 275 |
+
|
| 276 |
+
# -------- Player Tab (UNCHANGED; multi-select) --------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
with gr.Tab("Player"):
|
| 278 |
with gr.Row():
|
| 279 |
players_ms = gr.Dropdown(choices=[], multiselect=True, label="Players")
|
| 280 |
+
p_stat_dd = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
p_timeframe_radio = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
| 282 |
p_vig_num = gr.Number(label="Vig %", value=4.92, precision=2)
|
| 283 |
|
| 284 |
player_btn = gr.Button("Compute Player Table")
|
| 285 |
player_out_title = gr.Markdown(visible=False)
|
| 286 |
+
player_table = gr.Dataframe(label="Player Milestones", interactive=False, visible=False, wrap=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# ------------- Wiring -------------
|
| 289 |
def on_file(file):
|
| 290 |
+
team_update, players_update, team_players_update, msg = load_csv(file)
|
| 291 |
+
return team_update, players_update, team_players_update, msg
|
|
|
|
| 292 |
|
| 293 |
file_in.change(
|
| 294 |
on_file,
|
|
|
|
| 296 |
outputs=[team_dd, players_ms, team_players_ms, status]
|
| 297 |
)
|
| 298 |
|
| 299 |
+
# Limit the team player filter to that team's roster
|
| 300 |
def on_team_change(team):
|
| 301 |
if df is None or not team:
|
| 302 |
return gr.update(choices=[], value=[])
|
| 303 |
+
team_players = (
|
| 304 |
+
df.loc[df.get("player_team") == team, "player"]
|
| 305 |
+
.dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist()
|
| 306 |
+
)
|
| 307 |
+
return gr.update(choices=sorted(team_players), value=[])
|
| 308 |
|
| 309 |
team_dd.change(on_team_change, inputs=[team_dd], outputs=[team_players_ms])
|
| 310 |
|