Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,264 +1,210 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
-
import re
|
| 4 |
-
import tempfile
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from typing import Optional, Union
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
-
import openai
|
| 10 |
-
import pandas as pd
|
| 11 |
import requests
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# smolagents core
|
| 15 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
|
|
|
| 23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
def download_file_if_any(base_api_url: str, task_id: str) -> Optional[str]:
|
| 27 |
-
"""
|
| 28 |
-
Try GET /files/{task_id}.
|
| 29 |
-
• 200 → save to temp dir and return local path
|
| 30 |
-
• 404 → return None
|
| 31 |
-
• other errors → raise
|
| 32 |
-
"""
|
| 33 |
-
url = f"{base_api_url}/files/{task_id}"
|
| 34 |
-
resp = requests.get(url, timeout=30)
|
| 35 |
-
if resp.status_code == 404:
|
| 36 |
-
return None
|
| 37 |
-
resp.raise_for_status()
|
| 38 |
-
cdisp = resp.headers.get("content-disposition", "")
|
| 39 |
-
filename = task_id
|
| 40 |
-
m = re.search(r'filename="([^"]+)"', cdisp)
|
| 41 |
-
if m:
|
| 42 |
-
filename = m.group(1)
|
| 43 |
-
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
| 44 |
-
tmp_dir.mkdir(exist_ok=True)
|
| 45 |
-
path = tmp_dir / filename
|
| 46 |
-
with open(path, "wb") as f:
|
| 47 |
-
f.write(resp.content)
|
| 48 |
-
return str(path)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 52 |
-
# Custom Tools
|
| 53 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 54 |
-
|
| 55 |
class SpeechToTextTool(PipelineTool):
|
| 56 |
"""
|
| 57 |
-
Transcribes an audio file to text using OpenAI Whisper.
|
| 58 |
Only local file paths are supported.
|
| 59 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
name = "transcriber"
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
output_type = "string"
|
| 64 |
|
| 65 |
def __call__(self, audio: str) -> str:
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if not path.is_file():
|
| 68 |
-
|
| 69 |
with path.open("rb") as fp:
|
| 70 |
-
|
| 71 |
-
file=fp,
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
-
|
| 74 |
|
| 75 |
class ExcelToTextTool(Tool):
|
| 76 |
-
"""
|
| 77 |
-
Read an Excel file and return a Markdown table of a sheet.
|
| 78 |
-
"""
|
| 79 |
name = "excel_to_text"
|
| 80 |
-
description =
|
|
|
|
|
|
|
|
|
|
| 81 |
inputs = {
|
| 82 |
-
"excel_path": {
|
|
|
|
|
|
|
|
|
|
| 83 |
"sheet_name": {
|
| 84 |
"type": "string",
|
| 85 |
-
"description":
|
|
|
|
|
|
|
| 86 |
"nullable": True,
|
| 87 |
},
|
| 88 |
}
|
| 89 |
output_type = "string"
|
| 90 |
|
| 91 |
-
def forward(
|
| 92 |
-
|
| 93 |
-
) -> str:
|
| 94 |
-
path = Path(excel_path).expanduser().resolve()
|
| 95 |
if not path.exists():
|
| 96 |
return f"Error: Excel file not found at {path}"
|
| 97 |
try:
|
| 98 |
-
if
|
| 99 |
sheet = 0
|
| 100 |
-
elif sheet_name.isdigit():
|
| 101 |
-
sheet = int(sheet_name)
|
| 102 |
else:
|
| 103 |
-
sheet = sheet_name
|
| 104 |
-
df = pd.read_excel(path, sheet_name=sheet
|
| 105 |
-
if hasattr(
|
| 106 |
return df.to_markdown(index=False)
|
| 107 |
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
| 108 |
-
except Exception as
|
| 109 |
-
return f"Error reading Excel file: {
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 113 |
-
# SYSTEM_PROMPT
|
| 114 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 115 |
-
|
| 116 |
-
SYSTEM_PROMPT = """You are GAIA, a precision‑focused AI agent with access to these tools:
|
| 117 |
-
|
| 118 |
-
Tools:
|
| 119 |
-
• calculator(expression: str) → evaluates arithmetic only (digits, + - * / ^ ( ) .), returns a number as string
|
| 120 |
-
• wikipedia_search(query: str) → fetches a two‑sentence summary from Wikipedia
|
| 121 |
-
• DuckDuckGoSearchTool() → general web search, returns top text results
|
| 122 |
-
• PythonInterpreterTool() → runs arbitrary Python for complex data tasks
|
| 123 |
-
• transcriber(audio: str) → transcribes a local audio file via Whisper
|
| 124 |
-
• excel_to_text(excel_path: str, sheet_name: str) → renders an Excel sheet to Markdown
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
class
|
| 142 |
-
def __init__(self
|
| 143 |
-
self.model = OpenAIServerModel(model_id=model_id)
|
| 144 |
-
python_tool = PythonInterpreterTool(
|
| 145 |
-
additional_authorized_imports=["pandas", "numpy", "json", "os", "requests"]
|
| 146 |
-
)
|
| 147 |
-
tools = [
|
| 148 |
-
DuckDuckGoSearchTool(),
|
| 149 |
-
WikipediaSearchTool(),
|
| 150 |
-
SpeechToTextTool(),
|
| 151 |
-
ExcelToTextTool(),
|
| 152 |
-
python_tool,
|
| 153 |
-
]
|
| 154 |
self.agent = CodeAgent(
|
| 155 |
-
model=
|
| 156 |
-
tools=
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
add_base_tools=True,
|
|
|
|
| 159 |
)
|
|
|
|
| 160 |
|
| 161 |
def __call__(self, question: str) -> str:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Evaluation & Submission Logic
|
| 167 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 168 |
|
| 169 |
def run_and_submit_all(username: str):
|
| 170 |
-
"""
|
| 171 |
-
1. Fetch questions
|
| 172 |
-
2. Run GAIAAgent on each
|
| 173 |
-
3. Submit answers
|
| 174 |
-
"""
|
| 175 |
-
username = username.strip()
|
| 176 |
if not username:
|
| 177 |
-
return "Please
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
# 1. Fetch questions
|
| 180 |
try:
|
| 181 |
-
|
| 182 |
except Exception as e:
|
| 183 |
-
return f"Error
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
# 2. Instantiate agent
|
| 186 |
try:
|
| 187 |
-
|
|
|
|
|
|
|
| 188 |
except Exception as e:
|
| 189 |
-
return f"Error
|
| 190 |
|
| 191 |
-
|
| 192 |
payload = []
|
| 193 |
-
|
| 194 |
-
# 3. Run agent on each question
|
| 195 |
for item in questions:
|
| 196 |
-
|
| 197 |
q = item.get("question")
|
| 198 |
-
if not
|
| 199 |
continue
|
| 200 |
-
|
| 201 |
-
fp = None
|
| 202 |
try:
|
| 203 |
-
|
| 204 |
-
except Exception:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
prompt = q
|
| 208 |
-
if fp:
|
| 209 |
-
prompt += f"\n\n---\nA file was saved at: {fp}\n---\n"
|
| 210 |
-
|
| 211 |
try:
|
| 212 |
ans = agent(prompt)
|
| 213 |
except Exception as e:
|
| 214 |
ans = f"AGENT ERROR: {e}"
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
results.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
|
| 218 |
|
| 219 |
if not payload:
|
| 220 |
-
return "No answers
|
| 221 |
|
| 222 |
-
|
| 223 |
-
submission = {
|
| 224 |
-
"username": username,
|
| 225 |
-
"agent_code": "https://huggingface.co/spaces/your-space/tree/main",
|
| 226 |
-
"answers": payload,
|
| 227 |
-
}
|
| 228 |
try:
|
| 229 |
-
|
| 230 |
-
|
|
|
|
| 231 |
status = (
|
| 232 |
f"Submission Successful!\n"
|
| 233 |
-
f"
|
| 234 |
-
f"
|
| 235 |
-
f"({data.get('correct_count','?')}/{data.get('total_attempted','?')})"
|
| 236 |
)
|
| 237 |
except Exception as e:
|
| 238 |
status = f"Submission Failed: {e}"
|
| 239 |
|
| 240 |
-
return status, pd.DataFrame(
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 244 |
-
# Gradio Interface
|
| 245 |
-
# ──────────────────────────────────────────────────────────────────────────────
|
| 246 |
|
| 247 |
with gr.Blocks() as demo:
|
| 248 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 249 |
-
gr.
|
| 250 |
-
username_input = gr.Textbox(label="Username", placeholder="Your HF username")
|
| 251 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
run_btn.click(
|
| 256 |
-
fn=run_and_submit_all,
|
| 257 |
-
inputs=[username_input],
|
| 258 |
-
outputs=[status_box, results_tbl],
|
| 259 |
-
)
|
| 260 |
|
| 261 |
if __name__ == "__main__":
|
| 262 |
print("Launching GAIA Agent App…")
|
| 263 |
demo.launch(debug=True, share=False)
|
| 264 |
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
+
import openai
|
|
|
|
|
|
|
| 6 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import tempfile
|
| 9 |
+
from smolagents.tools import PipelineTool, Tool
|
| 10 |
+
import pathlib
|
| 11 |
+
from typing import Union, Optional
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from tabulate import tabulate # pragma: no cover – fallback path
|
| 14 |
+
import re
|
| 15 |
|
| 16 |
+
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class SpeechToTextTool(PipelineTool):
|
| 20 |
"""
|
| 21 |
+
Transcribes an audio file to text using the OpenAI Whisper API.
|
| 22 |
Only local file paths are supported.
|
| 23 |
"""
|
| 24 |
+
default_checkpoint = "openai/whisper-1"
|
| 25 |
+
description = (
|
| 26 |
+
"This tool sends an audio file to OpenAI Whisper and returns the "
|
| 27 |
+
"transcribed text."
|
| 28 |
+
)
|
| 29 |
name = "transcriber"
|
| 30 |
+
inputs = {
|
| 31 |
+
"audio": {
|
| 32 |
+
"type": "string",
|
| 33 |
+
"description": "Absolute or relative path to a local audio file.",
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
output_type = "string"
|
| 37 |
|
| 38 |
def __call__(self, audio: str) -> str:
|
| 39 |
+
return self._transcribe(audio)
|
| 40 |
+
|
| 41 |
+
@staticmethod
|
| 42 |
+
def _transcribe(audio_path: str) -> str:
|
| 43 |
+
if not isinstance(audio_path, str):
|
| 44 |
+
raise TypeError("Parameter 'audio' must be a string containing the file path.")
|
| 45 |
+
path = Path(audio_path).expanduser().resolve()
|
| 46 |
if not path.is_file():
|
| 47 |
+
raise FileNotFoundError(f"No such audio file: {path}")
|
| 48 |
with path.open("rb") as fp:
|
| 49 |
+
response = openai.audio.transcriptions.create(
|
| 50 |
+
file=fp,
|
| 51 |
+
model="whisper-1",
|
| 52 |
+
response_format="text"
|
| 53 |
)
|
| 54 |
+
return response
|
| 55 |
|
| 56 |
class ExcelToTextTool(Tool):
|
| 57 |
+
"""Render an Excel worksheet as Markdown text."""
|
|
|
|
|
|
|
| 58 |
name = "excel_to_text"
|
| 59 |
+
description = (
|
| 60 |
+
"Read an Excel file and return a Markdown table of the requested sheet. "
|
| 61 |
+
"Accepts either the sheet name or the zero-based index."
|
| 62 |
+
)
|
| 63 |
inputs = {
|
| 64 |
+
"excel_path": {
|
| 65 |
+
"type": "string",
|
| 66 |
+
"description": "Path to the Excel file (.xlsx / .xls).",
|
| 67 |
+
},
|
| 68 |
"sheet_name": {
|
| 69 |
"type": "string",
|
| 70 |
+
"description": (
|
| 71 |
+
"Worksheet name or zero‑based index *as a string* (optional; default first sheet)."
|
| 72 |
+
),
|
| 73 |
"nullable": True,
|
| 74 |
},
|
| 75 |
}
|
| 76 |
output_type = "string"
|
| 77 |
|
| 78 |
+
def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
|
| 79 |
+
path = pathlib.Path(excel_path).expanduser().resolve()
|
|
|
|
|
|
|
| 80 |
if not path.exists():
|
| 81 |
return f"Error: Excel file not found at {path}"
|
| 82 |
try:
|
| 83 |
+
if sheet_name is None or sheet_name == "":
|
| 84 |
sheet = 0
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
+
sheet = int(sheet_name) if sheet_name.isdigit() else sheet_name
|
| 87 |
+
df = pd.read_excel(path, sheet_name=sheet)
|
| 88 |
+
if hasattr(pd.DataFrame, "to_markdown"):
|
| 89 |
return df.to_markdown(index=False)
|
| 90 |
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
| 91 |
+
except Exception as exc:
|
| 92 |
+
return f"Error reading Excel file: {exc}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
| 95 |
+
url = f"{base_api_url}/files/{task_id}"
|
| 96 |
+
try:
|
| 97 |
+
resp = requests.get(url, timeout=30)
|
| 98 |
+
if resp.status_code == 404:
|
| 99 |
+
return None
|
| 100 |
+
resp.raise_for_status()
|
| 101 |
+
except requests.exceptions.HTTPError:
|
| 102 |
+
raise
|
| 103 |
+
cdisp = resp.headers.get("content-disposition", "")
|
| 104 |
+
filename = task_id
|
| 105 |
+
if "filename=" in cdisp:
|
| 106 |
+
m = re.search(r'filename="([^"]+)"', cdisp)
|
| 107 |
+
if m:
|
| 108 |
+
filename = m.group(1)
|
| 109 |
+
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
| 110 |
+
tmp_dir.mkdir(exist_ok=True)
|
| 111 |
+
file_path = tmp_dir / filename
|
| 112 |
+
with open(file_path, "wb") as f:
|
| 113 |
+
f.write(resp.content)
|
| 114 |
+
return str(file_path)
|
| 115 |
|
| 116 |
+
class BasicAgent:
|
| 117 |
+
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
self.agent = CodeAgent(
|
| 119 |
+
model=OpenAIServerModel(model_id="gpt-4o"),
|
| 120 |
+
tools=[
|
| 121 |
+
DuckDuckGoSearchTool(),
|
| 122 |
+
WikipediaSearchTool(),
|
| 123 |
+
SpeechToTextTool(),
|
| 124 |
+
ExcelToTextTool()
|
| 125 |
+
],
|
| 126 |
add_base_tools=True,
|
| 127 |
+
additional_authorized_imports=['pandas','numpy','csv','subprocess']
|
| 128 |
)
|
| 129 |
+
print("BasicAgent initialized.")
|
| 130 |
|
| 131 |
def __call__(self, question: str) -> str:
|
| 132 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 133 |
+
answer = self.agent.run(question)
|
| 134 |
+
print(f"Agent returning answer: {answer}")
|
| 135 |
+
return answer
|
|
|
|
|
|
|
| 136 |
|
| 137 |
def run_and_submit_all(username: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
if not username:
|
| 139 |
+
return "Please provide your username.", None
|
| 140 |
+
|
| 141 |
+
api_url = DEFAULT_API_URL
|
| 142 |
+
questions_url = f"{api_url}/questions"
|
| 143 |
+
submit_url = f"{api_url}/submit"
|
| 144 |
|
|
|
|
| 145 |
try:
|
| 146 |
+
agent = BasicAgent()
|
| 147 |
except Exception as e:
|
| 148 |
+
return f"Error initializing agent: {e}", None
|
| 149 |
+
|
| 150 |
+
agent_code = "https://huggingface.co/spaces/l3xv/Final_Assignment_Template/tree/main"
|
| 151 |
|
|
|
|
| 152 |
try:
|
| 153 |
+
resp = requests.get(questions_url, timeout=15)
|
| 154 |
+
resp.raise_for_status()
|
| 155 |
+
questions = resp.json()
|
| 156 |
except Exception as e:
|
| 157 |
+
return f"Error fetching questions: {e}", None
|
| 158 |
|
| 159 |
+
results_log = []
|
| 160 |
payload = []
|
|
|
|
|
|
|
| 161 |
for item in questions:
|
| 162 |
+
task_id = item.get("task_id")
|
| 163 |
q = item.get("question")
|
| 164 |
+
if not (task_id and q):
|
| 165 |
continue
|
| 166 |
+
file_path = None
|
|
|
|
| 167 |
try:
|
| 168 |
+
file_path = download_file_if_any(api_url, task_id)
|
| 169 |
+
except Exception as fe:
|
| 170 |
+
print(f"[file fetch error] {task_id}: {fe}")
|
| 171 |
+
prompt = q + (f"\n\n---\nFile at: {file_path}\n---\n\n" if file_path else "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
try:
|
| 173 |
ans = agent(prompt)
|
| 174 |
except Exception as e:
|
| 175 |
ans = f"AGENT ERROR: {e}"
|
| 176 |
+
payload.append({"task_id": task_id, "submitted_answer": ans})
|
| 177 |
+
results_log.append({"Task ID": task_id, "Question": q, "Answer": ans})
|
|
|
|
| 178 |
|
| 179 |
if not payload:
|
| 180 |
+
return "No answers generated.", pd.DataFrame(results_log)
|
| 181 |
|
| 182 |
+
sub = {"username": username.strip(), "agent_code": agent_code, "answers": payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
try:
|
| 184 |
+
r = requests.post(submit_url, json=sub, timeout=60)
|
| 185 |
+
r.raise_for_status()
|
| 186 |
+
res = r.json()
|
| 187 |
status = (
|
| 188 |
f"Submission Successful!\n"
|
| 189 |
+
f"Score: {res.get('score', 'N/A')}% "
|
| 190 |
+
f"({res.get('correct_count','?')}/{res.get('total_attempted','?')})"
|
|
|
|
| 191 |
)
|
| 192 |
except Exception as e:
|
| 193 |
status = f"Submission Failed: {e}"
|
| 194 |
|
| 195 |
+
return status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
with gr.Blocks() as demo:
|
| 198 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 199 |
+
username = gr.Textbox(label="Username", placeholder="Your HF username")
|
|
|
|
| 200 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 201 |
+
status = gr.Textbox(label="Status / Submission Result", interactive=False)
|
| 202 |
+
table = gr.DataFrame(label="Results")
|
| 203 |
|
| 204 |
+
run_btn.click(fn=run_and_submit_all, inputs=[username], outputs=[status, table])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
print("Launching GAIA Agent App…")
|
| 208 |
demo.launch(debug=True, share=False)
|
| 209 |
|
| 210 |
+
|