Spaces:
Sleeping
Sleeping
tool
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ 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 |
|
|
@@ -41,111 +42,11 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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 |
-
"""A function that fetches the current weather for a given location.
|
| 47 |
-
Args:
|
| 48 |
-
location: The name of the location (e.g., 'Rome, Italy') for which to fetch the weather.
|
| 49 |
-
"""
|
| 50 |
-
base_url = "https://api.open-meteo.com/v1/forecast"
|
| 51 |
-
|
| 52 |
-
lat_lng = get_latitude_longitude_by_location(location)
|
| 53 |
-
|
| 54 |
-
param = urllib.parse.urlencode(
|
| 55 |
-
{
|
| 56 |
-
"current": "weather_code",
|
| 57 |
-
"timezone": "Europe/Rome"
|
| 58 |
-
}
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
url = f"{base_url}?{lat_lng}&{param}"
|
| 62 |
-
|
| 63 |
-
try:
|
| 64 |
-
weather = urllib.request.urlopen(url)
|
| 65 |
-
except urllib.error.URLError:
|
| 66 |
-
print(f"Error while fetching the request for the weather")
|
| 67 |
-
else:
|
| 68 |
-
result = json.load(weather)
|
| 69 |
-
|
| 70 |
-
current_weather_code = result["current"]["weather_code"]
|
| 71 |
-
|
| 72 |
-
weather_descriptions = {
|
| 73 |
-
0: "Cielo sereno",
|
| 74 |
-
1: "Cielo prevalentemente sereno",
|
| 75 |
-
2: "Parzialmente nuvoloso",
|
| 76 |
-
3: "Nuvoloso",
|
| 77 |
-
45: "Nebbia",
|
| 78 |
-
48: "Nebbia brinata",
|
| 79 |
-
51: "Pioggerella leggera",
|
| 80 |
-
53: "Pioggerella moderata",
|
| 81 |
-
55: "Pioggerella intensa",
|
| 82 |
-
56: "Pioggerella ghiacciata leggera",
|
| 83 |
-
57: "Pioggerella ghiacciata intensa",
|
| 84 |
-
61: "Pioggia leggera",
|
| 85 |
-
63: "Pioggia moderata",
|
| 86 |
-
65: "Pioggia forte",
|
| 87 |
-
66: "Pioggia ghiacciata leggera",
|
| 88 |
-
67: "Pioggia ghiacciata forte",
|
| 89 |
-
71: "Nevicata leggera",
|
| 90 |
-
73: "Nevicata moderata",
|
| 91 |
-
75: "Nevicata forte",
|
| 92 |
-
77: "Grandinata",
|
| 93 |
-
80: "Rovesci leggeri",
|
| 94 |
-
81: "Rovesci moderati",
|
| 95 |
-
82: "Rovesci violenti",
|
| 96 |
-
85: "Rovesci di neve leggeri",
|
| 97 |
-
86: "Rovesci di neve forti",
|
| 98 |
-
95: "Temporale",
|
| 99 |
-
96: "Temporale con grandine leggera",
|
| 100 |
-
99: "Temporale con grandine forte"
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
description = weather_descriptions.get(current_weather_code, "Condizione meteo sconosciuta")
|
| 104 |
-
return description
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def get_latitude_longitude_by_location(location: str) -> str:
|
| 108 |
-
"""A function that fetches from a location the latitude and longitude via Google Geocoding API
|
| 109 |
-
Args:
|
| 110 |
-
location: a string passed from a tool that representing the location by string
|
| 111 |
-
"""
|
| 112 |
-
|
| 113 |
-
key = os.getenv('GOOGLE_MAPS_API')
|
| 114 |
-
base_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
| 115 |
-
|
| 116 |
-
params = urllib.parse.urlencode(
|
| 117 |
-
{
|
| 118 |
-
"address" : f"{location}",
|
| 119 |
-
"key" : f"{key}",
|
| 120 |
-
}
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
url = f"{base_url}?{params}"
|
| 124 |
-
|
| 125 |
-
try:
|
| 126 |
-
response = urllib.request.urlopen(url)
|
| 127 |
-
except urllib.error.URLError:
|
| 128 |
-
print(f"Error while fetching the request for latitude and longitude")
|
| 129 |
-
else:
|
| 130 |
-
result = json.load(response)
|
| 131 |
-
try:
|
| 132 |
-
latitude = result["results"][0]["geometry"]["location"]["lat"]
|
| 133 |
-
longitude = result["results"][0]["geometry"]["location"]["lng"]
|
| 134 |
-
|
| 135 |
-
lat_lng = urllib.parse.urlencode(
|
| 136 |
-
{
|
| 137 |
-
"latitude" : f"{latitude}",
|
| 138 |
-
"longitude" : f"{longitude}",
|
| 139 |
-
}
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
return lat_lng
|
| 143 |
-
except (KeyError, IndexError) as e:
|
| 144 |
-
print(f"Error parsing the json to fetch lat/lon: {e}")
|
| 145 |
-
|
| 146 |
|
| 147 |
final_answer = FinalAnswerTool()
|
| 148 |
|
|
|
|
|
|
|
| 149 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 150 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 151 |
|
|
@@ -165,7 +66,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 165 |
|
| 166 |
agent = CodeAgent(
|
| 167 |
model=model,
|
| 168 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 169 |
max_steps=6,
|
| 170 |
verbosity_level=1,
|
| 171 |
grammar=None,
|
|
|
|
| 10 |
import urllib.request
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
+
from tools.weather import WeatherTool
|
| 14 |
|
| 15 |
from Gradio_UI import GradioUI
|
| 16 |
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
final_answer = FinalAnswerTool()
|
| 47 |
|
| 48 |
+
weather_data = WeatherTool()
|
| 49 |
+
|
| 50 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 51 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 52 |
|
|
|
|
| 66 |
|
| 67 |
agent = CodeAgent(
|
| 68 |
model=model,
|
| 69 |
+
tools=[final_answer, weather_data], ## add your tools here (don't remove final answer)
|
| 70 |
max_steps=6,
|
| 71 |
verbosity_level=1,
|
| 72 |
grammar=None,
|