yaraa11 commited on
Commit
7f61a62
·
verified ·
1 Parent(s): fe023ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py CHANGED
@@ -33,6 +33,51 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ @tool
37
+ def get_weather(city: str) -> str:
38
+ """A tool that fetches the current weather for a given city, without requiring an API key.
39
+ Uses the free Open-Meteo API.
40
+ Args:
41
+ city: The name of the city to get weather for.
42
+ Returns:
43
+ A description of the current weather.
44
+ """
45
+ try:
46
+ # 1) Geocode city name → latitude/longitude
47
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
48
+ geo_response = requests.get(geo_url).json()
49
+
50
+ if "results" not in geo_response or len(geo_response["results"]) == 0:
51
+ return f"Could not find location for: {city}"
52
+
53
+ latitude = geo_response["results"][0]["latitude"]
54
+ longitude = geo_response["results"][0]["longitude"]
55
+ resolved_name = geo_response["results"][0]["name"]
56
+
57
+ # 2) Fetch current weather from Open-Meteo
58
+ weather_url = (
59
+ f"https://api.open-meteo.com/v1/forecast?"
60
+ f"latitude={latitude}&longitude={longitude}&current_weather=true"
61
+ )
62
+ weather_response = requests.get(weather_url).json()
63
+
64
+ if "current_weather" not in weather_response:
65
+ return f"No current weather data available for {resolved_name}"
66
+
67
+ weather = weather_response["current_weather"]
68
+ temp = weather["temperature"]
69
+ wind = weather["windspeed"]
70
+ code = weather["weathercode"]
71
+
72
+ return (
73
+ f"Weather in {resolved_name}:\n"
74
+ f"- Temperature: {temp}°C\n"
75
+ f"- Wind speed: {wind} km/h\n"
76
+ f"- Weather code: {code}"
77
+ )
78
+
79
+ except Exception as e:
80
+ return f"Error fetching weather: {str(e)}"
81
 
82
  final_answer = FinalAnswerTool()
83