Spaces:
Runtime error
Runtime error
Fix
Browse files
app.py
CHANGED
|
@@ -22,7 +22,14 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 22 |
|
| 23 |
@tool
|
| 24 |
def serper_search(query: str) -> str:
|
| 25 |
-
"""Enhanced web search using Serper API with better result processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
api_key = os.getenv("SERPER_API_KEY")
|
| 28 |
if not api_key:
|
|
@@ -78,9 +85,17 @@ def serper_search(query: str) -> str:
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Search error: {str(e)}"
|
| 80 |
|
|
|
|
| 81 |
@tool
|
| 82 |
def wikipedia_search(query: str) -> str:
|
| 83 |
-
"""Enhanced Wikipedia search with multiple strategies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
results = []
|
| 86 |
clean_query = query.replace(" ", "_")
|
|
@@ -157,9 +172,17 @@ def wikipedia_search(query: str) -> str:
|
|
| 157 |
except Exception as e:
|
| 158 |
return f"Wikipedia search error: {str(e)}"
|
| 159 |
|
|
|
|
| 160 |
@tool
|
| 161 |
def youtube_analyzer(url: str) -> str:
|
| 162 |
-
"""Enhanced YouTube video analyzer with transcript extraction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
try:
|
| 164 |
video_id_match = re.search(r'(?:v=|/|youtu\.be/)([A-Za-z0-9_-]{11})', url)
|
| 165 |
if not video_id_match:
|
|
@@ -238,9 +261,19 @@ def youtube_analyzer(url: str) -> str:
|
|
| 238 |
except Exception as e:
|
| 239 |
return f"YouTube analysis error: {str(e)}"
|
| 240 |
|
|
|
|
| 241 |
@tool
|
| 242 |
def text_processor(text: str, operation: str = "analyze") -> str:
|
| 243 |
-
"""Advanced text processing for various linguistic operations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
try:
|
| 245 |
if operation == "reverse":
|
| 246 |
return text[::-1]
|
|
@@ -305,9 +338,17 @@ def text_processor(text: str, operation: str = "analyze") -> str:
|
|
| 305 |
except Exception as e:
|
| 306 |
return f"Text processing error: {str(e)}"
|
| 307 |
|
|
|
|
| 308 |
@tool
|
| 309 |
def math_solver(problem: str) -> str:
|
| 310 |
-
"""Advanced mathematical problem solver with multiple strategies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
try:
|
| 312 |
problem_lower = problem.lower()
|
| 313 |
|
|
@@ -388,9 +429,19 @@ pattern recognition, or formula application"""
|
|
| 388 |
except Exception as e:
|
| 389 |
return f"Math solver error: {str(e)}"
|
| 390 |
|
|
|
|
| 391 |
@tool
|
| 392 |
def data_extractor(source: str, target: str, context: str = "") -> str:
|
| 393 |
-
"""Enhanced data extraction with context awareness
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
try:
|
| 395 |
target_lower = target.lower()
|
| 396 |
source_lower = source.lower()
|
|
@@ -476,9 +527,17 @@ def data_extractor(source: str, target: str, context: str = "") -> str:
|
|
| 476 |
except Exception as e:
|
| 477 |
return f"Data extraction error: {str(e)}"
|
| 478 |
|
|
|
|
| 479 |
@tool
|
| 480 |
def web_page_fetcher(url: str) -> str:
|
| 481 |
-
"""Fetch and extract text content from web pages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
try:
|
| 483 |
headers = {
|
| 484 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
@@ -508,9 +567,17 @@ def web_page_fetcher(url: str) -> str:
|
|
| 508 |
except Exception as e:
|
| 509 |
return f"Web fetch error: {str(e)}"
|
| 510 |
|
|
|
|
| 511 |
@tool
|
| 512 |
def calculator_tool(expression: str) -> str:
|
| 513 |
-
"""Safe calculator for mathematical expressions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
try:
|
| 515 |
expression = expression.strip()
|
| 516 |
|
|
|
|
| 22 |
|
| 23 |
@tool
|
| 24 |
def serper_search(query: str) -> str:
|
| 25 |
+
"""Enhanced web search using Serper API with better result processing.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
query (str): The search query to be executed.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
str: Formatted search results with relevance scoring.
|
| 32 |
+
"""
|
| 33 |
try:
|
| 34 |
api_key = os.getenv("SERPER_API_KEY")
|
| 35 |
if not api_key:
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"Search error: {str(e)}"
|
| 87 |
|
| 88 |
+
|
| 89 |
@tool
|
| 90 |
def wikipedia_search(query: str) -> str:
|
| 91 |
+
"""Enhanced Wikipedia search with multiple strategies.
|
| 92 |
+
|
| 93 |
+
Args:
|
| 94 |
+
query (str): Wikipedia search query to look up.
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
str: Comprehensive Wikipedia information.
|
| 98 |
+
"""
|
| 99 |
try:
|
| 100 |
results = []
|
| 101 |
clean_query = query.replace(" ", "_")
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
return f"Wikipedia search error: {str(e)}"
|
| 174 |
|
| 175 |
+
|
| 176 |
@tool
|
| 177 |
def youtube_analyzer(url: str) -> str:
|
| 178 |
+
"""Enhanced YouTube video analyzer with transcript extraction.
|
| 179 |
+
|
| 180 |
+
Args:
|
| 181 |
+
url (str): YouTube video URL to analyze.
|
| 182 |
+
|
| 183 |
+
Returns:
|
| 184 |
+
str: Comprehensive video analysis.
|
| 185 |
+
"""
|
| 186 |
try:
|
| 187 |
video_id_match = re.search(r'(?:v=|/|youtu\.be/)([A-Za-z0-9_-]{11})', url)
|
| 188 |
if not video_id_match:
|
|
|
|
| 261 |
except Exception as e:
|
| 262 |
return f"YouTube analysis error: {str(e)}"
|
| 263 |
|
| 264 |
+
|
| 265 |
@tool
|
| 266 |
def text_processor(text: str, operation: str = "analyze") -> str:
|
| 267 |
+
"""Advanced text processing for various linguistic operations.
|
| 268 |
+
|
| 269 |
+
Args:
|
| 270 |
+
text (str): Text to process.
|
| 271 |
+
operation (str, optional): Operation type (reverse, parse, analyze, extract_numbers, decode).
|
| 272 |
+
Defaults to "analyze".
|
| 273 |
+
|
| 274 |
+
Returns:
|
| 275 |
+
str: Processed text results.
|
| 276 |
+
"""
|
| 277 |
try:
|
| 278 |
if operation == "reverse":
|
| 279 |
return text[::-1]
|
|
|
|
| 338 |
except Exception as e:
|
| 339 |
return f"Text processing error: {str(e)}"
|
| 340 |
|
| 341 |
+
|
| 342 |
@tool
|
| 343 |
def math_solver(problem: str) -> str:
|
| 344 |
+
"""Advanced mathematical problem solver with multiple strategies.
|
| 345 |
+
|
| 346 |
+
Args:
|
| 347 |
+
problem (str): Mathematical problem or structure to analyze.
|
| 348 |
+
|
| 349 |
+
Returns:
|
| 350 |
+
str: Mathematical analysis and solution approach.
|
| 351 |
+
"""
|
| 352 |
try:
|
| 353 |
problem_lower = problem.lower()
|
| 354 |
|
|
|
|
| 429 |
except Exception as e:
|
| 430 |
return f"Math solver error: {str(e)}"
|
| 431 |
|
| 432 |
+
|
| 433 |
@tool
|
| 434 |
def data_extractor(source: str, target: str, context: str = "") -> str:
|
| 435 |
+
"""Enhanced data extraction with context awareness.
|
| 436 |
+
|
| 437 |
+
Args:
|
| 438 |
+
source (str): Source text/data to extract from.
|
| 439 |
+
target (str): What to extract from the source.
|
| 440 |
+
context (str, optional): Additional context for extraction. Defaults to "".
|
| 441 |
+
|
| 442 |
+
Returns:
|
| 443 |
+
str: Extracted and processed data.
|
| 444 |
+
"""
|
| 445 |
try:
|
| 446 |
target_lower = target.lower()
|
| 447 |
source_lower = source.lower()
|
|
|
|
| 527 |
except Exception as e:
|
| 528 |
return f"Data extraction error: {str(e)}"
|
| 529 |
|
| 530 |
+
|
| 531 |
@tool
|
| 532 |
def web_page_fetcher(url: str) -> str:
|
| 533 |
+
"""Fetch and extract text content from web pages.
|
| 534 |
+
|
| 535 |
+
Args:
|
| 536 |
+
url (str): URL to fetch content from.
|
| 537 |
+
|
| 538 |
+
Returns:
|
| 539 |
+
str: Extracted text content.
|
| 540 |
+
"""
|
| 541 |
try:
|
| 542 |
headers = {
|
| 543 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
|
|
| 567 |
except Exception as e:
|
| 568 |
return f"Web fetch error: {str(e)}"
|
| 569 |
|
| 570 |
+
|
| 571 |
@tool
|
| 572 |
def calculator_tool(expression: str) -> str:
|
| 573 |
+
"""Safe calculator for mathematical expressions.
|
| 574 |
+
|
| 575 |
+
Args:
|
| 576 |
+
expression (str): Mathematical expression to evaluate.
|
| 577 |
+
|
| 578 |
+
Returns:
|
| 579 |
+
str: Calculation result.
|
| 580 |
+
"""
|
| 581 |
try:
|
| 582 |
expression = expression.strip()
|
| 583 |
|