Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,173 +1,88 @@
|
|
| 1 |
-
|
| 2 |
-
#
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return output.strip(), latency
|
| 90 |
-
except Exception as e:
|
| 91 |
-
return f"ERROR: {e}", time.time() - start
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# ---------------- Benchmark Function ---------------- #
|
| 95 |
-
def benchmark(config_text, dataset_text, task):
|
| 96 |
-
cfg = yaml.safe_load(config_text)
|
| 97 |
-
data = [json.loads(line) for line in dataset_text.splitlines() if line.strip()]
|
| 98 |
-
|
| 99 |
-
models = cfg.get("models", [])
|
| 100 |
-
templates = cfg.get("prompt_templates", {})
|
| 101 |
-
template = templates.get(task, "{{text}}")
|
| 102 |
-
|
| 103 |
-
results = []
|
| 104 |
-
for m in models:
|
| 105 |
-
model_name = m["name"]
|
| 106 |
-
max_tokens = m.get("params", {}).get("max_tokens", 256)
|
| 107 |
-
temperature = m.get("params", {}).get("temperature", 0.2)
|
| 108 |
-
for ex in tqdm(data, desc=model_name):
|
| 109 |
-
variables = {k: ex.get(k, "") for k in ("question", "context", "text", "labels")}
|
| 110 |
-
prompt = template
|
| 111 |
-
for k, v in variables.items():
|
| 112 |
-
prompt = prompt.replace(f"{{{{{k}}}}}", str(v))
|
| 113 |
-
prediction, latency = hf_generate(model_name, prompt, max_new_tokens=max_tokens, temperature=temperature)
|
| 114 |
-
metrics = compute_metrics(task, prediction, ex.get("reference", ""))
|
| 115 |
-
row = {
|
| 116 |
-
"model": model_name,
|
| 117 |
-
"id": ex.get("id", ""),
|
| 118 |
-
"task": task,
|
| 119 |
-
"prompt": prompt,
|
| 120 |
-
"prediction": prediction,
|
| 121 |
-
"reference": ex.get("reference", ""),
|
| 122 |
-
"latency_seconds": latency,
|
| 123 |
-
**metrics
|
| 124 |
-
}
|
| 125 |
-
results.append(row)
|
| 126 |
-
|
| 127 |
-
df = pd.DataFrame(results)
|
| 128 |
-
summary = []
|
| 129 |
-
for model_name in set(df["model"]):
|
| 130 |
-
sub = df[df["model"] == model_name]
|
| 131 |
-
summary.append(f"## {model_name}")
|
| 132 |
-
summary.append(f"Samples: {len(sub)}")
|
| 133 |
-
for metric in ["exact_match", "f1", "rougeL_f", "bleu"]:
|
| 134 |
-
if metric in sub.columns:
|
| 135 |
-
vals = [v for v in sub[metric] if isinstance(v, (int, float))]
|
| 136 |
-
if vals:
|
| 137 |
-
summary.append(f"{metric}: mean={sum(vals)/len(vals):.4f}")
|
| 138 |
-
summary.append(f"Latency mean: {sum(sub['latency_seconds'])/len(sub):.3f}s\n")
|
| 139 |
-
|
| 140 |
-
return df, "\n".join(summary)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
def hf_judge(model_name, prompt, candidate, reference=None, rubric=None, max_new_tokens=256):
|
| 144 |
-
"""
|
| 145 |
-
Calls a judge model to score candidate output. Returns a dict of scores.
|
| 146 |
-
Rubric should instruct JSON output, e.g.:
|
| 147 |
-
{"relevance": int, "factuality": int, "clarity": int, "overall": float}
|
| 148 |
-
"""
|
| 149 |
-
from huggingface_hub import InferenceClient
|
| 150 |
-
client = InferenceClient(model=model_name, token=os.getenv("HF_TOKEN"))
|
| 151 |
-
|
| 152 |
-
rubric = rubric or (
|
| 153 |
-
"Evaluate the candidate answer. Score 1–5 for:\n"
|
| 154 |
-
"- Relevance: addresses the prompt\n"
|
| 155 |
-
"- Factuality: correct and supported\n"
|
| 156 |
-
"- Clarity: clear and well-structured\n"
|
| 157 |
-
"Return JSON: {\"relevance\": int, \"factuality\": int, \"clarity\": int, \"overall\": float}"
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
judge_prompt = (
|
| 161 |
-
f"{rubric}\n\nPrompt:\n{prompt}\n\n"
|
| 162 |
-
f"Candidate:\n{candidate}\n\n"
|
| 163 |
-
f"Reference (if available):\n{reference if reference is not None else 'N/A'}\n"
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
try:
|
| 167 |
-
text = client.text_generation(judge_prompt, max_new_tokens=max_new_tokens, temperature=0.0)
|
| 168 |
-
# Attempt to parse JSON anywhere in the response:
|
| 169 |
-
import re, json
|
| 170 |
-
m = re.search(r'\{.*\}', text, re.S)
|
| 171 |
-
return json.loads(m.group(0)) if m else {"raw": text}
|
| 172 |
-
except Exception as e:
|
| 173 |
-
return {"error": str(e)}
|
|
|
|
| 1 |
+
|
| 2 |
+
# ---------------- Gradio UI ---------------- #
|
| 3 |
+
with gr.Blocks() as demo:
|
| 4 |
+
gr.Markdown("# LLM Benchmarking App (Hugging Face)")
|
| 5 |
+
gr.Markdown("Upload config.yaml and dataset.jsonl, select task, and run benchmark.")
|
| 6 |
+
|
| 7 |
+
with gr.Row():
|
| 8 |
+
config_file = gr.File(label="Upload Config (YAML)", type="filepath")
|
| 9 |
+
dataset_file = gr.File(label="Upload Dataset (JSONL)", type="filepath")
|
| 10 |
+
|
| 11 |
+
task = gr.Dropdown(choices=["qa", "summarization", "classification", "conversation"], label="Select Task")
|
| 12 |
+
use_judge = gr.Checkbox(label="Enable Judge Scoring?", value=False)
|
| 13 |
+
run_btn = gr.Button("Run Benchmark")
|
| 14 |
+
|
| 15 |
+
results_table = gr.Dataframe(headers=[
|
| 16 |
+
"model","id","task","prompt","prediction","reference","latency_seconds",
|
| 17 |
+
"exact_match","f1","rougeL_f","bleu","judge_overall"
|
| 18 |
+
], label="Results")
|
| 19 |
+
|
| 20 |
+
summary_box = gr.Textbox(label="Summary", lines=10)
|
| 21 |
+
download_csv = gr.File(label="Download CSV")
|
| 22 |
+
|
| 23 |
+
def run_benchmark(config_path, dataset_path, task, use_judge):
|
| 24 |
+
if not config_path or not dataset_path:
|
| 25 |
+
return None, "Error: Please upload both files", None
|
| 26 |
+
|
| 27 |
+
config_text = open(config_path, "r", encoding="utf-8").read()
|
| 28 |
+
dataset_text = open(dataset_path, "r", encoding="utf-8").read()
|
| 29 |
+
cfg = yaml.safe_load(config_text)
|
| 30 |
+
data = [json.loads(line) for line in dataset_text.splitlines() if line.strip()]
|
| 31 |
+
template = cfg.get("prompt_templates", {}).get(task, "{{text}}")
|
| 32 |
+
judge_cfg = cfg.get("judge", {})
|
| 33 |
+
|
| 34 |
+
results = []
|
| 35 |
+
for m in cfg.get("models", []):
|
| 36 |
+
model_name = m["name"]
|
| 37 |
+
max_tokens = m.get("params", {}).get("max_tokens", 256)
|
| 38 |
+
temperature = m.get("params", {}).get("temperature", 0.2)
|
| 39 |
+
|
| 40 |
+
for ex in data:
|
| 41 |
+
variables = {k: ex.get(k, "") for k in ("question", "context", "text", "labels")}
|
| 42 |
+
prompt = template
|
| 43 |
+
for k, v in variables.items():
|
| 44 |
+
prompt = prompt.replace(f"{{{{{k}}}}}", str(v))
|
| 45 |
+
|
| 46 |
+
prediction, latency = hf_generate(model_name, prompt, max_new_tokens=max_tokens, temperature=temperature)
|
| 47 |
+
metrics = compute_metrics(task, prediction, ex.get("reference", ""))
|
| 48 |
+
|
| 49 |
+
row = {
|
| 50 |
+
"model": model_name,
|
| 51 |
+
"id": ex.get("id", ""),
|
| 52 |
+
"task": task,
|
| 53 |
+
"prompt": prompt,
|
| 54 |
+
"prediction": prediction,
|
| 55 |
+
"reference": ex.get("reference", ""),
|
| 56 |
+
"latency_seconds": latency,
|
| 57 |
+
**metrics
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
if use_judge and judge_cfg.get("enabled"):
|
| 61 |
+
scores = hf_judge(judge_cfg.get("model"), prompt, prediction, ex.get("reference", ""), judge_cfg.get("rubric"))
|
| 62 |
+
for k, v in (scores.items() if isinstance(scores, dict) else []):
|
| 63 |
+
row[f"judge_{k}"] = v
|
| 64 |
+
|
| 65 |
+
results.append(row)
|
| 66 |
+
|
| 67 |
+
df = pd.DataFrame(results)
|
| 68 |
+
csv_path = "results.csv"
|
| 69 |
+
df.to_csv(csv_path, index=False)
|
| 70 |
+
|
| 71 |
+
# Summary
|
| 72 |
+
summary = []
|
| 73 |
+
for model_name in set(df["model"]):
|
| 74 |
+
sub = df[df["model"] == model_name]
|
| 75 |
+
summary.append(f"## {model_name}")
|
| 76 |
+
summary.append(f"Samples: {len(sub)}")
|
| 77 |
+
for metric in ["exact_match", "f1", "rougeL_f", "bleu", "judge_overall"]:
|
| 78 |
+
if metric in sub.columns:
|
| 79 |
+
vals = [v for v in sub[metric] if isinstance(v, (int, float))]
|
| 80 |
+
if vals:
|
| 81 |
+
summary.append(f"{metric}: mean={sum(vals)/len(vals):.4f}")
|
| 82 |
+
summary.append(f"Latency mean: {sum(sub['latency_seconds'])/len(sub):.3f}s\n")
|
| 83 |
+
|
| 84 |
+
return df, "\n".join(summary), csv_path
|
| 85 |
+
|
| 86 |
+
run_btn.click(run_benchmark, inputs=[config_file, dataset_file, task, use_judge], outputs=[results_table, summary_box, download_csv])
|
| 87 |
+
|
| 88 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|