Spaces:
Sleeping
Sleeping
agharsallah
commited on
Commit
·
9f85174
1
Parent(s):
ae7a494
Adding current weather tool
Browse files- .gitignore +3 -0
- app.py +35 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.gradio
|
| 2 |
+
env
|
| 3 |
+
__pycache__
|
app.py
CHANGED
|
@@ -17,7 +17,42 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 17 |
arg2: the second argument
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 17 |
arg2: the second argument
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
+
@tool
|
| 21 |
+
def get_current_weather(city: str) -> str:
|
| 22 |
+
"""A tool that fetches the current weather in a specified city using Open Meteo API.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
city: A string representing the name of a city.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
A string containing the current weather information.
|
| 29 |
+
"""
|
| 30 |
+
# Geocode the city to get latitude and longitude (using a simple OpenStreetMap Nominatim API)
|
| 31 |
+
geocode_url = f"https://nominatim.openstreetmap.org/search?q={city}&format=json"
|
| 32 |
+
response = requests.get(geocode_url)
|
| 33 |
+
data = response.json()
|
| 34 |
|
| 35 |
+
if not data:
|
| 36 |
+
return f"City '{city}' not found."
|
| 37 |
+
|
| 38 |
+
latitude = data[0]['lat']
|
| 39 |
+
longitude = data[0]['lon']
|
| 40 |
+
|
| 41 |
+
# Fetch current weather data from Open Meteo API
|
| 42 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true"
|
| 43 |
+
weather_response = requests.get(weather_url)
|
| 44 |
+
weather_data = weather_response.json()
|
| 45 |
+
|
| 46 |
+
if 'current_weather' not in weather_data:
|
| 47 |
+
return "Failed to fetch weather data."
|
| 48 |
+
|
| 49 |
+
# Extract relevant weather information
|
| 50 |
+
current_weather = weather_data['current_weather']
|
| 51 |
+
temperature = current_weather['temperature']
|
| 52 |
+
wind_speed = current_weather['windspeed']
|
| 53 |
+
weather_description = current_weather.get('weathercode', 'N/A') # Weather description can vary
|
| 54 |
+
|
| 55 |
+
return f"Current weather in {city}:\nTemperature: {temperature}°C\nWind Speed: {wind_speed} km/h\nWeather: {weather_description}"
|
| 56 |
@tool
|
| 57 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 58 |
"""A tool that fetches the current local time in a specified timezone.
|