Spaces:
Build error
Build error
| import requests | |
| import os | |
| from huggingface_hub import list_models | |
| from langchain_community.tools import DuckDuckGoSearchResults | |
| from langchain.tools import Tool | |
| from dotenv import load_dotenv | |
| # Make sure environment variables are loaded | |
| load_dotenv() | |
| def get_weather_info(location: str) -> str: | |
| """Fetches real weather information for a given location using WeatherAPI.com.""" | |
| api_key = os.getenv("WEATHERAPI_KEY") | |
| if not api_key: | |
| return "Error: WeatherAPI key not found. Please set the WEATHERAPI_KEY environment variable." | |
| base_url = "http://api.weatherapi.com/v1/current.json" | |
| params = { | |
| "key": api_key, | |
| "q": location, | |
| "aqi": "no" # You can change this to "yes" if you want air quality info | |
| } | |
| try: | |
| response = requests.get(base_url, params=params) | |
| data = response.json() | |
| if response.status_code == 200: | |
| # Extract relevant information from the response | |
| location_data = data.get("location", {}) | |
| current_data = data.get("current", {}) | |
| location_name = location_data.get("name", "Unknown") | |
| region = location_data.get("region", "Unknown") | |
| country = location_data.get("country", "Unknown") | |
| temp_c = current_data.get("temp_c", "N/A") | |
| feelslike_c = current_data.get("feelslike_c", "N/A") | |
| condition_text = current_data.get("condition", {}).get("text", "N/A") | |
| humidity = current_data.get("humidity", "N/A") | |
| wind_kph = current_data.get("wind_kph", "N/A") | |
| return ( | |
| f"Weather in {location_name}, {region}, {country}: {condition_text}, " | |
| f"{temp_c}°C (feels like {feelslike_c}°C). Humidity: {humidity}%, Wind: {wind_kph} kph" | |
| ) | |
| else: | |
| error_message = data.get("error", {}).get("message", "Unknown error") | |
| return f"Error fetching weather data: {error_message}" | |
| except Exception as e: | |
| return f"An error occurred while fetching weather data: {str(e)}" | |
| # Initialize the tool | |
| weather_info_tool = Tool( | |
| name="get_weather_info", | |
| func=get_weather_info, | |
| description="Fetches real weather information for a given location. Provide the city name and optionally the country code (e.g., 'London' or 'London,UK')." | |
| ) | |
| def get_hub_stats(author: str) -> str: | |
| """Fetches the most downloaded model from a specific author on the Hugging Face Hub.""" | |
| try: | |
| # List models from the specified author, sorted by downloads | |
| models = list(list_models(author=author, sort="downloads", direction=-1, limit=1)) | |
| if models: | |
| model = models[0] | |
| return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads." | |
| else: | |
| return f"No models found for author {author}." | |
| except Exception as e: | |
| return f"Error fetching models for {author}: {str(e)}" | |
| # Initialize the tool | |
| hub_stats_tool = Tool( | |
| name="get_hub_stats", | |
| func=get_hub_stats, | |
| description="Fetches the most downloaded model from a specific author on the Hugging Face Hub." | |
| ) | |
| search_tool = DuckDuckGoSearchResults() |