Spaces:
Runtime error
Runtime error
| import random | |
| from httpcore import TimeoutException | |
| from huggingface_hub import list_models | |
| from langchain_community.tools import DuckDuckGoSearchRun | |
| from langchain_core.tools import Tool | |
| from selenium import webdriver | |
| driver = webdriver.Chrome() | |
| from selenium.webdriver.support.ui import WebDriverWait | |
| from selenium.webdriver.support import expected_conditions as EC | |
| import os | |
| try: | |
| from langchain_tavily import TavilySearch | |
| tavily_api_key = os.getenv("TAVILY_API_KEY") | |
| if tavily_api_key: | |
| tavilytool = TavilySearch( | |
| max_results=2, | |
| search_depth="advanced", # deep crawl mode | |
| include_raw_content=True, # returns full page text, not just snippets | |
| tavily_api_key=tavily_api_key | |
| ) | |
| else: | |
| tavilytool = None | |
| except ImportError: | |
| tavilytool = None | |
| class ToolRegistry: | |
| """ | |
| A registry that defines and exposes all LangChain tools. | |
| Import this class in downstream code and access tools via class methods or attributes. | |
| """ | |
| # ------------------------------------------------------------------ # | |
| # Internal implementations # | |
| # ------------------------------------------------------------------ # | |
| def _weather_info(location: str) -> str: | |
| """Fetches dummy weather information for a given location.""" | |
| weather_conditions = [ | |
| {"condition": "Rainy", "temp_c": 15}, | |
| {"condition": "Clear", "temp_c": 25}, | |
| {"condition": "Windy", "temp_c": 20}, | |
| ] | |
| data = random.choice(weather_conditions) | |
| return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" | |
| def _get_hub_stats(author: str) -> str: | |
| """Fetches the most downloaded model from a specific author on the Hugging Face Hub.""" | |
| try: | |
| models = list(list_models(author=author, sort="downloads", limit=1)) | |
| if models: | |
| model = models[0] | |
| return ( | |
| f"The most downloaded model by {author} is {model.id} " | |
| f"with {model.downloads:,} downloads." | |
| ) | |
| return f"No models found for author {author}." | |
| except Exception as e: | |
| return f"Error fetching models for {author}: {str(e)}" | |
| def _browse_url(url: str) -> str: | |
| """Opens a URL and returns the page text""" | |
| try: | |
| driver.get(url) | |
| WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located(("tag name", "body"))) | |
| return driver.find_element("tag name", "body").text[:3000] | |
| except TimeoutException as e: | |
| return f"Timeout loading {url}: {str(e)}. Current URL: {driver.current_url}" | |
| def _click_element(selector: str) -> str: | |
| """Clicks an element by CSS selector""" | |
| try: | |
| element = WebDriverWait(driver, 20).until( | |
| EC.element_to_be_clickable(("css selector", selector)) | |
| ) | |
| element.click() | |
| return f"Clicked {selector}" | |
| except TimeoutException as e: | |
| return f"Timeout clicking {selector}: {str(e)}. Current URL: {driver.current_url}" | |
| # ------------------------------------------------------------------ # | |
| # Tool factories # | |
| # ------------------------------------------------------------------ # | |
| def weather_info_tool(cls) -> Tool: | |
| """Returns a LangChain Tool for fetching weather information.""" | |
| return Tool( | |
| name="WeatherInfoTool", | |
| func=cls._weather_info, | |
| description="Retrieves weather information for a given location.", | |
| ) | |
| def hub_stats_tool(cls) -> Tool: | |
| """Returns a LangChain Tool for fetching Hugging Face Hub stats.""" | |
| return Tool( | |
| name="get_hub_stats", | |
| func=cls._get_hub_stats, | |
| description=( | |
| "Fetches the most downloaded model from a specific author " | |
| "on the Hugging Face Hub." | |
| ), | |
| ) | |
| def duck_duck_go_search_tool(cls) -> DuckDuckGoSearchRun: | |
| """Returns a LangChain DuckDuckGo search tool.""" | |
| return DuckDuckGoSearchRun() | |
| def tavily_search_tool(cls) -> TavilySearch: | |
| """Returns a LangChain Tavily search tool.""" | |
| if tavilytool is None: | |
| raise ValueError("Tavily tool not available. Please install langchain-tavily and set TAVILY_API_KEY environment variable.") | |
| return tavilytool | |
| # ------------------------------------------------------------------ # | |
| # Convenience: get all tools at once # | |
| # ------------------------------------------------------------------ # | |
| def get_all_tools(cls) -> list[Tool]: | |
| """Returns a list of all registered tools — ready to pass to an agent.""" | |
| return [ | |
| cls.weather_info_tool(), | |
| cls.hub_stats_tool(), | |
| cls.duck_duck_go_search_tool(), | |
| ] | |