Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
|
| 7 |
+
DEFAULT_CSV = os.environ.get("RESULTS_CSV_PATH", "results.csv")
|
| 8 |
+
|
| 9 |
+
EXPECTED_COLS = [
|
| 10 |
+
"timestamp_iso","run_id","model","prompt_id","category",
|
| 11 |
+
"quality_score","latency_s","energy_wh","tokens","notes"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def _load_df(file: gr.File | None):
|
| 15 |
+
path = DEFAULT_CSV
|
| 16 |
+
if file is not None:
|
| 17 |
+
path = file.name
|
| 18 |
+
if not os.path.exists(path):
|
| 19 |
+
return pd.DataFrame(columns=EXPECTED_COLS)
|
| 20 |
+
df = pd.read_csv(path)
|
| 21 |
+
# ensure expected cols exist
|
| 22 |
+
for c in EXPECTED_COLS:
|
| 23 |
+
if c not in df.columns:
|
| 24 |
+
df[c] = None
|
| 25 |
+
# numeric coercion
|
| 26 |
+
for c in ["quality_score","latency_s","energy_wh","tokens"]:
|
| 27 |
+
df[c] = pd.to_numeric(df[c], errors="coerce")
|
| 28 |
+
return df
|
| 29 |
+
|
| 30 |
+
def _summaries(df: pd.DataFrame):
|
| 31 |
+
if df.empty:
|
| 32 |
+
return df, pd.DataFrame(), pd.DataFrame(), None, None, None, None
|
| 33 |
+
|
| 34 |
+
def q_per_wh(row):
|
| 35 |
+
if pd.notna(row["mean_energy"]) and row["mean_energy"] > 0 and pd.notna(row["mean_quality"]):
|
| 36 |
+
return row["mean_quality"] / row["mean_energy"]
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
per_model = df.groupby("model", dropna=False).agg(
|
| 40 |
+
n_runs=("run_id","count"),
|
| 41 |
+
mean_quality=("quality_score","mean"),
|
| 42 |
+
median_latency=("latency_s","median"),
|
| 43 |
+
p95_latency=("latency_s", lambda x: x.dropna().quantile(0.95) if len(x.dropna()) else None),
|
| 44 |
+
mean_latency=("latency_s","mean"),
|
| 45 |
+
mean_energy=("energy_wh","mean"),
|
| 46 |
+
mean_tokens=("tokens","mean")
|
| 47 |
+
).reset_index()
|
| 48 |
+
per_model["quality_per_wh"] = per_model.apply(q_per_wh, axis=1)
|
| 49 |
+
|
| 50 |
+
per_model_cat = df.groupby(["model","category"], dropna=False).agg(
|
| 51 |
+
n_runs=("run_id","count"),
|
| 52 |
+
mean_quality=("quality_score","mean"),
|
| 53 |
+
mean_latency=("latency_s","mean"),
|
| 54 |
+
p95_latency=("latency_s", lambda x: x.dropna().quantile(0.95) if len(x.dropna()) else None),
|
| 55 |
+
mean_energy=("energy_wh","mean")
|
| 56 |
+
).reset_index()
|
| 57 |
+
|
| 58 |
+
c1 = px.bar(per_model.sort_values("mean_quality", ascending=False),
|
| 59 |
+
x="model", y="mean_quality", title="Mean Quality by Model")
|
| 60 |
+
c2 = px.bar(per_model.sort_values("mean_latency"),
|
| 61 |
+
x="model", y="mean_latency", title="Mean Latency (s) by Model")
|
| 62 |
+
c3 = px.bar(per_model.sort_values("p95_latency"),
|
| 63 |
+
x="model", y="p95_latency", title="P95 Latency (s) by Model")
|
| 64 |
+
c4 = px.bar(per_model.sort_values("quality_per_wh", ascending=False),
|
| 65 |
+
x="model", y="quality_per_wh", title="Quality per Watt-hour (↑ better)")
|
| 66 |
+
|
| 67 |
+
return df, per_model, per_model_cat, c1, c2, c3, c4
|
| 68 |
+
|
| 69 |
+
def _filter(df, model_sel, cat_sel, prompt_sel):
|
| 70 |
+
if df.empty:
|
| 71 |
+
return pd.DataFrame()
|
| 72 |
+
out = df.copy()
|
| 73 |
+
if model_sel and model_sel != "ALL":
|
| 74 |
+
out = out[out["model"] == model_sel]
|
| 75 |
+
if cat_sel and cat_sel != "ALL":
|
| 76 |
+
out = out[out["category"] == cat_sel]
|
| 77 |
+
if prompt_sel and prompt_sel != "ALL":
|
| 78 |
+
out = out[out["prompt_id"] == prompt_sel]
|
| 79 |
+
return out
|
| 80 |
+
|
| 81 |
+
def _choices(df):
|
| 82 |
+
models = ["ALL"] + sorted([m for m in df["model"].dropna().unique().tolist()])
|
| 83 |
+
cats = ["ALL"] + sorted([c for c in df["category"].dropna().unique().tolist()])
|
| 84 |
+
prompts = ["ALL"] + sorted([p for p in df["prompt_id"].dropna().unique().tolist()])
|
| 85 |
+
return models, cats, prompts
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(title="Compare’IA — Benchmark Dashboard") as demo:
|
| 88 |
+
gr.Markdown("## Compare’IA — Benchmark Dashboard\nUpload your CSV or use the default `results.csv` in the Space repo.")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
csv_file = gr.File(label="Upload results CSV", file_types=[".csv"])
|
| 92 |
+
refresh_btn = gr.Button("Refresh data")
|
| 93 |
+
|
| 94 |
+
raw_df = gr.Dataframe(label="Raw data", interactive=False, wrap=True, height=300)
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
model_dd = gr.Dropdown(choices=["ALL"], value="ALL", label="Model")
|
| 98 |
+
cat_dd = gr.Dropdown(choices=["ALL"], value="ALL", label="Category")
|
| 99 |
+
prompt_dd = gr.Dropdown(choices=["ALL"], value="ALL", label="Prompt ID")
|
| 100 |
+
apply_filter = gr.Button("Apply filter")
|
| 101 |
+
|
| 102 |
+
filtered_df = gr.Dataframe(label="Filtered rows", interactive=False, height=250)
|
| 103 |
+
|
| 104 |
+
with gr.Accordion("Aggregates & Charts", open=True):
|
| 105 |
+
per_model_df = gr.Dataframe(label="Per-model summary", interactive=False)
|
| 106 |
+
per_model_cat_df = gr.Dataframe(label="Per-model-per-category", interactive=False)
|
| 107 |
+
chart_quality = gr.Plot(label="Mean Quality by Model")
|
| 108 |
+
chart_mean_lat = gr.Plot(label="Mean Latency by Model")
|
| 109 |
+
chart_p95_lat = gr.Plot(label="P95 Latency by Model")
|
| 110 |
+
chart_q_per_wh = gr.Plot(label="Quality per Wh")
|
| 111 |
+
|
| 112 |
+
def _refresh(file):
|
| 113 |
+
df = _load_df(file)
|
| 114 |
+
models, cats, prompts = _choices(df)
|
| 115 |
+
full_df, pm, pmc, c1, c2, c3, c4 = _summaries(df)
|
| 116 |
+
return (full_df, gr.update(choices=models, value="ALL"),
|
| 117 |
+
gr.update(choices=cats, value="ALL"),
|
| 118 |
+
gr.update(choices=prompts, value="ALL"),
|
| 119 |
+
pm, pmc, c1, c2, c3, c4)
|
| 120 |
+
|
| 121 |
+
refresh_btn.click(_refresh, inputs=csv_file,
|
| 122 |
+
outputs=[raw_df, model_dd, cat_dd, prompt_dd, per_model_df, per_model_cat_df,
|
| 123 |
+
chart_quality, chart_mean_lat, chart_p95_lat, chart_q_per_wh])
|
| 124 |
+
|
| 125 |
+
def _apply(file, model_sel, cat_sel, prompt_sel):
|
| 126 |
+
df = _load_df(file)
|
| 127 |
+
out = _filter(df, model_sel, cat_sel, prompt_sel)
|
| 128 |
+
return out
|
| 129 |
+
|
| 130 |
+
apply_filter.click(_apply, inputs=[csv_file, model_dd, cat_dd, prompt_dd], outputs=[filtered_df])
|
| 131 |
+
|
| 132 |
+
demo.launch()
|