Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -18,38 +18,20 @@ def load_model(model_id):
|
|
18 |
model_cache[model_id] = generator
|
19 |
return generator
|
20 |
|
21 |
-
def format_prompt(item
|
22 |
-
|
23 |
-
|
24 |
-
answer = item['answer']
|
25 |
-
elif source == "TIGER-Lab/MMLU-Pro":
|
26 |
-
if all(opt in item for opt in ['A', 'B', 'C', 'D']):
|
27 |
-
prompt = f"{item['question']}\nA. {item['A']}\nB. {item['B']}\nC. {item['C']}\nD. {item['D']}\nAnswer:"
|
28 |
-
else:
|
29 |
-
choices = item.get("choices", ["", "", "", ""])
|
30 |
-
prompt = f"{item['question']}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nAnswer:"
|
31 |
-
answer = item['answer']
|
32 |
-
elif source == "cais/hle":
|
33 |
-
prompt = f"{item['question']}\n{item['A']}\n{item['B']}\n{item['C']}\n{item['D']}\nAnswer:"
|
34 |
-
answer = item['answer']
|
35 |
-
else:
|
36 |
-
prompt, answer = "", ""
|
37 |
-
return prompt, answer
|
38 |
-
|
39 |
-
def evaluate(model_id, dataset_name, sample_count):
|
40 |
-
gen = load_model(model_id)
|
41 |
-
dataset = load_dataset(dataset_name, token=HF_TOKEN)
|
42 |
-
if 'test' in dataset:
|
43 |
-
dataset = dataset['test']
|
44 |
-
else:
|
45 |
-
dataset = dataset[list(dataset.keys())[0]]
|
46 |
|
|
|
|
|
|
|
47 |
dataset = dataset.shuffle(seed=42).select(range(min(sample_count, len(dataset))))
|
|
|
48 |
correct = 0
|
49 |
results = []
|
50 |
|
51 |
for item in dataset:
|
52 |
-
prompt, answer = format_prompt(item
|
53 |
output = gen(prompt, max_new_tokens=10, do_sample=False)[0]["generated_text"]
|
54 |
output_letter = next((char for char in output[::-1] if char in "ABCD"), None)
|
55 |
is_correct = output_letter == answer
|
@@ -57,19 +39,17 @@ def evaluate(model_id, dataset_name, sample_count):
|
|
57 |
results.append((prompt, output.strip(), answer, output_letter, is_correct))
|
58 |
|
59 |
accuracy = correct / len(dataset) * 100
|
60 |
-
return accuracy, results
|
61 |
-
|
62 |
-
def run(model_id, benchmark, sample_count):
|
63 |
-
if benchmark != "cais/mmlu":
|
64 |
-
return "Only MMLU (cais/mmlu) is available now. MMLU-Pro and Humanity's Last Exam are coming soon.", ""
|
65 |
|
66 |
-
|
|
|
|
|
|
|
67 |
formatted = "\n\n".join([
|
68 |
f"### Question:\n{q}\n\n**Model Answer:** {o}\n**Expected:** {a}\n**Predicted:** {g}\n**Correct:** {c}"
|
69 |
for q, o, a, g, c in details
|
70 |
])
|
71 |
-
|
72 |
-
return f"Accuracy: {accuracy:.2f}%", formatted
|
73 |
|
74 |
def save_text(text):
|
75 |
return "evaluation_results.txt", text
|
@@ -81,15 +61,29 @@ with gr.Blocks(css="body {font-family: Inter, sans-serif; padding: 1em; max-widt
|
|
81 |
Currently, only **MMLU** (`cais/mmlu`) is available for evaluation.
|
82 |
**MMLU-Pro** and **Humanity's Last Exam** will be coming soon.
|
83 |
|
84 |
-
Enter your model ID, pick MMLU, and hit evaluate.
|
85 |
""")
|
86 |
|
87 |
with gr.Row():
|
88 |
model_id = gr.Textbox(label="Your Hugging Face Model ID", placeholder="e.g., your-org/your-model")
|
89 |
-
|
90 |
-
label="Choose
|
91 |
-
choices=[
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
)
|
94 |
sample_count = gr.Slider(label="Number of Samples", minimum=1, maximum=100, value=10, step=1)
|
95 |
|
@@ -98,7 +92,11 @@ with gr.Blocks(css="body {font-family: Inter, sans-serif; padding: 1em; max-widt
|
|
98 |
detail_output = gr.Textbox(label="Evaluation Details", lines=20, interactive=False)
|
99 |
download_button = gr.Button("📥 Download Full Evaluation")
|
100 |
|
101 |
-
run_button.click(run, inputs=[model_id,
|
102 |
download_button.click(save_text, inputs=detail_output, outputs=gr.File())
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
18 |
model_cache[model_id] = generator
|
19 |
return generator
|
20 |
|
21 |
+
def format_prompt(item):
|
22 |
+
prompt = f"{item['question']}\nA. {item['choices'][0]}\nB. {item['choices'][1]}\nC. {item['choices'][2]}\nD. {item['choices'][3]}\nAnswer:"
|
23 |
+
return prompt, item['answer']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def evaluate(model_id, sample_count, config_name):
|
26 |
+
gen = load_model(model_id)
|
27 |
+
dataset = load_dataset("cais/mmlu", config_name, token=HF_TOKEN)["test"]
|
28 |
dataset = dataset.shuffle(seed=42).select(range(min(sample_count, len(dataset))))
|
29 |
+
|
30 |
correct = 0
|
31 |
results = []
|
32 |
|
33 |
for item in dataset:
|
34 |
+
prompt, answer = format_prompt(item)
|
35 |
output = gen(prompt, max_new_tokens=10, do_sample=False)[0]["generated_text"]
|
36 |
output_letter = next((char for char in output[::-1] if char in "ABCD"), None)
|
37 |
is_correct = output_letter == answer
|
|
|
39 |
results.append((prompt, output.strip(), answer, output_letter, is_correct))
|
40 |
|
41 |
accuracy = correct / len(dataset) * 100
|
42 |
+
return f"Accuracy: {accuracy:.2f}%", results
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
def run(model_id, sample_count, config_name):
|
45 |
+
if config_name == "coming soon":
|
46 |
+
return "Only MMLU is currently available. MMLU-Pro and HLE coming soon.", ""
|
47 |
+
score, details = evaluate(model_id, sample_count, config_name)
|
48 |
formatted = "\n\n".join([
|
49 |
f"### Question:\n{q}\n\n**Model Answer:** {o}\n**Expected:** {a}\n**Predicted:** {g}\n**Correct:** {c}"
|
50 |
for q, o, a, g, c in details
|
51 |
])
|
52 |
+
return score, formatted
|
|
|
53 |
|
54 |
def save_text(text):
|
55 |
return "evaluation_results.txt", text
|
|
|
61 |
Currently, only **MMLU** (`cais/mmlu`) is available for evaluation.
|
62 |
**MMLU-Pro** and **Humanity's Last Exam** will be coming soon.
|
63 |
|
64 |
+
Enter your model ID, pick MMLU, choose a subject, and hit evaluate.
|
65 |
""")
|
66 |
|
67 |
with gr.Row():
|
68 |
model_id = gr.Textbox(label="Your Hugging Face Model ID", placeholder="e.g., your-org/your-model")
|
69 |
+
config_name = gr.Dropdown(
|
70 |
+
label="Choose MMLU Subject",
|
71 |
+
choices=[
|
72 |
+
"abstract_algebra", "anatomy", "astronomy", "business_ethics", "college_biology",
|
73 |
+
"college_chemistry", "college_computer_science", "college_mathematics", "college_medicine",
|
74 |
+
"college_physics", "computer_security", "econometrics", "electrical_engineering",
|
75 |
+
"elementary_mathematics", "formal_logic", "global_facts", "high_school_biology",
|
76 |
+
"high_school_chemistry", "high_school_computer_science", "high_school_european_history",
|
77 |
+
"high_school_geography", "high_school_government_and_politics", "high_school_macroeconomics",
|
78 |
+
"high_school_microeconomics", "high_school_physics", "high_school_psychology",
|
79 |
+
"high_school_statistics", "high_school_us_history", "high_school_world_history", "human_aging",
|
80 |
+
"human_sexuality", "international_law", "jurisprudence", "logical_fallacies", "machine_learning",
|
81 |
+
"management", "marketing", "medical_genetics", "miscellaneous", "moral_disputes",
|
82 |
+
"moral_scenarios", "nutrition", "philosophy", "prehistory", "professional_accounting",
|
83 |
+
"professional_law", "professional_medicine", "professional_psychology", "public_relations",
|
84 |
+
"security_studies", "sociology", "us_foreign_policy", "virology", "world_religions"
|
85 |
+
],
|
86 |
+
value="college_mathematics"
|
87 |
)
|
88 |
sample_count = gr.Slider(label="Number of Samples", minimum=1, maximum=100, value=10, step=1)
|
89 |
|
|
|
92 |
detail_output = gr.Textbox(label="Evaluation Details", lines=20, interactive=False)
|
93 |
download_button = gr.Button("📥 Download Full Evaluation")
|
94 |
|
95 |
+
run_button.click(run, inputs=[model_id, sample_count, config_name], outputs=[acc_output, detail_output])
|
96 |
download_button.click(save_text, inputs=detail_output, outputs=gr.File())
|
97 |
|
98 |
+
gr.Markdown("""
|
99 |
+
MMLU-Pro and HLE support will be added soon.
|
100 |
+
""")
|
101 |
+
|
102 |
+
demo.launch()
|