Spaces:
Runtime error
Runtime error
Deploy GAIA agent
Browse files
app.py
CHANGED
@@ -6,22 +6,19 @@ import requests
|
|
6 |
import pandas as pd
|
7 |
|
8 |
from smolagents import CodeAgent, tool
|
9 |
-
from
|
10 |
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
-
# ---
|
15 |
@tool
|
16 |
def simple_search(query: str) -> str:
|
17 |
"""
|
18 |
-
Perform a basic DuckDuckGo search.
|
19 |
-
|
20 |
Args:
|
21 |
-
query (str): Search query.
|
22 |
-
|
23 |
Returns:
|
24 |
-
str: Top 3
|
25 |
"""
|
26 |
try:
|
27 |
resp = requests.get(
|
@@ -33,15 +30,14 @@ def simple_search(query: str) -> str:
|
|
33 |
from bs4 import BeautifulSoup
|
34 |
soup = BeautifulSoup(resp.text, "html.parser")
|
35 |
items = soup.select("a.result__a")[:3]
|
36 |
-
|
37 |
-
return "\n\n".join(results) if results else "No results found."
|
38 |
except Exception as e:
|
39 |
return f"Search error: {e}"
|
40 |
|
41 |
-
# --- Enhanced Agent using
|
42 |
class BasicAgent:
|
43 |
def __init__(self):
|
44 |
-
print("BasicAgent initialized with
|
45 |
self.model = LiteLLMModel(
|
46 |
model_id="tiiuae/falcon-7b-instruct",
|
47 |
max_tokens=512,
|
@@ -53,17 +49,17 @@ class BasicAgent:
|
|
53 |
)
|
54 |
|
55 |
def __call__(self, question: str) -> str:
|
56 |
-
print(f"
|
57 |
try:
|
58 |
return self.agent.run(question)
|
59 |
except Exception as e:
|
60 |
return f"Agent error: {e}"
|
61 |
|
62 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
63 |
-
space_id = os.getenv("SPACE_ID")
|
64 |
if not profile:
|
65 |
-
return "Please log in to Hugging Face to submit.", None
|
66 |
username = profile.username
|
|
|
67 |
|
68 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
69 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
@@ -71,35 +67,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
71 |
try:
|
72 |
agent = BasicAgent()
|
73 |
except Exception as e:
|
74 |
-
return f"Agent
|
75 |
|
76 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
77 |
|
78 |
try:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
except Exception as e:
|
83 |
-
return f"
|
84 |
|
85 |
-
logs,
|
86 |
-
for item in
|
87 |
-
|
88 |
-
|
89 |
-
if not
|
90 |
continue
|
91 |
-
ans = agent(
|
92 |
-
|
93 |
-
logs.append({"Task ID":
|
94 |
|
95 |
-
if not
|
96 |
-
return "
|
97 |
|
98 |
-
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
data =
|
103 |
status = (
|
104 |
f"✅ Submission Successful!\n"
|
105 |
f"Score: {data.get('score','N/A')}% "
|
@@ -108,17 +104,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
108 |
)
|
109 |
return status, pd.DataFrame(logs)
|
110 |
except Exception as e:
|
111 |
-
return f"
|
112 |
|
113 |
-
# --- Gradio
|
114 |
with gr.Blocks() as demo:
|
115 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
116 |
gr.LoginButton()
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
print("Launching Gradio app...")
|
|
|
6 |
import pandas as pd
|
7 |
|
8 |
from smolagents import CodeAgent, tool
|
9 |
+
from smolagents.models import LiteLLMModel # ✅ correct import
|
10 |
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
+
# --- Simple Web Search Tool ---
|
15 |
@tool
|
16 |
def simple_search(query: str) -> str:
|
17 |
"""
|
|
|
|
|
18 |
Args:
|
19 |
+
query (str): Search query text.
|
|
|
20 |
Returns:
|
21 |
+
str: Top 3 DuckDuckGo search result titles & links.
|
22 |
"""
|
23 |
try:
|
24 |
resp = requests.get(
|
|
|
30 |
from bs4 import BeautifulSoup
|
31 |
soup = BeautifulSoup(resp.text, "html.parser")
|
32 |
items = soup.select("a.result__a")[:3]
|
33 |
+
return "\n\n".join(f"{a.get_text()}\n{a['href']}" for a in items) or "No results found."
|
|
|
34 |
except Exception as e:
|
35 |
return f"Search error: {e}"
|
36 |
|
37 |
+
# --- Enhanced Agent using Light Model ---
|
38 |
class BasicAgent:
|
39 |
def __init__(self):
|
40 |
+
print("BasicAgent initialized with LiteLLMModel (falcon-7b-instruct).")
|
41 |
self.model = LiteLLMModel(
|
42 |
model_id="tiiuae/falcon-7b-instruct",
|
43 |
max_tokens=512,
|
|
|
49 |
)
|
50 |
|
51 |
def __call__(self, question: str) -> str:
|
52 |
+
print(f"Question: {question[:60]}...")
|
53 |
try:
|
54 |
return self.agent.run(question)
|
55 |
except Exception as e:
|
56 |
return f"Agent error: {e}"
|
57 |
|
58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
59 |
if not profile:
|
60 |
+
return "Please log in to Hugging Face to submit answers.", None
|
61 |
username = profile.username
|
62 |
+
space_id = os.getenv("SPACE_ID", "")
|
63 |
|
64 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
65 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
|
|
67 |
try:
|
68 |
agent = BasicAgent()
|
69 |
except Exception as e:
|
70 |
+
return f"Agent initialization failed: {e}", None
|
71 |
|
72 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
73 |
|
74 |
try:
|
75 |
+
r = requests.get(questions_url, timeout=15)
|
76 |
+
r.raise_for_status()
|
77 |
+
questions = r.json()
|
78 |
except Exception as e:
|
79 |
+
return f"Error fetching questions: {e}", None
|
80 |
|
81 |
+
logs, answers = [], []
|
82 |
+
for item in questions:
|
83 |
+
task_id = item.get("task_id")
|
84 |
+
question = item.get("question")
|
85 |
+
if not task_id or question is None:
|
86 |
continue
|
87 |
+
ans = agent(question)
|
88 |
+
answers.append({"task_id": task_id, "submitted_answer": ans})
|
89 |
+
logs.append({"Task ID": task_id, "Question": question, "Submitted Answer": ans})
|
90 |
|
91 |
+
if not answers:
|
92 |
+
return "Agent produced no answers.", pd.DataFrame(logs)
|
93 |
|
94 |
+
payload = {"username": username, "agent_code": agent_code, "answers": answers}
|
95 |
try:
|
96 |
+
resp = requests.post(submit_url, json=payload, timeout=60)
|
97 |
+
resp.raise_for_status()
|
98 |
+
data = resp.json()
|
99 |
status = (
|
100 |
f"✅ Submission Successful!\n"
|
101 |
f"Score: {data.get('score','N/A')}% "
|
|
|
104 |
)
|
105 |
return status, pd.DataFrame(logs)
|
106 |
except Exception as e:
|
107 |
+
return f"Submission failed: {e}", pd.DataFrame(logs)
|
108 |
|
109 |
+
# --- Gradio Interface ---
|
110 |
with gr.Blocks() as demo:
|
111 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
112 |
gr.LoginButton()
|
113 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
114 |
+
status_box = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
|
115 |
+
result_table = gr.DataFrame(label="Questions & Agent Answers", wrap=True)
|
116 |
|
117 |
+
run_button.click(run_and_submit_all, outputs=[status_box, result_table])
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
print("Launching Gradio app...")
|