Lurosm commited on
Commit
99b8e29
·
verified ·
1 Parent(s): 5d102e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -6,7 +6,6 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
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
@@ -21,7 +20,7 @@ class PDOKLocationSearchInput(BaseModel):
21
  @validator("postal_code")
22
  def validate_postal_code(cls, v):
23
  if v is not None and (len(v) != 7 or not v[0:4].isdigit() or v[4] != " " or not v[5:7].isalpha()):
24
- raise ValueError("Invalid postal code format. It must be '1234 AA'.")
25
  return v
26
 
27
  def construct_query(self) -> str:
@@ -58,8 +57,9 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
58
  city: City name.
59
 
60
  Returns:
61
- A string containing relevant information about the location, or an error message.
62
  """
 
63
  base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
64
  headers = {"accept": "application/json"}
65
 
@@ -67,7 +67,7 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
67
  query_string = input_data.construct_query()
68
 
69
  if not query_string:
70
- return "Please provide either a postal code and house number, or a street name, city, and house number."
71
 
72
  params = {
73
  "q": query_string,
@@ -97,16 +97,22 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
97
 
98
  first_result = docs[0]
99
  location_info = PDOKLocationInfo(**first_result)
100
-
101
  # Format the output in a more user-friendly way
102
  print(f"Information about: {location_info.weergavenaam}")
103
  print("-" * 30)
104
  for field, value in location_info.dict().items():
105
  print(f" {field.capitalize()}: {value}")
106
-
107
- # Add more features as needed, such as geocoding, reverse geocoding, and caching
108
  return location_info
109
 
 
 
 
 
 
 
 
110
  @tool
111
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
112
  #Keep this format for the description / args / args description but feel free to modify the tool
 
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
11
  from pydantic import BaseModel, Field, validator
 
20
  @validator("postal_code")
21
  def validate_postal_code(cls, v):
22
  if v is not None and (len(v) != 7 or not v[0:4].isdigit() or v[4] != " " or not v[5:7].isalpha()):
23
+ raise ValueError("Invalid postal code format. It must be '1234 AA'.")
24
  return v
25
 
26
  def construct_query(self) -> str:
 
57
  city: City name.
58
 
59
  Returns:
60
+ A PDOKLocationInfo object containing the location information, or None if no results are found.
61
  """
62
+
63
  base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
64
  headers = {"accept": "application/json"}
65
 
 
67
  query_string = input_data.construct_query()
68
 
69
  if not query_string:
70
+ return None
71
 
72
  params = {
73
  "q": query_string,
 
97
 
98
  first_result = docs[0]
99
  location_info = PDOKLocationInfo(**first_result)
100
+
101
  # Format the output in a more user-friendly way
102
  print(f"Information about: {location_info.weergavenaam}")
103
  print("-" * 30)
104
  for field, value in location_info.dict().items():
105
  print(f" {field.capitalize()}: {value}")
106
+
 
107
  return location_info
108
 
109
+ except requests.exceptions.RequestException as e:
110
+ print(f"Error during API request: {e}")
111
+ return None
112
+ except (ValueError, KeyError) as e:
113
+ print(f"Error processing API response: {e}")
114
+ return None
115
+
116
  @tool
117
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
118
  #Keep this format for the description / args / args description but feel free to modify the tool