Spaces:
Runtime error
Runtime error
Deploy GAIA agent
Browse files
app.py
CHANGED
@@ -3,126 +3,113 @@
|
|
3 |
import os
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
-
import inspect
|
7 |
import pandas as pd
|
|
|
8 |
|
9 |
-
|
10 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
|
11 |
|
12 |
# --- Constants ---
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
14 |
|
15 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
-
print("BasicAgent initialized
|
19 |
-
|
20 |
-
# Initialize tools and model
|
21 |
-
self.search_tool = DuckDuckGoSearchTool()
|
22 |
self.model = InferenceClientModel()
|
23 |
self.agent = CodeAgent(
|
24 |
model=self.model,
|
25 |
-
tools=[
|
26 |
)
|
27 |
|
28 |
def __call__(self, question: str) -> str:
|
29 |
-
print(
|
30 |
try:
|
31 |
-
|
32 |
-
print(f"Agent response (first 50 chars): {response[:50]}...")
|
33 |
-
return response
|
34 |
except Exception as e:
|
35 |
-
|
36 |
-
return f"Error in agent: {e}"
|
37 |
|
38 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
39 |
-
"""
|
40 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
41 |
-
and displays the results.
|
42 |
-
"""
|
43 |
space_id = os.getenv("SPACE_ID")
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
print(f"User logged in: {username}")
|
48 |
-
else:
|
49 |
-
print("User not logged in.")
|
50 |
-
return "Please login to Hugging Face to submit answers.", None
|
51 |
-
|
52 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
53 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
54 |
|
55 |
-
try:
|
56 |
-
agent = BasicAgent()
|
57 |
except Exception as e:
|
58 |
-
|
59 |
-
return f"Error initializing agent: {e}", None
|
60 |
|
61 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
62 |
-
print(agent_code)
|
63 |
-
|
64 |
-
# Fetch questions
|
65 |
try:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
if not questions_data:
|
70 |
-
return "Empty or invalid question list.", None
|
71 |
-
print(f"Fetched {len(questions_data)} questions.")
|
72 |
except Exception as e:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
if not task_id or question_text is None:
|
83 |
-
continue
|
84 |
-
try:
|
85 |
-
submitted = agent(question_text)
|
86 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted})
|
87 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted})
|
88 |
-
except Exception as e:
|
89 |
-
print(f"Error on task {task_id}: {e}")
|
90 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
91 |
|
92 |
-
if not
|
93 |
-
return "
|
94 |
|
95 |
-
|
96 |
-
payload = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
97 |
try:
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
f"
|
103 |
-
f"
|
104 |
-
f"
|
105 |
-
f"({result_json.get('correct_count', '?')}/{result_json.get('total_attempted', '?')} correct)\n"
|
106 |
-
f"Message: {result_json.get('message', '')}"
|
107 |
)
|
108 |
-
return
|
109 |
except Exception as e:
|
110 |
-
return f"
|
111 |
|
112 |
# --- Gradio UI ---
|
113 |
with gr.Blocks() as demo:
|
114 |
-
gr.Markdown("#
|
115 |
-
gr.Markdown("""
|
116 |
-
Modify `BasicAgent` to add more tools or logic.
|
117 |
-
Log in, click **Run Evaluation & Submit All Answers**, and watch it process automatically.
|
118 |
-
""")
|
119 |
gr.LoginButton()
|
120 |
-
run_btn = gr.Button("Run Evaluation & Submit
|
121 |
-
status = gr.Textbox(
|
122 |
-
results = gr.DataFrame(
|
123 |
-
|
124 |
-
run_btn.click(fn=run_and_submit_all, outputs=[status, results])
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
-
print("Launching app...")
|
128 |
demo.launch(debug=True, share=False)
|
|
|
3 |
import os
|
4 |
import gradio as gr
|
5 |
import requests
|
|
|
6 |
import pandas as pd
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
|
9 |
+
from smolagents import CodeAgent, tool, InferenceClientModel
|
|
|
10 |
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
+
# --- Custom Web Search Tool (avoiding proxies) ---
|
15 |
+
@tool
|
16 |
+
def simple_search(query: str) -> str:
|
17 |
+
"""
|
18 |
+
Search DuckDuckGo via HTML parsing.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
query (str): search query string.
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
str: top 3 titles and URLs.
|
25 |
+
"""
|
26 |
+
try:
|
27 |
+
resp = requests.get(
|
28 |
+
"https://html.duckduckgo.com/html/",
|
29 |
+
params={"q": query},
|
30 |
+
timeout=10
|
31 |
+
)
|
32 |
+
resp.raise_for_status()
|
33 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
34 |
+
results = []
|
35 |
+
for a in soup.select("a.result__a")[:3]:
|
36 |
+
title = a.get_text()
|
37 |
+
url = a["href"]
|
38 |
+
results.append(f"- {title}\n {url}")
|
39 |
+
return "\n".join(results) if results else "No results."
|
40 |
+
except Exception as e:
|
41 |
+
return f"Search error: {e}"
|
42 |
+
|
43 |
+
# --- Enhanced Agent ---
|
44 |
class BasicAgent:
|
45 |
def __init__(self):
|
46 |
+
print("BasicAgent initialized.")
|
|
|
|
|
|
|
47 |
self.model = InferenceClientModel()
|
48 |
self.agent = CodeAgent(
|
49 |
model=self.model,
|
50 |
+
tools=[simple_search]
|
51 |
)
|
52 |
|
53 |
def __call__(self, question: str) -> str:
|
54 |
+
print("Received:", question[:50], "...")
|
55 |
try:
|
56 |
+
return self.agent.run(question)
|
|
|
|
|
57 |
except Exception as e:
|
58 |
+
return f"Agent run error: {e}"
|
|
|
59 |
|
60 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
61 |
space_id = os.getenv("SPACE_ID")
|
62 |
+
if not profile:
|
63 |
+
return "Please login to Hugging Face", None
|
64 |
+
username = profile.username
|
|
|
|
|
|
|
|
|
|
|
65 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
66 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
67 |
|
68 |
+
try: agent = BasicAgent()
|
|
|
69 |
except Exception as e:
|
70 |
+
return f"Agent init error: {e}", None
|
|
|
71 |
|
72 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
|
|
|
73 |
try:
|
74 |
+
r = requests.get(questions_url, timeout=15)
|
75 |
+
r.raise_for_status()
|
76 |
+
qs = r.json()
|
|
|
|
|
|
|
77 |
except Exception as e:
|
78 |
+
return f"Fetch questions error: {e}", None
|
79 |
+
|
80 |
+
logs, payload = [], []
|
81 |
+
for item in qs:
|
82 |
+
tid, q = item.get("task_id"), item.get("question")
|
83 |
+
if not tid or q is None: continue
|
84 |
+
ans = agent(q)
|
85 |
+
payload.append({"task_id": tid, "submitted_answer": ans})
|
86 |
+
logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
if not payload:
|
89 |
+
return "No answers generated.", pd.DataFrame(logs)
|
90 |
|
91 |
+
submit = {"username": username, "agent_code": agent_code, "answers": payload}
|
|
|
92 |
try:
|
93 |
+
resp = requests.post(submit_url, json=submit, timeout=60)
|
94 |
+
resp.raise_for_status()
|
95 |
+
data = resp.json()
|
96 |
+
status = (
|
97 |
+
f"✅ Score: {data.get('score','N/A')}% "
|
98 |
+
f"({data.get('correct_count','?')}/{data.get('total_attempted','?')} correct)\n"
|
99 |
+
f"{data.get('message','No message')}"
|
|
|
|
|
100 |
)
|
101 |
+
return status, pd.DataFrame(logs)
|
102 |
except Exception as e:
|
103 |
+
return f"Submit error: {e}", pd.DataFrame(logs)
|
104 |
|
105 |
# --- Gradio UI ---
|
106 |
with gr.Blocks() as demo:
|
107 |
+
gr.Markdown("# GAIA Evaluation Runner")
|
|
|
|
|
|
|
|
|
108 |
gr.LoginButton()
|
109 |
+
run_btn = gr.Button("Run Evaluation & Submit")
|
110 |
+
status = gr.Textbox(lines=5, interactive=False)
|
111 |
+
results = gr.DataFrame(wrap=True)
|
112 |
+
run_btn.click(run_and_submit_all, outputs=[status, results])
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
|
|
115 |
demo.launch(debug=True, share=False)
|