Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,82 +5,57 @@ import pandas as pd
|
|
5 |
|
6 |
from agent import create_agent, fetch_random_question
|
7 |
|
8 |
-
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
-
|
12 |
def run_and_submit_all(profile):
|
13 |
-
"""
|
14 |
-
Fetch all questions, run the agent on them, submit all answers,
|
15 |
-
and return the status and results table.
|
16 |
-
"""
|
17 |
-
# Check login
|
18 |
if not profile:
|
19 |
return "Please login to Hugging Face with the login button.", None
|
20 |
-
|
21 |
-
# Extract username (support both .username and .name)
|
22 |
username = getattr(profile, "username", None) or getattr(profile, "name", None)
|
23 |
if not username:
|
24 |
return "Login error: username not found.", None
|
25 |
|
26 |
-
# Build URLs
|
27 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
28 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
29 |
-
|
30 |
-
# Instantiate agent
|
31 |
-
try:
|
32 |
-
agent = create_agent()
|
33 |
-
except Exception as e:
|
34 |
-
return f"Error initializing agent: {e}", None
|
35 |
-
|
36 |
# Fetch questions
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
questions =
|
41 |
except Exception as e:
|
42 |
return f"Error fetching questions: {e}", None
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
for q in questions:
|
48 |
-
tid = q.get("task_id")
|
49 |
-
|
50 |
-
if not tid or not text:
|
51 |
continue
|
52 |
try:
|
53 |
-
ans = agent.run(
|
54 |
except Exception as e:
|
55 |
ans = f"ERROR: {e}"
|
56 |
-
results.append({
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
})
|
61 |
-
answers_payload.append({
|
62 |
-
"task_id": tid,
|
63 |
-
"submitted_answer": ans
|
64 |
-
})
|
65 |
-
|
66 |
-
if not answers_payload:
|
67 |
return "Agent returned no answers.", pd.DataFrame(results)
|
68 |
|
69 |
-
# Submit
|
70 |
-
|
71 |
"username": username,
|
72 |
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
73 |
-
"answers":
|
74 |
}
|
75 |
try:
|
76 |
-
resp = requests.post(
|
77 |
resp.raise_for_status()
|
78 |
data = resp.json()
|
79 |
status = (
|
80 |
f"Submission Successful!\n"
|
81 |
-
f"User: {data
|
82 |
-
f"Score: {data
|
83 |
-
f"Message: {data
|
84 |
)
|
85 |
except Exception as e:
|
86 |
status = f"Submission Failed: {e}"
|
@@ -89,24 +64,15 @@ def run_and_submit_all(profile):
|
|
89 |
|
90 |
|
91 |
def test_random_question(profile):
|
92 |
-
"""
|
93 |
-
Fetch a random GAIA question and return the agent's answer.
|
94 |
-
"""
|
95 |
if not profile:
|
96 |
return "Please login to Hugging Face with the login button.", ""
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
agent = create_agent()
|
103 |
-
answer = agent.run(question=question)
|
104 |
-
return question, answer
|
105 |
-
except Exception as e:
|
106 |
-
return f"Error during test: {e}", ""
|
107 |
|
108 |
|
109 |
-
# --- Gradio Interface ---
|
110 |
with gr.Blocks() as demo:
|
111 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
112 |
gr.Markdown(
|
@@ -119,18 +85,16 @@ with gr.Blocks() as demo:
|
|
119 |
)
|
120 |
|
121 |
login = gr.LoginButton()
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
results_table = gr.DataFrame(label="Full Results Table", wrap=True)
|
128 |
-
question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
129 |
-
answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
test_button.click(fn=test_random_question, inputs=[login], outputs=[question_box, answer_box])
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
demo.launch(debug=True, share=False)
|
|
|
5 |
|
6 |
from agent import create_agent, fetch_random_question
|
7 |
|
|
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
|
|
10 |
def run_and_submit_all(profile):
|
|
|
|
|
|
|
|
|
|
|
11 |
if not profile:
|
12 |
return "Please login to Hugging Face with the login button.", None
|
|
|
|
|
13 |
username = getattr(profile, "username", None) or getattr(profile, "name", None)
|
14 |
if not username:
|
15 |
return "Login error: username not found.", None
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Fetch questions
|
18 |
try:
|
19 |
+
qs = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
20 |
+
qs.raise_for_status()
|
21 |
+
questions = qs.json()
|
22 |
except Exception as e:
|
23 |
return f"Error fetching questions: {e}", None
|
24 |
|
25 |
+
# Instantiate agent
|
26 |
+
agent = create_agent()
|
27 |
+
|
28 |
+
# Run and collect answers
|
29 |
+
results, payload = [], []
|
30 |
for q in questions:
|
31 |
+
tid, text = q.get("task_id"), q.get("question")
|
32 |
+
if not (tid and text):
|
|
|
33 |
continue
|
34 |
try:
|
35 |
+
ans = agent.run(text) # <-- positional!
|
36 |
except Exception as e:
|
37 |
ans = f"ERROR: {e}"
|
38 |
+
results.append({"Task ID": tid, "Question": text, "Answer": ans})
|
39 |
+
payload.append({"task_id": tid, "submitted_answer": ans})
|
40 |
+
|
41 |
+
if not payload:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return "Agent returned no answers.", pd.DataFrame(results)
|
43 |
|
44 |
+
# Submit
|
45 |
+
submission = {
|
46 |
"username": username,
|
47 |
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
48 |
+
"answers": payload
|
49 |
}
|
50 |
try:
|
51 |
+
resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
52 |
resp.raise_for_status()
|
53 |
data = resp.json()
|
54 |
status = (
|
55 |
f"Submission Successful!\n"
|
56 |
+
f"User: {data['username']}\n"
|
57 |
+
f"Score: {data['score']}% ({data['correct_count']}/{data['total_attempted']})\n"
|
58 |
+
f"Message: {data['message']}"
|
59 |
)
|
60 |
except Exception as e:
|
61 |
status = f"Submission Failed: {e}"
|
|
|
64 |
|
65 |
|
66 |
def test_random_question(profile):
|
|
|
|
|
|
|
67 |
if not profile:
|
68 |
return "Please login to Hugging Face with the login button.", ""
|
69 |
+
q = fetch_random_question()
|
70 |
+
agent = create_agent()
|
71 |
+
# Positional call again
|
72 |
+
answer = agent.run(q["question"])
|
73 |
+
return q["question"], answer
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
|
|
|
76 |
with gr.Blocks() as demo:
|
77 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
78 |
gr.Markdown(
|
|
|
85 |
)
|
86 |
|
87 |
login = gr.LoginButton()
|
88 |
+
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
89 |
+
test_btn = gr.Button("Test Random Question")
|
90 |
|
91 |
+
status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
92 |
+
table_out = gr.DataFrame(label="Full Results Table", wrap=True)
|
93 |
+
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
94 |
+
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
|
|
|
|
|
|
95 |
|
96 |
+
run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_out, table_out])
|
97 |
+
test_btn.click(fn=test_random_question, inputs=[login], outputs=[question_out, answer_out])
|
|
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
demo.launch(debug=True, share=False)
|