Spaces:
Runtime error
Runtime error
fix
Browse files- app.py +339 -263
- requirements.txt +6 -1
app.py
CHANGED
|
@@ -5,337 +5,413 @@ import pandas as pd
|
|
| 5 |
import json
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
-
|
| 9 |
-
|
| 10 |
from io import BytesIO
|
| 11 |
from PIL import Image
|
| 12 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# --- Custom Tools ---
|
| 18 |
|
| 19 |
@tool
|
| 20 |
def serper_search(query: str) -> str:
|
| 21 |
-
"""
|
| 22 |
-
Search the web using Serper API for current information and specific queries.
|
| 23 |
-
|
| 24 |
-
Args:
|
| 25 |
-
query: The search query string.
|
| 26 |
-
|
| 27 |
-
Returns:
|
| 28 |
-
Search results as a formatted string.
|
| 29 |
-
"""
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
if not api_key:
|
| 33 |
-
return "SERPER_API_KEY environment variable not found"
|
| 34 |
-
url = "https://google.serper.dev/search"
|
| 35 |
-
payload = json.dumps({"q": query, "num": 10})
|
| 36 |
-
headers = {
|
| 37 |
-
'X-API-KEY': api_key,
|
| 38 |
-
'Content-Type': 'application/json'
|
| 39 |
-
}
|
| 40 |
-
response = requests.post(url, headers=headers, data=payload, timeout=30)
|
| 41 |
-
response.raise_for_status()
|
| 42 |
-
data = response.json()
|
| 43 |
-
results = []
|
| 44 |
-
# Process organic results
|
| 45 |
-
if 'organic' in data:
|
| 46 |
-
for item in data['organic'][:5]:
|
| 47 |
-
results.append(f"Title: {item.get('title', '')}\nSnippet: {item.get('snippet', '')}\nURL: {item.get('link', '')}\n")
|
| 48 |
-
# Add knowledge graph if available
|
| 49 |
-
if 'knowledgeGraph' in data:
|
| 50 |
-
kg = data['knowledgeGraph']
|
| 51 |
-
results.insert(0, f"Knowledge Graph: {kg.get('title', '')} - {kg.get('description', '')}\n")
|
| 52 |
-
return "\n".join(results) if results else "No results found"
|
| 53 |
except Exception as e:
|
| 54 |
return f"Search error: {str(e)}"
|
| 55 |
|
| 56 |
-
@
|
| 57 |
-
def
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
"""
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return f"
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
data = response.json()
|
| 85 |
-
results = []
|
| 86 |
-
for item in data.get('query', {}).get('search', []):
|
| 87 |
-
results.append(f"Title: {item['title']}\nSnippet: {item['snippet']}")
|
| 88 |
-
return "\n\n".join(results) if results else "No Wikipedia results found"
|
| 89 |
except Exception as e:
|
| 90 |
-
return f"Wikipedia
|
| 91 |
|
| 92 |
@tool
|
| 93 |
-
def
|
| 94 |
-
"""
|
| 95 |
-
Analyze YouTube videos to extract information from titles, descriptions, and comments.
|
| 96 |
-
|
| 97 |
-
Args:
|
| 98 |
-
url: YouTube video URL.
|
| 99 |
-
|
| 100 |
-
Returns:
|
| 101 |
-
Video information and analysis as a string.
|
| 102 |
-
"""
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
return "Invalid YouTube URL"
|
| 107 |
-
video_id = video_id_match.group(1)
|
| 108 |
-
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
|
| 109 |
-
response = requests.get(oembed_url, timeout=15)
|
| 110 |
-
if response.status_code == 200:
|
| 111 |
-
data = response.json()
|
| 112 |
-
result = f"Title: {data.get('title', '')}\nAuthor: {data.get('author_name', '')}\n"
|
| 113 |
-
# Try to get additional info by scraping (basic)
|
| 114 |
-
try:
|
| 115 |
-
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 116 |
-
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 117 |
-
page_response = requests.get(video_url, headers=headers, timeout=15)
|
| 118 |
-
if page_response.status_code == 200:
|
| 119 |
-
content = page_response.text
|
| 120 |
-
desc_match = re.search(r'"description":{"simpleText":"([^"]+)"', content)
|
| 121 |
-
if desc_match:
|
| 122 |
-
result += f"Description: {desc_match.group(1)}\n"
|
| 123 |
-
except Exception:
|
| 124 |
-
pass
|
| 125 |
-
return result
|
| 126 |
-
else:
|
| 127 |
-
return "Could not retrieve video information"
|
| 128 |
except Exception as e:
|
| 129 |
-
return f"
|
| 130 |
|
| 131 |
@tool
|
| 132 |
-
def
|
| 133 |
-
"""
|
| 134 |
-
Process text for various operations like reversing, parsing, and analyzing.
|
| 135 |
-
|
| 136 |
-
Args:
|
| 137 |
-
text: Text to process.
|
| 138 |
-
operation: Operation to perform (reverse, parse, analyze).
|
| 139 |
-
|
| 140 |
-
Returns:
|
| 141 |
-
Processed text result as a string.
|
| 142 |
-
"""
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
except Exception as e:
|
| 152 |
-
return f"
|
| 153 |
|
| 154 |
@tool
|
| 155 |
-
def
|
| 156 |
-
"""
|
| 157 |
-
Solve mathematical problems and analyze mathematical structures.
|
| 158 |
-
|
| 159 |
-
Args:
|
| 160 |
-
problem: Mathematical problem or structure to analyze.
|
| 161 |
-
|
| 162 |
-
Returns:
|
| 163 |
-
Mathematical analysis and solution as a string.
|
| 164 |
-
"""
|
| 165 |
try:
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
-
return f"
|
| 174 |
|
| 175 |
@tool
|
| 176 |
-
def
|
| 177 |
-
"""
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
"""
|
| 187 |
try:
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
-
return f"
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
|
|
|
| 204 |
def __init__(self):
|
| 205 |
-
print("Initializing GAIA Agent...")
|
|
|
|
|
|
|
| 206 |
try:
|
| 207 |
self.model = InferenceClientModel(
|
| 208 |
-
model_id="
|
| 209 |
-
token=os.getenv("HUGGINGFACE_INFERENCE_TOKEN")
|
|
|
|
| 210 |
)
|
| 211 |
-
except
|
| 212 |
-
print(f"Error initializing model: {e}")
|
| 213 |
self.model = InferenceClientModel(
|
| 214 |
-
model_id="
|
| 215 |
)
|
|
|
|
|
|
|
| 216 |
custom_tools = [
|
| 217 |
serper_search,
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
| 223 |
]
|
| 224 |
-
|
| 225 |
-
|
| 226 |
self.agent = CodeAgent(
|
| 227 |
-
tools=
|
| 228 |
-
model=self.model
|
|
|
|
| 229 |
)
|
| 230 |
-
|
|
|
|
| 231 |
|
| 232 |
def __call__(self, question: str) -> str:
|
| 233 |
-
print(f"
|
|
|
|
| 234 |
try:
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
if
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
elif "
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
except Exception as e:
|
| 267 |
-
print(f"Error
|
| 268 |
-
|
| 269 |
-
return serper_search(question)
|
| 270 |
-
except Exception:
|
| 271 |
-
return f"I encountered an error processing this question: {question}. Please try rephrasing or breaking it into smaller parts."
|
| 272 |
|
|
|
|
| 273 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 274 |
"""
|
| 275 |
-
Fetches
|
| 276 |
-
and displays the results.
|
| 277 |
-
|
| 278 |
-
Args:
|
| 279 |
-
profile: OAuth profile object for authentication.
|
| 280 |
-
|
| 281 |
-
Returns:
|
| 282 |
-
Tuple of (submission result message, result object or None).
|
| 283 |
"""
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
else:
|
| 289 |
-
print("User not logged in.")
|
| 290 |
-
return "Please Login to Hugging Face with the button.", None
|
| 291 |
api_url = DEFAULT_API_URL
|
| 292 |
questions_url = f"{api_url}/questions"
|
| 293 |
submit_url = f"{api_url}/submit"
|
| 294 |
-
|
|
|
|
| 295 |
try:
|
| 296 |
-
agent =
|
| 297 |
except Exception as e:
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
#
|
| 301 |
-
print(f"Fetching questions from: {questions_url}")
|
| 302 |
try:
|
| 303 |
response = requests.get(questions_url, timeout=15)
|
| 304 |
-
response.raise_for_status()
|
| 305 |
questions_data = response.json()
|
| 306 |
-
|
| 307 |
-
print("Fetched questions list is empty.")
|
| 308 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 309 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 310 |
-
except requests.exceptions.RequestException as e:
|
| 311 |
-
print(f"Error fetching questions: {e}")
|
| 312 |
-
return f"Error fetching questions: {e}", None
|
| 313 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 314 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 315 |
-
print(f"Response text: {response.text[:500]}")
|
| 316 |
-
return f"Error decoding server response for questions: {e}", None
|
| 317 |
except Exception as e:
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
#
|
| 321 |
-
|
|
|
|
|
|
|
| 322 |
for i, item in enumerate(questions_data):
|
| 323 |
task_id = item.get("task_id")
|
| 324 |
-
|
| 325 |
-
|
|
|
|
| 326 |
continue
|
|
|
|
|
|
|
| 327 |
try:
|
| 328 |
-
answer = agent(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
except Exception as e:
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
try:
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
result =
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
except Exception as e:
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
+
import base64
|
| 9 |
+
import numpy as np
|
| 10 |
from io import BytesIO
|
| 11 |
from PIL import Image
|
| 12 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
|
| 13 |
+
from typing import Dict, Any, List
|
| 14 |
+
import wikipediaapi
|
| 15 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 16 |
+
import whisper
|
| 17 |
+
import openpyxl
|
| 18 |
+
import ast
|
| 19 |
+
import io
|
| 20 |
+
import concurrent.futures
|
| 21 |
+
from functools import lru_cache
|
| 22 |
|
| 23 |
# --- Constants ---
|
| 24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 25 |
+
VEGETABLE_DB = ["broccoli", "celery", "lettuce", "sweet potato", "basil", "asparagus",
|
| 26 |
+
"brussels sprouts", "cabbage", "carrot", "cauliflower", "kale", "spinach"]
|
| 27 |
|
| 28 |
# --- Custom Tools ---
|
| 29 |
|
| 30 |
@tool
|
| 31 |
def serper_search(query: str) -> str:
|
| 32 |
+
"""Search the web using Serper API with result caching"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
+
return _cached_serper_search(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Search error: {str(e)}"
|
| 37 |
|
| 38 |
+
@lru_cache(maxsize=100)
|
| 39 |
+
def _cached_serper_search(query: str) -> str:
|
| 40 |
+
"""Cached implementation of Serper search"""
|
| 41 |
+
api_key = os.getenv("SERPER_API_KEY")
|
| 42 |
+
if not api_key:
|
| 43 |
+
return "SERPER_API_KEY missing"
|
| 44 |
+
|
| 45 |
+
url = "https://google.serper.dev/search"
|
| 46 |
+
payload = json.dumps({"q": query, "num": 10})
|
| 47 |
+
headers = {'X-API-KEY': api_key, 'Content-Type': 'application/json'}
|
| 48 |
+
response = requests.post(url, headers=headers, data=payload, timeout=30)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
|
| 51 |
+
data = response.json()
|
| 52 |
+
results = []
|
| 53 |
+
|
| 54 |
+
# Process knowledge graph
|
| 55 |
+
if 'knowledgeGraph' in data:
|
| 56 |
+
kg = data['knowledgeGraph']
|
| 57 |
+
results.append(f"Knowledge Graph: {kg.get('title', '')} - {kg.get('description', '')}")
|
| 58 |
+
|
| 59 |
+
# Process organic results
|
| 60 |
+
for item in data.get('organic', [])[:5]:
|
| 61 |
+
results.append(f"Title: {item.get('title', '')}\nSnippet: {item.get('snippet', '')}\nURL: {item.get('link', '')}")
|
| 62 |
+
|
| 63 |
+
return "\n\n".join(results) if results else "No results found"
|
| 64 |
|
| 65 |
+
@tool
|
| 66 |
+
def wikipedia_detailed(query: str, section: str = None) -> str:
|
| 67 |
+
"""Fetch detailed Wikipedia content with section extraction"""
|
| 68 |
try:
|
| 69 |
+
wiki_wiki = wikipediaapi.Wikipedia('en')
|
| 70 |
+
page = wiki_wiki.page(query)
|
| 71 |
+
|
| 72 |
+
if not page.exists():
|
| 73 |
+
return f"Wikipedia page '{query}' not found"
|
| 74 |
+
|
| 75 |
+
# Extract specific section if requested
|
| 76 |
+
if section:
|
| 77 |
+
section_content = page.section_by_title(section)
|
| 78 |
+
if section_content:
|
| 79 |
+
return section_content.text[:4000]
|
| 80 |
+
|
| 81 |
+
# Return summary + section list
|
| 82 |
+
sections = "\n".join([s.title for s in page.sections])
|
| 83 |
+
return f"Summary: {page.summary[:2000]}\n\nSections Available: {sections}"
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
return f"Wikipedia error: {str(e)}"
|
| 87 |
|
| 88 |
@tool
|
| 89 |
+
def youtube_transcript(video_id: str) -> str:
|
| 90 |
+
"""Get YouTube video transcript"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 93 |
+
return " ".join([entry['text'] for entry in transcript])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
+
return f"Transcript error: {str(e)}"
|
| 96 |
|
| 97 |
@tool
|
| 98 |
+
def transcribe_audio(audio_url: str) -> str:
|
| 99 |
+
"""Transcribe audio using Whisper"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
try:
|
| 101 |
+
response = requests.get(audio_url, timeout=30)
|
| 102 |
+
audio_data = io.BytesIO(response.content)
|
| 103 |
+
|
| 104 |
+
# Load whisper model (base is smallest)
|
| 105 |
+
model = whisper.load_model("base")
|
| 106 |
+
result = model.transcribe(audio_data)
|
| 107 |
+
return result["text"]
|
| 108 |
except Exception as e:
|
| 109 |
+
return f"Transcription error: {str(e)}"
|
| 110 |
|
| 111 |
@tool
|
| 112 |
+
def analyze_operation_table(table_md: str) -> str:
|
| 113 |
+
"""Parse markdown tables and check commutativity"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
try:
|
| 115 |
+
# Parse markdown table
|
| 116 |
+
lines = table_md.strip().split('\n')
|
| 117 |
+
headers = [h.strip() for h in lines[1].split('|')[1:-1]]
|
| 118 |
+
matrix = {}
|
| 119 |
+
|
| 120 |
+
# Build operation matrix
|
| 121 |
+
for line in lines[3:]:
|
| 122 |
+
cells = [c.strip() for c in line.split('|')[1:-1]]
|
| 123 |
+
if len(cells) != len(headers):
|
| 124 |
+
continue
|
| 125 |
+
row_header = cells[0]
|
| 126 |
+
matrix[row_header] = {headers[i]: cells[i] for i in range(1, len(headers))}
|
| 127 |
+
|
| 128 |
+
# Find non-commutative pairs
|
| 129 |
+
counter_examples = set()
|
| 130 |
+
for a in headers:
|
| 131 |
+
for b in headers:
|
| 132 |
+
if a == b: continue
|
| 133 |
+
if matrix.get(a, {}).get(b) != matrix.get(b, {}).get(a):
|
| 134 |
+
counter_examples.add(a)
|
| 135 |
+
counter_examples.add(b)
|
| 136 |
+
|
| 137 |
+
return ",".join(sorted(counter_examples))
|
| 138 |
+
|
| 139 |
except Exception as e:
|
| 140 |
+
return f"Table analysis error: {str(e)}"
|
| 141 |
|
| 142 |
@tool
|
| 143 |
+
def parse_excel(file_url: str) -> str:
|
| 144 |
+
"""Extract and process Excel data"""
|
| 145 |
+
try:
|
| 146 |
+
response = requests.get(file_url, timeout=30)
|
| 147 |
+
wb = openpyxl.load_workbook(io.BytesIO(response.content))
|
| 148 |
+
sheet = wb.active
|
| 149 |
+
|
| 150 |
+
# Extract data (simple implementation)
|
| 151 |
+
data = []
|
| 152 |
+
for row in sheet.iter_rows(values_only=True):
|
| 153 |
+
data.append(row)
|
| 154 |
+
|
| 155 |
+
return f"Excel data: {str(data)[:2000]}"
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"Excel error: {str(e)}"
|
| 158 |
|
| 159 |
+
@tool
|
| 160 |
+
def execute_python(code: str) -> str:
|
| 161 |
+
"""Safely execute Python code"""
|
| 162 |
try:
|
| 163 |
+
# Create safe environment
|
| 164 |
+
safe_globals = {'__builtins__': None}
|
| 165 |
+
safe_locals = {}
|
| 166 |
+
|
| 167 |
+
# Execute code
|
| 168 |
+
exec(code, safe_globals, safe_locals)
|
| 169 |
+
|
| 170 |
+
# Find output variable
|
| 171 |
+
if 'result' in safe_locals:
|
| 172 |
+
return str(safe_locals['result'])
|
| 173 |
+
return "No 'result' variable found"
|
| 174 |
except Exception as e:
|
| 175 |
+
return f"Execution error: {str(e)}"
|
| 176 |
|
| 177 |
+
@tool
|
| 178 |
+
def classify_botanical(items: str) -> str:
|
| 179 |
+
"""Classify items as botanical vegetables"""
|
| 180 |
+
try:
|
| 181 |
+
vegetable_list = []
|
| 182 |
+
for item in items.split(','):
|
| 183 |
+
item = item.strip().lower()
|
| 184 |
+
if any(veg in item for veg in VEGETABLE_DB):
|
| 185 |
+
vegetable_list.append(item.split()[-1]) # Get last word as name
|
| 186 |
+
|
| 187 |
+
return ", ".join(sorted(set(vegetable_list)))
|
| 188 |
+
except Exception as e:
|
| 189 |
+
return f"Classification error: {str(e)}"
|
| 190 |
|
| 191 |
+
# --- Enhanced Agent Definition ---
|
| 192 |
+
class EnhancedGAIAAgent:
|
| 193 |
def __init__(self):
|
| 194 |
+
print("Initializing Enhanced GAIA Agent...")
|
| 195 |
+
|
| 196 |
+
# Initialize model
|
| 197 |
try:
|
| 198 |
self.model = InferenceClientModel(
|
| 199 |
+
model_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 200 |
+
token=os.getenv("HUGGINGFACE_INFERENCE_TOKEN"),
|
| 201 |
+
timeout=60
|
| 202 |
)
|
| 203 |
+
except:
|
|
|
|
| 204 |
self.model = InferenceClientModel(
|
| 205 |
+
model_id="HuggingFaceH4/zephyr-7b-beta"
|
| 206 |
)
|
| 207 |
+
|
| 208 |
+
# Custom tools list
|
| 209 |
custom_tools = [
|
| 210 |
serper_search,
|
| 211 |
+
wikipedia_detailed,
|
| 212 |
+
youtube_transcript,
|
| 213 |
+
transcribe_audio,
|
| 214 |
+
analyze_operation_table,
|
| 215 |
+
parse_excel,
|
| 216 |
+
execute_python,
|
| 217 |
+
classify_botanical,
|
| 218 |
+
DuckDuckGoSearchTool() # Include DDG as fallback
|
| 219 |
]
|
| 220 |
+
|
| 221 |
+
# Create agent with all tools
|
| 222 |
self.agent = CodeAgent(
|
| 223 |
+
tools=custom_tools,
|
| 224 |
+
model=self.model,
|
| 225 |
+
max_iters=5
|
| 226 |
)
|
| 227 |
+
|
| 228 |
+
print("Enhanced GAIA Agent initialized successfully.")
|
| 229 |
|
| 230 |
def __call__(self, question: str) -> str:
|
| 231 |
+
print(f"Processing: {question[:100]}...")
|
| 232 |
+
|
| 233 |
try:
|
| 234 |
+
# Question type routing
|
| 235 |
+
q_lower = question.lower()
|
| 236 |
+
|
| 237 |
+
# Wikipedia discography question
|
| 238 |
+
if "mercedes sosa" in q_lower and "studio albums" in q_lower:
|
| 239 |
+
result = wikipedia_detailed("Mercedes Sosa", "Discography")
|
| 240 |
+
# Count albums between 2000-2009
|
| 241 |
+
count = sum(1 for year in range(2000, 2010) if str(year) in result)
|
| 242 |
+
return str(count)
|
| 243 |
+
|
| 244 |
+
# YouTube bird species question
|
| 245 |
+
elif "youtube.com" in q_lower and "bird species" in q_lower:
|
| 246 |
+
video_id = re.search(r'v=([a-zA-Z0-9_-]+)', question).group(1)
|
| 247 |
+
transcript = youtube_transcript(video_id)
|
| 248 |
+
# Extract highest number
|
| 249 |
+
numbers = [int(word) for word in transcript.split() if word.isdigit()]
|
| 250 |
+
return str(max(numbers)) if numbers else "0"
|
| 251 |
+
|
| 252 |
+
# Reversed text question
|
| 253 |
+
elif "ecnetnes siht dnatsrednu" in q_lower:
|
| 254 |
+
reversed_text = question.split('"')[1]
|
| 255 |
+
return reversed_text[::-1].split()[0]
|
| 256 |
+
|
| 257 |
+
# Operation table question
|
| 258 |
+
elif "table defining *" in q_lower:
|
| 259 |
+
table_start = question.find("|*|a|b|c|d|e|")
|
| 260 |
+
table_end = question.find("\n\n", table_start)
|
| 261 |
+
table_md = question[table_start:table_end]
|
| 262 |
+
return analyze_operation_table(table_md)
|
| 263 |
+
|
| 264 |
+
# Botanical classification
|
| 265 |
+
elif "botanical" in q_lower and "vegetable" in q_lower:
|
| 266 |
+
food_list = re.search(r'milk.*?peanuts', question, re.DOTALL).group(0)
|
| 267 |
+
return classify_botanical(food_list)
|
| 268 |
+
|
| 269 |
+
# Audio transcription
|
| 270 |
+
elif "audio recording" in q_lower or "voice memo" in q_lower:
|
| 271 |
+
audio_url = re.search(r'https?://\S+\.(mp3|wav)', question).group(0)
|
| 272 |
+
return transcribe_audio(audio_url)
|
| 273 |
+
|
| 274 |
+
# Excel processing
|
| 275 |
+
elif "excel file" in q_lower and "sales" in q_lower:
|
| 276 |
+
excel_url = re.search(r'https?://\S+\.(xlsx|xls)', question).group(0)
|
| 277 |
+
return parse_excel(excel_url)
|
| 278 |
+
|
| 279 |
+
# Python execution
|
| 280 |
+
elif "python code" in q_lower and "output" in q_lower:
|
| 281 |
+
code_match = re.search(r'```python(.*?)```', question, re.DOTALL)
|
| 282 |
+
if code_match:
|
| 283 |
+
return execute_python(code_match.group(1))
|
| 284 |
+
return "No Python code found"
|
| 285 |
+
|
| 286 |
+
# General question fallback
|
| 287 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 288 |
+
future_wiki = executor.submit(wikipedia_detailed, question.split()[0])
|
| 289 |
+
future_serper = executor.submit(serper_search, question)
|
| 290 |
+
|
| 291 |
+
wiki_result = future_wiki.result()
|
| 292 |
+
search_result = future_serper.result()
|
| 293 |
+
|
| 294 |
+
if "Summary:" in wiki_result:
|
| 295 |
+
return f"Wikipedia: {wiki_result[:2000]}\n\nSearch: {search_result}"
|
| 296 |
+
return search_result
|
| 297 |
+
|
| 298 |
except Exception as e:
|
| 299 |
+
print(f"Error: {str(e)}")
|
| 300 |
+
return serper_search(question)
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
# --- Gradio Interface Functions ---
|
| 303 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 304 |
"""
|
| 305 |
+
Fetches questions, runs agent, and submits answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
"""
|
| 307 |
+
if not profile:
|
| 308 |
+
return "Please log in first", None
|
| 309 |
+
|
| 310 |
+
username = profile.username
|
|
|
|
|
|
|
|
|
|
| 311 |
api_url = DEFAULT_API_URL
|
| 312 |
questions_url = f"{api_url}/questions"
|
| 313 |
submit_url = f"{api_url}/submit"
|
| 314 |
+
|
| 315 |
+
# Instantiate agent
|
| 316 |
try:
|
| 317 |
+
agent = EnhancedGAIAAgent()
|
| 318 |
except Exception as e:
|
| 319 |
+
return f"Agent init failed: {str(e)}", None
|
| 320 |
+
|
| 321 |
+
# Fetch questions
|
|
|
|
| 322 |
try:
|
| 323 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 324 |
questions_data = response.json()
|
| 325 |
+
print(f"Fetched {len(questions_data)} questions")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
except Exception as e:
|
| 327 |
+
return f"Failed to get questions: {str(e)}", None
|
| 328 |
+
|
| 329 |
+
# Process questions
|
| 330 |
+
results = []
|
| 331 |
+
answers = []
|
| 332 |
+
|
| 333 |
for i, item in enumerate(questions_data):
|
| 334 |
task_id = item.get("task_id")
|
| 335 |
+
question = item.get("question")
|
| 336 |
+
|
| 337 |
+
if not task_id or not question:
|
| 338 |
continue
|
| 339 |
+
|
| 340 |
+
print(f"Processing {i+1}/{len(questions_data)}: {task_id}")
|
| 341 |
try:
|
| 342 |
+
answer = agent(question)
|
| 343 |
+
answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 344 |
+
results.append({
|
| 345 |
+
"Task ID": task_id,
|
| 346 |
+
"Question": question[:100] + "...",
|
| 347 |
+
"Answer": answer[:200] + "..." if isinstance(answer, str) else str(answer)
|
| 348 |
+
})
|
| 349 |
+
time.sleep(1) # Rate limiting
|
| 350 |
except Exception as e:
|
| 351 |
+
print(f"Error on {task_id}: {str(e)}")
|
| 352 |
+
results.append({"Task ID": task_id, "Question": question[:100] + "...", "Answer": f"Error: {str(e)}"})
|
| 353 |
+
|
| 354 |
+
# Submit answers
|
| 355 |
+
submission = {
|
| 356 |
+
"username": username,
|
| 357 |
+
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}",
|
| 358 |
+
"answers": answers
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
try:
|
| 362 |
+
response = requests.post(submit_url, json=submission, timeout=60)
|
| 363 |
+
response.raise_for_status()
|
| 364 |
+
result = response.json()
|
| 365 |
+
status = (
|
| 366 |
+
f"Submitted {len(answers)} answers\n"
|
| 367 |
+
f"Score: {result.get('score', 'N/A')}% "
|
| 368 |
+
f"({result.get('correct_count', 0)}/{len(answers)} correct)\n"
|
| 369 |
+
f"Message: {result.get('message', '')}"
|
| 370 |
+
)
|
| 371 |
+
return status, pd.DataFrame(results)
|
| 372 |
except Exception as e:
|
| 373 |
+
return f"Submission failed: {str(e)}", pd.DataFrame(results)
|
| 374 |
+
|
| 375 |
+
# --- Gradio Interface ---
|
| 376 |
+
with gr.Blocks(title="Enhanced GAIA Agent") as demo:
|
| 377 |
+
gr.Markdown("# 🚀 Enhanced GAIA Benchmark Agent")
|
| 378 |
+
gr.Markdown("""
|
| 379 |
+
**Specialized agent for GAIA benchmark with:**
|
| 380 |
+
- Wikipedia section extraction
|
| 381 |
+
- YouTube transcript analysis
|
| 382 |
+
- Audio transcription
|
| 383 |
+
- Excel/Python processing
|
| 384 |
+
- Botanical classification
|
| 385 |
+
- Advanced question routing
|
| 386 |
+
""")
|
| 387 |
+
|
| 388 |
+
gr.LoginButton()
|
| 389 |
+
|
| 390 |
+
with gr.Row():
|
| 391 |
+
run_btn = gr.Button("Run Full Evaluation & Submit", variant="primary")
|
| 392 |
+
|
| 393 |
+
with gr.Row():
|
| 394 |
+
status_out = gr.Textbox(label="Submission Status", interactive=False)
|
| 395 |
+
results_table = gr.DataFrame(label="Results", wrap=True, max_rows=20)
|
| 396 |
+
|
| 397 |
+
run_btn.click(
|
| 398 |
+
fn=run_and_submit_all,
|
| 399 |
+
outputs=[status_out, results_table]
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
if __name__ == "__main__":
|
| 403 |
+
print("Starting Enhanced GAIA Agent...")
|
| 404 |
+
|
| 405 |
+
# Environment checks
|
| 406 |
+
required_vars = ["SERPER_API_KEY", "HUGGINGFACE_INFERENCE_TOKEN"]
|
| 407 |
+
missing = [var for var in required_vars if not os.getenv(var)]
|
| 408 |
+
|
| 409 |
+
if missing:
|
| 410 |
+
print(f"⚠️ Missing environment variables: {', '.join(missing)}")
|
| 411 |
+
|
| 412 |
+
# Launch interface
|
| 413 |
+
demo.launch(
|
| 414 |
+
server_name="0.0.0.0",
|
| 415 |
+
server_port=int(os.getenv("PORT", 7860)),
|
| 416 |
+
share=False
|
| 417 |
+
)
|
requirements.txt
CHANGED
|
@@ -9,4 +9,9 @@ Pillow==10.0.1
|
|
| 9 |
numpy==1.24.3
|
| 10 |
datasets==2.14.6
|
| 11 |
accelerate==0.24.1
|
| 12 |
-
duckduckgo-search
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
numpy==1.24.3
|
| 10 |
datasets==2.14.6
|
| 11 |
accelerate==0.24.1
|
| 12 |
+
duckduckgo-search
|
| 13 |
+
wikipedia-api
|
| 14 |
+
youtube-transcript-api
|
| 15 |
+
whisper
|
| 16 |
+
openpyxl
|
| 17 |
+
smolagents
|