Spaces:
Sleeping
Sleeping
new weather tool
Browse files- app.py +115 -0
- tools/weather.py +103 -0
app.py
CHANGED
|
@@ -3,10 +3,18 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
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(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
@@ -32,7 +40,114 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
import urllib.error
|
| 9 |
+
import urllib.parse
|
| 10 |
+
import urllib.request
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 19 |
@tool
|
| 20 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 40 |
return f"The current local time in {timezone} is: {local_time}"
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 43 |
+
|
| 44 |
+
@tool
|
| 45 |
+
def get_current_weather_on_location(location: str) -> str:
|
| 46 |
+
"""
|
| 47 |
+
A function that from a location fetches the current weather on a location. This tool use the function to fetch
|
| 48 |
+
from a location the latitude and the longitude via Geocoding called get_latitude_longitude_by_location. After fetched the
|
| 49 |
+
latitude and the longitude, call via API the open-meteo API for getting the weather.
|
| 50 |
+
Args:
|
| 51 |
+
location: a string that representing the location that the user want to know the weather
|
| 52 |
+
"""
|
| 53 |
+
base_url = "https://api.open-meteo.com/v1/forecast"
|
| 54 |
+
|
| 55 |
+
lat_lng = get_latitude_longitude_by_location(location)
|
| 56 |
+
|
| 57 |
+
param = urllib.parse.urlencode(
|
| 58 |
+
{
|
| 59 |
+
"current": "weather_code",
|
| 60 |
+
"timezone": "Europe/Rome"
|
| 61 |
+
}
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
url = f"{base_url}?{lat_lng}&{param}"
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
weather = urllib.request.urlopen(url)
|
| 68 |
+
except urllib.error.URLError:
|
| 69 |
+
print(f"Error while fetching the request for the weather")
|
| 70 |
+
else:
|
| 71 |
+
result = json.load(weather)
|
| 72 |
+
|
| 73 |
+
current_weather_code = result["current"]["weather_code"]
|
| 74 |
+
|
| 75 |
+
weather_descriptions = {
|
| 76 |
+
0: "Cielo sereno",
|
| 77 |
+
1: "Cielo prevalentemente sereno",
|
| 78 |
+
2: "Parzialmente nuvoloso",
|
| 79 |
+
3: "Nuvoloso",
|
| 80 |
+
45: "Nebbia",
|
| 81 |
+
48: "Nebbia brinata",
|
| 82 |
+
51: "Pioggerella leggera",
|
| 83 |
+
53: "Pioggerella moderata",
|
| 84 |
+
55: "Pioggerella intensa",
|
| 85 |
+
56: "Pioggerella ghiacciata leggera",
|
| 86 |
+
57: "Pioggerella ghiacciata intensa",
|
| 87 |
+
61: "Pioggia leggera",
|
| 88 |
+
63: "Pioggia moderata",
|
| 89 |
+
65: "Pioggia forte",
|
| 90 |
+
66: "Pioggia ghiacciata leggera",
|
| 91 |
+
67: "Pioggia ghiacciata forte",
|
| 92 |
+
71: "Nevicata leggera",
|
| 93 |
+
73: "Nevicata moderata",
|
| 94 |
+
75: "Nevicata forte",
|
| 95 |
+
77: "Grandinata",
|
| 96 |
+
80: "Rovesci leggeri",
|
| 97 |
+
81: "Rovesci moderati",
|
| 98 |
+
82: "Rovesci violenti",
|
| 99 |
+
85: "Rovesci di neve leggeri",
|
| 100 |
+
86: "Rovesci di neve forti",
|
| 101 |
+
95: "Temporale",
|
| 102 |
+
96: "Temporale con grandine leggera",
|
| 103 |
+
99: "Temporale con grandine forte"
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
description = weather_descriptions.get(current_weather_code, "Condizione meteo sconosciuta")
|
| 107 |
+
return description
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
@function
|
| 111 |
+
def get_latitude_longitude_by_location(location: str) -> str:
|
| 112 |
+
"""
|
| 113 |
+
A function that fetches from a location the latitude and longitude via Google Geocoding API
|
| 114 |
+
Args:
|
| 115 |
+
location: a string passed from a tool that representing the location by string
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
key = os.getenv('GOOGLE_MAPS_API')
|
| 119 |
+
base_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
| 120 |
+
|
| 121 |
+
params = urllib.parse.urlencode(
|
| 122 |
+
{
|
| 123 |
+
"address" : f"{location}",
|
| 124 |
+
"key" : f"{key}",
|
| 125 |
+
}
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
url = f"{base_url}?{params}"
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
response = urllib.request.urlopen(url)
|
| 132 |
+
except urllib.error.URLError:
|
| 133 |
+
print(f"Error while fetching the request for latitude and longitude")
|
| 134 |
+
else:
|
| 135 |
+
result = json.load(response)
|
| 136 |
+
try:
|
| 137 |
+
latitude = result["results"][0]["geometry"]["location"]["lat"]
|
| 138 |
+
longitude = result["results"][0]["geometry"]["location"]["lng"]
|
| 139 |
+
|
| 140 |
+
lat_lng = urllib.parse.urlencode(
|
| 141 |
+
{
|
| 142 |
+
"latitude" : f"{latitude}",
|
| 143 |
+
"longitude" : f"{longitude}",
|
| 144 |
+
}
|
| 145 |
+
)
|
| 146 |
|
| 147 |
+
return lat_lng
|
| 148 |
+
except (KeyError, IndexError) as e:
|
| 149 |
+
print(f"Error parsing the json to fetch lat/lon: {e}")
|
| 150 |
+
|
| 151 |
|
| 152 |
final_answer = FinalAnswerTool()
|
| 153 |
|
tools/weather.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import urllib.error
|
| 4 |
+
import urllib.parse
|
| 5 |
+
import urllib.request
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from smolagents.tools import Tool
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
class WeatherTool(Tool):
|
| 12 |
+
def get_current_weather_on_location(location: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
A function that from a location fetches the current weather on a location. This tool use the function to fetch
|
| 15 |
+
from a location the latitude and the longitude via Geocoding called get_latitude_longitude_by_location. After fetched the
|
| 16 |
+
latitude and the longitude, call via API the open-meteo API for getting the weather.
|
| 17 |
+
Args:
|
| 18 |
+
location: a string that representing the location that the user want to know the weather
|
| 19 |
+
"""
|
| 20 |
+
base_url = "https://api.open-meteo.com/v1/forecast"
|
| 21 |
+
|
| 22 |
+
key = os.getenv('GOOGLE_MAPS_API')
|
| 23 |
+
maps_base_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
| 24 |
+
|
| 25 |
+
params = urllib.parse.urlencode(
|
| 26 |
+
{
|
| 27 |
+
"address" : f"{location}",
|
| 28 |
+
"key" : f"{key}",
|
| 29 |
+
}
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
url = f"{maps_base_url}?{params}"
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = urllib.request.urlopen(url)
|
| 36 |
+
except urllib.error.URLError:
|
| 37 |
+
print(f"Error while fetching the request for latitude and longitude")
|
| 38 |
+
else:
|
| 39 |
+
result = json.load(response)
|
| 40 |
+
try:
|
| 41 |
+
latitude = result["results"][0]["geometry"]["location"]["lat"]
|
| 42 |
+
longitude = result["results"][0]["geometry"]["location"]["lng"]
|
| 43 |
+
except (KeyError, IndexError) as e:
|
| 44 |
+
print(f"Error parsing the json to fetch lat/lon: {e}")
|
| 45 |
+
|
| 46 |
+
param = urllib.parse.urlencode(
|
| 47 |
+
{
|
| 48 |
+
"latitude" : f"{latitude}",
|
| 49 |
+
"longitude" : f"{longitude}",
|
| 50 |
+
"current": "weather_code",
|
| 51 |
+
"timezone": "Europe/Rome"
|
| 52 |
+
}
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
url = f"{base_url}?{param}"
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
weather = urllib.request.urlopen(url)
|
| 59 |
+
except urllib.error.URLError:
|
| 60 |
+
print(f"Error while fetching the request for the weather")
|
| 61 |
+
else:
|
| 62 |
+
result = json.load(weather)
|
| 63 |
+
|
| 64 |
+
current_weather_code = result["current"]["weather_code"]
|
| 65 |
+
|
| 66 |
+
weather_descriptions = {
|
| 67 |
+
0: "Cielo sereno",
|
| 68 |
+
1: "Cielo prevalentemente sereno",
|
| 69 |
+
2: "Parzialmente nuvoloso",
|
| 70 |
+
3: "Nuvoloso",
|
| 71 |
+
45: "Nebbia",
|
| 72 |
+
48: "Nebbia brinata",
|
| 73 |
+
51: "Pioggerella leggera",
|
| 74 |
+
53: "Pioggerella moderata",
|
| 75 |
+
55: "Pioggerella intensa",
|
| 76 |
+
56: "Pioggerella ghiacciata leggera",
|
| 77 |
+
57: "Pioggerella ghiacciata intensa",
|
| 78 |
+
61: "Pioggia leggera",
|
| 79 |
+
63: "Pioggia moderata",
|
| 80 |
+
65: "Pioggia forte",
|
| 81 |
+
66: "Pioggia ghiacciata leggera",
|
| 82 |
+
67: "Pioggia ghiacciata forte",
|
| 83 |
+
71: "Nevicata leggera",
|
| 84 |
+
73: "Nevicata moderata",
|
| 85 |
+
75: "Nevicata forte",
|
| 86 |
+
77: "Grandinata",
|
| 87 |
+
80: "Rovesci leggeri",
|
| 88 |
+
81: "Rovesci moderati",
|
| 89 |
+
82: "Rovesci violenti",
|
| 90 |
+
85: "Rovesci di neve leggeri",
|
| 91 |
+
86: "Rovesci di neve forti",
|
| 92 |
+
95: "Temporale",
|
| 93 |
+
96: "Temporale con grandine leggera",
|
| 94 |
+
99: "Temporale con grandine forte"
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
description = weather_descriptions.get(current_weather_code, "Condizione meteo sconosciuta")
|
| 98 |
+
return description
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|