Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,50 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def my_custom_tool(city: str, info_type: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
A tool that fetches real-time weather or time information for a given city.
|
| 15 |
Args:
|
| 16 |
+
city: The name of the city (e.g., 'London').
|
| 17 |
+
info_type: Either 'weather' or 'time'. Determines what info to fetch.
|
| 18 |
+
Returns:
|
| 19 |
+
A string with the requested information.
|
| 20 |
"""
|
| 21 |
+
if info_type.lower() == "weather":
|
| 22 |
+
# Example using Open-Meteo's free API (no key required)
|
| 23 |
+
try:
|
| 24 |
+
url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
| 25 |
+
geo = requests.get(url).json()
|
| 26 |
+
if not geo.get("results"):
|
| 27 |
+
return f"Could not find location for city '{city}'."
|
| 28 |
+
lat = geo["results"][0]["latitude"]
|
| 29 |
+
lon = geo["results"][0]["longitude"]
|
| 30 |
+
weather_url = (
|
| 31 |
+
f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 32 |
+
)
|
| 33 |
+
weather = requests.get(weather_url).json()
|
| 34 |
+
temp = weather["current_weather"]["temperature"]
|
| 35 |
+
wind = weather["current_weather"]["windspeed"]
|
| 36 |
+
return f"The current weather in {city} is {temp}°C with wind speed {wind} km/h."
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error fetching weather: {str(e)}"
|
| 39 |
+
elif info_type.lower() == "time":
|
| 40 |
+
try:
|
| 41 |
+
# Use WorldTimeAPI for time info
|
| 42 |
+
url = f"http://worldtimeapi.org/api/timezone"
|
| 43 |
+
timezones = requests.get(url).json()
|
| 44 |
+
# Try to find a timezone containing the city name
|
| 45 |
+
tz = next((z for z in timezones if city.replace(" ", "_") in z), None)
|
| 46 |
+
if not tz:
|
| 47 |
+
return f"Could not find timezone for city '{city}'."
|
| 48 |
+
time_url = f"http://worldtimeapi.org/api/timezone/{tz}"
|
| 49 |
+
time_data = requests.get(time_url).json()
|
| 50 |
+
datetime_str = time_data["datetime"]
|
| 51 |
+
return f"The current local time in {city} is {datetime_str}."
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error fetching time: {str(e)}"
|
| 54 |
+
else:
|
| 55 |
+
return "info_type must be either 'weather' or 'time'."
|
| 56 |
|
| 57 |
@tool
|
| 58 |
def get_current_time_in_timezone(timezone: str) -> str:
|