Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,46 +22,45 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 22 |
@tool
|
| 23 |
def get_weather(city: str, days: Optional[int] = 1) -> str:
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
Args:
|
| 28 |
-
city (str): The city
|
| 29 |
days (Optional[int]): The number of days for the forecast (default is 1).
|
| 30 |
-
|
| 31 |
Returns:
|
| 32 |
-
str: A formatted weather report.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
-
# 1️
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# 3️⃣ Wetterdaten abrufen
|
| 50 |
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 51 |
weather_response = requests.get(weather_url)
|
| 52 |
weather_response.raise_for_status()
|
| 53 |
weather_data = weather_response.json()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
weather = weather_data["current_weather"]
|
| 57 |
temp = weather["temperature"]
|
| 58 |
windspeed = weather["windspeed"]
|
| 59 |
condition = weather["weathercode"]
|
| 60 |
|
| 61 |
-
return f"
|
| 62 |
|
| 63 |
except requests.exceptions.RequestException as e:
|
| 64 |
-
return f"
|
|
|
|
| 65 |
|
| 66 |
@tool
|
| 67 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 22 |
@tool
|
| 23 |
def get_weather(city: str, days: Optional[int] = 1) -> str:
|
| 24 |
"""
|
| 25 |
+
Fetches weather information for a given city using Open-Meteo API.
|
| 26 |
+
|
| 27 |
Args:
|
| 28 |
+
city (str): The name of the city for which to retrieve weather data.
|
| 29 |
days (Optional[int]): The number of days for the forecast (default is 1).
|
| 30 |
+
|
| 31 |
Returns:
|
| 32 |
+
str: A formatted weather report including temperature and wind speed.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
+
# 1️ Stadt in Koordinaten umwandeln
|
| 36 |
+
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
|
| 37 |
+
geo_response = requests.get(geo_url)
|
| 38 |
+
geo_response.raise_for_status()
|
| 39 |
+
geo_data = geo_response.json()
|
| 40 |
+
|
| 41 |
+
if "results" not in geo_data or len(geo_data["results"]) == 0:
|
| 42 |
+
return f"Keine Wetterdaten für {city} gefunden."
|
| 43 |
+
|
| 44 |
+
lat = geo_data["results"][0]["latitude"]
|
| 45 |
+
lon = geo_data["results"][0]["longitude"]
|
| 46 |
+
|
| 47 |
+
# 2️ Wetterdaten abrufen
|
|
|
|
|
|
|
| 48 |
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 49 |
weather_response = requests.get(weather_url)
|
| 50 |
weather_response.raise_for_status()
|
| 51 |
weather_data = weather_response.json()
|
| 52 |
|
| 53 |
+
# 3️ Wetterdaten formatieren
|
| 54 |
weather = weather_data["current_weather"]
|
| 55 |
temp = weather["temperature"]
|
| 56 |
windspeed = weather["windspeed"]
|
| 57 |
condition = weather["weathercode"]
|
| 58 |
|
| 59 |
+
return f" Wetter in {city}: {temp}°C, Windgeschwindigkeit: {windspeed} km/h, Wettercode: {condition}"
|
| 60 |
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
+
return f" Fehler beim Abrufen der Wetterdaten: {str(e)}"
|
| 63 |
+
|
| 64 |
|
| 65 |
@tool
|
| 66 |
def get_current_time_in_timezone(timezone: str) -> str:
|