Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
-
import statsmodels.api as sm
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
import plotly.express as px
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# =======================
|
| 9 |
-
# LOAD
|
| 10 |
# =======================
|
| 11 |
df = pd.read_csv("chess_analysis.csv")
|
| 12 |
|
|
@@ -30,188 +31,151 @@ df_long["engine"] = np.where(df_long["year"] >= 1996, "Post-1996", "Pre-1996")
|
|
| 30 |
players = sorted(df_long["player"].unique().tolist())
|
| 31 |
|
| 32 |
# =======================
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# =======================
|
| 35 |
|
| 36 |
def overview_plot():
|
| 37 |
fig = px.scatter(
|
| 38 |
-
|
| 39 |
x="year",
|
| 40 |
y="acpl",
|
| 41 |
opacity=0.3,
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
fig.update_layout(template="plotly_white")
|
| 45 |
return fig
|
| 46 |
|
| 47 |
|
|
|
|
| 48 |
def player_analysis(player):
|
| 49 |
data = df_long[df_long["player"] == player]
|
| 50 |
|
| 51 |
avg = data["acpl"].mean()
|
| 52 |
games = len(data)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
fig = px.scatter(
|
| 55 |
data,
|
| 56 |
x="year",
|
| 57 |
y="acpl",
|
| 58 |
-
|
| 59 |
)
|
| 60 |
|
| 61 |
return f"Avg ACPL: {avg:.2f} | Games: {games}", fig
|
| 62 |
|
| 63 |
|
| 64 |
def engine_plot():
|
|
|
|
|
|
|
| 65 |
fig = px.scatter(
|
| 66 |
-
|
| 67 |
x="year",
|
| 68 |
y="acpl",
|
| 69 |
color="engine",
|
| 70 |
opacity=0.3,
|
| 71 |
-
|
| 72 |
)
|
| 73 |
fig.update_layout(template="plotly_white")
|
| 74 |
return fig
|
| 75 |
|
| 76 |
|
| 77 |
def prediction_plot():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
with open("acpl_trend_model.pkl", "rb") as f:
|
| 83 |
-
model = pickle.load(f)
|
| 84 |
-
|
| 85 |
-
df_sorted = df_long.sort_values("year")
|
| 86 |
-
|
| 87 |
-
# Predictions on existing data
|
| 88 |
-
df_sorted["pred"] = model.predict(sm.add_constant(df_sorted["year"]))
|
| 89 |
-
|
| 90 |
-
# Future years
|
| 91 |
-
future_years = pd.DataFrame({
|
| 92 |
-
"year": np.arange(df_long["year"].max(), df_long["year"].max() + 10)
|
| 93 |
-
})
|
| 94 |
-
|
| 95 |
-
future_years["pred"] = model.predict(sm.add_constant(future_years["year"]))
|
| 96 |
-
# ---- Fit model ----
|
| 97 |
-
X = sm.add_constant(df_long["year"])
|
| 98 |
-
model = sm.OLS(df_long["acpl"], X).fit()
|
| 99 |
-
|
| 100 |
-
# ---- Sort data ----
|
| 101 |
-
df_sorted = df_long.sort_values("year")
|
| 102 |
-
|
| 103 |
-
# ---- Fitted values ----
|
| 104 |
-
df_sorted["pred"] = model.predict(sm.add_constant(df_sorted["year"]))
|
| 105 |
-
|
| 106 |
-
# ---- Future years ----
|
| 107 |
-
future_years = pd.DataFrame({
|
| 108 |
-
"year": np.arange(df_long["year"].max(), df_long["year"].max() + 10)
|
| 109 |
-
})
|
| 110 |
-
|
| 111 |
-
future_years["pred"] = model.predict(sm.add_constant(future_years["year"]))
|
| 112 |
-
import plotly.graph_objects as go
|
| 113 |
-
|
| 114 |
-
# ---- Plot ----
|
| 115 |
fig = go.Figure()
|
| 116 |
|
| 117 |
-
# Scatter (actual data)
|
| 118 |
fig.add_trace(go.Scatter(
|
| 119 |
-
x=
|
| 120 |
-
y=
|
| 121 |
mode="markers",
|
| 122 |
opacity=0.2,
|
| 123 |
name="Observed"
|
| 124 |
))
|
| 125 |
|
| 126 |
-
# Fitted trend line
|
| 127 |
fig.add_trace(go.Scatter(
|
| 128 |
x=df_sorted["year"],
|
| 129 |
y=df_sorted["pred"],
|
| 130 |
mode="lines",
|
| 131 |
-
name="Trend
|
| 132 |
))
|
| 133 |
|
| 134 |
-
# Future prediction line
|
| 135 |
fig.add_trace(go.Scatter(
|
| 136 |
x=future_years["year"],
|
| 137 |
y=future_years["pred"],
|
| 138 |
mode="lines",
|
| 139 |
line=dict(dash="dash"),
|
| 140 |
-
name="Future
|
| 141 |
))
|
| 142 |
|
| 143 |
-
fig.update_layout(
|
| 144 |
-
title="๐ฎ Future Performance Trend",
|
| 145 |
-
xaxis_title="Year",
|
| 146 |
-
yaxis_title="ACPL",
|
| 147 |
-
template="plotly_white"
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
return fig
|
| 151 |
|
| 152 |
|
| 153 |
def summary_stats():
|
| 154 |
avg_acpl = df_long["acpl"].mean()
|
| 155 |
-
|
| 156 |
-
player_stats = df_long.groupby("player")["acpl"].mean()
|
| 157 |
-
best_player = player_stats.idxmin()
|
| 158 |
|
| 159 |
return f"๐ Avg ACPL: {avg_acpl:.2f}", f"๐ Best Player: {best_player}"
|
| 160 |
|
| 161 |
|
| 162 |
# =======================
|
| 163 |
-
# UI
|
| 164 |
# =======================
|
| 165 |
|
| 166 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
gr.Markdown(
|
| 170 |
-
"""
|
| 171 |
-
# โ๏ธ Chess Performance Dashboard
|
| 172 |
-
Explore how player performance has evolved over time using ACPL.
|
| 173 |
-
"""
|
| 174 |
-
)
|
| 175 |
|
| 176 |
-
# ===== KPI ROW =====
|
| 177 |
with gr.Row():
|
| 178 |
kpi1 = gr.Textbox(label="Average Performance", interactive=False)
|
| 179 |
kpi2 = gr.Textbox(label="Top Player", interactive=False)
|
| 180 |
|
| 181 |
app.load(summary_stats, outputs=[kpi1, kpi2])
|
| 182 |
|
| 183 |
-
# ===== MAIN LAYOUT =====
|
| 184 |
with gr.Row():
|
| 185 |
|
| 186 |
-
# ===== SIDEBAR =====
|
| 187 |
with gr.Column(scale=1):
|
| 188 |
-
gr.
|
| 189 |
-
|
| 190 |
-
player_dropdown = gr.Dropdown(
|
| 191 |
-
players,
|
| 192 |
-
label="Select Player",
|
| 193 |
-
value=players[0]
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
gr.Markdown("### ๐ก Tips")
|
| 197 |
-
gr.Markdown("""
|
| 198 |
-
- Lower ACPL = better performance
|
| 199 |
-
- Compare players across years
|
| 200 |
-
- Observe trends after 1996
|
| 201 |
-
""")
|
| 202 |
-
|
| 203 |
-
# ===== MAIN CONTENT =====
|
| 204 |
with gr.Column(scale=3):
|
| 205 |
|
| 206 |
with gr.Tabs():
|
| 207 |
|
| 208 |
-
|
| 209 |
-
with gr.Tab("๐ Overview"):
|
| 210 |
-
gr.Markdown("### Overall Performance Trend")
|
| 211 |
gr.Plot(overview_plot)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
with gr.Tab("๐ Player Analysis"):
|
| 215 |
output_text = gr.Textbox()
|
| 216 |
player_plot = gr.Plot()
|
| 217 |
|
|
@@ -221,12 +185,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 221 |
outputs=[output_text, player_plot]
|
| 222 |
)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
with gr.Tab("๐ค Engine Impact"):
|
| 226 |
gr.Plot(engine_plot)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
with gr.Tab("๐ฎ Prediction"):
|
| 230 |
gr.Plot(prediction_plot)
|
| 231 |
|
| 232 |
# RUN
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
from functools import lru_cache
|
| 8 |
|
| 9 |
# =======================
|
| 10 |
+
# LOAD DATA (ONCE)
|
| 11 |
# =======================
|
| 12 |
df = pd.read_csv("chess_analysis.csv")
|
| 13 |
|
|
|
|
| 31 |
players = sorted(df_long["player"].unique().tolist())
|
| 32 |
|
| 33 |
# =======================
|
| 34 |
+
# LOAD MODEL ONCE
|
| 35 |
+
# =======================
|
| 36 |
+
with open("acpl_trend_model.pkl", "rb") as f:
|
| 37 |
+
model = pickle.load(f)
|
| 38 |
+
|
| 39 |
+
# =======================
|
| 40 |
+
# PRECOMPUTE (VERY IMPORTANT)
|
| 41 |
+
# =======================
|
| 42 |
+
df_sorted = df_long.sort_values("year")
|
| 43 |
+
|
| 44 |
+
df_sorted["pred"] = model.predict(
|
| 45 |
+
pd.DataFrame({"const": 1, "year": df_sorted["year"]})
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
future_years = pd.DataFrame({
|
| 49 |
+
"year": np.arange(df_long["year"].max(), df_long["year"].max() + 10)
|
| 50 |
+
})
|
| 51 |
+
future_years["pred"] = model.predict(
|
| 52 |
+
pd.DataFrame({"const": 1, "year": future_years["year"]})
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Downsample for fast rendering
|
| 56 |
+
df_sample = df_long.sample(min(len(df_long), 1000))
|
| 57 |
+
|
| 58 |
+
# =======================
|
| 59 |
+
# FUNCTIONS (OPTIMIZED)
|
| 60 |
# =======================
|
| 61 |
|
| 62 |
def overview_plot():
|
| 63 |
fig = px.scatter(
|
| 64 |
+
df_sample,
|
| 65 |
x="year",
|
| 66 |
y="acpl",
|
| 67 |
opacity=0.3,
|
| 68 |
+
render_mode="webgl" # FAST
|
| 69 |
+
)
|
| 70 |
+
fig.add_scatter(
|
| 71 |
+
x=df_sorted["year"],
|
| 72 |
+
y=df_sorted["pred"],
|
| 73 |
+
mode="lines",
|
| 74 |
+
name="Trend"
|
| 75 |
)
|
| 76 |
fig.update_layout(template="plotly_white")
|
| 77 |
return fig
|
| 78 |
|
| 79 |
|
| 80 |
+
@lru_cache(maxsize=128)
|
| 81 |
def player_analysis(player):
|
| 82 |
data = df_long[df_long["player"] == player]
|
| 83 |
|
| 84 |
avg = data["acpl"].mean()
|
| 85 |
games = len(data)
|
| 86 |
|
| 87 |
+
# Downsample
|
| 88 |
+
data = data.sample(min(len(data), 200))
|
| 89 |
+
|
| 90 |
fig = px.scatter(
|
| 91 |
data,
|
| 92 |
x="year",
|
| 93 |
y="acpl",
|
| 94 |
+
render_mode="webgl"
|
| 95 |
)
|
| 96 |
|
| 97 |
return f"Avg ACPL: {avg:.2f} | Games: {games}", fig
|
| 98 |
|
| 99 |
|
| 100 |
def engine_plot():
|
| 101 |
+
data = df_sample
|
| 102 |
+
|
| 103 |
fig = px.scatter(
|
| 104 |
+
data,
|
| 105 |
x="year",
|
| 106 |
y="acpl",
|
| 107 |
color="engine",
|
| 108 |
opacity=0.3,
|
| 109 |
+
render_mode="webgl"
|
| 110 |
)
|
| 111 |
fig.update_layout(template="plotly_white")
|
| 112 |
return fig
|
| 113 |
|
| 114 |
|
| 115 |
def prediction_plot():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
fig = go.Figure()
|
| 117 |
|
|
|
|
| 118 |
fig.add_trace(go.Scatter(
|
| 119 |
+
x=df_sample["year"],
|
| 120 |
+
y=df_sample["acpl"],
|
| 121 |
mode="markers",
|
| 122 |
opacity=0.2,
|
| 123 |
name="Observed"
|
| 124 |
))
|
| 125 |
|
|
|
|
| 126 |
fig.add_trace(go.Scatter(
|
| 127 |
x=df_sorted["year"],
|
| 128 |
y=df_sorted["pred"],
|
| 129 |
mode="lines",
|
| 130 |
+
name="Trend"
|
| 131 |
))
|
| 132 |
|
|
|
|
| 133 |
fig.add_trace(go.Scatter(
|
| 134 |
x=future_years["year"],
|
| 135 |
y=future_years["pred"],
|
| 136 |
mode="lines",
|
| 137 |
line=dict(dash="dash"),
|
| 138 |
+
name="Future"
|
| 139 |
))
|
| 140 |
|
| 141 |
+
fig.update_layout(template="plotly_white")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return fig
|
| 143 |
|
| 144 |
|
| 145 |
def summary_stats():
|
| 146 |
avg_acpl = df_long["acpl"].mean()
|
| 147 |
+
best_player = df_long.groupby("player")["acpl"].mean().idxmin()
|
|
|
|
|
|
|
| 148 |
|
| 149 |
return f"๐ Avg ACPL: {avg_acpl:.2f}", f"๐ Best Player: {best_player}"
|
| 150 |
|
| 151 |
|
| 152 |
# =======================
|
| 153 |
+
# UI
|
| 154 |
# =======================
|
| 155 |
|
| 156 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 157 |
|
| 158 |
+
gr.Markdown("# โ๏ธ Chess Performance Dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
|
|
|
| 160 |
with gr.Row():
|
| 161 |
kpi1 = gr.Textbox(label="Average Performance", interactive=False)
|
| 162 |
kpi2 = gr.Textbox(label="Top Player", interactive=False)
|
| 163 |
|
| 164 |
app.load(summary_stats, outputs=[kpi1, kpi2])
|
| 165 |
|
|
|
|
| 166 |
with gr.Row():
|
| 167 |
|
|
|
|
| 168 |
with gr.Column(scale=1):
|
| 169 |
+
player_dropdown = gr.Dropdown(players, value=players[0])
|
| 170 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
with gr.Column(scale=3):
|
| 172 |
|
| 173 |
with gr.Tabs():
|
| 174 |
|
| 175 |
+
with gr.Tab("Overview"):
|
|
|
|
|
|
|
| 176 |
gr.Plot(overview_plot)
|
| 177 |
|
| 178 |
+
with gr.Tab("Player"):
|
|
|
|
| 179 |
output_text = gr.Textbox()
|
| 180 |
player_plot = gr.Plot()
|
| 181 |
|
|
|
|
| 185 |
outputs=[output_text, player_plot]
|
| 186 |
)
|
| 187 |
|
| 188 |
+
with gr.Tab("Engine"):
|
|
|
|
| 189 |
gr.Plot(engine_plot)
|
| 190 |
|
| 191 |
+
with gr.Tab("Prediction"):
|
|
|
|
| 192 |
gr.Plot(prediction_plot)
|
| 193 |
|
| 194 |
# RUN
|