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
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""tabular_gradio.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1-_lDVeqDrMkiSuBA380Q7IC2WYa0NHcF
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install autogluon.tabular --quiet
|
| 11 |
+
|
| 12 |
+
import os # For filesystem operations
|
| 13 |
+
import shutil # For directory cleanup
|
| 14 |
+
import zipfile # For extracting model archives
|
| 15 |
+
import pathlib # For path manipulations
|
| 16 |
+
import pandas as pd # For tabular data handling
|
| 17 |
+
import gradio as gr # For interactive UI
|
| 18 |
+
import huggingface_hub # For downloading model assets
|
| 19 |
+
import autogluon.tabular # For loading and running AutoGluon predictors
|
| 20 |
+
|
| 21 |
+
# Settings
|
| 22 |
+
MODEL_REPO_ID = "mrob937/2024-24679-tabular-autogluon-predictor"
|
| 23 |
+
ZIP_FILENAME = "autogluon_predictor_dir.zip"
|
| 24 |
+
CACHE_DIR = pathlib.Path("hf_assets")
|
| 25 |
+
EXTRACT_DIR = CACHE_DIR / "predictor_native"
|
| 26 |
+
|
| 27 |
+
FEATURE_COLS = [
|
| 28 |
+
"Tgt",
|
| 29 |
+
"Rec",
|
| 30 |
+
"Yds",
|
| 31 |
+
"YBC/R",
|
| 32 |
+
"YAC/R",
|
| 33 |
+
"ADOT",
|
| 34 |
+
"Drop%",
|
| 35 |
+
"Rat"
|
| 36 |
+
]
|
| 37 |
+
TARGET_COL = "Elite"
|
| 38 |
+
|
| 39 |
+
def _prepare_predictor_dir() -> str:
|
| 40 |
+
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
| 41 |
+
local_zip = huggingface_hub.hf_hub_download(
|
| 42 |
+
repo_id=MODEL_REPO_ID,
|
| 43 |
+
filename=ZIP_FILENAME,
|
| 44 |
+
repo_type="model",
|
| 45 |
+
local_dir=str(CACHE_DIR),
|
| 46 |
+
local_dir_use_symlinks=False,
|
| 47 |
+
)
|
| 48 |
+
if EXTRACT_DIR.exists():
|
| 49 |
+
shutil.rmtree(EXTRACT_DIR)
|
| 50 |
+
EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
|
| 51 |
+
with zipfile.ZipFile(local_zip, "r") as zf:
|
| 52 |
+
zf.extractall(str(EXTRACT_DIR))
|
| 53 |
+
contents = list(EXTRACT_DIR.iterdir())
|
| 54 |
+
predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
|
| 55 |
+
return str(predictor_root)
|
| 56 |
+
|
| 57 |
+
PREDICTOR_DIR = _prepare_predictor_dir()
|
| 58 |
+
PREDICTOR = autogluon.tabular.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False)
|
| 59 |
+
|
| 60 |
+
OUTCOME_MAP = {
|
| 61 |
+
0: "Not Elite",
|
| 62 |
+
1: "Elite",
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def _human_label(c):
|
| 66 |
+
try:
|
| 67 |
+
ci = int(c)
|
| 68 |
+
if ci in OUTCOME_MAP:
|
| 69 |
+
return OUTCOME_MAP[ci]
|
| 70 |
+
except Exception:
|
| 71 |
+
pass
|
| 72 |
+
return str(c)
|
| 73 |
+
|
| 74 |
+
def do_predict(player, tgt, rec, yds, ybc_r, yac_r, adot, drop_pct, rat):
|
| 75 |
+
row = {
|
| 76 |
+
"Player": str(player),
|
| 77 |
+
FEATURE_COLS[0]: float(tgt),
|
| 78 |
+
FEATURE_COLS[1]: float(rec),
|
| 79 |
+
FEATURE_COLS[2]: float(yds),
|
| 80 |
+
FEATURE_COLS[3]: float(ybc_r),
|
| 81 |
+
FEATURE_COLS[4]: float(yac_r),
|
| 82 |
+
FEATURE_COLS[5]: float(adot),
|
| 83 |
+
FEATURE_COLS[6]: float(drop_pct),
|
| 84 |
+
FEATURE_COLS[7]: float(rat),
|
| 85 |
+
}
|
| 86 |
+
X = pd.DataFrame([row], columns=["Player"] + FEATURE_COLS)
|
| 87 |
+
|
| 88 |
+
pred_series = PREDICTOR.predict(X)
|
| 89 |
+
raw_pred = pred_series.iloc[0]
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
proba = PREDICTOR.predict_proba(X)
|
| 93 |
+
if isinstance(proba, pd.Series):
|
| 94 |
+
proba = proba.to_frame().T
|
| 95 |
+
except Exception:
|
| 96 |
+
proba = None
|
| 97 |
+
|
| 98 |
+
pred_label = _human_label(raw_pred)
|
| 99 |
+
|
| 100 |
+
proba_dict = None
|
| 101 |
+
if proba is not None:
|
| 102 |
+
row0 = proba.iloc[0]
|
| 103 |
+
tmp = {}
|
| 104 |
+
for cls, val in row0.items():
|
| 105 |
+
key = _human_label(cls)
|
| 106 |
+
tmp[key] = float(val) + float(tmp.get(key, 0.0))
|
| 107 |
+
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 108 |
+
|
| 109 |
+
# integrate player name into output dict
|
| 110 |
+
if proba_dict:
|
| 111 |
+
return {f"{player} → {k}": v for k, v in proba_dict.items()}
|
| 112 |
+
else:
|
| 113 |
+
return {f"{player} → {pred_label}": 1.0}
|
| 114 |
+
|
| 115 |
+
# -------- Example rows (with player names) --------
|
| 116 |
+
EXAMPLES = [
|
| 117 |
+
["Justin Jefferson", 8.0, 5.0, 65.0, 6.0, 4.0, 8.5, 1.5, 7.2],
|
| 118 |
+
["Cooper Kupp", 15.0, 10.0, 140.0, 8.0, 6.0, 9.0, 0.5, 8.5],
|
| 119 |
+
["Rookie WR", 3.0, 2.0, 12.0, 4.0, 3.0, 5.0, 4.0, 5.5],
|
| 120 |
+
["Tyreek Hill", 20.0, 14.0, 220.0, 10.0, 8.0, 12.0, 0.2, 9.0],
|
| 121 |
+
["Bench Player", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 3.0],
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
# -------- Gradio UI --------
|
| 125 |
+
with gr.Blocks() as demo:
|
| 126 |
+
gr.Markdown("# Football: Will this player be Elite?")
|
| 127 |
+
gr.Markdown("Enter the player's name and stats below. The model will predict whether they're Elite and show probabilities.")
|
| 128 |
+
|
| 129 |
+
player_name = gr.Textbox(value="Example Player", label="Player Name")
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
tgt = gr.Slider(0, 30, value=8.0, step=0.5, label=FEATURE_COLS[0])
|
| 133 |
+
rec = gr.Slider(0, 20, value=5.0, step=0.5, label=FEATURE_COLS[1])
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
yds = gr.Number(value=60.0, precision=1, label=FEATURE_COLS[2])
|
| 137 |
+
ybc_r = gr.Number(value=6.0, precision=2, label=FEATURE_COLS[3])
|
| 138 |
+
|
| 139 |
+
with gr.Row():
|
| 140 |
+
yac_r = gr.Number(value=4.0, precision=2, label=FEATURE_COLS[4])
|
| 141 |
+
adot = gr.Number(value=8.0, precision=2, label=FEATURE_COLS[5])
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
drop_pct = gr.Slider(0.0, 100.0, value=1.5, step=0.1, label=FEATURE_COLS[6])
|
| 145 |
+
rat = gr.Slider(0.0, 10.0, value=7.2, step=0.1, label=FEATURE_COLS[7])
|
| 146 |
+
|
| 147 |
+
proba_pretty = gr.Label(num_top_classes=5, label="Class probabilities")
|
| 148 |
+
|
| 149 |
+
inputs = [player_name, tgt, rec, yds, ybc_r, yac_r, adot, drop_pct, rat]
|
| 150 |
+
for comp in inputs:
|
| 151 |
+
comp.change(fn=do_predict, inputs=inputs, outputs=[proba_pretty])
|
| 152 |
+
|
| 153 |
+
gr.Examples(
|
| 154 |
+
examples=EXAMPLES,
|
| 155 |
+
inputs=inputs,
|
| 156 |
+
label="Representative examples",
|
| 157 |
+
examples_per_page=5,
|
| 158 |
+
cache_examples=False,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
demo.launch()
|