Spaces:
Sleeping
Sleeping
Christopher Román Jaimes
commited on
Commit
•
551b39e
1
Parent(s):
26951fd
feat: add autocomplete ui.
Browse files- app.py +106 -0
- gdf_polygons_deploy.parquet +3 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import folium
|
3 |
+
import geopandas as gpd
|
4 |
+
import requests
|
5 |
+
import unidecode
|
6 |
+
|
7 |
+
gdf_polygons = gpd.read_parquet("gdf_polygons_deploy.parquet")
|
8 |
+
|
9 |
+
def get_polygon(gdf_polygons,x):
|
10 |
+
polygon = gdf_polygons.query(f"(polygon_id == {x['polygon']['id']}) and (polygon_type == '{x['polygon']['type']}')")
|
11 |
+
polygon = polygon.iloc[0].geometry
|
12 |
+
return polygon
|
13 |
+
|
14 |
+
def standardize_user_input(user_input):
|
15 |
+
# lowercase
|
16 |
+
user_input = user_input.lower()
|
17 |
+
|
18 |
+
# strip
|
19 |
+
user_input = user_input.strip()
|
20 |
+
|
21 |
+
# remove accents
|
22 |
+
user_input = unidecode.unidecode(user_input)
|
23 |
+
|
24 |
+
return user_input
|
25 |
+
|
26 |
+
def get_autocomplete(polygon_id_general: str = None, user_input: str = None):
|
27 |
+
url = 'https://data.dev.dd360.mx/autocomplete/v1/data'
|
28 |
+
data = {"last_search": polygon_id_general, "user_input": standardize_user_input(user_input) if user_input else None}
|
29 |
+
|
30 |
+
try:
|
31 |
+
response = requests.post(url, json=data, timeout=1)
|
32 |
+
response_json = response.json()
|
33 |
+
for item in response_json:
|
34 |
+
item["polygon_id_general"] = str(item["polygon"]["type"]) + "_" + str(item["polygon"]["id"])
|
35 |
+
item["polygon"] = get_polygon(gdf_polygons,item)
|
36 |
+
item["latitude"] = item["centroid"]["latitude"]
|
37 |
+
item["longitude"] = item["centroid"]["longitude"]
|
38 |
+
return response_json
|
39 |
+
except requests.exceptions.Timeout:
|
40 |
+
return []
|
41 |
+
except Exception:
|
42 |
+
return []
|
43 |
+
|
44 |
+
# Function to query DynamoDB based on user input
|
45 |
+
def query_dynamodb(user_input, last_selection):
|
46 |
+
if user_input:
|
47 |
+
return get_autocomplete(user_input = user_input)
|
48 |
+
|
49 |
+
if last_selection and last_selection[0]:
|
50 |
+
return get_autocomplete(polygon_id_general = last_selection[0]["polygon_id_general"])
|
51 |
+
|
52 |
+
return []
|
53 |
+
|
54 |
+
# Function to update the dropdown based on user input
|
55 |
+
def update_dropdown(query, last_selection):
|
56 |
+
suggestions = query_dynamodb(query, last_selection)
|
57 |
+
selected_value = suggestions[0]["address_recommendation"] if suggestions else ""
|
58 |
+
return gr.update(choices=[s["address_recommendation"] for s in suggestions], value=selected_value), suggestions
|
59 |
+
|
60 |
+
def show_map(selected_address, suggestions, last_selection):
|
61 |
+
selected_coords = next((s for s in suggestions if s["address_recommendation"] == selected_address), None)
|
62 |
+
|
63 |
+
if selected_coords:
|
64 |
+
m = folium.Map(location=[selected_coords["latitude"], selected_coords["longitude"]], zoom_start=15)
|
65 |
+
|
66 |
+
folium.Marker(
|
67 |
+
location=[selected_coords["latitude"], selected_coords["longitude"]],
|
68 |
+
popup=selected_coords["address_recommendation"],
|
69 |
+
icon=folium.Icon(color='blue')
|
70 |
+
).add_to(m)
|
71 |
+
|
72 |
+
# Add the polygon to the map
|
73 |
+
if 'polygon' in selected_coords:
|
74 |
+
folium.GeoJson(
|
75 |
+
selected_coords['polygon'],
|
76 |
+
style_function=lambda x: {'color': 'orange', 'fillOpacity': 0.3}
|
77 |
+
).add_to(m)
|
78 |
+
|
79 |
+
map_html = m._repr_html_()
|
80 |
+
|
81 |
+
# Update last_selection
|
82 |
+
last_selection[0] = selected_coords # Update the last_selection with the current selection
|
83 |
+
return map_html, last_selection # Return the HTML and the updated last_selection
|
84 |
+
else:
|
85 |
+
return None, last_selection # Return None if no selection
|
86 |
+
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("<h2 style='text-align: center; font-size: 40px;'>Address Autocomplete</h2>") # Larger Title
|
89 |
+
gr.Markdown(
|
90 |
+
"<ul>"
|
91 |
+
"<li>Type an address to see matching suggestions.</li>"
|
92 |
+
"<li>Selecting one will show its polygon.</li>"
|
93 |
+
"<li>Previous searches will also suggest addresses.</li>"
|
94 |
+
"</ul>"
|
95 |
+
)
|
96 |
+
|
97 |
+
query_input = gr.Textbox(label="Type your address")
|
98 |
+
address_dropdown = gr.Dropdown(label="Address Suggestions", choices=[], value="", interactive=True)
|
99 |
+
map_output = gr.HTML(label="Map")
|
100 |
+
suggestions = gr.State([])
|
101 |
+
last_selection = gr.State([None])
|
102 |
+
|
103 |
+
query_input.change(fn=update_dropdown, inputs=[query_input, last_selection], outputs=[address_dropdown, suggestions])
|
104 |
+
address_dropdown.change(fn=show_map, inputs=[address_dropdown, suggestions, last_selection], outputs=[map_output, last_selection])
|
105 |
+
|
106 |
+
demo.launch()
|
gdf_polygons_deploy.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cd378b055b69179a68cbe3bc9bdf2dfc2bc96320194b2ceb05694ca9f40a33d3
|
3 |
+
size 32512416
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas==2.2.2
|
2 |
+
unidecode==1.3.8
|
3 |
+
gradio==4.44.1
|
4 |
+
folium==0.17.0
|
5 |
+
geopandas==1.0.1
|