21spl commited on
Commit
b5e871f
·
verified ·
1 Parent(s): be01ce9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -15,12 +15,21 @@ import time
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
 
@@ -30,10 +39,12 @@ def get_weather(city: str) -> str:
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
  else:
38
  return "Error fetching weather data."
39
 
 
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 the current weather for a specified city using Open-Meteo API (No API Key Needed).
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
 
 
39
 
40
  if weather_response.status_code == 200:
41
  weather_data = weather_response.json()["current_weather"]
 
42
  temp = weather_data["temperature"]
43
  wind_speed = weather_data["windspeed"]
44
+
45
+ return (f"Weather in {city}:
46
+ Temperature: {temp}°C
47
+ Wind Speed: {wind_speed} m/s")
48
  else:
49
  return "Error fetching weather data."
50