Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ class PDOKLocationSearchInput(BaseModel):
|
|
| 32 |
else:
|
| 33 |
return ""
|
| 34 |
|
| 35 |
-
class PDOKLocationInfo(BaseModel):
|
| 36 |
type: str
|
| 37 |
weergavenaam: str
|
| 38 |
straatnaam: Optional[str] = None
|
|
@@ -51,7 +51,7 @@ class PDOKLocationInfo(BaseModel):
|
|
| 51 |
|
| 52 |
@tool
|
| 53 |
def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> str:
|
| 54 |
-
"""Provides information about a Dutch address or postal code.
|
| 55 |
|
| 56 |
Args:
|
| 57 |
postal_code: Postal code in the format '1234 AA'.
|
|
@@ -60,7 +60,7 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 60 |
city: Name of the city.
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
-
str: JSON string containing the location information
|
| 64 |
"""
|
| 65 |
|
| 66 |
debug_info = []
|
|
@@ -84,16 +84,14 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 84 |
params = {
|
| 85 |
"q": query_string,
|
| 86 |
"fl": "*",
|
| 87 |
-
"fq": "type:(gemeente OR woonplaats OR
|
| 88 |
"df": "tekst",
|
| 89 |
-
"bq": "type:provincie^1.5",
|
| 90 |
"bq": "type:gemeente^1.5",
|
| 91 |
"bq": "type:woonplaats^1.5",
|
| 92 |
-
"bq": "type:weg^1.5",
|
| 93 |
"bq": "type:postcode^0.5",
|
| 94 |
"bq": "type:adres^1",
|
| 95 |
"start": 0,
|
| 96 |
-
"rows":
|
| 97 |
"sort": "score desc,sortering asc,weergavenaam asc",
|
| 98 |
"wt": "json",
|
| 99 |
}
|
|
@@ -114,7 +112,7 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 114 |
first_result = docs[0]
|
| 115 |
debug_info.append(f"First result data: {first_result}")
|
| 116 |
|
| 117 |
-
# Create a dictionary with only the fields we need
|
| 118 |
location_info = {
|
| 119 |
"straatnaam": first_result.get("straatnaam", ""),
|
| 120 |
"huisnummer": first_result.get("huisnummer", ""),
|
|
@@ -127,48 +125,21 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 127 |
"centroide_ll": first_result.get("centroide_ll", ""),
|
| 128 |
"centroide_rd": first_result.get("centroide_rd", "")
|
| 129 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
return json.dumps(location_info)
|
| 132 |
|
| 133 |
except requests.exceptions.RequestException as e:
|
| 134 |
return f"Error during API request: {e}\nDebug info:\n" + "\n".join(debug_info)
|
| 135 |
except (ValueError, KeyError) as e:
|
| 136 |
return f"Error processing API response: {e}\nDebug info:\n" + "\n".join(debug_info)
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
location_info_str = pdok_location_info(postal_code=None, house_number="13", street_name="Emmastraat", city="Den Haag")
|
| 140 |
-
|
| 141 |
-
try:
|
| 142 |
-
location_info = json.loads(location_info_str)
|
| 143 |
-
if location_info:
|
| 144 |
-
print(f"Street Name: {location_info.get('straatnaam', 'N/A')}")
|
| 145 |
-
print(f"House Number: {location_info.get('huisnummer', 'N/A')}")
|
| 146 |
-
print(f"Postal Code: {location_info.get('postcode', 'N/A')}")
|
| 147 |
-
print(f"City: {location_info.get('woonplaatsnaam', 'N/A')}")
|
| 148 |
-
print(f"Municipality: {location_info.get('gemeentenaam', 'N/A')}")
|
| 149 |
-
print(f"Province: {location_info.get('provincienaam', 'N/A')}")
|
| 150 |
-
print(f"Neighborhood: {location_info.get('buurtnaam', 'N/A')}")
|
| 151 |
-
print(f"District: {location_info.get('wijknaam', 'N/A')}")
|
| 152 |
-
print(f"Centroid (Lat/Lon): {location_info.get('centroide_ll', 'N/A')}")
|
| 153 |
-
print(f"Centroid (RD): {location_info.get('centroide_rd', 'N/A')}")
|
| 154 |
-
|
| 155 |
-
# Add Google Maps link
|
| 156 |
-
centroide_ll = location_info.get('centroide_ll')
|
| 157 |
-
if centroide_ll:
|
| 158 |
-
# Extract latitude and longitude from POINT(lon lat)
|
| 159 |
-
lon, lat = centroide_ll.replace("POINT(", "").replace(")", "").split()
|
| 160 |
-
google_maps_url = f"https://www.google.com/maps/search/?api=1&query={lat},{lon}"
|
| 161 |
-
print(f"Google Maps Link: {google_maps_url}")
|
| 162 |
-
|
| 163 |
-
else:
|
| 164 |
-
print("No information found for the given address.")
|
| 165 |
-
except json.JSONDecodeError:
|
| 166 |
-
print(f"Error parsing response: {location_info_str}")
|
| 167 |
-
except Exception as e:
|
| 168 |
-
print(f"Error: {str(e)}")
|
| 169 |
-
print(f"Raw response: {location_info_str}")
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
@tool
|
| 174 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 32 |
else:
|
| 33 |
return ""
|
| 34 |
|
| 35 |
+
class PDOKLocationInfo(BaseModel): # This is not used in the tool's return anymore
|
| 36 |
type: str
|
| 37 |
weergavenaam: str
|
| 38 |
straatnaam: Optional[str] = None
|
|
|
|
| 51 |
|
| 52 |
@tool
|
| 53 |
def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> str:
|
| 54 |
+
"""Provides information about a Dutch address or postal code, including a Google Maps link.
|
| 55 |
|
| 56 |
Args:
|
| 57 |
postal_code: Postal code in the format '1234 AA'.
|
|
|
|
| 60 |
city: Name of the city.
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
+
str: JSON string containing the location information, including a Google Maps link, or an error message.
|
| 64 |
"""
|
| 65 |
|
| 66 |
debug_info = []
|
|
|
|
| 84 |
params = {
|
| 85 |
"q": query_string,
|
| 86 |
"fl": "*",
|
| 87 |
+
"fq": "type:(gemeente OR woonplaats OR postcode OR adres)",
|
| 88 |
"df": "tekst",
|
|
|
|
| 89 |
"bq": "type:gemeente^1.5",
|
| 90 |
"bq": "type:woonplaats^1.5",
|
|
|
|
| 91 |
"bq": "type:postcode^0.5",
|
| 92 |
"bq": "type:adres^1",
|
| 93 |
"start": 0,
|
| 94 |
+
"rows": 1,
|
| 95 |
"sort": "score desc,sortering asc,weergavenaam asc",
|
| 96 |
"wt": "json",
|
| 97 |
}
|
|
|
|
| 112 |
first_result = docs[0]
|
| 113 |
debug_info.append(f"First result data: {first_result}")
|
| 114 |
|
| 115 |
+
# Create a dictionary with only the fields we need, *including* the Google Maps link
|
| 116 |
location_info = {
|
| 117 |
"straatnaam": first_result.get("straatnaam", ""),
|
| 118 |
"huisnummer": first_result.get("huisnummer", ""),
|
|
|
|
| 125 |
"centroide_ll": first_result.get("centroide_ll", ""),
|
| 126 |
"centroide_rd": first_result.get("centroide_rd", "")
|
| 127 |
}
|
| 128 |
+
|
| 129 |
+
# Add Google Maps link *to the dictionary that will be returned*
|
| 130 |
+
centroide_ll = location_info.get('centroide_ll')
|
| 131 |
+
if centroide_ll:
|
| 132 |
+
lon, lat = centroide_ll.replace("POINT(", "").replace(")", "").split()
|
| 133 |
+
location_info["google_maps_url"] = f"https://www.google.com/maps/search/?api=1&query={lat},{lon}"
|
| 134 |
|
| 135 |
+
return json.dumps(location_info) # Return the dictionary as a JSON string
|
| 136 |
|
| 137 |
except requests.exceptions.RequestException as e:
|
| 138 |
return f"Error during API request: {e}\nDebug info:\n" + "\n".join(debug_info)
|
| 139 |
except (ValueError, KeyError) as e:
|
| 140 |
return f"Error processing API response: {e}\nDebug info:\n" + "\n".join(debug_info)
|
| 141 |
|
| 142 |
+
# --- Rest of your code (agent definition, etc.) remains the same ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
@tool
|
| 145 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|