Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,10 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
from typing import Optional, Dict, Any
|
11 |
-
from pydantic import BaseModel, Field, validator
|
12 |
-
|
13 |
-
from typing import Optional, Dict, Any
|
14 |
import requests
|
15 |
from pydantic import BaseModel, Field, validator
|
16 |
|
17 |
-
# No change to PDOKLocationSearchInput needed, as it *was* correctly defined
|
18 |
class PDOKLocationSearchInput(BaseModel):
|
19 |
"""Input for the PDOK location search tool."""
|
20 |
postal_code: Optional[str] = Field(None, description="Postal code in the format '1234 AA'.")
|
@@ -35,31 +31,33 @@ class PDOKLocationSearchInput(BaseModel):
|
|
35 |
elif self.street_name and self.city and self.house_number:
|
36 |
return f"{self.street_name} {self.house_number}, {self.city}"
|
37 |
else:
|
38 |
-
return ""
|
39 |
|
40 |
@tool
|
41 |
-
def
|
42 |
-
"""
|
43 |
|
44 |
Args:
|
45 |
postal_code: Postal code in the format '1234 AA'.
|
46 |
house_number: House number.
|
47 |
street_name: Street name.
|
48 |
city: City name.
|
|
|
|
|
|
|
49 |
"""
|
50 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
51 |
headers = {"accept": "application/json"}
|
52 |
|
53 |
-
# Create an instance of the Pydantic model from the function arguments
|
54 |
input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
|
55 |
-
|
56 |
query_string = input_data.construct_query()
|
|
|
57 |
if not query_string:
|
58 |
return "Please provide either a postal code and house number, or a street name, city, and house number."
|
59 |
|
60 |
params = {
|
61 |
"q": query_string,
|
62 |
-
"fl": "
|
63 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
64 |
"df": "tekst",
|
65 |
"bq": "type:provincie^1.5",
|
@@ -84,18 +82,47 @@ def pdok_location_search(postal_code: Optional[str] = None, house_number: Option
|
|
84 |
return "No results found for the given query."
|
85 |
|
86 |
first_result = docs[0]
|
87 |
-
provincie_naam = first_result.get("provincienaam")
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
except requests.exceptions.RequestException as e:
|
95 |
return f"Error during API request: {e}"
|
96 |
except (ValueError, KeyError) as e:
|
97 |
return f"Error processing API response: {e}"
|
98 |
|
|
|
|
|
|
|
|
|
99 |
@tool
|
100 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
101 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
from typing import Optional, Dict, Any, List
|
|
|
|
|
|
|
11 |
import requests
|
12 |
from pydantic import BaseModel, Field, validator
|
13 |
|
|
|
14 |
class PDOKLocationSearchInput(BaseModel):
|
15 |
"""Input for the PDOK location search tool."""
|
16 |
postal_code: Optional[str] = Field(None, description="Postal code in the format '1234 AA'.")
|
|
|
31 |
elif self.street_name and self.city and self.house_number:
|
32 |
return f"{self.street_name} {self.house_number}, {self.city}"
|
33 |
else:
|
34 |
+
return ""
|
35 |
|
36 |
@tool
|
37 |
+
def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> str:
|
38 |
+
"""Provides information about a Dutch address or postal code.
|
39 |
|
40 |
Args:
|
41 |
postal_code: Postal code in the format '1234 AA'.
|
42 |
house_number: House number.
|
43 |
street_name: Street name.
|
44 |
city: City name.
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
A string containing relevant information about the location, or an error message.
|
48 |
"""
|
49 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
50 |
headers = {"accept": "application/json"}
|
51 |
|
|
|
52 |
input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
|
|
|
53 |
query_string = input_data.construct_query()
|
54 |
+
|
55 |
if not query_string:
|
56 |
return "Please provide either a postal code and house number, or a street name, city, and house number."
|
57 |
|
58 |
params = {
|
59 |
"q": query_string,
|
60 |
+
"fl": "*", # Request all fields
|
61 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
62 |
"df": "tekst",
|
63 |
"bq": "type:provincie^1.5",
|
|
|
82 |
return "No results found for the given query."
|
83 |
|
84 |
first_result = docs[0]
|
|
|
85 |
|
86 |
+
# Format the output in a more readable and comprehensive way
|
87 |
+
output_lines = []
|
88 |
+
output_lines.append(f"Information about: {first_result.get('weergavenaam', 'N/A')}")
|
89 |
+
output_lines.append("-" * 30)
|
90 |
+
|
91 |
+
# Key information, handle potential missing values gracefully
|
92 |
+
output_lines.append(f" Type: {first_result.get('type', 'N/A')}")
|
93 |
+
output_lines.append(f" Street: {first_result.get('straatnaam', 'N/A')}")
|
94 |
+
output_lines.append(f" House Number: {first_result.get('huisnummer', 'N/A')}")
|
95 |
+
if 'huisletter' in first_result:
|
96 |
+
output_lines.append(f" House Letter: {first_result.get('huisletter', 'N/A')}")
|
97 |
+
if 'huisnummertoevoeging' in first_result:
|
98 |
+
output_lines.append(f" House Number Addition: {first_result.get('huisnummertoevoeging', 'N/A')}")
|
99 |
+
output_lines.append(f" Postal Code: {first_result.get('postcode', 'N/A')}")
|
100 |
+
output_lines.append(f" City: {first_result.get('woonplaatsnaam', 'N/A')}")
|
101 |
+
output_lines.append(f" Municipality: {first_result.get('gemeentenaam', 'N/A')}")
|
102 |
+
output_lines.append(f" Province: {first_result.get('provincienaam', 'N/A')}")
|
103 |
+
output_lines.append(f" Coordinates (Lat/Lon): {first_result.get('centroide_ll', 'N/A')}")
|
104 |
+
output_lines.append(f" Coordinates (RD): {first_result.get('centroide_rd', 'N/A')}")
|
105 |
+
|
106 |
+
# Add more fields if they are present and relevant
|
107 |
+
if 'buurtnaam' in first_result:
|
108 |
+
output_lines.append(f" Neighborhood: {first_result.get('buurtnaam', 'N/A')}")
|
109 |
+
if 'wijknaam' in first_result:
|
110 |
+
output_lines.append(f" District: {first_result.get('wijknaam', 'N/A')}")
|
111 |
+
if 'waterschapsnaam' in first_result:
|
112 |
+
output_lines.append(f" Water Authority: {first_result.get('waterschapsnaam', 'N/A')}")
|
113 |
+
|
114 |
+
|
115 |
+
return "\n".join(output_lines)
|
116 |
|
117 |
except requests.exceptions.RequestException as e:
|
118 |
return f"Error during API request: {e}"
|
119 |
except (ValueError, KeyError) as e:
|
120 |
return f"Error processing API response: {e}"
|
121 |
|
122 |
+
# Corrected call: Use the correct function name (pdok_location_info)
|
123 |
+
location_info = pdok_location_info(street_name="Emmastraat", house_number="13", city="Den Haag")
|
124 |
+
return (location_info)
|
125 |
+
|
126 |
@tool
|
127 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
128 |
#Keep this format for the description / args / args description but feel free to modify the tool
|