Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,12 +16,9 @@ def get_weather_api_key() -> str:
|
|
16 |
"""
|
17 |
api_key = '978b53bb6550f5b81b031a823a63f5dc'
|
18 |
#api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
19 |
-
if api_key:
|
20 |
-
return api_key
|
21 |
-
else:
|
22 |
-
return "Error: OpenWeatherMap API key not found in environment variables."
|
23 |
-
|
24 |
|
|
|
|
|
25 |
@tool
|
26 |
def get_weather_forecast(city: str, date: str, api_key: str) -> str:
|
27 |
"""Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
|
@@ -72,37 +69,60 @@ def get_weather_forecast(city: str, date: str, api_key: str) -> str:
|
|
72 |
return "Invalid date format. Please use YYYY-MM-DD."
|
73 |
except KeyError:
|
74 |
return "Weather data not available or API key is invalid"
|
|
|
75 |
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
Args:
|
80 |
-
|
|
|
|
|
81 |
|
82 |
Returns:
|
83 |
-
|
84 |
"""
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
|
108 |
@tool
|
|
|
16 |
"""
|
17 |
api_key = '978b53bb6550f5b81b031a823a63f5dc'
|
18 |
#api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
return api_key
|
21 |
+
'''
|
22 |
@tool
|
23 |
def get_weather_forecast(city: str, date: str, api_key: str) -> str:
|
24 |
"""Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
|
|
|
69 |
return "Invalid date format. Please use YYYY-MM-DD."
|
70 |
except KeyError:
|
71 |
return "Weather data not available or API key is invalid"
|
72 |
+
'''
|
73 |
|
74 |
+
@tool
|
75 |
+
def get_weather_forecast(city: str, date: str, api_key: str) -> str:
|
76 |
+
"""Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
|
77 |
|
78 |
Args:
|
79 |
+
city: The name of the city.
|
80 |
+
date: The date in YYYY-MM-DD format.
|
81 |
+
api_key: Your OpenWeatherMap API key.
|
82 |
|
83 |
Returns:
|
84 |
+
A string containing the weather forecast, or an error message.
|
85 |
"""
|
86 |
+
try:
|
87 |
+
date_obj = datetime.datetime.strptime(date, "%Y-%m-%d").date()
|
88 |
+
today = datetime.date.today()
|
89 |
+
if date_obj < today:
|
90 |
+
return "Cannot get weather from past dates."
|
91 |
|
92 |
+
# Convert date to Unix timestamp for OpenWeatherMap API
|
93 |
+
dt = int(datetime.datetime.combine(date_obj, datetime.datetime.min.time()).timestamp())
|
94 |
|
95 |
+
# Get latitude and longitude of the city using geocoding API
|
96 |
+
geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
|
97 |
+
geocoding_response = requests.get(geocoding_url)
|
98 |
+
geocoding_data = geocoding_response.json()
|
99 |
|
100 |
+
if not geocoding_data:
|
101 |
+
return f"Could not find coordinates for {city}."
|
102 |
+
|
103 |
+
lat = geocoding_data[0]["lat"]
|
104 |
+
lon = geocoding_data[0]["lon"]
|
105 |
+
|
106 |
+
# Get weather data using One Call API day_summary endpoint
|
107 |
+
weather_url = f"https://api.openweathermap.org/data/3.0/onecall/day_summary?lat={lat}&lon={lon}&date={dt}&appid={api_key}"
|
108 |
+
weather_response = requests.get(weather_url)
|
109 |
+
weather_data = weather_response.json()
|
110 |
+
|
111 |
+
if "weather" in weather_data and weather_data["weather"]:
|
112 |
+
weather_description = weather_data["weather"][0]["description"]
|
113 |
+
temp_max = weather_data["temp"]["max"]
|
114 |
+
return f"{weather_description}, with a high of {temp_max} degrees Celsius."
|
115 |
+
elif "message" in weather_data:
|
116 |
+
return f"OpenWeatherMap API Error: {weather_data['message']}"
|
117 |
+
else:
|
118 |
+
return "Weather data not available for the specified date."
|
119 |
+
|
120 |
+
except requests.exceptions.RequestException as e:
|
121 |
+
return f"Error connecting to OpenWeatherMap API: {e}"
|
122 |
+
except ValueError:
|
123 |
+
return "Invalid date format. Please use YYYY-MM-DD."
|
124 |
+
except KeyError:
|
125 |
+
return "Weather data not available or API key is invalid"
|
126 |
|
127 |
|
128 |
@tool
|