Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,32 +3,86 @@ import os
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import pandas as pd
|
|
|
|
|
6 |
|
7 |
-
from tools import AnswerTool
|
8 |
from smolagents import CodeAgent, OpenAIServerModel
|
9 |
from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool
|
10 |
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
class BasicAgent:
|
15 |
def __init__(self):
|
16 |
-
# Initialize CodeAgent with GPT-4o,
|
17 |
-
model
|
18 |
-
wiki_tool = WikipediaSearchTool()
|
19 |
-
web_tool = DuckDuckGoSearchTool()
|
20 |
answer_tool = AnswerTool()
|
|
|
|
|
|
|
|
|
|
|
21 |
self.agent = CodeAgent(
|
22 |
model=model,
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
max_steps=
|
27 |
-
|
|
|
28 |
)
|
29 |
|
30 |
-
def __call__(self, question: str) -> str:
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
|
34 |
def run_and_submit_all(username):
|
@@ -45,17 +99,19 @@ def run_and_submit_all(username):
|
|
45 |
except Exception as e:
|
46 |
return f"Error fetching questions: {e}", None
|
47 |
|
48 |
-
# 2.
|
49 |
-
agent
|
50 |
results = []
|
51 |
payload = []
|
|
|
|
|
52 |
for q in questions:
|
53 |
-
tid
|
54 |
text = q.get("question")
|
55 |
if not (tid and text):
|
56 |
continue
|
57 |
try:
|
58 |
-
ans = agent(text)
|
59 |
except Exception as e:
|
60 |
ans = f"ERROR: {e}"
|
61 |
results.append({"Task ID": tid, "Question": text, "Answer": ans})
|
@@ -64,11 +120,11 @@ def run_and_submit_all(username):
|
|
64 |
if not payload:
|
65 |
return "Agent returned no answers.", pd.DataFrame(results)
|
66 |
|
67 |
-
#
|
68 |
submission = {
|
69 |
-
"username":
|
70 |
-
"agent_code":
|
71 |
-
"answers":
|
72 |
}
|
73 |
try:
|
74 |
sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
@@ -92,12 +148,11 @@ def test_random_question(username):
|
|
92 |
try:
|
93 |
q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
|
94 |
question = q.get("question", "")
|
95 |
-
ans
|
96 |
return question, ans
|
97 |
except Exception as e:
|
98 |
return f"Error during test: {e}", ""
|
99 |
|
100 |
-
|
101 |
# --- Gradio UI ---
|
102 |
with gr.Blocks() as demo:
|
103 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
@@ -111,17 +166,18 @@ with gr.Blocks() as demo:
|
|
111 |
)
|
112 |
|
113 |
username_input = gr.Textbox(label="Hugging Face Username", placeholder="your-username")
|
114 |
-
run_btn
|
115 |
-
test_btn
|
116 |
|
117 |
-
status_out
|
118 |
-
table_out
|
119 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
120 |
-
answer_out
|
121 |
|
122 |
-
run_btn.click(fn=run_and_submit_all,
|
123 |
test_btn.click(fn=test_random_question, inputs=[username_input], outputs=[question_out, answer_out])
|
124 |
|
125 |
if __name__ == "__main__":
|
126 |
demo.launch(debug=True, share=False)
|
127 |
|
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
+
import tempfile
|
7 |
+
from pathlib import Path
|
8 |
|
9 |
+
from tools import AnswerTool, SpeechToTextTool, ExcelToTextTool
|
10 |
from smolagents import CodeAgent, OpenAIServerModel
|
11 |
from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool
|
12 |
|
13 |
# --- Constants ---
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
16 |
+
|
17 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
18 |
+
"""
|
19 |
+
Try GET /files/{task_id}.
|
20 |
+
• On HTTP 200 → save to a temp dir and return local path.
|
21 |
+
• On 404 → return None.
|
22 |
+
• On other errors → raise so caller can log / handle.
|
23 |
+
"""
|
24 |
+
url = f"{base_api_url}/files/{task_id}"
|
25 |
+
try:
|
26 |
+
resp = requests.get(url, timeout=30)
|
27 |
+
if resp.status_code == 404:
|
28 |
+
return None
|
29 |
+
resp.raise_for_status()
|
30 |
+
except requests.exceptions.HTTPError as e:
|
31 |
+
raise e
|
32 |
+
|
33 |
+
# Determine filename from headers or default to task_id
|
34 |
+
cd = resp.headers.get("content-disposition", "")
|
35 |
+
filename = task_id
|
36 |
+
if "filename=" in cd:
|
37 |
+
import re
|
38 |
+
m = re.search(r'filename="([^"]+)"', cd)
|
39 |
+
if m:
|
40 |
+
filename = m.group(1)
|
41 |
+
|
42 |
+
# Save to temp dir
|
43 |
+
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
44 |
+
tmp_dir.mkdir(exist_ok=True)
|
45 |
+
file_path = tmp_dir / filename
|
46 |
+
with open(file_path, "wb") as f:
|
47 |
+
f.write(resp.content)
|
48 |
+
return str(file_path)
|
49 |
+
|
50 |
+
|
51 |
class BasicAgent:
|
52 |
def __init__(self):
|
53 |
+
# Initialize CodeAgent with GPT-4o, file/audio/excel, web and wiki tools, plus final answer
|
54 |
+
model = OpenAIServerModel(model_id="gpt-4o")
|
|
|
|
|
55 |
answer_tool = AnswerTool()
|
56 |
+
speech_tool = SpeechToTextTool()
|
57 |
+
excel_tool = ExcelToTextTool()
|
58 |
+
web_tool = DuckDuckGoSearchTool()
|
59 |
+
wiki_tool = WikipediaSearchTool()
|
60 |
+
|
61 |
self.agent = CodeAgent(
|
62 |
model=model,
|
63 |
+
tools=[speech_tool, excel_tool, wiki_tool, web_tool, answer_tool],
|
64 |
+
add_base_tools=False,
|
65 |
+
additional_authorized_imports=["pandas", "openpyxl"],
|
66 |
+
max_steps=4,
|
67 |
+
planning_interval=1,
|
68 |
+
verbosity_level=1
|
69 |
)
|
70 |
|
71 |
+
def __call__(self, task_id: str, question: str) -> str:
|
72 |
+
# Pre-fetch any file for this task
|
73 |
+
file_path = None
|
74 |
+
try:
|
75 |
+
file_path = download_file_if_any(DEFAULT_API_URL, task_id)
|
76 |
+
except Exception:
|
77 |
+
pass
|
78 |
+
|
79 |
+
# Build prompt including file context if any
|
80 |
+
if file_path:
|
81 |
+
prompt = f"{question}\n\n---\nA file for this task was downloaded and saved at: {file_path}\n---"
|
82 |
+
else:
|
83 |
+
prompt = question
|
84 |
+
|
85 |
+
return self.agent.run(prompt)
|
86 |
|
87 |
|
88 |
def run_and_submit_all(username):
|
|
|
99 |
except Exception as e:
|
100 |
return f"Error fetching questions: {e}", None
|
101 |
|
102 |
+
# 2. Instantiate agent
|
103 |
+
agent = BasicAgent()
|
104 |
results = []
|
105 |
payload = []
|
106 |
+
|
107 |
+
# 3. Run agent on all questions
|
108 |
for q in questions:
|
109 |
+
tid = q.get("task_id")
|
110 |
text = q.get("question")
|
111 |
if not (tid and text):
|
112 |
continue
|
113 |
try:
|
114 |
+
ans = agent(tid, text)
|
115 |
except Exception as e:
|
116 |
ans = f"ERROR: {e}"
|
117 |
results.append({"Task ID": tid, "Question": text, "Answer": ans})
|
|
|
120 |
if not payload:
|
121 |
return "Agent returned no answers.", pd.DataFrame(results)
|
122 |
|
123 |
+
# 4. Submit answers
|
124 |
submission = {
|
125 |
+
"username": username,
|
126 |
+
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
127 |
+
"answers": payload
|
128 |
}
|
129 |
try:
|
130 |
sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
|
|
148 |
try:
|
149 |
q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
|
150 |
question = q.get("question", "")
|
151 |
+
ans = BasicAgent()(q.get('task_id'), question)
|
152 |
return question, ans
|
153 |
except Exception as e:
|
154 |
return f"Error during test: {e}", ""
|
155 |
|
|
|
156 |
# --- Gradio UI ---
|
157 |
with gr.Blocks() as demo:
|
158 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
166 |
)
|
167 |
|
168 |
username_input = gr.Textbox(label="Hugging Face Username", placeholder="your-username")
|
169 |
+
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
170 |
+
test_btn = gr.Button("Test Random Question")
|
171 |
|
172 |
+
status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
173 |
+
table_out = gr.DataFrame(label="Full Results Table", wrap=True)
|
174 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
175 |
+
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
176 |
|
177 |
+
run_btn.click(fn=run_and_submit_all, inputs=[username_input], outputs=[status_out, table_out])
|
178 |
test_btn.click(fn=test_random_question, inputs=[username_input], outputs=[question_out, answer_out])
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
demo.launch(debug=True, share=False)
|
182 |
|
183 |
+
|