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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 + PREP DATA
|
| 10 |
+
# =======================
|
| 11 |
+
df = pd.read_csv("chess_analysis.csv")
|
| 12 |
+
|
| 13 |
+
df_long = pd.concat([
|
| 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"].unique().tolist())
|
| 31 |
+
|
| 32 |
+
# =======================
|
| 33 |
+
# FUNCTIONS
|
| 34 |
+
# =======================
|
| 35 |
+
|
| 36 |
+
def overview_plot():
|
| 37 |
+
fig = px.scatter(
|
| 38 |
+
df_long,
|
| 39 |
+
x="year",
|
| 40 |
+
y="acpl",
|
| 41 |
+
opacity=0.3,
|
| 42 |
+
title="๐ Performance Over Time"
|
| 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 |
+
title=f"{player} Performance",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return f"Avg ACPL: {avg:.2f} | Games: {games}", fig
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def engine_plot():
|
| 65 |
+
fig = px.scatter(
|
| 66 |
+
df_long,
|
| 67 |
+
x="year",
|
| 68 |
+
y="acpl",
|
| 69 |
+
color="engine",
|
| 70 |
+
opacity=0.3,
|
| 71 |
+
title="๐ค Engine Effect (Pre vs Post 1996)"
|
| 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=df_sorted["year"],
|
| 120 |
+
y=df_sorted["acpl"],
|
| 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 (Fitted)"
|
| 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 Prediction"
|
| 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 DESIGN
|
| 164 |
+
# =======================
|
| 165 |
+
|
| 166 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 167 |
+
|
| 168 |
+
# ===== HEADER =====
|
| 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.Markdown("## โ๏ธ Controls")
|
| 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 |
+
# -------- OVERVIEW --------
|
| 209 |
+
with gr.Tab("๐ Overview"):
|
| 210 |
+
gr.Markdown("### Overall Performance Trend")
|
| 211 |
+
gr.Plot(overview_plot)
|
| 212 |
+
|
| 213 |
+
# -------- PLAYER --------
|
| 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 |
+
# -------- ENGINE --------
|
| 225 |
+
with gr.Tab("๐ค Engine Impact"):
|
| 226 |
+
gr.Plot(engine_plot)
|
| 227 |
+
|
| 228 |
+
# -------- PREDICTION --------
|
| 229 |
+
with gr.Tab("๐ฎ Prediction"):
|
| 230 |
+
gr.Plot(prediction_plot)
|
| 231 |
+
|
| 232 |
+
# RUN
|
| 233 |
+
app.launch(share=True, debug=True)
|