Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,217 +3,204 @@ 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 |
-
|
| 17 |
-
|
| 18 |
-
if not OPENAI_API_KEY:
|
| 19 |
-
raise RuntimeError(
|
| 20 |
-
"Please set the OPENAI_API_KEY environment variable (e.g. export OPENAI_API_KEY=sk-β¦)"
|
| 21 |
-
)
|
| 22 |
-
openai.api_key = OPENAI_API_KEY
|
| 23 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
class SpeechToTextTool(PipelineTool):
|
| 29 |
-
"""
|
| 30 |
-
Transcribes an audio file to text using the OpenAI Whisper API.
|
| 31 |
-
Only local file paths are supported.
|
| 32 |
-
"""
|
| 33 |
default_checkpoint = "openai/whisper-1"
|
| 34 |
-
description = (
|
| 35 |
-
"This tool sends an audio file to OpenAI Whisper and returns the "
|
| 36 |
-
"transcribed text."
|
| 37 |
-
)
|
| 38 |
name = "transcriber"
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"type": "string",
|
| 42 |
-
"description": "Absolute or relative path to a local audio file.",
|
| 43 |
-
}
|
| 44 |
-
}
|
| 45 |
output_type = "string"
|
| 46 |
-
|
| 47 |
def __call__(self, audio: str) -> str:
|
| 48 |
return self._transcribe(audio)
|
| 49 |
-
|
| 50 |
@staticmethod
|
| 51 |
def _transcribe(audio_path: str) -> str:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
response = openai.audio.transcriptions.create(
|
| 59 |
-
file=fp,
|
| 60 |
-
model="whisper-1",
|
| 61 |
-
response_format="text"
|
| 62 |
)
|
| 63 |
-
return response
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
class ExcelToTextTool(Tool):
|
| 66 |
-
"""Render an Excel worksheet as Markdown text."""
|
| 67 |
name = "excel_to_text"
|
| 68 |
-
description =
|
| 69 |
-
"Read an Excel file and return a Markdown table of the requested sheet. "
|
| 70 |
-
"Accepts either the sheet name or the zero-based index."
|
| 71 |
-
)
|
| 72 |
inputs = {
|
| 73 |
-
"excel_path": {
|
| 74 |
-
"type": "string",
|
| 75 |
-
"description": "Path to the Excel file (.xlsx / .xls).",
|
| 76 |
-
},
|
| 77 |
"sheet_name": {
|
| 78 |
"type": "string",
|
| 79 |
-
"description": (
|
| 80 |
-
"Worksheet name or zeroβbased index *as a string* (optional; default first sheet)."
|
| 81 |
-
),
|
| 82 |
"nullable": True,
|
| 83 |
},
|
| 84 |
}
|
| 85 |
output_type = "string"
|
| 86 |
-
|
| 87 |
-
def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
|
| 88 |
path = pathlib.Path(excel_path).expanduser().resolve()
|
| 89 |
if not path.exists():
|
| 90 |
return f"Error: Excel file not found at {path}"
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
-
df = pd.read_excel(path, sheet_name=
|
| 94 |
-
if hasattr(
|
| 95 |
return df.to_markdown(index=False)
|
|
|
|
| 96 |
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
| 97 |
-
except Exception as
|
| 98 |
-
return f"Error reading Excel file: {
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
| 101 |
url = f"{base_api_url}/files/{task_id}"
|
| 102 |
resp = requests.get(url, timeout=30)
|
| 103 |
if resp.status_code == 404:
|
| 104 |
return None
|
| 105 |
resp.raise_for_status()
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
if "filename=" in
|
| 109 |
-
m = re.search(r'filename="([^"]+)"',
|
| 110 |
if m:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
return str(file_path)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
class BasicAgent:
|
| 120 |
def __init__(self):
|
| 121 |
self.agent = CodeAgent(
|
| 122 |
model=OpenAIServerModel(model_id="gpt-3.5-turbo"),
|
| 123 |
tools=[
|
| 124 |
-
|
| 125 |
WikipediaSearchTool(),
|
| 126 |
SpeechToTextTool(),
|
| 127 |
-
ExcelToTextTool()
|
| 128 |
],
|
| 129 |
add_base_tools=True,
|
| 130 |
-
additional_authorized_imports=[
|
| 131 |
-
|
| 132 |
-
|
|
|
|
| 133 |
)
|
| 134 |
-
print("BasicAgent initialized.")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
return answer
|
| 141 |
|
| 142 |
def run_and_submit_all(username: str):
|
| 143 |
-
if not username:
|
| 144 |
-
return "
|
| 145 |
|
| 146 |
api_url = DEFAULT_API_URL
|
| 147 |
questions_url = f"{api_url}/questions"
|
| 148 |
submit_url = f"{api_url}/submit"
|
| 149 |
|
| 150 |
try:
|
| 151 |
-
|
| 152 |
except Exception as e:
|
| 153 |
return f"Error initializing agent: {e}", None
|
| 154 |
|
| 155 |
-
agent_code = "https://huggingface.co/spaces/l3xv/Final_Assignment_Template/tree/main"
|
| 156 |
-
|
| 157 |
try:
|
| 158 |
resp = requests.get(questions_url, timeout=15)
|
| 159 |
resp.raise_for_status()
|
| 160 |
-
|
| 161 |
except Exception as e:
|
| 162 |
return f"Error fetching questions: {e}", None
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
for item in
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
if not (
|
| 170 |
continue
|
|
|
|
| 171 |
try:
|
| 172 |
-
|
| 173 |
-
except
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
ans =
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
submission = {
|
| 188 |
-
"username": username.strip(),
|
| 189 |
-
"agent_code": agent_code,
|
| 190 |
-
"answers": payload
|
| 191 |
-
}
|
| 192 |
try:
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
res =
|
| 196 |
status = (
|
| 197 |
f"Submission Successful!\n"
|
| 198 |
-
f"
|
| 199 |
-
f"
|
|
|
|
| 200 |
)
|
| 201 |
except Exception as e:
|
| 202 |
status = f"Submission Failed: {e}"
|
| 203 |
|
| 204 |
-
return status, pd.DataFrame(
|
| 205 |
|
|
|
|
|
|
|
|
|
|
| 206 |
with gr.Blocks() as demo:
|
| 207 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
table = gr.DataFrame(label="Results")
|
| 212 |
|
| 213 |
-
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
print("Launching GAIA Agent Appβ¦")
|
| 217 |
demo.launch(debug=True, share=False)
|
| 218 |
|
| 219 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import openai
|
|
|
|
|
|
|
| 6 |
import tempfile
|
|
|
|
| 7 |
import pathlib
|
|
|
|
|
|
|
|
|
|
| 8 |
import re
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
| 11 |
|
| 12 |
+
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
| 13 |
+
from smolagents.tools import Tool, PipelineTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
+
# 1) Oneβshot DuckDuckGo wrapper (never retries on 202)
|
| 17 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
+
class OneShotSearch(DuckDuckGoSearchTool):
|
| 19 |
+
def __call__(self, query: str) -> str:
|
| 20 |
+
try:
|
| 21 |
+
return super().__call__(query)
|
| 22 |
+
except Exception:
|
| 23 |
+
return ""
|
| 24 |
|
| 25 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
+
# 2) Whisperβbased audio β text
|
| 27 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
class SpeechToTextTool(PipelineTool):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
default_checkpoint = "openai/whisper-1"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
name = "transcriber"
|
| 31 |
+
description = "Transcribe a local audio file via OpenAI Whisper."
|
| 32 |
+
inputs = {"audio": {"type": "string", "description": "Local audio file path."}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
output_type = "string"
|
|
|
|
| 34 |
def __call__(self, audio: str) -> str:
|
| 35 |
return self._transcribe(audio)
|
|
|
|
| 36 |
@staticmethod
|
| 37 |
def _transcribe(audio_path: str) -> str:
|
| 38 |
+
p = pathlib.Path(audio_path).expanduser().resolve()
|
| 39 |
+
if not p.is_file():
|
| 40 |
+
raise FileNotFoundError(f"No such audio file: {p}")
|
| 41 |
+
with p.open("rb") as fp:
|
| 42 |
+
return openai.audio.transcriptions.create(
|
| 43 |
+
file=fp, model="whisper-1", response_format="text"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
|
|
|
| 45 |
|
| 46 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
# 3) Excel β Markdown table
|
| 48 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
class ExcelToTextTool(Tool):
|
|
|
|
| 50 |
name = "excel_to_text"
|
| 51 |
+
description = "Load an .xlsx/.xls and render a sheet as a Markdown table."
|
|
|
|
|
|
|
|
|
|
| 52 |
inputs = {
|
| 53 |
+
"excel_path": {"type": "string", "description": "Path to .xlsx/.xls file"},
|
|
|
|
|
|
|
|
|
|
| 54 |
"sheet_name": {
|
| 55 |
"type": "string",
|
| 56 |
+
"description": "Sheet name or zero-based index (as string).",
|
|
|
|
|
|
|
| 57 |
"nullable": True,
|
| 58 |
},
|
| 59 |
}
|
| 60 |
output_type = "string"
|
| 61 |
+
def forward(self, excel_path: str, sheet_name: str | None = None) -> str:
|
|
|
|
| 62 |
path = pathlib.Path(excel_path).expanduser().resolve()
|
| 63 |
if not path.exists():
|
| 64 |
return f"Error: Excel file not found at {path}"
|
| 65 |
try:
|
| 66 |
+
idx = int(sheet_name) if sheet_name and sheet_name.isdigit() else sheet_name or 0
|
| 67 |
+
df = pd.read_excel(path, sheet_name=idx)
|
| 68 |
+
if hasattr(df, "to_markdown"):
|
| 69 |
return df.to_markdown(index=False)
|
| 70 |
+
from tabulate import tabulate
|
| 71 |
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"Error reading Excel file: {e}"
|
| 74 |
|
| 75 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
+
# 4) Helper to download any attached file
|
| 77 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
| 79 |
url = f"{base_api_url}/files/{task_id}"
|
| 80 |
resp = requests.get(url, timeout=30)
|
| 81 |
if resp.status_code == 404:
|
| 82 |
return None
|
| 83 |
resp.raise_for_status()
|
| 84 |
+
disp = resp.headers.get("content-disposition", "")
|
| 85 |
+
fn = task_id
|
| 86 |
+
if "filename=" in disp:
|
| 87 |
+
m = re.search(r'filename="([^"]+)"', disp)
|
| 88 |
if m:
|
| 89 |
+
fn = m.group(1)
|
| 90 |
+
tmp = pathlib.Path(tempfile.gettempdir()) / "gaia_files"
|
| 91 |
+
tmp.mkdir(exist_ok=True)
|
| 92 |
+
fp = tmp / fn
|
| 93 |
+
fp.write_bytes(resp.content)
|
| 94 |
+
return str(fp)
|
|
|
|
| 95 |
|
| 96 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 97 |
+
# 5) Timeout wrapper around agent.run()
|
| 98 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 99 |
+
def safe_run(agent: CodeAgent, question: str, timeout: float = 20.0) -> str:
|
| 100 |
+
with ThreadPoolExecutor(max_workers=1) as exe:
|
| 101 |
+
fut = exe.submit(agent.run, question)
|
| 102 |
+
try:
|
| 103 |
+
return fut.result(timeout=timeout)
|
| 104 |
+
except (TimeoutError, Exception):
|
| 105 |
+
return "N/A"
|
| 106 |
+
|
| 107 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 108 |
+
# 6) Your BasicAgent, with max_steps and our custom tools
|
| 109 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 110 |
class BasicAgent:
|
| 111 |
def __init__(self):
|
| 112 |
self.agent = CodeAgent(
|
| 113 |
model=OpenAIServerModel(model_id="gpt-3.5-turbo"),
|
| 114 |
tools=[
|
| 115 |
+
OneShotSearch(),
|
| 116 |
WikipediaSearchTool(),
|
| 117 |
SpeechToTextTool(),
|
| 118 |
+
ExcelToTextTool(),
|
| 119 |
],
|
| 120 |
add_base_tools=True,
|
| 121 |
+
additional_authorized_imports=[
|
| 122 |
+
"pandas", "numpy", "math", "re", "csv", "subprocess"
|
| 123 |
+
],
|
| 124 |
+
max_steps=4,
|
| 125 |
)
|
|
|
|
| 126 |
|
| 127 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 128 |
+
# 7) Gradio runner (no login button, just a username box)
|
| 129 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 130 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 131 |
|
| 132 |
def run_and_submit_all(username: str):
|
| 133 |
+
if not username.strip():
|
| 134 |
+
return "Error: please enter your Hugging Face username.", None
|
| 135 |
|
| 136 |
api_url = DEFAULT_API_URL
|
| 137 |
questions_url = f"{api_url}/questions"
|
| 138 |
submit_url = f"{api_url}/submit"
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
agent_wrapper = BasicAgent()
|
| 142 |
except Exception as e:
|
| 143 |
return f"Error initializing agent: {e}", None
|
| 144 |
|
|
|
|
|
|
|
| 145 |
try:
|
| 146 |
resp = requests.get(questions_url, timeout=15)
|
| 147 |
resp.raise_for_status()
|
| 148 |
+
qs = resp.json()
|
| 149 |
except Exception as e:
|
| 150 |
return f"Error fetching questions: {e}", None
|
| 151 |
|
| 152 |
+
answers = []
|
| 153 |
+
log = []
|
| 154 |
+
for item in qs:
|
| 155 |
+
tid = item.get("task_id")
|
| 156 |
+
qtxt = item.get("question")
|
| 157 |
+
if not (tid and qtxt):
|
| 158 |
continue
|
| 159 |
+
fp = None
|
| 160 |
try:
|
| 161 |
+
fp = download_file_if_any(api_url, tid)
|
| 162 |
+
except:
|
| 163 |
+
pass
|
| 164 |
+
prompt = f"{qtxt}\n\n---\nFile at: {fp}\n---\n" if fp else qtxt
|
| 165 |
+
ans = safe_run(agent_wrapper.agent, prompt, timeout=20.0)
|
| 166 |
+
if not ans:
|
| 167 |
+
ans = "N/A"
|
| 168 |
+
answers.append({"task_id": tid, "submitted_answer": ans})
|
| 169 |
+
log.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
|
| 170 |
+
|
| 171 |
+
if not answers:
|
| 172 |
+
return "Agent produced no answers.", pd.DataFrame(log)
|
| 173 |
+
|
| 174 |
+
submission = {"username": username.strip(), "agent_code": "", "answers": answers}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
+
r2 = requests.post(submit_url, json=submission, timeout=60)
|
| 177 |
+
r2.raise_for_status()
|
| 178 |
+
res = r2.json()
|
| 179 |
status = (
|
| 180 |
f"Submission Successful!\n"
|
| 181 |
+
f"User: {res.get('username')}\n"
|
| 182 |
+
f"Score: {res.get('score')}% "
|
| 183 |
+
f"({res.get('correct_count')}/{res.get('total_attempted')})"
|
| 184 |
)
|
| 185 |
except Exception as e:
|
| 186 |
status = f"Submission Failed: {e}"
|
| 187 |
|
| 188 |
+
return status, pd.DataFrame(log)
|
| 189 |
|
| 190 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 191 |
+
# 8) Gradio UI
|
| 192 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 193 |
with gr.Blocks() as demo:
|
| 194 |
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 195 |
+
gr.Markdown("Enter your HF username and click **Run & Submit**")
|
| 196 |
+
username = gr.Textbox(label="Username", placeholder="huggingface.co username")
|
| 197 |
+
runbtn = gr.Button("Run & Submit")
|
| 198 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 199 |
table = gr.DataFrame(label="Results")
|
| 200 |
|
| 201 |
+
runbtn.click(fn=run_and_submit_all, inputs=[username], outputs=[status, table])
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
|
|
|
| 204 |
demo.launch(debug=True, share=False)
|
| 205 |
|
| 206 |
|