Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,81 @@
|
|
1 |
import os
|
2 |
-
import gradio as gr
|
3 |
import requests
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
"""
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return f"Error fetching questions: {e}", None
|
38 |
|
39 |
-
if not questions:
|
40 |
-
return "No questions fetched.", None
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
resp.raise_for_status()
|
62 |
-
data = resp.json()
|
63 |
-
status = (
|
64 |
-
f"Submission Successful!\n"
|
65 |
-
f"User: {data.get('username')}\n"
|
66 |
-
f"Score: {data.get('score')}% ({data.get('correct_count')}/{data.get('total_attempted')})\n"
|
67 |
-
f"Message: {data.get('message')}"
|
68 |
-
)
|
69 |
-
except Exception as e:
|
70 |
-
status = f"Submission Failed: {e}"
|
71 |
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
-
def
|
76 |
-
"""
|
77 |
-
Fetch a random GAIA question and return its answer by the agent.
|
78 |
"""
|
79 |
-
|
80 |
-
|
81 |
-
try:
|
82 |
-
q = fetch_random_question()
|
83 |
-
agent = create_agent()
|
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 |
-
|
91 |
-
with
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
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)
|
|
|
1 |
import os
|
|
|
2 |
import requests
|
3 |
+
from smolagents import CodeAgent, tool, OpenAIServerModel
|
4 |
+
|
5 |
+
# ------------------------
|
6 |
+
# Constants
|
7 |
+
# ------------------------
|
8 |
+
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
+
|
10 |
+
# ------------------------
|
11 |
+
# Tool definitions
|
12 |
+
# ------------------------
|
13 |
+
@tool
|
14 |
+
def fetch_questions() -> list:
|
15 |
"""
|
16 |
+
Fetch the full list of GAIA evaluation questions.
|
17 |
|
18 |
+
Returns:
|
19 |
+
list: A list of question dicts, each with 'task_id' and 'question'.
|
20 |
+
"""
|
21 |
+
resp = requests.get(f"{API_URL}/questions", timeout=15)
|
22 |
+
resp.raise_for_status()
|
23 |
+
return resp.json()
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
@tool
|
27 |
+
def fetch_random_question() -> dict:
|
28 |
+
"""
|
29 |
+
Fetch a single random GAIA question.
|
30 |
|
31 |
+
Returns:
|
32 |
+
dict: A dict with keys 'task_id' and 'question'.
|
33 |
+
"""
|
34 |
+
resp = requests.get(f"{API_URL}/random-question", timeout=15)
|
35 |
+
resp.raise_for_status()
|
36 |
+
return resp.json()
|
|
|
37 |
|
|
|
|
|
38 |
|
39 |
+
@tool
|
40 |
+
def submit_answers(username: str, agent_code: str, answers: list) -> dict:
|
41 |
+
"""
|
42 |
+
Submit the agent's answers to GAIA and get the scoring.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
Args:
|
45 |
+
username (str): The Hugging Face username identifying the submission.
|
46 |
+
agent_code (str): URL to your Space code repository for verification.
|
47 |
+
answers (list): A list of dicts, each with 'task_id' and 'submitted_answer'.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
Returns:
|
50 |
+
dict: A dict containing 'score', 'correct_count', 'total_attempted', 'message', etc.
|
51 |
+
"""
|
52 |
+
payload = {
|
53 |
+
"username": username,
|
54 |
+
"agent_code": agent_code,
|
55 |
+
"answers": answers
|
56 |
+
}
|
57 |
+
resp = requests.post(f"{API_URL}/submit", json=payload, timeout=60)
|
58 |
+
resp.raise_for_status()
|
59 |
+
return resp.json()
|
60 |
|
61 |
|
62 |
+
def create_agent() -> CodeAgent:
|
|
|
|
|
63 |
"""
|
64 |
+
Build and return a configured CodeAgent using OpenAI GPT-3.5 Turbo.
|
65 |
+
Requires OPENAI_API_KEY in the environment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
Returns:
|
68 |
+
CodeAgent: An instance of CodeAgent configured with the GAIA tools.
|
69 |
+
"""
|
70 |
+
# Use 'model_id' to match the OpenAIServerModel signature
|
71 |
+
model = OpenAIServerModel(model_id="gpt-3.5-turbo")
|
72 |
+
agent = CodeAgent(
|
73 |
+
tools=[fetch_questions, fetch_random_question, submit_answers],
|
74 |
+
model=model,
|
75 |
+
prompt_template=(
|
76 |
+
"Here is a GAIA question:\n"
|
77 |
+
"{question}\n"
|
78 |
+
"Provide ONLY the exact answer (exact-match), with no extra text."
|
79 |
+
)
|
80 |
)
|
81 |
+
return agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|