Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,41 +15,26 @@ import time
|
|
15 |
|
16 |
HF_TOKEN = os.getenv("HF_TOKEN", "default_hf_token_if_not_found")
|
17 |
|
18 |
-
|
19 |
@tool
|
20 |
def get_weather(city: str) -> str:
|
21 |
-
"""Fetches
|
22 |
Args:
|
23 |
city: The name of the city.
|
24 |
"""
|
25 |
-
try:
|
26 |
-
# Get city coordinates from Open-Meteo's geocoding API
|
27 |
-
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
28 |
-
geo_response = requests.get(geo_url)
|
29 |
-
|
30 |
-
if geo_response.status_code == 200 and geo_response.json().get("results"):
|
31 |
-
city_data = geo_response.json()["results"][0]
|
32 |
-
lat, lon = city_data["latitude"], city_data["longitude"]
|
33 |
else:
|
34 |
return f"Error: Unable to find location for '{city}'."
|
35 |
|
36 |
-
# Fetch
|
37 |
-
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true
|
38 |
weather_response = requests.get(weather_url)
|
39 |
|
40 |
if weather_response.status_code == 200:
|
41 |
weather_data = weather_response.json()["current_weather"]
|
42 |
-
|
43 |
temp = weather_data["temperature"]
|
44 |
wind_speed = weather_data["windspeed"]
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
return (f"Weather in {city}:\n"
|
49 |
-
f"Temperature: {temp}°C\n"
|
50 |
-
f"Humidity: {humidity}%\n"
|
51 |
-
f"Wind Speed: {wind_speed} m/s\n"
|
52 |
-
f"Chance of Rain: {precipitation_chance}%")
|
53 |
else:
|
54 |
return "Error fetching weather data."
|
55 |
|
|
|
15 |
|
16 |
HF_TOKEN = os.getenv("HF_TOKEN", "default_hf_token_if_not_found")
|
17 |
|
|
|
18 |
@tool
|
19 |
def get_weather(city: str) -> str:
|
20 |
+
"""Fetches the current weather for a specified city using Open-Meteo API (No API Key Needed).
|
21 |
Args:
|
22 |
city: The name of the city.
|
23 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
else:
|
25 |
return f"Error: Unable to find location for '{city}'."
|
26 |
|
27 |
+
# Fetch weather data
|
28 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
29 |
weather_response = requests.get(weather_url)
|
30 |
|
31 |
if weather_response.status_code == 200:
|
32 |
weather_data = weather_response.json()["current_weather"]
|
33 |
+
|
34 |
temp = weather_data["temperature"]
|
35 |
wind_speed = weather_data["windspeed"]
|
36 |
+
return f"Weather in {city}: Temp: {temp}°C, Wind Speed: {wind_speed} m/s"
|
37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
return "Error fetching weather data."
|
40 |
|