Spaces:
Sleeping
Sleeping
added weather api tool from tomorrow.io
Browse files
app.py
CHANGED
|
@@ -9,15 +9,136 @@ 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
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
| 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.
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def my_weather_tool(location: str) -> str:
|
| 13 |
+
"""A tool that fetches the current weather for a location using Tomorrow.io API.
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
+
location: Location name (e.g., 'New York') or lat,lon coordinates (e.g., '40.7128,-74.0060')
|
|
|
|
| 17 |
"""
|
| 18 |
+
# Get API key from environment
|
| 19 |
+
api_key = os.getenv("VYK6ASAfHq4z7TzyljTaZg5nICJKFmL8")
|
| 20 |
+
if not api_key:
|
| 21 |
+
return "Error: TOMORROW_API_KEY environment variable is not set. Please set it to use this tool."
|
| 22 |
+
|
| 23 |
+
# API endpoints
|
| 24 |
+
geocoding_endpoint = "https://api.tomorrow.io/v4/geocoding"
|
| 25 |
+
realtime_endpoint = "https://api.tomorrow.io/v4/weather/realtime"
|
| 26 |
+
|
| 27 |
+
# Weather code to description mapping
|
| 28 |
+
weather_codes = {
|
| 29 |
+
0: "Unknown",
|
| 30 |
+
1000: "Clear, Sunny",
|
| 31 |
+
1100: "Mostly Clear",
|
| 32 |
+
1101: "Partly Cloudy",
|
| 33 |
+
1102: "Mostly Cloudy",
|
| 34 |
+
1001: "Cloudy",
|
| 35 |
+
2000: "Fog",
|
| 36 |
+
2100: "Light Fog",
|
| 37 |
+
4000: "Drizzle",
|
| 38 |
+
4001: "Rain",
|
| 39 |
+
4200: "Light Rain",
|
| 40 |
+
4201: "Heavy Rain",
|
| 41 |
+
5000: "Snow",
|
| 42 |
+
5001: "Flurries",
|
| 43 |
+
5100: "Light Snow",
|
| 44 |
+
5101: "Heavy Snow",
|
| 45 |
+
6000: "Freezing Drizzle",
|
| 46 |
+
6001: "Freezing Rain",
|
| 47 |
+
6200: "Light Freezing Rain",
|
| 48 |
+
6201: "Heavy Freezing Rain",
|
| 49 |
+
7000: "Ice Pellets",
|
| 50 |
+
7101: "Heavy Ice Pellets",
|
| 51 |
+
7102: "Light Ice Pellets",
|
| 52 |
+
8000: "Thunderstorm"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Determine if input is coordinates or location name
|
| 57 |
+
if "," in location and all(
|
| 58 |
+
part.replace("-", "").replace(".", "").strip().isdigit()
|
| 59 |
+
for part in location.split(",", 1)
|
| 60 |
+
):
|
| 61 |
+
# Input appears to be coordinates
|
| 62 |
+
lat, lon = map(float, location.split(",", 1))
|
| 63 |
+
location_name = f"Coordinates ({lat}, {lon})"
|
| 64 |
+
else:
|
| 65 |
+
# Input is a location name, geocode it
|
| 66 |
+
params = {
|
| 67 |
+
"apikey": api_key,
|
| 68 |
+
"query": location
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
response = requests.get(geocoding_endpoint, params=params)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
|
| 74 |
+
data = response.json()
|
| 75 |
+
|
| 76 |
+
# Check if we got valid results
|
| 77 |
+
if data.get("data") and len(data["data"]) > 0:
|
| 78 |
+
# Use the first (best) match
|
| 79 |
+
location_data = data["data"][0]
|
| 80 |
+
lat = location_data["latitude"]
|
| 81 |
+
lon = location_data["longitude"]
|
| 82 |
+
location_name = f"{location_data.get('name', '')}, {location_data.get('country', '')}"
|
| 83 |
+
else:
|
| 84 |
+
return f"Location '{location}' not found. Please try a different location name or coordinates."
|
| 85 |
+
|
| 86 |
+
# Get current weather data
|
| 87 |
+
params = {
|
| 88 |
+
"apikey": api_key,
|
| 89 |
+
"location": f"{lat},{lon}",
|
| 90 |
+
"units": "metric"
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
response = requests.get(realtime_endpoint, params=params)
|
| 94 |
+
response.raise_for_status()
|
| 95 |
+
|
| 96 |
+
# Parse weather data
|
| 97 |
+
weather_data = response.json()
|
| 98 |
+
values = weather_data.get("data", {}).get("values", {})
|
| 99 |
+
|
| 100 |
+
# Get timestamp
|
| 101 |
+
timestamp = weather_data.get("data", {}).get("time")
|
| 102 |
+
if timestamp:
|
| 103 |
+
try:
|
| 104 |
+
dt = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
|
| 105 |
+
formatted_time = dt.strftime("%Y-%m-%d %H:%M:%S %Z")
|
| 106 |
+
except:
|
| 107 |
+
formatted_time = timestamp
|
| 108 |
+
else:
|
| 109 |
+
formatted_time = "Unknown"
|
| 110 |
+
|
| 111 |
+
# Get weather code and description
|
| 112 |
+
weather_code = values.get("weatherCode")
|
| 113 |
+
weather_description = weather_codes.get(weather_code, "Unknown")
|
| 114 |
+
|
| 115 |
+
# Create a readable weather report
|
| 116 |
+
weather_report = f"""
|
| 117 |
+
Current Weather for {location_name} (as of {formatted_time}):
|
| 118 |
+
------------------------------------------------------------
|
| 119 |
+
Condition: {weather_description}
|
| 120 |
+
Temperature: {values.get('temperature', 'N/A')}°C (feels like {values.get('temperatureApparent', 'N/A')}°C)
|
| 121 |
+
Humidity: {values.get('humidity', 'N/A')}%
|
| 122 |
+
Wind: {values.get('windSpeed', 'N/A')} m/s, gusting to {values.get('windGust', 'N/A')} m/s, from {values.get('windDirection', 'N/A')}°
|
| 123 |
+
Visibility: {values.get('visibility', 'N/A')} km
|
| 124 |
+
UV Index: {values.get('uvIndex', 'N/A')}
|
| 125 |
+
Cloud Cover: {values.get('cloudCover', 'N/A')}%
|
| 126 |
+
Precipitation Probability: {values.get('precipitationProbability', 'N/A')}%
|
| 127 |
+
Rain Intensity: {values.get('rainIntensity', 'N/A')} mm/hr
|
| 128 |
+
Snow Intensity: {values.get('snowIntensity', 'N/A')} mm/hr
|
| 129 |
+
Pressure: {values.get('pressureSurfaceLevel', 'N/A')} hPa
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
return weather_report.strip()
|
| 133 |
+
|
| 134 |
+
except requests.exceptions.HTTPError as e:
|
| 135 |
+
status_code = e.response.status_code if hasattr(e, 'response') else "unknown"
|
| 136 |
+
error_detail = e.response.text if hasattr(e, 'response') else str(e)
|
| 137 |
+
|
| 138 |
+
return f"HTTP error ({status_code}) while fetching weather data: {error_detail}"
|
| 139 |
+
|
| 140 |
+
except Exception as e:
|
| 141 |
+
return f"Error fetching weather data: {str(e)}"
|
| 142 |
@tool
|
| 143 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 144 |
"""A tool that fetches the current local time in a specified timezone.
|