Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import xgboost as xgb
|
| 5 |
-
from pybaseball import pitching_stats, cache
|
| 6 |
-
import matplotlib.pyplot as plt
|
| 7 |
-
|
| 8 |
-
cache.enable()
|
| 9 |
-
|
| 10 |
-
class DecadalEngine:
|
| 11 |
-
def __init__(self):
|
| 12 |
-
raw_data = pitching_stats(2015, 2025, ind=1)
|
| 13 |
-
if 'Ball%' not in raw_data.columns: raw_data['Ball%'] = 0.35
|
| 14 |
-
if 'SIERA' not in raw_data.columns: raw_data['SIERA'] = raw_data['FIP']
|
| 15 |
-
self.features = ['SwStr%', 'Ball%', 'SIERA', 'GB%', 'FIP', 'Age', 'IP']
|
| 16 |
-
df = raw_data[self.features + ['SO', 'Name', 'Season']].dropna()
|
| 17 |
-
dtrain = xgb.DMatrix(df[self.features], label=df['SO'])
|
| 18 |
-
self.model = xgb.train({'objective': 'reg:squarederror', 'max_depth': 5}, dtrain)
|
| 19 |
-
self.latest_stats = df[df['Season'] == 2025]
|
| 20 |
-
|
| 21 |
-
engine = DecadalEngine()
|
| 22 |
-
|
| 23 |
-
def run_visual_sim(p1, p2, conf_str):
|
| 24 |
-
z_map = {"90%": 1.645, "95%": 1.96, "99% (Ultra-Precision)": 2.576}
|
| 25 |
-
z_val = z_map[conf_str]
|
| 26 |
-
|
| 27 |
-
names = [n for n in [p1, p2] if n]
|
| 28 |
-
projections, lowers, uppers = [], [], []
|
| 29 |
-
|
| 30 |
-
for name in names:
|
| 31 |
-
player_data = engine.latest_stats[engine.latest_stats['Name'] == name]
|
| 32 |
-
input_row = player_data[engine.features].copy().iloc[0:1]
|
| 33 |
-
input_row['Age'] += 1
|
| 34 |
-
|
| 35 |
-
mu = float(engine.model.predict(xgb.DMatrix(input_row))[0])
|
| 36 |
-
std_error = np.sqrt(mu) * (0.6 if player_data['SIERA'].values[0] < 3.4 else 0.9)
|
| 37 |
-
|
| 38 |
-
projections.append(mu)
|
| 39 |
-
lowers.append(mu - (z_val * std_error))
|
| 40 |
-
uppers.append(mu + (z_val * std_error))
|
| 41 |
-
|
| 42 |
-
# --- Create Horizontal Bar Chart ---
|
| 43 |
-
fig, ax = plt.subplots(figsize=(10, 4), facecolor='#f8f9fa')
|
| 44 |
-
y_pos = np.arange(len(names))
|
| 45 |
-
# Error bar needs relative lengths: (mu - low) and (high - mu)
|
| 46 |
-
xerr = [[p - l for p, l in zip(projections, lowers)],
|
| 47 |
-
[u - p for u, p in zip(uppers, projections)]]
|
| 48 |
-
|
| 49 |
-
bars = ax.barh(y_pos, projections, xerr=xerr, capsize=10,
|
| 50 |
-
color=['#003566', '#d62828'], alpha=0.7, height=0.6,
|
| 51 |
-
error_kw={'ecolor': 'black', 'lw': 2, 'capthick': 2})
|
| 52 |
-
|
| 53 |
-
ax.set_yticks(y_pos)
|
| 54 |
-
ax.set_yticklabels(names, fontweight='bold', fontsize=12)
|
| 55 |
-
ax.set_xlabel("Projected 2026 Strikeouts", fontsize=10)
|
| 56 |
-
ax.set_title(f"2026 Strikeout Projections with {conf_str} Confidence Intervals", fontweight='bold')
|
| 57 |
-
|
| 58 |
-
# Label the bars with the exact projection value
|
| 59 |
-
for i, v in enumerate(projections):
|
| 60 |
-
ax.text(v + 3, i, f"{int(v)} K", va='center', fontweight='bold', color='black')
|
| 61 |
-
|
| 62 |
-
plt.tight_layout()
|
| 63 |
-
|
| 64 |
-
summary = "\n\n".join([f"**{names[i]}**: {int(projections[i])} Ks (Range: {int(lowers[i])}–{int(uppers[i])})" for i in range(len(names))])
|
| 65 |
-
return fig, summary
|
| 66 |
-
|
| 67 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 68 |
-
gr.Markdown("# 📊 MLB 2026 Visual Prediction Engine")
|
| 69 |
-
with gr.Row():
|
| 70 |
-
with gr.Column(scale=1):
|
| 71 |
-
p1 = gr.Dropdown(choices=sorted(engine.latest_stats['Name'].tolist()), label="Pitcher 1", value="Jesus Luzardo")
|
| 72 |
-
p2 = gr.Dropdown(choices=sorted(engine.latest_stats['Name'].tolist()), label="Pitcher 2", value="Andre Pallante")
|
| 73 |
-
conf = gr.Radio(["90%", "95%", "99% (Ultra-Precision)"], label="Confidence Level", value="95%")
|
| 74 |
-
btn = gr.Button("🚀 Generate Visual Report", variant="primary")
|
| 75 |
-
with gr.Column(scale=2):
|
| 76 |
-
plot, txt = gr.Plot(), gr.Markdown()
|
| 77 |
-
|
| 78 |
-
btn.click(run_visual_sim, inputs=[p1, p2, conf], outputs=[plot, txt])
|
| 79 |
-
|
| 80 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|