Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,64 +9,55 @@ from agent import create_agent, fetch_random_question
|
|
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 SmolAgent on them, submit all answers,
|
15 |
and display the results.
|
16 |
"""
|
17 |
space_id = os.getenv("SPACE_ID")
|
18 |
|
19 |
-
if profile:
|
20 |
-
username = profile.username
|
21 |
-
print(f"User logged in: {username}")
|
22 |
-
else:
|
23 |
-
print("User not logged in.")
|
24 |
return "Please login to Hugging Face with the button.", None
|
|
|
25 |
|
26 |
-
|
27 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
28 |
-
|
29 |
try:
|
30 |
agent = create_agent()
|
31 |
-
print("SmolAgent initialized.")
|
32 |
except Exception as e:
|
33 |
-
print(f"Error instantiating agent: {e}")
|
34 |
return f"Error initializing agent: {e}", None
|
35 |
|
36 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
37 |
-
print(f"Agent code URL: {agent_code}")
|
38 |
|
|
|
39 |
try:
|
40 |
-
response = requests.get(
|
41 |
response.raise_for_status()
|
42 |
questions = response.json()
|
43 |
-
if not questions:
|
44 |
-
return "No questions fetched.", None
|
45 |
-
print(f"Fetched {len(questions)} questions.")
|
46 |
except Exception as e:
|
47 |
-
print(f"Error fetching questions: {e}")
|
48 |
return f"Error fetching questions: {e}", None
|
49 |
|
|
|
|
|
|
|
|
|
50 |
results = []
|
51 |
payload = []
|
52 |
for q in questions:
|
53 |
-
|
54 |
-
|
55 |
-
if not
|
56 |
continue
|
57 |
try:
|
58 |
-
|
59 |
except Exception as e:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
submission = {"username": username, "agent_code": agent_code, "answers": payload}
|
68 |
try:
|
69 |
-
resp = requests.post(
|
70 |
resp.raise_for_status()
|
71 |
data = resp.json()
|
72 |
status = (
|
@@ -76,17 +67,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
76 |
f"Message: {data.get('message')}"
|
77 |
)
|
78 |
except Exception as e:
|
79 |
-
print(f"Submission error: {e}")
|
80 |
status = f"Submission Failed: {e}"
|
81 |
|
82 |
return status, pd.DataFrame(results)
|
83 |
|
84 |
|
85 |
-
def test_random_question(profile
|
86 |
"""
|
87 |
-
Fetch a random GAIA question and return the agent
|
88 |
"""
|
89 |
-
if
|
90 |
return "Please login to test.", ""
|
91 |
try:
|
92 |
q = fetch_random_question()
|
@@ -94,30 +84,32 @@ def test_random_question(profile: gr.OAuthProfile | None):
|
|
94 |
ans = agent.run(question=q.get("question", ""))
|
95 |
return q.get("question", ""), ans
|
96 |
except Exception as e:
|
97 |
-
print(f"Test error: {e}")
|
98 |
return f"Error: {e}", ""
|
99 |
|
|
|
100 |
# --- Gradio Interface ---
|
101 |
with gr.Blocks() as demo:
|
102 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
103 |
gr.Markdown(
|
104 |
"""
|
105 |
-
**
|
106 |
-
1. Clone
|
107 |
-
2.
|
108 |
-
3.
|
109 |
"""
|
110 |
)
|
|
|
111 |
login = gr.LoginButton()
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch(debug=True, share=False)
|
|
|
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 SmolAgent on them, submit all answers,
|
15 |
and display the results.
|
16 |
"""
|
17 |
space_id = os.getenv("SPACE_ID")
|
18 |
|
19 |
+
if profile is None:
|
|
|
|
|
|
|
|
|
20 |
return "Please login to Hugging Face with the button.", None
|
21 |
+
username = profile.username
|
22 |
|
23 |
+
# Instantiate the agent
|
|
|
|
|
24 |
try:
|
25 |
agent = create_agent()
|
|
|
26 |
except Exception as e:
|
|
|
27 |
return f"Error initializing agent: {e}", None
|
28 |
|
29 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
30 |
|
31 |
+
# Fetch questions
|
32 |
try:
|
33 |
+
response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
34 |
response.raise_for_status()
|
35 |
questions = response.json()
|
|
|
|
|
|
|
36 |
except Exception as e:
|
|
|
37 |
return f"Error fetching questions: {e}", None
|
38 |
|
39 |
+
if not questions:
|
40 |
+
return "No questions fetched.", None
|
41 |
+
|
42 |
+
# Run agent on each question
|
43 |
results = []
|
44 |
payload = []
|
45 |
for q in questions:
|
46 |
+
task_id = q.get("task_id")
|
47 |
+
question_text = q.get("question")
|
48 |
+
if not task_id or not question_text:
|
49 |
continue
|
50 |
try:
|
51 |
+
answer = agent.run(question=question_text)
|
52 |
except Exception as e:
|
53 |
+
answer = f"ERROR: {e}"
|
54 |
+
results.append({"Task ID": task_id, "Question": question_text, "Answer": answer})
|
55 |
+
payload.append({"task_id": task_id, "submitted_answer": answer})
|
56 |
|
57 |
+
# Submit answers
|
58 |
+
submit_payload = {"username": username, "agent_code": agent_code, "answers": payload}
|
|
|
|
|
59 |
try:
|
60 |
+
resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submit_payload, timeout=60)
|
61 |
resp.raise_for_status()
|
62 |
data = resp.json()
|
63 |
status = (
|
|
|
67 |
f"Message: {data.get('message')}"
|
68 |
)
|
69 |
except Exception as e:
|
|
|
70 |
status = f"Submission Failed: {e}"
|
71 |
|
72 |
return status, pd.DataFrame(results)
|
73 |
|
74 |
|
75 |
+
def test_random_question(profile):
|
76 |
"""
|
77 |
+
Fetch a random GAIA question and return its answer by the agent.
|
78 |
"""
|
79 |
+
if profile is None:
|
80 |
return "Please login to test.", ""
|
81 |
try:
|
82 |
q = fetch_random_question()
|
|
|
84 |
ans = agent.run(question=q.get("question", ""))
|
85 |
return q.get("question", ""), ans
|
86 |
except Exception as e:
|
|
|
87 |
return f"Error: {e}", ""
|
88 |
|
89 |
+
|
90 |
# --- Gradio Interface ---
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
93 |
gr.Markdown(
|
94 |
"""
|
95 |
+
**Instructions:**
|
96 |
+
1. Clone this space and define your agent logic in agent.py.
|
97 |
+
2. Log in with your Hugging Face account.
|
98 |
+
3. Use 'Run Evaluation & Submit All Answers' or 'Test Random Question'.
|
99 |
"""
|
100 |
)
|
101 |
+
|
102 |
login = gr.LoginButton()
|
103 |
+
run_all_btn = gr.Button("Run Evaluation & Submit All Answers")
|
104 |
+
test_btn = gr.Button("Test Random Question")
|
105 |
+
|
106 |
+
status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
107 |
+
table = gr.DataFrame(label="Full Results Table", wrap=True)
|
108 |
+
question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
109 |
+
answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
110 |
+
|
111 |
+
run_all_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_output, table])
|
112 |
+
test_btn.click(fn=test_random_question, inputs=[login], outputs=[question_box, answer_box])
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
demo.launch(debug=True, share=False)
|