Spaces:
Runtime error
Runtime error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -16,73 +16,6 @@ import re
|
|
| 16 |
import time
|
| 17 |
from typing import Optional, List, Dict, Any
|
| 18 |
|
| 19 |
-
# class WebSearchTool:
|
| 20 |
-
# def __init__(self):
|
| 21 |
-
# self.last_request_time = 0
|
| 22 |
-
# self.min_request_interval = 2.0
|
| 23 |
-
# self.max_retries = 5
|
| 24 |
-
|
| 25 |
-
# def _do_search(self, query: str, domain: Optional[str] = None) -> str:
|
| 26 |
-
# base_url = "https://html.duckduckgo.com/html"
|
| 27 |
-
# params = {"q": query}
|
| 28 |
-
# if domain:
|
| 29 |
-
# params["q"] += f" site:{domain}"
|
| 30 |
-
|
| 31 |
-
# headers = {"User-Agent": "Mozilla/5.0"}
|
| 32 |
-
# response = requests.get(base_url, params=params, headers=headers, timeout=10)
|
| 33 |
-
# response.raise_for_status()
|
| 34 |
-
|
| 35 |
-
# if response.status_code == 202:
|
| 36 |
-
# raise Exception("202 Ratelimit")
|
| 37 |
-
|
| 38 |
-
# soup = BeautifulSoup(response.text, 'html.parser')
|
| 39 |
-
# results = []
|
| 40 |
-
# for result in soup.find_all('div', {'class': 'result'}):
|
| 41 |
-
# title = result.find('a', {'class': 'result__a'})
|
| 42 |
-
# snippet = result.find('a', {'class': 'result__snippet'})
|
| 43 |
-
# if title and snippet:
|
| 44 |
-
# results.append({
|
| 45 |
-
# 'title': title.get_text(),
|
| 46 |
-
# 'snippet': snippet.get_text(),
|
| 47 |
-
# 'url': title.get('href')
|
| 48 |
-
# })
|
| 49 |
-
|
| 50 |
-
# if not results:
|
| 51 |
-
# return "No results found."
|
| 52 |
-
|
| 53 |
-
# # Format results with title, URL, and snippet
|
| 54 |
-
# formatted = "\n\n".join(
|
| 55 |
-
# f"**{r['title']}**\n{r['url']}\n{r['snippet']}" for r in results[:5]
|
| 56 |
-
# )
|
| 57 |
-
# return formatted
|
| 58 |
-
|
| 59 |
-
# def search(self, query: str, domain: Optional[str] = None) -> str:
|
| 60 |
-
# for attempt in range(self.max_retries):
|
| 61 |
-
# current_time = time.time()
|
| 62 |
-
# time_since_last = current_time - self.last_request_time
|
| 63 |
-
# if time_since_last < self.min_request_interval:
|
| 64 |
-
# time.sleep(self.min_request_interval - time_since_last)
|
| 65 |
-
# try:
|
| 66 |
-
# result = self._do_search(query, domain)
|
| 67 |
-
# self.last_request_time = time.time()
|
| 68 |
-
# return result
|
| 69 |
-
# except Exception as e:
|
| 70 |
-
# time.sleep(2 ** attempt)
|
| 71 |
-
# return "Search failed after retries."
|
| 72 |
-
|
| 73 |
-
# web_search_tool = WebSearchTool()
|
| 74 |
-
|
| 75 |
-
# @tool
|
| 76 |
-
# def search_web(query: str) -> str:
|
| 77 |
-
# """
|
| 78 |
-
# Perform a full web search using DuckDuckGo HTML scraping.
|
| 79 |
-
# Returns multiple top results as text snippets.
|
| 80 |
-
# Use this when Wikipedia or standard search is not enough or fails to return numeric/statistical data.
|
| 81 |
-
# Example: "How many albums did Mercedes Sosa release between 2000 and 2009?"
|
| 82 |
-
# """
|
| 83 |
-
# print(f"🛠️ search_web called with: {query}")
|
| 84 |
-
# return web_search_tool.search(query)
|
| 85 |
-
|
| 86 |
@tool
|
| 87 |
def extract_number_from_text(text: str) -> str:
|
| 88 |
"""
|
|
@@ -98,32 +31,6 @@ def extract_number_from_text(text: str) -> str:
|
|
| 98 |
except Exception as e:
|
| 99 |
return f"Error extracting number: {e}"
|
| 100 |
|
| 101 |
-
# @tool
|
| 102 |
-
# def search_and_extract_number(query: str) -> str:
|
| 103 |
-
# """
|
| 104 |
-
# Search the web and extract a number from the top results.
|
| 105 |
-
# Best for questions like: 'How many X did Y do?' or 'What year was Z?'
|
| 106 |
-
# Example: "Mercedes Sosa studio albums 2000–2009"
|
| 107 |
-
# """
|
| 108 |
-
# print(f"🛠️ search_and_extract_number called with: {query}")
|
| 109 |
-
# raw = web_search_tool.search(query)
|
| 110 |
-
# return extract_number_from_text.invoke(raw)
|
| 111 |
-
|
| 112 |
-
# @tool
|
| 113 |
-
# def search_duckduckgo(query: str) -> str:
|
| 114 |
-
# """
|
| 115 |
-
# Perform a general-purpose web search using DuckDuckGo.
|
| 116 |
-
# Use this when no other tool applies or you need open-ended facts from the internet.
|
| 117 |
-
# Returns the top result snippet.
|
| 118 |
-
# """
|
| 119 |
-
# print(f"🛠️ search_duckduckgo called with: {query}")
|
| 120 |
-
# with DDGS() as ddgs:
|
| 121 |
-
# results = ddgs.text(query, safesearch="Moderate")
|
| 122 |
-
# if results:
|
| 123 |
-
# return results[0]["body"]
|
| 124 |
-
# else:
|
| 125 |
-
# return "No results found."
|
| 126 |
-
|
| 127 |
@tool
|
| 128 |
def search_wikipedia(query: str) -> str:
|
| 129 |
"""
|
|
@@ -141,113 +48,6 @@ def search_wikipedia(query: str) -> str:
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error: {e}"
|
| 143 |
|
| 144 |
-
# @tool
|
| 145 |
-
# def classify_vegetable(item: str) -> str:
|
| 146 |
-
# """
|
| 147 |
-
# Determine if the food item is a botanical vegetable or fruit.
|
| 148 |
-
# Use this for tasks involving scientific food classification.
|
| 149 |
-
# Returns 'vegetable' or 'fruit'.
|
| 150 |
-
# """
|
| 151 |
-
# print(f"🛠️ classify_vegetable called with: {item}")
|
| 152 |
-
# fruits = {"sweet potatoes", "plums", "bell pepper", "corn", "green beans"}
|
| 153 |
-
# if item.lower() in fruits:
|
| 154 |
-
# return "fruit"
|
| 155 |
-
# else:
|
| 156 |
-
# return "vegetable"
|
| 157 |
-
|
| 158 |
-
# @tool
|
| 159 |
-
# def solve_chess_position(fen: str) -> str:
|
| 160 |
-
# """
|
| 161 |
-
# Analyze a chess position in FEN format using Stockfish engine.
|
| 162 |
-
# Returns the best move for the current player.
|
| 163 |
-
# Use this for decision-making or evaluation in chess scenarios.
|
| 164 |
-
# """
|
| 165 |
-
# print(f"🛠️ solve_chess_position called with: {fen}")
|
| 166 |
-
# try:
|
| 167 |
-
# board = chess.Board(fen)
|
| 168 |
-
# with chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish") as engine:
|
| 169 |
-
# result = engine.play(board, chess.engine.Limit(time=0.1))
|
| 170 |
-
# return board.san(result.move)
|
| 171 |
-
# except Exception as e:
|
| 172 |
-
# return f"Error solving chess: {e}"
|
| 173 |
-
|
| 174 |
-
# @tool
|
| 175 |
-
# def math_solver(expression: str) -> str:
|
| 176 |
-
# """
|
| 177 |
-
# Solve or simplify a symbolic math expression.
|
| 178 |
-
# Use this for algebraic simplifications or symbolic math steps.
|
| 179 |
-
# """
|
| 180 |
-
# print(f"🛠️ math_solver called with: {expression}")
|
| 181 |
-
# try:
|
| 182 |
-
# result = sympy.simplify(expression)
|
| 183 |
-
# return str(result)
|
| 184 |
-
# except Exception as e:
|
| 185 |
-
# return f"Error solving math: {e}"
|
| 186 |
-
|
| 187 |
-
# @tool
|
| 188 |
-
# def document_search(filepath: str, query: str) -> str:
|
| 189 |
-
# """
|
| 190 |
-
# Search for a specific keyword or sentence inside a PDF document.
|
| 191 |
-
# Use this for information retrieval from uploaded documents.
|
| 192 |
-
# Returns the page text where the query was found.
|
| 193 |
-
# """
|
| 194 |
-
# print(f"🛠️ document_search called with: {filepath}, query={query}")
|
| 195 |
-
# try:
|
| 196 |
-
# doc = fitz.open(filepath)
|
| 197 |
-
# for page in doc:
|
| 198 |
-
# text = page.get_text()
|
| 199 |
-
# if query.lower() in text.lower():
|
| 200 |
-
# return text
|
| 201 |
-
# return "Not found."
|
| 202 |
-
# except Exception as e:
|
| 203 |
-
# return f"Error searching document: {e}"
|
| 204 |
-
|
| 205 |
-
# @tool
|
| 206 |
-
# def transcribe_audio(file_path: str) -> str:
|
| 207 |
-
# """
|
| 208 |
-
# Transcribe an audio file into text using Whisper model.
|
| 209 |
-
# Use this when the user provides audio recordings.
|
| 210 |
-
# Returns the full transcript.
|
| 211 |
-
# """
|
| 212 |
-
# print(f"🛠️ transcribe_audio called with: {file_path}")
|
| 213 |
-
# try:
|
| 214 |
-
# model = whisper.load_model("small")
|
| 215 |
-
# result = model.transcribe(file_path)
|
| 216 |
-
# return result["text"]
|
| 217 |
-
# except Exception as e:
|
| 218 |
-
# return f"Error transcribing audio: {e}"
|
| 219 |
-
|
| 220 |
-
# @tool
|
| 221 |
-
# def search_imdb(title: str) -> str:
|
| 222 |
-
# """
|
| 223 |
-
# Look up a movie or show title on IMDb.
|
| 224 |
-
# Use this to identify media titles, actors, or related entertainment metadata.
|
| 225 |
-
# Returns the best matching title.
|
| 226 |
-
# """
|
| 227 |
-
# print(f"🛠️ search_imdb called with: {title}")
|
| 228 |
-
# try:
|
| 229 |
-
# ia = IMDb()
|
| 230 |
-
# results = ia.search_movie(title)
|
| 231 |
-
# if results:
|
| 232 |
-
# return results[0]['title']
|
| 233 |
-
# return "Not found."
|
| 234 |
-
# except Exception as e:
|
| 235 |
-
# return f"Error searching IMDb: {e}"
|
| 236 |
-
|
| 237 |
-
# @tool
|
| 238 |
-
# def run_python_code(code: str) -> str:
|
| 239 |
-
# """
|
| 240 |
-
# Safely execute a block of Python code.
|
| 241 |
-
# Use this for running basic computations or logic blocks shared in string form.
|
| 242 |
-
# Returns the result of 'result' variable if defined.
|
| 243 |
-
# """
|
| 244 |
-
# print(f"🛠️ run_python_code called with code snippet: {code[:60]}...")
|
| 245 |
-
# try:
|
| 246 |
-
# exec_globals = {}
|
| 247 |
-
# exec(code, exec_globals)
|
| 248 |
-
# return str(exec_globals.get("result", "Executed"))
|
| 249 |
-
# except Exception as e:
|
| 250 |
-
# return f"Error executing code: {e}"
|
| 251 |
|
| 252 |
@tool
|
| 253 |
def analyze_youtube_video(url: str) -> str:
|
|
@@ -314,115 +114,10 @@ def fetch_video_metadata(video_id: str) -> str:
|
|
| 314 |
except Exception as e:
|
| 315 |
return f"❌ Error fetching metadata: {e}"
|
| 316 |
|
| 317 |
-
# @tool
|
| 318 |
-
# def search_baseball_stats(player: str) -> str:
|
| 319 |
-
# """
|
| 320 |
-
# Simulate a lookup of a baseball player's stats on Baseball Reference.
|
| 321 |
-
# Use this for sports questions involving MLB players.
|
| 322 |
-
# """
|
| 323 |
-
# print(f"🛠️ search_baseball_stats called with: {player}")
|
| 324 |
-
# try:
|
| 325 |
-
# url = f"https://www.baseball-reference.com/search/search.fcgi?search={player}"
|
| 326 |
-
# r = requests.get(url)
|
| 327 |
-
# return "Player found on Baseball Reference." if r.status_code == 200 else "Player not found."
|
| 328 |
-
# except Exception as e:
|
| 329 |
-
# return f"Error searching baseball stats: {e}"
|
| 330 |
-
|
| 331 |
-
# @tool
|
| 332 |
-
# def scrape_document(url: str) -> str:
|
| 333 |
-
# """
|
| 334 |
-
# Download and extract raw text from a web page.
|
| 335 |
-
# Use this when needing the full text content of an online article.
|
| 336 |
-
# """
|
| 337 |
-
# print(f"🛠️ scrape_document called with: {url}")
|
| 338 |
-
# try:
|
| 339 |
-
# r = requests.get(url)
|
| 340 |
-
# soup = BeautifulSoup(r.text, 'html.parser')
|
| 341 |
-
# return soup.get_text()
|
| 342 |
-
# except Exception as e:
|
| 343 |
-
# return f"Error scraping document: {e}"
|
| 344 |
-
|
| 345 |
-
# @tool
|
| 346 |
-
# def search_zoological_database(query: str) -> str:
|
| 347 |
-
# """
|
| 348 |
-
# Simulate a search in a zoological records database.
|
| 349 |
-
# Use for biological or species-related queries when the user references animal data.
|
| 350 |
-
# """
|
| 351 |
-
# print(f"🛠️ search_zoological_database called with: {query}")
|
| 352 |
-
# return f"Simulated search result for {query} in zoological database."
|
| 353 |
-
|
| 354 |
-
# @tool
|
| 355 |
-
# def search_olympic_database(country: str) -> str:
|
| 356 |
-
# """
|
| 357 |
-
# Simulate a country search in an Olympics dataset.
|
| 358 |
-
# Use this for questions about Olympic history, participation, or medal stats by country.
|
| 359 |
-
# """
|
| 360 |
-
# print(f"🛠️ search_olympic_database called with: {country}")
|
| 361 |
-
# return f"Simulated search for {country} in Olympics database."
|
| 362 |
-
|
| 363 |
-
# @tool
|
| 364 |
-
# def search_baseball_roster(player_number: str) -> str:
|
| 365 |
-
# """
|
| 366 |
-
# Simulate a roster lookup for a baseball player by jersey number.
|
| 367 |
-
# Use for lineup, identity, or number-matching tasks.
|
| 368 |
-
# """
|
| 369 |
-
# print(f"🛠️ search_baseball_roster called with: {player_number}")
|
| 370 |
-
# return f"Simulated search result for player number {player_number}."
|
| 371 |
-
|
| 372 |
-
# @tool
|
| 373 |
-
# def process_excel(file_path: str) -> str:
|
| 374 |
-
# """
|
| 375 |
-
# Analyze an Excel sheet to calculate total food sales.
|
| 376 |
-
# Use this for business, finance, or menu data-related tasks.
|
| 377 |
-
# Returns total food sales as formatted currency.
|
| 378 |
-
# """
|
| 379 |
-
# print(f"🛠️ process_excel called with: {file_path}")
|
| 380 |
-
# try:
|
| 381 |
-
# df = pd.read_excel(file_path)
|
| 382 |
-
# food_sales = df[df['Category'] == 'Food']['Sales'].sum()
|
| 383 |
-
# return f"{food_sales:.2f} USD"
|
| 384 |
-
# except Exception as e:
|
| 385 |
-
# return f"Error processing Excel file: {e}"
|
| 386 |
-
|
| 387 |
-
# @tool
|
| 388 |
-
# def search_classical_music_database(query: str) -> str:
|
| 389 |
-
# """
|
| 390 |
-
# Simulate a lookup in a classical music competition or archive database.
|
| 391 |
-
# Use for questions about award winners, orchestras, or composers.
|
| 392 |
-
# """
|
| 393 |
-
# print(f"🛠️ search_classical_music_database called with: {query}")
|
| 394 |
-
# return f"Simulated search result for {query} in classical music competition database."
|
| 395 |
-
|
| 396 |
-
# @tool
|
| 397 |
-
# def reverse_sentence(sentence: str) -> str:
|
| 398 |
-
# """
|
| 399 |
-
# Reverse each word in a sentence individually.
|
| 400 |
-
# Use this for reversing letter order in each word, not the sentence order.
|
| 401 |
-
# """
|
| 402 |
-
# print(f"🛠️ reverse_sentence called with: {sentence}")
|
| 403 |
-
# return " ".join(word[::-1] for word in sentence.split())
|
| 404 |
|
| 405 |
# List of all tools
|
| 406 |
all_tools = [
|
| 407 |
-
# search_and_extract_number,
|
| 408 |
-
# search_web,
|
| 409 |
extract_number_from_text,
|
| 410 |
-
# search_duckduckgo,
|
| 411 |
search_wikipedia,
|
| 412 |
analyze_youtube_video,
|
| 413 |
-
# reverse_sentence,
|
| 414 |
-
# solve_chess_position,
|
| 415 |
-
# math_solver,
|
| 416 |
-
# document_search,
|
| 417 |
-
# classify_vegetable,
|
| 418 |
-
# transcribe_audio,
|
| 419 |
-
# search_imdb,
|
| 420 |
-
# run_python_code,
|
| 421 |
-
# search_baseball_stats,
|
| 422 |
-
# scrape_document,
|
| 423 |
-
# search_zoological_database,
|
| 424 |
-
# search_olympic_database,
|
| 425 |
-
# search_baseball_roster,
|
| 426 |
-
# process_excel,
|
| 427 |
-
# search_classical_music_database,
|
| 428 |
]
|
|
|
|
| 16 |
import time
|
| 17 |
from typing import Optional, List, Dict, Any
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@tool
|
| 20 |
def extract_number_from_text(text: str) -> str:
|
| 21 |
"""
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error extracting number: {e}"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
@tool
|
| 35 |
def search_wikipedia(query: str) -> str:
|
| 36 |
"""
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"Error: {e}"
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
@tool
|
| 53 |
def analyze_youtube_video(url: str) -> str:
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
return f"❌ Error fetching metadata: {e}"
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# List of all tools
|
| 119 |
all_tools = [
|
|
|
|
|
|
|
| 120 |
extract_number_from_text,
|
|
|
|
| 121 |
search_wikipedia,
|
| 122 |
analyze_youtube_video,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
]
|