Spaces:
Runtime error
Runtime error
fixing
Browse files- app.py +226 -247
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -5,8 +5,7 @@ import pandas as pd
|
|
| 5 |
import json
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 9 |
-
from smolagents.tools import Tool
|
| 10 |
from typing import Dict, Any, List
|
| 11 |
import base64
|
| 12 |
from io import BytesIO
|
|
@@ -18,237 +17,222 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 18 |
|
| 19 |
# --- Custom Tools ---
|
| 20 |
|
| 21 |
-
|
| 22 |
-
name = "serper_search"
|
| 23 |
-
description = "Search the web using Serper API for current information and specific queries"
|
| 24 |
-
inputs = {
|
| 25 |
-
"query": {
|
| 26 |
-
"type": "string",
|
| 27 |
-
"description": "The search query"
|
| 28 |
-
}
|
| 29 |
-
}
|
| 30 |
-
output_type = "string"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
response = requests.
|
| 47 |
-
response.raise_for_status()
|
| 48 |
-
|
| 49 |
data = response.json()
|
| 50 |
-
results = []
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
results.append(f"Title: {item.get('title', '')}\nSnippet: {item.get('snippet', '')}\nURL: {item.get('link', '')}\n")
|
| 56 |
-
|
| 57 |
-
# Add knowledge graph if available
|
| 58 |
-
if 'knowledgeGraph' in data:
|
| 59 |
-
kg = data['knowledgeGraph']
|
| 60 |
-
results.insert(0, f"Knowledge Graph: {kg.get('title', '')} - {kg.get('description', '')}\n")
|
| 61 |
|
| 62 |
-
return "\n".join(results) if results else "No results found"
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
class WikipediaSearchTool(Tool):
|
| 68 |
-
name = "wikipedia_search"
|
| 69 |
-
description = "Search Wikipedia for detailed information on topics"
|
| 70 |
-
inputs = {
|
| 71 |
-
"query": {
|
| 72 |
-
"type": "string",
|
| 73 |
-
"description": "The Wikipedia search query"
|
| 74 |
-
}
|
| 75 |
-
}
|
| 76 |
-
output_type = "string"
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
search_api = "https://en.wikipedia.org/w/api.php"
|
| 90 |
-
params = {
|
| 91 |
-
"action": "query",
|
| 92 |
-
"format": "json",
|
| 93 |
-
"list": "search",
|
| 94 |
-
"srsearch": query,
|
| 95 |
-
"srlimit": 3
|
| 96 |
-
}
|
| 97 |
-
response = requests.get(search_api, params=params, timeout=15)
|
| 98 |
-
data = response.json()
|
| 99 |
-
|
| 100 |
-
results = []
|
| 101 |
-
for item in data.get('query', {}).get('search', []):
|
| 102 |
-
results.append(f"Title: {item['title']}\nSnippet: {item['snippet']}")
|
| 103 |
-
|
| 104 |
-
return "\n\n".join(results) if results else "No Wikipedia results found"
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
output_type = "string"
|
| 119 |
-
|
| 120 |
-
def forward(self, url: str) -> str:
|
| 121 |
-
try:
|
| 122 |
-
# Extract video ID
|
| 123 |
-
video_id_match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url)
|
| 124 |
-
if not video_id_match:
|
| 125 |
-
return "Invalid YouTube URL"
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
result = f"Title: {data.get('title', '')}\nAuthor: {data.get('author_name', '')}\n"
|
| 136 |
-
|
| 137 |
-
# Try to get additional info by scraping (basic)
|
| 138 |
-
try:
|
| 139 |
-
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 140 |
-
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
|
| 141 |
-
page_response = requests.get(video_url, headers=headers, timeout=15)
|
| 142 |
-
|
| 143 |
-
if page_response.status_code == 200:
|
| 144 |
-
content = page_response.text
|
| 145 |
-
# Extract description from meta tags
|
| 146 |
-
desc_match = re.search(r'"description":{"simpleText":"([^"]+)"', content)
|
| 147 |
-
if desc_match:
|
| 148 |
-
result += f"Description: {desc_match.group(1)}\n"
|
| 149 |
-
|
| 150 |
-
except:
|
| 151 |
-
pass
|
| 152 |
-
|
| 153 |
-
return result
|
| 154 |
-
else:
|
| 155 |
-
return "Could not retrieve video information"
|
| 156 |
-
|
| 157 |
-
except Exception as e:
|
| 158 |
-
return f"YouTube analysis error: {str(e)}"
|
| 159 |
-
|
| 160 |
-
class TextProcessorTool(Tool):
|
| 161 |
-
name = "text_processor"
|
| 162 |
-
description = "Process text for various operations like reversing, parsing, and analyzing"
|
| 163 |
-
inputs = {
|
| 164 |
-
"text": {
|
| 165 |
-
"type": "string",
|
| 166 |
-
"description": "Text to process"
|
| 167 |
-
},
|
| 168 |
-
"operation": {
|
| 169 |
-
"type": "string",
|
| 170 |
-
"description": "Operation to perform: reverse, parse, analyze"
|
| 171 |
-
}
|
| 172 |
-
}
|
| 173 |
-
output_type = "string"
|
| 174 |
-
|
| 175 |
-
def forward(self, text: str, operation: str = "analyze") -> str:
|
| 176 |
-
try:
|
| 177 |
-
if operation == "reverse":
|
| 178 |
-
return text[::-1]
|
| 179 |
-
elif operation == "parse":
|
| 180 |
-
# Extract meaningful information
|
| 181 |
-
words = text.split()
|
| 182 |
-
return f"Word count: {len(words)}\nFirst word: {words[0] if words else 'None'}\nLast word: {words[-1] if words else 'None'}"
|
| 183 |
-
else:
|
| 184 |
-
# General analysis
|
| 185 |
-
return f"Text length: {len(text)}\nWord count: {len(text.split())}\nText: {text[:200]}..."
|
| 186 |
-
except Exception as e:
|
| 187 |
-
return f"Text processing error: {str(e)}"
|
| 188 |
-
|
| 189 |
-
class MathSolverTool(Tool):
|
| 190 |
-
name = "math_solver"
|
| 191 |
-
description = "Solve mathematical problems and analyze mathematical structures"
|
| 192 |
-
inputs = {
|
| 193 |
-
"problem": {
|
| 194 |
-
"type": "string",
|
| 195 |
-
"description": "Mathematical problem or structure to analyze"
|
| 196 |
-
}
|
| 197 |
-
}
|
| 198 |
-
output_type = "string"
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
if any(veg in item_lower for veg in ["potato", "basil", "broccoli", "celery", "lettuce"]):
|
| 243 |
-
vegetables.append(item)
|
| 244 |
-
|
| 245 |
-
vegetables.sort()
|
| 246 |
-
return ", ".join(vegetables)
|
| 247 |
|
| 248 |
-
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# --- Enhanced Agent Definition ---
|
| 254 |
class GAIAAgent:
|
|
@@ -261,22 +245,26 @@ class GAIAAgent:
|
|
| 261 |
token=os.getenv("HUGGINGFACE_INFERENCE_TOKEN")
|
| 262 |
)
|
| 263 |
|
| 264 |
-
#
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
DataExtractorTool()
|
| 273 |
]
|
| 274 |
|
| 275 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
self.agent = CodeAgent(
|
| 277 |
-
tools=
|
| 278 |
model=self.model,
|
| 279 |
-
max_iterations=
|
| 280 |
)
|
| 281 |
|
| 282 |
print("GAIA Agent initialized successfully.")
|
|
@@ -291,60 +279,52 @@ class GAIAAgent:
|
|
| 291 |
# Handle reversed text question
|
| 292 |
if "ecnetnes siht dnatsrednu uoy fi" in question.lower():
|
| 293 |
# This is the reversed sentence question
|
| 294 |
-
processor = TextProcessorTool()
|
| 295 |
reversed_part = question.split("?,")[0] # Get the reversed part
|
| 296 |
-
normal_text =
|
| 297 |
if "left" in normal_text.lower():
|
| 298 |
return "right"
|
| 299 |
|
| 300 |
# Handle YouTube video questions
|
| 301 |
elif "youtube.com" in question:
|
| 302 |
-
youtube_tool = YouTubeAnalyzerTool()
|
| 303 |
# Extract URL
|
| 304 |
url_match = re.search(r'https://www\.youtube\.com/watch\?v=[^\s,?.]+', question)
|
| 305 |
if url_match:
|
| 306 |
url = url_match.group(0)
|
| 307 |
-
video_info =
|
| 308 |
|
| 309 |
# Use search to get more specific info about the video content
|
| 310 |
-
search_tool = SerperSearchTool()
|
| 311 |
search_query = f"site:youtube.com {url} transcript content"
|
| 312 |
-
search_results =
|
| 313 |
|
| 314 |
return f"Video Analysis: {video_info}\n\nAdditional Info: {search_results}"
|
| 315 |
|
| 316 |
# Handle botanical/grocery list questions
|
| 317 |
elif "botanical" in question_lower and "vegetable" in question_lower:
|
| 318 |
-
extractor = DataExtractorTool()
|
| 319 |
# Extract the list from the question
|
| 320 |
list_match = re.search(r'milk.*?peanuts', question)
|
| 321 |
if list_match:
|
| 322 |
food_list = list_match.group(0)
|
| 323 |
-
return
|
| 324 |
|
| 325 |
# Handle mathematical problems
|
| 326 |
elif "commutative" in question_lower or "chess" in question_lower:
|
| 327 |
-
|
| 328 |
-
math_result = math_tool.forward(question)
|
| 329 |
|
| 330 |
# For commutative question, also search for more specific help
|
| 331 |
if "commutative" in question_lower:
|
| 332 |
-
|
| 333 |
-
search_result = search_tool.forward("group theory commutative operation counter examples")
|
| 334 |
return f"{math_result}\n\nAdditional context: {search_result}"
|
|
|
|
|
|
|
| 335 |
|
| 336 |
# Handle specific factual questions
|
| 337 |
else:
|
| 338 |
# Use search tools for factual questions
|
| 339 |
-
|
| 340 |
-
wiki_tool = WikipediaSearchTool()
|
| 341 |
-
|
| 342 |
-
# Try Serper search first
|
| 343 |
-
search_results = search_tool.forward(question)
|
| 344 |
|
| 345 |
# For some questions, also try Wikipedia
|
| 346 |
if any(term in question_lower for term in ["mercedes sosa", "dinosaur", "wikipedia", "olympics"]):
|
| 347 |
-
wiki_results =
|
| 348 |
return f"Search Results: {search_results}\n\nWikipedia: {wiki_results}"
|
| 349 |
|
| 350 |
return search_results
|
|
@@ -353,8 +333,7 @@ class GAIAAgent:
|
|
| 353 |
print(f"Error in agent processing: {e}")
|
| 354 |
# Fallback to basic search
|
| 355 |
try:
|
| 356 |
-
|
| 357 |
-
return search_tool.forward(question)
|
| 358 |
except:
|
| 359 |
return f"I encountered an error processing this question: {question}. Please try rephrasing or breaking it into smaller parts."
|
| 360 |
|
|
|
|
| 5 |
import json
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
|
|
|
| 9 |
from typing import Dict, Any, List
|
| 10 |
import base64
|
| 11 |
from io import BytesIO
|
|
|
|
| 17 |
|
| 18 |
# --- Custom Tools ---
|
| 19 |
|
| 20 |
+
# --- Custom Tools ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@tool
|
| 23 |
+
def serper_search(query: str) -> str:
|
| 24 |
+
"""Search the web using Serper API for current information and specific queries
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
query: The search query
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
Search results as formatted string
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
api_key = os.getenv("SERPER_API_KEY")
|
| 34 |
+
if not api_key:
|
| 35 |
+
return "SERPER_API_KEY environment variable not found"
|
| 36 |
+
|
| 37 |
+
url = "https://google.serper.dev/search"
|
| 38 |
+
payload = json.dumps({"q": query, "num": 10})
|
| 39 |
+
headers = {
|
| 40 |
+
'X-API-KEY': api_key,
|
| 41 |
+
'Content-Type': 'application/json'
|
| 42 |
+
}
|
| 43 |
+
response = requests.post(url, headers=headers, data=payload, timeout=30)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
|
| 46 |
+
data = response.json()
|
| 47 |
+
results = []
|
| 48 |
+
|
| 49 |
+
# Process organic results
|
| 50 |
+
if 'organic' in data:
|
| 51 |
+
for item in data['organic'][:5]:
|
| 52 |
+
results.append(f"Title: {item.get('title', '')}\nSnippet: {item.get('snippet', '')}\nURL: {item.get('link', '')}\n")
|
| 53 |
+
|
| 54 |
+
# Add knowledge graph if available
|
| 55 |
+
if 'knowledgeGraph' in data:
|
| 56 |
+
kg = data['knowledgeGraph']
|
| 57 |
+
results.insert(0, f"Knowledge Graph: {kg.get('title', '')} - {kg.get('description', '')}\n")
|
| 58 |
+
|
| 59 |
+
return "\n".join(results) if results else "No results found"
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Search error: {str(e)}"
|
| 63 |
|
| 64 |
+
@tool
|
| 65 |
+
def wikipedia_search(query: str) -> str:
|
| 66 |
+
"""Search Wikipedia for detailed information on topics
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
query: The Wikipedia search query
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
Wikipedia search results
|
| 73 |
+
"""
|
| 74 |
+
try:
|
| 75 |
+
# Search for pages
|
| 76 |
+
search_url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
|
| 77 |
+
response = requests.get(search_url, timeout=15)
|
| 78 |
+
|
| 79 |
+
if response.status_code == 200:
|
| 80 |
+
data = response.json()
|
| 81 |
+
return f"Title: {data.get('title', '')}\nSummary: {data.get('extract', '')}\nURL: {data.get('content_urls', {}).get('desktop', {}).get('page', '')}"
|
| 82 |
+
else:
|
| 83 |
+
# Fallback to search API
|
| 84 |
+
search_api = "https://en.wikipedia.org/w/api.php"
|
| 85 |
+
params = {
|
| 86 |
+
"action": "query",
|
| 87 |
+
"format": "json",
|
| 88 |
+
"list": "search",
|
| 89 |
+
"srsearch": query,
|
| 90 |
+
"srlimit": 3
|
| 91 |
}
|
| 92 |
+
response = requests.get(search_api, params=params, timeout=15)
|
|
|
|
|
|
|
| 93 |
data = response.json()
|
|
|
|
| 94 |
|
| 95 |
+
results = []
|
| 96 |
+
for item in data.get('query', {}).get('search', []):
|
| 97 |
+
results.append(f"Title: {item['title']}\nSnippet: {item['snippet']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
return "\n\n".join(results) if results else "No Wikipedia results found"
|
| 100 |
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return f"Wikipedia search error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
@tool
|
| 105 |
+
def youtube_analyzer(url: str) -> str:
|
| 106 |
+
"""Analyze YouTube videos to extract information from titles, descriptions, and comments
|
| 107 |
+
|
| 108 |
+
Args:
|
| 109 |
+
url: YouTube video URL
|
| 110 |
+
|
| 111 |
+
Returns:
|
| 112 |
+
Video information and analysis
|
| 113 |
+
"""
|
| 114 |
+
try:
|
| 115 |
+
# Extract video ID
|
| 116 |
+
video_id_match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url)
|
| 117 |
+
if not video_id_match:
|
| 118 |
+
return "Invalid YouTube URL"
|
| 119 |
+
|
| 120 |
+
video_id = video_id_match.group(1)
|
| 121 |
+
|
| 122 |
+
# Use oEmbed API to get basic info
|
| 123 |
+
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
|
| 124 |
+
response = requests.get(oembed_url, timeout=15)
|
| 125 |
+
|
| 126 |
+
if response.status_code == 200:
|
| 127 |
+
data = response.json()
|
| 128 |
+
result = f"Title: {data.get('title', '')}\nAuthor: {data.get('author_name', '')}\n"
|
| 129 |
|
| 130 |
+
# Try to get additional info by scraping (basic)
|
| 131 |
+
try:
|
| 132 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 133 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
|
| 134 |
+
page_response = requests.get(video_url, headers=headers, timeout=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
if page_response.status_code == 200:
|
| 137 |
+
content = page_response.text
|
| 138 |
+
# Extract description from meta tags
|
| 139 |
+
desc_match = re.search(r'"description":{"simpleText":"([^"]+)"', content)
|
| 140 |
+
if desc_match:
|
| 141 |
+
result += f"Description: {desc_match.group(1)}\n"
|
| 142 |
+
|
| 143 |
+
# Look for bird-related content
|
| 144 |
+
if "bird" in content.lower():
|
| 145 |
+
bird_matches = re.findall(r'\b\d+\s+bird', content.lower())
|
| 146 |
+
if bird_matches:
|
| 147 |
+
result += f"Bird mentions found: {bird_matches}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
except:
|
| 150 |
+
pass
|
| 151 |
|
| 152 |
+
return result
|
| 153 |
+
else:
|
| 154 |
+
return "Could not retrieve video information"
|
| 155 |
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"YouTube analysis error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
@tool
|
| 160 |
+
def text_processor(text: str, operation: str = "analyze") -> str:
|
| 161 |
+
"""Process text for various operations like reversing, parsing, and analyzing
|
| 162 |
+
|
| 163 |
+
Args:
|
| 164 |
+
text: Text to process
|
| 165 |
+
operation: Operation to perform (reverse, parse, analyze)
|
| 166 |
+
|
| 167 |
+
Returns:
|
| 168 |
+
Processed text result
|
| 169 |
+
"""
|
| 170 |
+
try:
|
| 171 |
+
if operation == "reverse":
|
| 172 |
+
return text[::-1]
|
| 173 |
+
elif operation == "parse":
|
| 174 |
+
# Extract meaningful information
|
| 175 |
+
words = text.split()
|
| 176 |
+
return f"Word count: {len(words)}\nFirst word: {words[0] if words else 'None'}\nLast word: {words[-1] if words else 'None'}"
|
| 177 |
+
else:
|
| 178 |
+
# General analysis
|
| 179 |
+
return f"Text length: {len(text)}\nWord count: {len(text.split())}\nText: {text[:200]}..."
|
| 180 |
+
except Exception as e:
|
| 181 |
+
return f"Text processing error: {str(e)}"
|
| 182 |
|
| 183 |
+
@tool
|
| 184 |
+
def math_solver(problem: str) -> str:
|
| 185 |
+
"""Solve mathematical problems and analyze mathematical structures
|
| 186 |
+
|
| 187 |
+
Args:
|
| 188 |
+
problem: Mathematical problem or structure to analyze
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
Mathematical analysis and solution
|
| 192 |
+
"""
|
| 193 |
+
try:
|
| 194 |
+
# Basic math operations and analysis
|
| 195 |
+
if "commutative" in problem.lower():
|
| 196 |
+
return "To check commutativity, verify if a*b = b*a for all elements. Find counter-examples where this fails."
|
| 197 |
+
elif "chess" in problem.lower():
|
| 198 |
+
return "For chess problems, analyze the position systematically: check for checks, captures, tactical motifs like pins, forks, or checkmate patterns."
|
| 199 |
+
else:
|
| 200 |
+
return f"Mathematical analysis needed for: {problem[:100]}..."
|
| 201 |
+
except Exception as e:
|
| 202 |
+
return f"Math solver error: {str(e)}"
|
| 203 |
|
| 204 |
+
@tool
|
| 205 |
+
def data_extractor(source: str, target: str) -> str:
|
| 206 |
+
"""Extract structured data from various sources
|
| 207 |
+
|
| 208 |
+
Args:
|
| 209 |
+
source: Data source or content to extract from
|
| 210 |
+
target: What to extract
|
| 211 |
+
|
| 212 |
+
Returns:
|
| 213 |
+
Extracted data
|
| 214 |
+
"""
|
| 215 |
+
try:
|
| 216 |
+
# Botanical classification helper
|
| 217 |
+
if "botanical" in target.lower() or "vegetable" in target.lower():
|
| 218 |
+
vegetables = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
# Common botanical classifications - only true vegetables
|
| 221 |
+
items = [item.strip() for item in source.split(",")]
|
| 222 |
|
| 223 |
+
for item in items:
|
| 224 |
+
item_lower = item.lower()
|
| 225 |
+
# Only include botanically true vegetables (not fruits used as vegetables)
|
| 226 |
+
if any(veg in item_lower for veg in ["sweet potato", "basil", "broccoli", "celery", "lettuce"]):
|
| 227 |
+
vegetables.append(item)
|
| 228 |
+
|
| 229 |
+
vegetables.sort()
|
| 230 |
+
return ", ".join(vegetables)
|
| 231 |
+
|
| 232 |
+
return f"Data extraction for {target} from {source[:100]}..."
|
| 233 |
+
|
| 234 |
+
except Exception as e:
|
| 235 |
+
return f"Data extraction error: {str(e)}"
|
| 236 |
|
| 237 |
# --- Enhanced Agent Definition ---
|
| 238 |
class GAIAAgent:
|
|
|
|
| 245 |
token=os.getenv("HUGGINGFACE_INFERENCE_TOKEN")
|
| 246 |
)
|
| 247 |
|
| 248 |
+
# Custom tools list
|
| 249 |
+
custom_tools = [
|
| 250 |
+
serper_search,
|
| 251 |
+
wikipedia_search,
|
| 252 |
+
youtube_analyzer,
|
| 253 |
+
text_processor,
|
| 254 |
+
math_solver,
|
| 255 |
+
data_extractor
|
|
|
|
| 256 |
]
|
| 257 |
|
| 258 |
+
# Add DuckDuckGo search tool
|
| 259 |
+
ddg_tool = DuckDuckGoSearchTool()
|
| 260 |
+
|
| 261 |
+
# Create agent with all tools
|
| 262 |
+
all_tools = custom_tools + [ddg_tool]
|
| 263 |
+
|
| 264 |
self.agent = CodeAgent(
|
| 265 |
+
tools=all_tools,
|
| 266 |
model=self.model,
|
| 267 |
+
max_iterations=3
|
| 268 |
)
|
| 269 |
|
| 270 |
print("GAIA Agent initialized successfully.")
|
|
|
|
| 279 |
# Handle reversed text question
|
| 280 |
if "ecnetnes siht dnatsrednu uoy fi" in question.lower():
|
| 281 |
# This is the reversed sentence question
|
|
|
|
| 282 |
reversed_part = question.split("?,")[0] # Get the reversed part
|
| 283 |
+
normal_text = text_processor(reversed_part, "reverse")
|
| 284 |
if "left" in normal_text.lower():
|
| 285 |
return "right"
|
| 286 |
|
| 287 |
# Handle YouTube video questions
|
| 288 |
elif "youtube.com" in question:
|
|
|
|
| 289 |
# Extract URL
|
| 290 |
url_match = re.search(r'https://www\.youtube\.com/watch\?v=[^\s,?.]+', question)
|
| 291 |
if url_match:
|
| 292 |
url = url_match.group(0)
|
| 293 |
+
video_info = youtube_analyzer(url)
|
| 294 |
|
| 295 |
# Use search to get more specific info about the video content
|
|
|
|
| 296 |
search_query = f"site:youtube.com {url} transcript content"
|
| 297 |
+
search_results = serper_search(search_query)
|
| 298 |
|
| 299 |
return f"Video Analysis: {video_info}\n\nAdditional Info: {search_results}"
|
| 300 |
|
| 301 |
# Handle botanical/grocery list questions
|
| 302 |
elif "botanical" in question_lower and "vegetable" in question_lower:
|
|
|
|
| 303 |
# Extract the list from the question
|
| 304 |
list_match = re.search(r'milk.*?peanuts', question)
|
| 305 |
if list_match:
|
| 306 |
food_list = list_match.group(0)
|
| 307 |
+
return data_extractor(food_list, "botanical vegetables")
|
| 308 |
|
| 309 |
# Handle mathematical problems
|
| 310 |
elif "commutative" in question_lower or "chess" in question_lower:
|
| 311 |
+
math_result = math_solver(question)
|
|
|
|
| 312 |
|
| 313 |
# For commutative question, also search for more specific help
|
| 314 |
if "commutative" in question_lower:
|
| 315 |
+
search_result = serper_search("group theory commutative operation counter examples")
|
|
|
|
| 316 |
return f"{math_result}\n\nAdditional context: {search_result}"
|
| 317 |
+
|
| 318 |
+
return math_result
|
| 319 |
|
| 320 |
# Handle specific factual questions
|
| 321 |
else:
|
| 322 |
# Use search tools for factual questions
|
| 323 |
+
search_results = serper_search(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
# For some questions, also try Wikipedia
|
| 326 |
if any(term in question_lower for term in ["mercedes sosa", "dinosaur", "wikipedia", "olympics"]):
|
| 327 |
+
wiki_results = wikipedia_search(question)
|
| 328 |
return f"Search Results: {search_results}\n\nWikipedia: {wiki_results}"
|
| 329 |
|
| 330 |
return search_results
|
|
|
|
| 333 |
print(f"Error in agent processing: {e}")
|
| 334 |
# Fallback to basic search
|
| 335 |
try:
|
| 336 |
+
return serper_search(question)
|
|
|
|
| 337 |
except:
|
| 338 |
return f"I encountered an error processing this question: {question}. Please try rephrasing or breaking it into smaller parts."
|
| 339 |
|
requirements.txt
CHANGED
|
@@ -2,10 +2,10 @@ gradio==4.44.0
|
|
| 2 |
requests==2.31.0
|
| 3 |
pandas==2.0.3
|
| 4 |
smolagents==1.19.0
|
| 5 |
-
transformers==4.
|
|
|
|
| 6 |
torch==2.1.0
|
| 7 |
Pillow==10.0.1
|
| 8 |
numpy==1.24.3
|
| 9 |
-
huggingface-hub==0.19.4
|
| 10 |
datasets==2.14.6
|
| 11 |
accelerate==0.24.1
|
|
|
|
| 2 |
requests==2.31.0
|
| 3 |
pandas==2.0.3
|
| 4 |
smolagents==1.19.0
|
| 5 |
+
transformers==4.44.2
|
| 6 |
+
huggingface-hub>=0.31.2
|
| 7 |
torch==2.1.0
|
| 8 |
Pillow==10.0.1
|
| 9 |
numpy==1.24.3
|
|
|
|
| 10 |
datasets==2.14.6
|
| 11 |
accelerate==0.24.1
|