Spaces:
Paused
Paused
Download tools.py from ApeMaster/gala-agent: direct link, hf CLI and curl.
- Browser
- Download file 6.44 kB
-
https://huggingface.co/spaces/ApeMaster/gala-agent/resolve/main/tools.py
- Command line
-
hf download hf://spaces/ApeMaster/gala-agent/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/ApeMaster/gala-agent/resolve/main/tools.py
6.44 kB
| import requests | |
| from smolagents import Tool | |
| # -------------------------------------------------- | |
| # WEATHER TOOL | |
| # -------------------------------------------------- | |
| class WeatherInfoTool(Tool): | |
| name = "weather_info" | |
| description = ( | |
| "Get the current weather for a city using the Open-Meteo API. " | |
| "Provide a city name." | |
| ) | |
| inputs = { | |
| "city": { | |
| "type": "string", | |
| "description": "The city to get the weather for." | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, city: str) -> str: | |
| try: | |
| # Find city coordinates | |
| geo_url = "https://geocoding-api.open-meteo.com/v1/search" | |
| geo_response = requests.get( | |
| geo_url, | |
| params={"name": city, "count": 1}, | |
| timeout=15 | |
| ) | |
| geo_response.raise_for_status() | |
| geo_data = geo_response.json() | |
| if not geo_data.get("results"): | |
| return f"I couldn't find the location: {city}" | |
| location = geo_data["results"][0] | |
| latitude = location["latitude"] | |
| longitude = location["longitude"] | |
| # Get current weather | |
| weather_url = "https://api.open-meteo.com/v1/forecast" | |
| weather_response = requests.get( | |
| weather_url, | |
| params={ | |
| "latitude": latitude, | |
| "longitude": longitude, | |
| "current": "temperature_2m,relative_humidity_2m," | |
| "apparent_temperature,weather_code," | |
| "wind_speed_10m" | |
| }, | |
| timeout=15 | |
| ) | |
| weather_response.raise_for_status() | |
| weather = weather_response.json().get("current", {}) | |
| return ( | |
| f"Weather for {location.get('name', city)}, " | |
| f"{location.get('country', '')}:\n" | |
| f"Temperature: {weather.get('temperature_2m')}°C\n" | |
| f"Feels like: {weather.get('apparent_temperature')}°C\n" | |
| f"Humidity: {weather.get('relative_humidity_2m')}%\n" | |
| f"Wind speed: {weather.get('wind_speed_10m')} km/h\n" | |
| f"Weather code: {weather.get('weather_code')}" | |
| ) | |
| except Exception as e: | |
| return f"Weather lookup failed: {str(e)}" | |
| # -------------------------------------------------- | |
| # HUGGING FACE HUB STATISTICS TOOL | |
| # -------------------------------------------------- | |
| class HubStatsTool(Tool): | |
| name = "hugging_face_hub_stats" | |
| description = ( | |
| "Search for popular machine learning models on Hugging Face. " | |
| "Provide a search query such as text-generation or image-classification." | |
| ) | |
| inputs = { | |
| "query": { | |
| "type": "string", | |
| "description": "The model task or keyword to search for." | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, query: str) -> str: | |
| try: | |
| from huggingface_hub import HfApi | |
| api = HfApi() | |
| models = api.list_models( | |
| search=query, | |
| sort="downloads", | |
| direction=-1, | |
| limit=5 | |
| ) | |
| results = [] | |
| for model in models: | |
| results.append( | |
| f"Model: {model.id}\n" | |
| f"Downloads: {getattr(model, 'downloads', 'Unknown')}\n" | |
| f"Likes: {getattr(model, 'likes', 'Unknown')}\n" | |
| ) | |
| if not results: | |
| return f"No Hugging Face models found for: {query}" | |
| return "\n---\n".join(results) | |
| except Exception as e: | |
| return f"Hugging Face search failed: {str(e)}" | |
| # -------------------------------------------------- | |
| # GUEST INFORMATION RETRIEVER TOOL | |
| # -------------------------------------------------- | |
| class GuestInfoRetrieverTool(Tool): | |
| name = "guest_information_retriever" | |
| description = ( | |
| "Search the Hugging Face Agents Course guest information dataset. " | |
| "Use this tool to answer questions about course guests, their " | |
| "backgrounds, expertise, and other information in the dataset. " | |
| "Provide a search query." | |
| ) | |
| inputs = { | |
| "query": { | |
| "type": "string", | |
| "description": "The question or information to search for." | |
| } | |
| } | |
| output_type = "string" | |
| def __init__(self): | |
| super().__init__() | |
| self.dataset = None | |
| self.initialization_error = None | |
| try: | |
| from datasets import load_dataset | |
| from langchain_community.retrievers import BM25Retriever | |
| from langchain_core.documents import Document | |
| dataset = load_dataset( | |
| "agents-course/unit3-invitees", | |
| split="train" | |
| ) | |
| documents = [] | |
| for row in dataset: | |
| content = "\n".join( | |
| f"{key}: {value}" | |
| for key, value in row.items() | |
| if value is not None | |
| ) | |
| documents.append( | |
| Document( | |
| page_content=content, | |
| metadata={"source": "agents-course/unit3-invitees"} | |
| ) | |
| ) | |
| if documents: | |
| self.retriever = BM25Retriever.from_documents(documents) | |
| self.retriever.k = 3 | |
| else: | |
| self.retriever = None | |
| except Exception as e: | |
| self.retriever = None | |
| self.initialization_error = str(e) | |
| print(f"Guest retriever initialization error: {e}") | |
| def forward(self, query: str) -> str: | |
| if self.retriever is None: | |
| return ( | |
| "The guest information dataset could not be loaded. " | |
| f"Initialization error: {self.initialization_error}" | |
| ) | |
| try: | |
| documents = self.retriever.invoke(query) | |
| if not documents: | |
| return "No matching guest information was found." | |
| results = [] | |
| for document in documents: | |
| results.append(document.page_content) | |
| return "\n\n---\n\n".join(results) | |
| except Exception as e: | |
| return f"Guest information search failed: {str(e)}" | |