Lurosm commited on
Commit
8423570
·
verified ·
1 Parent(s): fa54933

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -96
app.py CHANGED
@@ -8,109 +8,109 @@ from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
  from typing import Optional, Dict, Any, List
10
  import requests
11
- from pydantic import BaseModel, Field, validator, FieldValidator
12
 
13
  class PDOKLocationSearchInput(BaseModel):
14
- postal_code: Optional[str] = Field(None, description="Postal code in the format '1234 AA'.")
15
- house_number: Optional[str] = Field(None, description="House number.")
16
- street_name: Optional[str] = Field(None, description="Street name.")
17
- city: Optional[str] = Field(None, description="City name.")
18
-
19
- @field_validator('postal_code')
20
- def validate_postal_code(cls, v):
21
- if v is not None and (len(v) != 7 or not v[0:4].isdigit() or v[4] != " " or not v[5:7].isalpha()):
22
- raise ValueError("Invalid postal code format. It must be '1234 AA'.")
23
- return v
24
-
25
- def construct_query(self) -> str:
26
- """Constructs the query string based on provided inputs."""
27
- if self.postal_code and self.house_number:
28
- return f"{self.postal_code} {self.house_number}"
29
- elif self.street_name and self.city and self.house_number:
30
- return f"{self.street_name} {self.house_number}, {self.city}"
31
- else:
32
- return ""
33
 
34
  class PDOKLocationInfo(BaseModel):
35
- type: str
36
- weergavenaam: str
37
- straatnaam: str
38
- huisnummer: int
39
- huisletter: Optional[str]
40
- huisnummertoevoeging: Optional[str]
41
- postcode: str
42
- woonplaatsnaam: str
43
- gemeentenaam: str
44
- provincienaam: str
45
- centroide_ll: str
46
- centroide_rd: str
47
 
48
  @tool
49
  def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> Optional[PDOKLocationInfo]:
50
- """Provides information about a Dutch address or postal code.
51
-
52
- Args:
53
- postal_code: Postal code in the format '1234 AA'.
54
- house_number: House number.
55
- street_name: Street name.
56
- city: City name.
57
-
58
- Returns:
59
- A PDOKLocationInfo object containing the location information, or None if no results are found.
60
- """
61
-
62
- base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
63
- headers = {"accept": "application/json"}
64
-
65
- input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
66
- query_string = input_data.construct_query()
67
-
68
- if not query_string:
69
- return None
70
-
71
- params = {
72
- "q": query_string,
73
- "fl": "*", # Request all fields
74
- "fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
75
- "df": "tekst",
76
- "bq": "type:provincie^1.5",
77
- "bq": "type:gemeente^1.5",
78
- "bq": "type:woonplaats^1.5",
79
- "bq": "type:weg^1.5",
80
- "bq": "type:postcode^0.5",
81
- "bq": "type:adres^1",
82
- "start": 0,
83
- "rows": 10,
84
- "sort": "score desc,sortering asc,weergavenaam asc",
85
- "wt": "json",
86
- }
87
-
88
- try:
89
- response = requests.get(base_url, params=params, headers=headers)
90
- response.raise_for_status()
91
- data = response.json()
92
-
93
- docs = data.get("response", {}).get("docs", [])
94
- if not docs:
95
- return None
96
-
97
- first_result = docs[0]
98
- location_info = PDOKLocationInfo(**first_result)
99
-
100
- # Format the output in a more user-friendly way
101
- print(f"Information about: {location_info.weergavenaam}")
102
- print("-" * 30)
103
- for field, value in location_info.dict().items():
104
- print(f" {field.capitalize()}: {value}")
105
-
106
- return location_info
107
-
108
- except requests.exceptions.RequestException as e:
109
- print(f"Error during API request: {e}")
110
- return None
111
- except (ValueError, KeyError) as e:
112
- print(f"Error processing API response: {e}")
113
- return None
114
 
115
  @tool
116
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
8
  from Gradio_UI import GradioUI
