Spaces:
Runtime error
Runtime error
File size: 1,591 Bytes
ab6d29f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import requests
import os
from datetime import datetime
def add_weather_context(location="London"):
"""Add current weather context to the query"""
try:
api_key = os.getenv("OPENWEATHER_API_KEY")
if not api_key:
return "Weather data unavailable (API key not configured)"
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
return f"Current weather in {location}: {data['weather'][0]['description']}, {data['main']['temp']}°C"
except Exception as e:
return f"Weather data unavailable: {str(e)}"
def add_space_weather_context():
"""Add space weather context to the query"""
try:
api_key = os.getenv("NASA_API_KEY")
if not api_key:
return "Space weather data unavailable (API key not configured)"
# Using a different NASA endpoint that doesn't require parameters
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
return f"Space context: Astronomy Picture of the Day - {data.get('title', 'N/A')}"
except Exception as e:
return f"Space weather data unavailable: {str(e)}"
def add_time_context():
"""Add current time context"""
now = datetime.now()
return f"Current date and time: {now.strftime('%Y-%m-%d %H:%M:%S %Z')}"
|