Download analysis.py from rbbist/basecamp-chatbot-eval-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 5.78 kB
-
https://huggingface.co/spaces/rbbist/basecamp-chatbot-eval-dashboard/resolve/main/analysis.py
- Command line
-
hf download hf://spaces/rbbist/basecamp-chatbot-eval-dashboard/analysis.py
-
curl -L -o analysis.py https://huggingface.co/spaces/rbbist/basecamp-chatbot-eval-dashboard/resolve/main/analysis.py
5.78 kB
| from datetime import datetime | |
| import pandas as pd | |
| RET_KEY = "failures_with_insufficient_context (retrieval problem)" | |
| GEN_KEY = "failures_with_sufficient_context (generation problem)" | |
| AVG_METRICS = [("avg_faithfulness", "Faithfulness"), | |
| ("avg_relevance", "Relevance"), | |
| ("avg_correctness", "Correctness")] | |
| def parse_path(path: str): | |
| base = path.split("/")[-1] | |
| stamp, _, name = base.partition("__") | |
| try: | |
| when = datetime.strptime(stamp, "%Y%m%d-%H%M%S").strftime("%Y-%m-%d %H:%M") | |
| except ValueError: | |
| when, name = "unknown", base | |
| return when, name | |
| def run_label(path: str) -> str: | |
| when, name = parse_path(path) | |
| return f"{when} UTC · {name}" | |
| def case_key(r: dict) -> str: | |
| if r.get("conversation_id") is not None: | |
| return f"{r['conversation_id']}-T{r.get('turn')}" | |
| return f"#{r.get('ind')}" | |
| def pass_pct(data: dict): | |
| res = data.get("results", []) | |
| if not res: | |
| return None | |
| return round(100 * sum(r.get("status") == "PASS" for r in res) / len(res), 1) | |
| def summary_row(path: str, data: dict) -> dict: | |
| s = data.get("summary", {}) | |
| when, name = parse_path(path) | |
| return { | |
| "Run ID": path, | |
| "Uploaded (UTC)": when, | |
| "File": name, | |
| "Cases": len(data.get("results", [])), | |
| "Passed": s.get("passed"), | |
| "Pass %": pass_pct(data), | |
| "Faithfulness": s.get("avg_faithfulness"), | |
| "Relevance": s.get("avg_relevance"), | |
| "Correctness": s.get("avg_correctness"), | |
| "Retrieval Fails": len(s.get(RET_KEY, [])), | |
| "Generation Fails": len(s.get(GEN_KEY, [])), | |
| "Retrieval OK": s.get("retrieval_all_expected_found", "-"), | |
| "Judge": s.get("judge_model"), | |
| } | |
| def results_df(data: dict) -> pd.DataFrame: | |
| rows = [] | |
| for r in data.get("results", []): | |
| j = r.get("judge") or {} | |
| t = r.get("timings") or {} | |
| rows.append({ | |
| "Ind": r.get("ind"), | |
| "Key": case_key(r), | |
| "Status": r.get("status"), | |
| "Category": str(r.get("category")), | |
| "Topic": r.get("dataset_category"), | |
| "Query": r.get("query"), | |
| "Ground Truth": r.get("ground_truth"), | |
| "Generated Answer": r.get("generated_answer"), | |
| "Faithfulness": j.get("faithfulness"), | |
| "Relevance": j.get("relevance"), | |
| "Correctness": j.get("correctness"), | |
| "Judge Verdict": (None if j.get("passed") is None | |
| else ("PASS" if j.get("passed") else "FAIL")), | |
| "Judge Reasoning": j.get("reasoning"), | |
| "Context Sufficient": j.get("context_sufficient"), | |
| "Fail Reason": r.get("fail_reason"), | |
| "Latency (s)": round(sum(t.values()), 2) if t else 0, | |
| }) | |
| return pd.DataFrame(rows) | |
| def filter_df(df, status, categories, search): | |
| if df.empty: | |
| return df | |
| if status and status != "All": | |
| df = df[df["Status"] == status] | |
| if categories: | |
| df = df[df["Category"].isin(categories)] | |
| if search: | |
| s = search.lower() | |
| hay = (df["Query"].fillna("") + " " + df["Generated Answer"].fillna("") + " " | |
| + df["Ground Truth"].fillna("") + " " + df["Topic"].fillna("").astype(str)) | |
| df = df[hay.str.lower().str.contains(s, regex=False)] | |
| return df | |
| def _delta(a, b): | |
| try: | |
| return round(float(b) - float(a), 2) | |
| except (TypeError, ValueError): | |
| return None | |
| def compare_runs(a: dict, b: dict, changed_only: bool): | |
| sa, sb = a.get("summary", {}), b.get("summary", {}) | |
| rows = [{"Metric": "Pass %", "A": pass_pct(a), "B": pass_pct(b), | |
| "Δ (B−A)": _delta(pass_pct(a), pass_pct(b))}] | |
| for key, label in AVG_METRICS: | |
| rows.append({"Metric": label, "A": sa.get(key), "B": sb.get(key), | |
| "Δ (B−A)": _delta(sa.get(key), sb.get(key))}) | |
| for key, label in [(RET_KEY, "Retrieval fails"), (GEN_KEY, "Generation fails")]: | |
| va, vb = len(sa.get(key, [])), len(sb.get(key, [])) | |
| rows.append({"Metric": label, "A": va, "B": vb, "Δ (B−A)": vb - va}) | |
| metrics = pd.DataFrame(rows) | |
| da = results_df(a).set_index("Key") | |
| db = results_df(b).set_index("Key") | |
| out = [] | |
| for key in list(dict.fromkeys(list(da.index) + list(db.index))): | |
| ra = da.loc[key] if key in da.index else None | |
| rb = db.loc[key] if key in db.index else None | |
| sta = ra["Status"] if ra is not None else None | |
| stb = rb["Status"] if rb is not None else None | |
| if ra is None: | |
| change = "🆕 New in B" | |
| elif rb is None: | |
| change = "➖ Only in A" | |
| elif sta == "FAIL" and stb == "PASS": | |
| change = "✅ Fixed" | |
| elif sta == "PASS" and stb == "FAIL": | |
| change = "🔻 Regressed" | |
| else: | |
| change = "—" | |
| ca = ra["Correctness"] if ra is not None else None | |
| cb = rb["Correctness"] if rb is not None else None | |
| out.append({ | |
| "Key": key, | |
| "Query": (rb if rb is not None else ra)["Query"], | |
| "Status A": sta, "Status B": stb, | |
| "Correctness A": ca, "Correctness B": cb, | |
| "Δ Correctness": _delta(ca, cb), | |
| "Change": change, | |
| "Answer A": ra["Generated Answer"] if ra is not None else None, | |
| "Answer B": rb["Generated Answer"] if rb is not None else None, | |
| }) | |
| cases = pd.DataFrame(out) | |
| if changed_only and not cases.empty: | |
| cases = cases[(cases["Change"] != "—") | (cases["Δ Correctness"].fillna(0) != 0)] | |
| return metrics, cases |