Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
import pickle
|
| 5 |
import plotly.express as px
|
| 6 |
import plotly.graph_objects as go
|
|
|
|
| 7 |
|
| 8 |
# =======================
|
| 9 |
# LOAD DATA
|
|
@@ -14,146 +14,218 @@ df_long = pd.concat([
|
|
| 14 |
pd.DataFrame({
|
| 15 |
"year": df["Year"],
|
| 16 |
"acpl": df["White.ACPL"],
|
| 17 |
-
"player": df["White.Player"]
|
|
|
|
| 18 |
}),
|
| 19 |
pd.DataFrame({
|
| 20 |
"year": df["Year"],
|
| 21 |
"acpl": df["Black.ACPL"],
|
| 22 |
-
"player": df["Black.Player"]
|
|
|
|
| 23 |
})
|
| 24 |
]).dropna()
|
| 25 |
|
| 26 |
df_long["engine"] = np.where(df_long["year"] >= 1996, "Post-1996", "Pre-1996")
|
| 27 |
|
| 28 |
-
players = sorted(df_long["player"].unique().tolist())
|
| 29 |
|
| 30 |
# =======================
|
| 31 |
-
#
|
| 32 |
# =======================
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
# =======================
|
| 37 |
-
# PRECOMPUTE
|
| 38 |
# =======================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# ๐น Overview plot (ONCE)
|
| 44 |
-
overview_fig = px.scatter(
|
| 45 |
-
df_sample, x="year", y="acpl",
|
| 46 |
-
opacity=0.3, render_mode="webgl"
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
# ๐น Engine plot (ONCE)
|
| 50 |
-
engine_fig = px.scatter(
|
| 51 |
-
df_sample, x="year", y="acpl",
|
| 52 |
-
color="engine", opacity=0.3,
|
| 53 |
-
render_mode="webgl"
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# ๐น Prediction plot (ONCE)
|
| 57 |
-
df_sorted = df_long.sort_values("year")
|
| 58 |
-
df_sorted["pred"] = model.predict(
|
| 59 |
-
pd.DataFrame({"const": 1, "year": df_sorted["year"]})
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
future = pd.DataFrame({
|
| 63 |
-
"year": np.arange(df_long["year"].max(), df_long["year"].max()+10)
|
| 64 |
-
})
|
| 65 |
-
future["pred"] = model.predict(
|
| 66 |
-
pd.DataFrame({"const": 1, "year": future["year"]})
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
prediction_fig = go.Figure()
|
| 70 |
-
prediction_fig.add_trace(go.Scatter(
|
| 71 |
-
x=df_sample["year"], y=df_sample["acpl"],
|
| 72 |
-
mode="markers", opacity=0.2
|
| 73 |
-
))
|
| 74 |
-
prediction_fig.add_trace(go.Scatter(
|
| 75 |
-
x=df_sorted["year"], y=df_sorted["pred"],
|
| 76 |
-
mode="lines"
|
| 77 |
-
))
|
| 78 |
-
prediction_fig.add_trace(go.Scatter(
|
| 79 |
-
x=future["year"], y=future["pred"],
|
| 80 |
-
mode="lines", line=dict(dash="dash")
|
| 81 |
-
))
|
| 82 |
-
|
| 83 |
-
# ๐น PLAYER PLOTS (PRECOMPUTE ALL)
|
| 84 |
-
player_cache = {}
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
data = data.sample(min(len(data), 150))
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# =======================
|
| 99 |
-
# FUNCTIONS (FAST
|
| 100 |
# =======================
|
| 101 |
|
| 102 |
-
def
|
| 103 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
def
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
return engine_fig
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
def stats():
|
| 115 |
-
avg = df_long["acpl"].mean()
|
| 116 |
-
best = df_long.groupby("player")["acpl"].mean().idxmin()
|
| 117 |
-
return f"{avg:.2f}", best
|
| 118 |
|
| 119 |
# =======================
|
| 120 |
# UI
|
| 121 |
# =======================
|
| 122 |
|
| 123 |
-
with gr.Blocks() as app:
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
|
|
|
| 127 |
with gr.Row():
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
app.load(
|
| 132 |
|
|
|
|
| 133 |
with gr.Row():
|
| 134 |
|
|
|
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
with gr.Column(scale=3):
|
| 139 |
|
| 140 |
with gr.Tabs():
|
| 141 |
|
| 142 |
-
with gr.Tab("Overview"):
|
| 143 |
-
gr.Plot(
|
| 144 |
|
| 145 |
-
with gr.Tab("Player"):
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
with gr.Tab("Engine"):
|
| 152 |
-
gr.Plot(
|
| 153 |
|
| 154 |
-
with gr.Tab("Prediction"):
|
| 155 |
-
gr.Plot(
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
app.queue()
|
| 159 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
+
import statsmodels.api as sm
|
| 7 |
|
| 8 |
# =======================
|
| 9 |
# LOAD DATA
|
|
|
|
| 14 |
pd.DataFrame({
|
| 15 |
"year": df["Year"],
|
| 16 |
"acpl": df["White.ACPL"],
|
| 17 |
+
"player": df["White.Player"],
|
| 18 |
+
"color": "White"
|
| 19 |
}),
|
| 20 |
pd.DataFrame({
|
| 21 |
"year": df["Year"],
|
| 22 |
"acpl": df["Black.ACPL"],
|
| 23 |
+
"player": df["Black.Player"],
|
| 24 |
+
"color": "Black"
|
| 25 |
})
|
| 26 |
]).dropna()
|
| 27 |
|
| 28 |
df_long["engine"] = np.where(df_long["year"] >= 1996, "Post-1996", "Pre-1996")
|
| 29 |
|
| 30 |
+
players = sorted(df_long["player"].dropna().unique().tolist())
|
| 31 |
|
| 32 |
# =======================
|
| 33 |
+
# GLOBAL CACHE
|
| 34 |
# =======================
|
| 35 |
+
PLOTS = {}
|
| 36 |
+
player_cache = {}
|
| 37 |
|
| 38 |
# =======================
|
| 39 |
+
# PRECOMPUTE PLOTS
|
| 40 |
# =======================
|
| 41 |
+
def build_all_plots():
|
| 42 |
+
global PLOTS
|
| 43 |
+
|
| 44 |
+
# ---- OVERVIEW ----
|
| 45 |
+
fig_overview = px.scatter(
|
| 46 |
+
df_long,
|
| 47 |
+
x="year",
|
| 48 |
+
y="acpl",
|
| 49 |
+
opacity=0.3,
|
| 50 |
+
title="๐ Performance Over Time"
|
| 51 |
+
)
|
| 52 |
+
fig_overview.update_layout(template="plotly_white")
|
| 53 |
+
|
| 54 |
+
# ---- ENGINE ----
|
| 55 |
+
fig_engine = px.scatter(
|
| 56 |
+
df_long,
|
| 57 |
+
x="year",
|
| 58 |
+
y="acpl",
|
| 59 |
+
color="engine",
|
| 60 |
+
opacity=0.3,
|
| 61 |
+
title="๐ค Engine Effect (Pre vs Post 1996)"
|
| 62 |
+
)
|
| 63 |
+
fig_engine.update_layout(template="plotly_white")
|
| 64 |
|
| 65 |
+
# ---- PREDICTION ----
|
| 66 |
+
X = sm.add_constant(df_long["year"])
|
| 67 |
+
model = sm.OLS(df_long["acpl"], X).fit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
df_sorted = df_long.sort_values("year")
|
| 70 |
+
df_sorted["pred"] = model.predict(sm.add_constant(df_sorted["year"]))
|
|
|
|
| 71 |
|
| 72 |
+
future_years = pd.DataFrame({
|
| 73 |
+
"year": np.arange(df_long["year"].max(), df_long["year"].max() + 10)
|
| 74 |
+
})
|
| 75 |
+
future_years["pred"] = model.predict(sm.add_constant(future_years["year"]))
|
| 76 |
+
|
| 77 |
+
fig_pred = go.Figure()
|
| 78 |
+
|
| 79 |
+
# Actual data
|
| 80 |
+
fig_pred.add_trace(go.Scatter(
|
| 81 |
+
x=df_sorted["year"],
|
| 82 |
+
y=df_sorted["acpl"],
|
| 83 |
+
mode="markers",
|
| 84 |
+
opacity=0.2,
|
| 85 |
+
name="Observed"
|
| 86 |
+
))
|
| 87 |
+
|
| 88 |
+
# Trend line
|
| 89 |
+
fig_pred.add_trace(go.Scatter(
|
| 90 |
+
x=df_sorted["year"],
|
| 91 |
+
y=df_sorted["pred"],
|
| 92 |
+
mode="lines",
|
| 93 |
+
name="Trend"
|
| 94 |
+
))
|
| 95 |
+
|
| 96 |
+
# Future prediction
|
| 97 |
+
fig_pred.add_trace(go.Scatter(
|
| 98 |
+
x=future_years["year"],
|
| 99 |
+
y=future_years["pred"],
|
| 100 |
+
mode="lines",
|
| 101 |
+
line=dict(dash="dash"),
|
| 102 |
+
name="Future"
|
| 103 |
+
))
|
| 104 |
+
|
| 105 |
+
fig_pred.update_layout(
|
| 106 |
+
title="๐ฎ Future Performance Trend",
|
| 107 |
+
xaxis_title="Year",
|
| 108 |
+
yaxis_title="ACPL",
|
| 109 |
+
template="plotly_white"
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Save in cache
|
| 113 |
+
PLOTS["overview"] = fig_overview
|
| 114 |
+
PLOTS["engine"] = fig_engine
|
| 115 |
+
PLOTS["prediction"] = fig_pred
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# Run once at startup
|
| 119 |
+
build_all_plots()
|
| 120 |
|
| 121 |
# =======================
|
| 122 |
+
# FUNCTIONS (FAST)
|
| 123 |
# =======================
|
| 124 |
|
| 125 |
+
def overview_plot():
|
| 126 |
+
return PLOTS["overview"]
|
| 127 |
+
|
| 128 |
+
def engine_plot():
|
| 129 |
+
return PLOTS["engine"]
|
| 130 |
+
|
| 131 |
+
def prediction_plot():
|
| 132 |
+
return PLOTS["prediction"]
|
| 133 |
|
| 134 |
+
def player_analysis(player):
|
| 135 |
+
if player in player_cache:
|
| 136 |
+
return player_cache[player]
|
| 137 |
|
| 138 |
+
data = df_long[df_long["player"] == player]
|
|
|
|
| 139 |
|
| 140 |
+
avg = data["acpl"].mean()
|
| 141 |
+
games = len(data)
|
| 142 |
+
|
| 143 |
+
fig = px.scatter(
|
| 144 |
+
data,
|
| 145 |
+
x="year",
|
| 146 |
+
y="acpl",
|
| 147 |
+
title=f"{player} Performance",
|
| 148 |
+
)
|
| 149 |
+
fig.update_layout(template="plotly_white")
|
| 150 |
+
|
| 151 |
+
result = (f"Avg ACPL: {avg:.2f} | Games: {games}", fig)
|
| 152 |
+
player_cache[player] = result
|
| 153 |
+
|
| 154 |
+
return result
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def summary_stats():
|
| 158 |
+
avg_acpl = df_long["acpl"].mean()
|
| 159 |
+
player_stats = df_long.groupby("player")["acpl"].mean()
|
| 160 |
+
best_player = player_stats.idxmin()
|
| 161 |
+
|
| 162 |
+
return f"๐ Avg ACPL: {avg_acpl:.2f}", f"๐ Best Player: {best_player}"
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# =======================
|
| 166 |
# UI
|
| 167 |
# =======================
|
| 168 |
|
| 169 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 170 |
|
| 171 |
+
# Header
|
| 172 |
+
gr.Markdown(
|
| 173 |
+
"""
|
| 174 |
+
# โ๏ธ Chess Performance Dashboard
|
| 175 |
+
Explore how player performance has improved over time.
|
| 176 |
+
"""
|
| 177 |
+
)
|
| 178 |
|
| 179 |
+
# KPI row
|
| 180 |
with gr.Row():
|
| 181 |
+
kpi1 = gr.Textbox(label="Average Performance", interactive=False)
|
| 182 |
+
kpi2 = gr.Textbox(label="Top Player", interactive=False)
|
| 183 |
|
| 184 |
+
app.load(summary_stats, outputs=[kpi1, kpi2])
|
| 185 |
|
| 186 |
+
# Layout
|
| 187 |
with gr.Row():
|
| 188 |
|
| 189 |
+
# Sidebar
|
| 190 |
with gr.Column(scale=1):
|
| 191 |
+
gr.Markdown("## โ๏ธ Controls")
|
| 192 |
+
|
| 193 |
+
player_dropdown = gr.Dropdown(
|
| 194 |
+
players,
|
| 195 |
+
label="Select Player",
|
| 196 |
+
value=players[0]
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
gr.Markdown("### ๐ก Tips")
|
| 200 |
+
gr.Markdown("""
|
| 201 |
+
- Lower ACPL = better
|
| 202 |
+
- Compare players
|
| 203 |
+
- Observe trends
|
| 204 |
+
""")
|
| 205 |
+
|
| 206 |
+
# Main content
|
| 207 |
with gr.Column(scale=3):
|
| 208 |
|
| 209 |
with gr.Tabs():
|
| 210 |
|
| 211 |
+
with gr.Tab("๐ Overview"):
|
| 212 |
+
gr.Plot(overview_plot)
|
| 213 |
|
| 214 |
+
with gr.Tab("๐ Player Analysis"):
|
| 215 |
+
output_text = gr.Textbox()
|
| 216 |
+
player_plot = gr.Plot()
|
| 217 |
|
| 218 |
+
player_dropdown.change(
|
| 219 |
+
player_analysis,
|
| 220 |
+
inputs=player_dropdown,
|
| 221 |
+
outputs=[output_text, player_plot]
|
| 222 |
+
)
|
| 223 |
|
| 224 |
+
with gr.Tab("๐ค Engine Impact"):
|
| 225 |
+
gr.Plot(engine_plot)
|
| 226 |
|
| 227 |
+
with gr.Tab("๐ฎ Prediction"):
|
| 228 |
+
gr.Plot(prediction_plot)
|
| 229 |
|
| 230 |
+
# Run with queue (important for HF)
|
| 231 |
+
app.queue().launch()
|
|
|