9
  from typing import Optional, Dict, Any, List
10
  import requests
11
+ from pydantic import BaseModel, Field, field_validator
12
 
13
  class PDOKLocationSearchInput(BaseModel):
14
+     postal_code: Optional[str] = Field(None, description="Postal code in the format '1234 AA'.")
15
+     house_number: Optional[str] = Field(None, description="House number.")
16
+     street_name: Optional[str] = Field(None, description="Street name.")
17
+     city: Optional[str] = Field(None, description="City name.")
18
+
19
+     @field_validator('postal_code')
20
+     def validate_postal_code(cls, v):
21
+         if v is not None and (len(v) != 7 or not v[0:4].isdigit() or v[4] != " " or not v[5:7].isalpha()):
22
+             raise ValueError("Invalid postal code format. It must be '1234 AA'.")
23
+         return v
24
+         
25
+     def construct_query(self) -> str:
26
+         """Constructs the query string based on provided inputs."""
27
+         if self.postal_code and self.house_number:
28
+             return f"{self.postal_code} {self.house_number}"
29
+         elif self.street_name and self.city and self.house_number:
30
+             return f"{self.street_name} {self.house_number}, {self.city}"
31
+         else:
32
+             return ""
33
 
34
  class PDOKLocationInfo(BaseModel):
35
+     type: str
36
+     weergavenaam: str
37
+     straatnaam: str
38
+     huisnummer: int
39
+     huisletter: Optional[str]
40
+     huisnummertoevoeging: Optional[str]
41
+     postcode: str
42
+     woonplaatsnaam: str
43
+     gemeentenaam: str
44
+     provincienaam: str
45
+     centroide_ll: str
46
+     centroide_rd: str
47
 
48
  @tool
49
  def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> Optional[PDOKLocationInfo]:
50
+     """Provides information about a Dutch address or postal code.
51
+
52
+     Args:
53
+         postal_code: Postal code in the format '1234 AA'.
54
+         house_number: House number.
55
+         street_name: Street name.
56
+         city: City name.
57
+
58
+     Returns:
59
+         A PDOKLocationInfo object containing the location information, or None if no results are found.
60
+     """
61
+
62
+     base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
63
+     headers = {"accept": "application/json"}
64
+
65
+     input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
66
+     query_string = input_data.construct_query()
67
+
68
+     if not query_string:
69
+         return None
70
+
71
+     params = {
72
+         "q": query_string,
73
+         "fl": "*",  # Request all fields
74
+         "fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
75
+         "df": "tekst",
76
+         "bq": "type:provincie^1.5",
77
+         "bq": "type:gemeente^1.5",
78
+         "bq": "type:woonplaats^1.5",
79
+         "bq": "type:weg^1.5",
80
+         "bq": "type:postcode^0.5",
81
+         "bq": "type:adres^1",
82
+         "start": 0,
83
+         "rows": 10,
84
+         "sort": "score desc,sortering asc,weergavenaam asc",
85
+         "wt": "json",
86
+     }
87
+
88
+     try:
89
+         response = requests.get(base_url, params=params, headers=headers)
90
+         response.raise_for_status()
91
+         data = response.json()
92
+
93
+         docs = data.get("response", {}).get("docs", [])
94
+         if not docs:
95
+             return None
96
+
97
+         first_result = docs[0]
98
+         location_info = PDOKLocationInfo(**first_result)
99
+
100
+         # Format the output in a more user-friendly way
101
+         print(f"Information about: {location_info.weergavenaam}")
102
+         print("-" * 30)
103
+         for field, value in location_info.dict().items():
104
+             print(f"  {field.capitalize()}: {value}")
105
+
106
+         return location_info
107
+
108
+     except requests.exceptions.RequestException as e:
109
+         print(f"Error during API request: {e}")
110
+         return None
111
+     except (ValueError, KeyError) as e:
112
+         print(f"Error processing API response: {e}")
113
+         return None
114
 
115
  @tool
116
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type