Spaces:
Sleeping
Sleeping
Add tool to get coordinates of a city from an API
Browse files
app.py
CHANGED
@@ -4,19 +4,55 @@ 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
|
13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -55,7 +91,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
+
from typing import List, Dict, Union
|
8 |
+
import time
|
9 |
+
|
10 |
|
11 |
from Gradio_UI import GradioUI
|
12 |
|
13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
14 |
@tool
|
15 |
+
def get_city_coordinates(city_name: str, country_code: str = None) -> List[Dict[str, Union[str, float]]]:
|
16 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
17 |
+
"""
|
18 |
+
A tool that retrieves geographical coordinates and location details for a given city using Nominatim API.
|
19 |
+
|
20 |
+
The function queries OpenStreetMap's Nominatim service to get location data.
|
21 |
+
It includes a 1-second delay to respect API rate limits and returns multiple matches if found.
|
22 |
+
|
23 |
Args:
|
24 |
+
city_name: Name of the city to search for
|
25 |
+
country_code: Optional two-letter country code (ISO 3166-1 alpha-2) to filter results
|
26 |
"""
|
27 |
+
time.sleep(1) # Rate limiting
|
28 |
+
|
29 |
+
params = {
|
30 |
+
'q': city_name,
|
31 |
+
'format': 'json',
|
32 |
+
'addressdetails': 1,
|
33 |
+
'countrycodes': country_code if country_code else None
|
34 |
+
}
|
35 |
+
|
36 |
+
try:
|
37 |
+
response = requests.get(
|
38 |
+
"https://nominatim.openstreetmap.org/search",
|
39 |
+
params={k: v for k, v in params.items() if v is not None},
|
40 |
+
headers={'User-Agent': 'MyGeocodingApp/1.0'}
|
41 |
+
)
|
42 |
+
response.raise_for_status()
|
43 |
+
|
44 |
+
return [{
|
45 |
+
'name': city_name,
|
46 |
+
'latitude': float(result['lat']),
|
47 |
+
'longitude': float(result['lon']),
|
48 |
+
'country': result['address'].get('country', 'Unknown'),
|
49 |
+
'state': result['address'].get('state', 'Unknown'),
|
50 |
+
'type': result['type']
|
51 |
+
} for result in response.json()]
|
52 |
+
|
53 |
+
except requests.exceptions.RequestException as e:
|
54 |
+
print(f"Error fetching coordinates: {e}")
|
55 |
+
return []
|
56 |
|
57 |
@tool
|
58 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
91 |
|
92 |
agent = CodeAgent(
|
93 |
model=model,
|
94 |
+
tools=[get_city_coordinates, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
|
95 |
max_steps=6,
|
96 |
verbosity_level=1,
|
97 |
grammar=None,
|