Lurosm commited on
Commit
38be34b
·
verified ·
1 Parent(s): e0d70a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -4
app.py CHANGED
@@ -4,7 +4,7 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
  from typing import Optional, Dict, Any, List
10
  import requests
@@ -114,14 +114,73 @@ 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
- location_info = PDOKLocationInfo(**first_result)
118
- return location_info.model_dump_json()
119
-
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  except requests.exceptions.RequestException as e:
121
  return f"Error during API request: {e}\nDebug info:\n" + "\n".join(debug_info)
122
  except (ValueError, KeyError) as e:
123
  return f"Error processing API response: {e}\nDebug info:\n" + "\n".join(debug_info)
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  @tool
126
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
127
  #Keep this format for the description / args / args description but feel free to modify the tool
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ import json
8
  from Gradio_UI import GradioUI
9
  from typing import Optional, Dict, Any, List
10
  import requests
 
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", ""),
121
+ "postcode": first_result.get("postcode", ""),
122
+ "woonplaatsnaam": first_result.get("woonplaatsnaam", ""),
123
+ "gemeentenaam": first_result.get("gemeentenaam", ""),
124
+ "provincienaam": first_result.get("provincienaam", ""),
125
+ "buurtnaam": first_result.get("buurtnaam", ""),
126
+ "wijknaam": first_result.get("wijknaam", ""),
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
+ # Usage code:
139
+ location_info_str = pdok_location_info(postal_code=None, house_number="13", street_name="Emmastraat", city="Den Haag")
140
+
141
+ try:
142
+ # Parse the JSON string into a dictionary
143
+ location_info = json.loads(location_info_str)
144
+
145
+ # Create info dictionary with safe access to potentially missing fields
146
+ info = {}
147
+
148
+ # Basic fields
149
+ for field in ["straatnaam", "huisnummer", "postcode", "woonplaatsnaam", "gemeentenaam", "provincienaam", "buurtnaam", "wijknaam"]:
150
+ info[field] = location_info.get(field, "N/A")
151
+
152
+ # Coordinate fields that need parsing
153
+ if location_info.get("centroide_ll"):
154
+ try:
155
+ ll_parts = location_info["centroide_ll"].replace("POINT(", "").replace(")", "").split()
156
+ info["Latitude (WGS84)"] = ll_parts[0] if len(ll_parts) > 0 else "N/A"
157
+ info["Longitude (WGS84)"] = ll_parts[1] if len(ll_parts) > 1 else "N/A"
158
+ except:
159
+ info["Latitude (WGS84)"] = "N/A"
160
+ info["Longitude (WGS84)"] = "N/A"
161
+
162
+ if location_info.get("centroide_rd"):
163
+ try:
164
+ rd_parts = location_info["centroide_rd"].replace("POINT(", "").replace(")", "").split()
165
+ info["X (RD)"] = rd_parts[0] if len(rd_parts) > 0 else "N/A"
166
+ info["Y (RD)"] = rd_parts[1] if len(rd_parts) > 1 else "N/A"
167
+ except:
168
+ info["X (RD)"] = "N/A"
169
+ info["Y (RD)"] = "N/A"
170
+
171
+ # Print the information
172
+ print("Location Information:")
173
+ print("-" * 50)
174
+ for key, value in info.items():
175
+ print(f"{key}: {value}")
176
+
177
+ except json.JSONDecodeError:
178
+ print("Error: Unable to parse location information")
179
+ print(f"Raw response: {location_info_str}")
180
+ except Exception as e:
181
+ print(f"Error processing location information: {str(e)}")
182
+ print(f"Raw response: {location_info_str}")
183
+
184
  @tool
185
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
186
  #Keep this format for the description / args / args description but feel free to modify the tool