Spaces:
Sleeping
Sleeping
Upload tools.py with huggingface_hub
Browse files209 unmodified lines21021121221321421521621721821922022122222322422522622722814 unmodified lines243244245246247248209 unmodified linesreturn cached_resultstry:# Add delay to avoid rate limitingimport timetime.sleep(3.0) # 3 second delay between searchesresults = []search_results = self.ddgs.text(query, max_results=max_results)for result in search_results:results.append({'title': result.get('title', ''),'link': result.get('link', ''),'snippet': result.get('body', '')})# Cache successful resultsif results:14 unmodified lines# Return empty list for search failuresreturn []def _get_fallback_results(self, query: str) -> List[Dict[str, str]]:"""Provide fallback results for common queries when search fails"""q_lower = query.lower()209 unmodified lines21021121221321421521621714 unmodified lines232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305209 unmodified linesreturn cached_resultstry:# Try alternative search engines in sequenceresults = self._try_alternative_searches(query, max_results)# Cache successful resultsif results:14 unmodified lines# Return empty list for search failuresreturn []def _try_alternative_searches(self, query: str, max_results: int) -> List[Dict[str, str]]:"""Try alternative search engines in sequence"""# Method 1: Wikipedia API (no rate limits)try:results = self._search_wikipedia(query, max_results)if results:logger.info(f"Wikipedia search successful for '{query}'")return resultsexcept Exception as e:logger.debug(f"Wikipedia search failed: {e}")# Method 2: DuckDuckGo with retry (original method)try:import timetime.sleep(2.0) # Reduced delayresults = []search_results = self.ddgs.text(query, max_results=max_results)for result in search_results:results.append({'title': result.get('title', ''),'link': result.get('link', ''),'snippet': result.get('body', '')})if results:logger.info(f"DuckDuckGo search successful for '{query}'")return resultsexcept Exception as e:logger.debug(f"DuckDuckGo search failed: {e}")# Method 3: Direct knowledge fallbackreturn self._get_fallback_results(query)def _search_wikipedia(self, query: str, max_results: int) -> List[Dict[str, str]]:"""Search Wikipedia API directly (no rate limits)"""import requestsurl = "https://en.wikipedia.org/w/api.php"params = {'action': 'opensearch','search': query,'limit': max_results,'format': 'json'}response = requests.get(url, params=params, timeout=10)if response.status_code == 200:data = response.json()if len(data) >= 4:titles = data[1]descriptions = data[2]urls = data[3]results = []for i in range(min(len(titles), max_results)):if titles[i] and urls[i]: # Skip empty resultsresults.append({'title': titles[i],'link': urls[i],'snippet': descriptions[i] if i < len(descriptions) else ''})return resultsreturn []def _get_fallback_results(self, query: str) -> List[Dict[str, str]]:"""Provide fallback results for common queries when search fails"""q_lower = query.lower()
tools.py+70-13