Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
from agent import answer_question
|
6 |
+
|
7 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
+
|
9 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
10 |
+
if not profile:
|
11 |
+
return "Please Login to Hugging Face with the button.", None
|
12 |
+
|
13 |
+
username = profile.username
|
14 |
+
space_id = os.getenv("SPACE_ID", "YOUR_SPACE_ID")
|
15 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
16 |
+
|
17 |
+
api_url = DEFAULT_API_URL
|
18 |
+
questions_url = f"{api_url}/questions"
|
19 |
+
submit_url = f"{api_url}/submit"
|
20 |
+
|
21 |
+
# Fetch questions
|
22 |
+
try:
|
23 |
+
response = requests.get(questions_url, timeout=15)
|
24 |
+
response.raise_for_status()
|
25 |
+
questions_data = response.json()
|
26 |
+
if not questions_data:
|
27 |
+
return "Fetched questions list is empty or invalid format.", None
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error fetching questions: {e}", None
|
30 |
+
|
31 |
+
# Run agent on questions
|
32 |
+
results_log = []
|
33 |
+
answers_payload = []
|
34 |
+
for item in questions_data:
|
35 |
+
task_id = item.get("task_id")
|
36 |
+
question_text = item.get("question")
|
37 |
+
if not task_id or not question_text:
|
38 |
+
continue
|
39 |
+
try:
|
40 |
+
submitted_answer = answer_question(question_text)
|
41 |
+
# Remove "FINAL ANSWER: " if not required by API (check course instructions)
|
42 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
43 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
44 |
+
except Exception as e:
|
45 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
46 |
+
|
47 |
+
if not answers_payload:
|
48 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
49 |
+
|
50 |
+
# Submit answers
|
51 |
+
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
52 |
+
try:
|
53 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
54 |
+
response.raise_for_status()
|
55 |
+
result_data = response.json()
|
56 |
+
final_status = (
|
57 |
+
f"Submission Successful!\n"
|
58 |
+
f"User: {result_data.get('username')}\n"
|
59 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
60 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
61 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
62 |
+
)
|
63 |
+
return final_status, pd.DataFrame(results_log)
|
64 |
+
except Exception as e:
|
65 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
66 |
+
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown("# smolagents GAIA Benchmark Submission")
|
69 |
+
gr.LoginButton()
|
70 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
71 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5)
|
72 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
73 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch(debug=True, share=False)
|