Update app.py
#760
by MoussaAgne - opened
app.py
CHANGED
|
@@ -33,6 +33,97 @@ 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_current_weather(city) -> str:
|
| 38 |
+
"""
|
| 39 |
+
Retrieve the current weather for a given city.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# 1. Retrieve the city's coordinates
|
| 43 |
+
api_url = "https://geocoding-api.open-meteo.com/v1/search"
|
| 44 |
+
|
| 45 |
+
params = {
|
| 46 |
+
"name": city,
|
| 47 |
+
"count": 1,
|
| 48 |
+
"language": "fr",
|
| 49 |
+
"format": "json",
|
| 50 |
+
"countryCode": "FR"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
response = requests.get(api_url, params=params, timeout=10)
|
| 54 |
+
response.raise_for_status()
|
| 55 |
+
data = response.json()
|
| 56 |
+
|
| 57 |
+
# Check whether the city was found
|
| 58 |
+
if not data.get("results"):
|
| 59 |
+
return f"No city found for: {city}"
|
| 60 |
+
|
| 61 |
+
city_data = data["results"][0]
|
| 62 |
+
latitude = city_data["latitude"]
|
| 63 |
+
longitude = city_data["longitude"]
|
| 64 |
+
|
| 65 |
+
# 2. Retrieve the current weather
|
| 66 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
| 67 |
+
|
| 68 |
+
weather_params = {
|
| 69 |
+
"latitude": latitude,
|
| 70 |
+
"longitude": longitude,
|
| 71 |
+
"current": "temperature_2m,relative_humidity_2m,weather_code",
|
| 72 |
+
"timezone": "auto"
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
response = requests.get(
|
| 76 |
+
weather_url,
|
| 77 |
+
params=weather_params,
|
| 78 |
+
timeout=10
|
| 79 |
+
)
|
| 80 |
+
response.raise_for_status()
|
| 81 |
+
weather_data = response.json()
|
| 82 |
+
|
| 83 |
+
# 3. Extract the current weather information
|
| 84 |
+
current = weather_data["current"]
|
| 85 |
+
|
| 86 |
+
temperature = current["temperature_2m"]
|
| 87 |
+
humidity = current["relative_humidity_2m"]
|
| 88 |
+
weather_code = current["weather_code"]
|
| 89 |
+
|
| 90 |
+
# 4. Convert the weather code into a description
|
| 91 |
+
weather_descriptions = {
|
| 92 |
+
0: "Clear sky",
|
| 93 |
+
1: "Mainly clear",
|
| 94 |
+
2: "Partly cloudy",
|
| 95 |
+
3: "Overcast",
|
| 96 |
+
45: "Fog",
|
| 97 |
+
48: "Depositing rime fog",
|
| 98 |
+
51: "Light drizzle",
|
| 99 |
+
53: "Moderate drizzle",
|
| 100 |
+
55: "Dense drizzle",
|
| 101 |
+
61: "Slight rain",
|
| 102 |
+
63: "Moderate rain",
|
| 103 |
+
65: "Heavy rain",
|
| 104 |
+
71: "Slight snowfall",
|
| 105 |
+
73: "Moderate snowfall",
|
| 106 |
+
75: "Heavy snowfall",
|
| 107 |
+
80: "Slight rain showers",
|
| 108 |
+
81: "Moderate rain showers",
|
| 109 |
+
82: "Violent rain showers",
|
| 110 |
+
95: "Thunderstorm"
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
description = weather_descriptions.get(
|
| 114 |
+
weather_code,
|
| 115 |
+
"Unknown weather conditions"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# 5. Return the weather report
|
| 119 |
+
return (
|
| 120 |
+
f"In {city}, the temperature is {temperature} °C, "
|
| 121 |
+
f"with a relative humidity of {humidity}%. "
|
| 122 |
+
f"Conditions: {description}."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
print(get_current_weather("Paris"))
|
| 127 |
|
| 128 |
final_answer = FinalAnswerTool()
|
| 129 |
|