Download utils/weather.py from Abhlash/BurnerBot: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/Abhlash/BurnerBot/resolve/main/utils/weather.py
- Command line
-
hf download hf://spaces/Abhlash/BurnerBot/utils/weather.py
-
curl -L -o weather.py https://huggingface.co/spaces/Abhlash/BurnerBot/resolve/main/utils/weather.py
2.72 kB
| import os | |
| import requests | |
| from datetime import datetime | |
| import logging | |
| from config import WEATHER_API_KEY, GROQ_API_KEY | |
| from .weather_agent import WeatherAgent | |
| # Set up logging | |
| logger = logging.getLogger(__name__) | |
| # Initialize the weather agent with API key | |
| weather_agent = WeatherAgent(groq_api_key=GROQ_API_KEY) | |
| def get_weather(): | |
| """Fetch weather data from OpenWeather API""" | |
| logger.info("Attempting to fetch weather data from OpenWeather API") | |
| # Coordinates for Black Rock Desert, Nevada | |
| lat = 40.7864 | |
| lon = -119.2065 | |
| # Use Current Weather Data API endpoint | |
| url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon=-119.2065&units=imperial&appid={WEATHER_API_KEY}" | |
| logger.info(f"Using API key: {WEATHER_API_KEY[:5]}...") | |
| logger.info(f"Using URL: {url}") | |
| try: | |
| logger.info("Making request to OpenWeather API") | |
| response = requests.get(url) | |
| if response.status_code == 401: | |
| logger.error(f"OpenWeather API key is invalid or expired. Status: {response.status_code}") | |
| logger.error(f"Response text: {response.text}") | |
| return None | |
| response.raise_for_status() | |
| data = response.json() | |
| logger.info("Successfully received weather data") | |
| # Format current weather data | |
| forecast = [{ | |
| "date": datetime.fromtimestamp(data['dt']).strftime("%Y-%m-%d"), | |
| "high": round(data['main']['temp_max']), | |
| "low": round(data['main']['temp_min']), | |
| "conditions": data['weather'][0]['main'], | |
| "description": data['weather'][0]['description'], | |
| "wind_speed": round(data['wind']['speed']), | |
| "humidity": data['main']['humidity'], | |
| "feels_like": round(data['main']['feels_like']), | |
| "pressure": data['main']['pressure'], | |
| "visibility": data['visibility'] | |
| }] | |
| logger.info(f"Processed weather data successfully") | |
| return forecast | |
| except requests.exceptions.RequestException as e: | |
| logger.error(f"Error fetching weather data: {str(e)}") | |
| if hasattr(e.response, 'text'): | |
| logger.error(f"API Response: {e.response.text}") | |
| return None | |
| def format_weather_report(forecast): | |
| """Get AI-enhanced weather report""" | |
| logger.info("Formatting weather report with AI analysis") | |
| try: | |
| report = weather_agent.analyze_weather_data(forecast) | |
| logger.info("Successfully generated AI weather analysis") | |
| return report | |
| except Exception as e: | |
| logger.error(f"Error in AI analysis, falling back to basic format: {str(e)}") | |
| return weather_agent.format_basic_forecast(forecast) |