import gradio as gr
import pandas as pd
from db.store import get_runs, delete_run, get_run
from core.diff import render_diff_html
_DEFAULT_COLS = ["id", "timestamp", "model", "compression_ratio", "quality_score", "feedback"]
_ALL_COLS = [
"id", "timestamp", "model", "tokenizer",
"input_tokens", "output_tokens", "target_tokens",
"compression_ratio", "quality_score", "duration_ms",
"feedback", "feedback_comment",
]
_SESSION_WARNING = (
'
'
'⚠️ Session only — history is stored in memory and will be '
'cleared when you close or refresh this page. No data is persisted to disk.'
'
'
)
def load_history(selected_cols, run_store):
cols = selected_cols if selected_cols else _DEFAULT_COLS
runs = get_runs(run_store, limit=100)
if not runs:
return pd.DataFrame(columns=cols), "", "", ""
df = pd.DataFrame(runs)
existing = [c for c in cols if c in df.columns]
df = df[existing]
avg_quality = f"{df['quality_score'].mean():.4f}" if "quality_score" in df.columns else "—"
avg_ratio = f"{df['compression_ratio'].mean():.4f}" if "compression_ratio" in df.columns else "—"
return df, avg_quality, avg_ratio, ""
def on_row_select(evt: gr.SelectData, df: pd.DataFrame, run_store: list):
if df is None or df.empty:
return None, "", "No rows available."
row_idx = evt.index[0]
run_id = int(df.iloc[row_idx]["id"])
record = get_run(run_store, run_id)
if not record:
return None, "", f"Row {run_id} not found."
diff_html = render_diff_html(record)
return run_id, diff_html, f"Row {run_id} selected — click Delete to remove."
def delete_selected(run_id, selected_cols, run_store):
if run_id is None:
df, avg_q, avg_r, _ = load_history(selected_cols, run_store)
return df, avg_q, avg_r, None, "", "No row selected.", run_store
new_store = delete_run(run_store, run_id)
df, avg_q, avg_r, _ = load_history(selected_cols, new_store)
return df, avg_q, avg_r, None, "", f"Row {run_id} deleted.", new_store
def build_history_tab(run_store) -> gr.Tab:
with gr.Tab("History") as tab:
gr.Markdown("## Compression Run History")
gr.HTML(_SESSION_WARNING)
with gr.Row():
refresh_btn = gr.Button("Refresh", variant="secondary")
delete_btn = gr.Button("Delete Selected Row", variant="stop")
with gr.Accordion("Column visibility", open=False):
col_picker = gr.CheckboxGroup(
choices=_ALL_COLS,
value=_DEFAULT_COLS,
label=None,
)
with gr.Row():
avg_quality = gr.Textbox(label="Avg Quality Score", interactive=False)
avg_ratio = gr.Textbox(label="Avg Compression Ratio", interactive=False)
history_table = gr.DataFrame(
label="Past Runs — click a row to see its diff",
interactive=False,
)
delete_status = gr.Textbox(
label="Status", value="Click a row to select it.", interactive=False
)
gr.Markdown("### Side-by-side Diff")
diff_panel = gr.HTML(value="")
selected_id = gr.State(value=None)
_outputs = [history_table, avg_quality, avg_ratio, diff_panel]
refresh_btn.click(fn=load_history, inputs=[col_picker, run_store], outputs=_outputs)
tab.select(fn=load_history, inputs=[col_picker, run_store], outputs=_outputs)
col_picker.change(fn=load_history, inputs=[col_picker, run_store], outputs=_outputs)
history_table.select(
fn=on_row_select,
inputs=[history_table, run_store],
outputs=[selected_id, diff_panel, delete_status],
)
delete_btn.click(
fn=delete_selected,
inputs=[selected_id, col_picker, run_store],
outputs=[history_table, avg_quality, avg_ratio, selected_id, diff_panel, delete_status, run_store],
)
return tab