Spaces:
Sleeping
Sleeping
DreamStream-1
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
-
import
|
2 |
import folium
|
3 |
from folium.plugins import MarkerCluster
|
4 |
-
from streamlit_folium import st_folium # Change here: import st_folium instead of folium_static
|
5 |
import googlemaps
|
6 |
from datetime import datetime
|
7 |
import os
|
@@ -51,56 +50,61 @@ def search_medical_centers(query, location, radius=10000):
|
|
51 |
places_result = gmaps.places_nearby(location, radius=radius, type='hospital', keyword=query)
|
52 |
return places_result.get('results', [])
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
destination_location = st.sidebar.text_input("Destination Location", "Maui, HI")
|
58 |
-
if st.sidebar.button('Get Directions'):
|
59 |
steps, coords = get_directions_and_coords(source_location, destination_location)
|
|
|
60 |
if steps and coords:
|
61 |
-
|
62 |
-
|
63 |
-
st.write(f"{i+1}. {step['html_instructions']}")
|
64 |
-
st.subheader('Route on Map:')
|
65 |
-
m1 = render_folium_map(coords)
|
66 |
-
st_folium(m1) # Changed here to use st_folium instead of folium_static
|
67 |
else:
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
if geocode_result:
|
79 |
-
location_coords = geocode_result[0]['geometry']['location']
|
80 |
-
lat, lon = location_coords['lat'], location_coords['lng']
|
81 |
-
|
82 |
-
# Fetch nearby medical centers in Hawaii
|
83 |
-
medical_centers = search_medical_centers(medical_center_query, (lat, lon))
|
84 |
-
|
85 |
-
if medical_centers:
|
86 |
-
st.subheader('Found Medical Centers:')
|
87 |
-
for center in medical_centers:
|
88 |
-
name = center['name']
|
89 |
-
vicinity = center.get('vicinity', 'N/A')
|
90 |
-
rating = center.get('rating', 'N/A')
|
91 |
-
st.write(f"**{name}** - {vicinity} - Rating: {rating}")
|
92 |
-
|
93 |
-
# Initialize the map object here before adding markers
|
94 |
-
if 'm2' not in locals():
|
95 |
-
m2 = folium.Map(location=[lat, lon], zoom_start=13)
|
96 |
-
|
97 |
-
# Add marker for each medical center on map
|
98 |
-
folium.Marker([center['geometry']['location']['lat'], center['geometry']['location']['lng']],
|
99 |
-
popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m2)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import folium
|
3 |
from folium.plugins import MarkerCluster
|
|
|
4 |
import googlemaps
|
5 |
from datetime import datetime
|
6 |
import os
|
|
|
50 |
places_result = gmaps.places_nearby(location, radius=radius, type='hospital', keyword=query)
|
51 |
return places_result.get('results', [])
|
52 |
|
53 |
+
# Function to get directions and display on Gradio UI
|
54 |
+
def get_route_and_medical_centers(source_location, destination_location, medical_center_query, location_input):
|
55 |
+
# Fetch the directions for the route
|
|
|
|
|
56 |
steps, coords = get_directions_and_coords(source_location, destination_location)
|
57 |
+
|
58 |
if steps and coords:
|
59 |
+
route_info = [f"{i+1}. {step['html_instructions']}" for i, step in enumerate(steps)]
|
60 |
+
m = render_folium_map(coords)
|
|
|
|
|
|
|
|
|
61 |
else:
|
62 |
+
route_info = "No available routes."
|
63 |
+
m = folium.Map(location=[20, 0], zoom_start=2) # Default map if no route
|
64 |
+
|
65 |
+
# Fetch nearby medical centers if the location input is provided
|
66 |
+
if location_input and medical_center_query:
|
67 |
+
# Fetch the coordinates of the source location using Google Geocoding
|
68 |
+
geocode_result = gmaps.geocode(location_input)
|
69 |
+
if geocode_result:
|
70 |
+
location_coords = geocode_result[0]['geometry']['location']
|
71 |
+
lat, lon = location_coords['lat'], location_coords['lng']
|
72 |
+
|
73 |
+
# Fetch nearby medical centers in Hawaii
|
74 |
+
medical_centers = search_medical_centers(medical_center_query, (lat, lon))
|
75 |
+
|
76 |
+
if medical_centers:
|
77 |
+
for center in medical_centers:
|
78 |
+
name = center['name']
|
79 |
+
vicinity = center.get('vicinity', 'N/A')
|
80 |
+
rating = center.get('rating', 'N/A')
|
81 |
+
folium.Marker([center['geometry']['location']['lat'], center['geometry']['location']['lng']],
|
82 |
+
popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m)
|
83 |
+
else:
|
84 |
+
route_info.append("No medical centers found matching your query.")
|
85 |
+
else:
|
86 |
+
route_info.append("Could not retrieve location coordinates.")
|
87 |
+
|
88 |
+
# Return both the route info and the map
|
89 |
+
return "\n".join(route_info), m
|
90 |
|
91 |
+
# Gradio UI components
|
92 |
+
source_location_input = gr.inputs.Textbox(default="Honolulu, HI", label="Source Location")
|
93 |
+
destination_location_input = gr.inputs.Textbox(default="Maui, HI", label="Destination Location")
|
94 |
+
medical_center_query_input = gr.inputs.Textbox(default="Hawaii Medical Center", label="Medical Center Query")
|
95 |
+
location_input = gr.inputs.Textbox(default="Honolulu, HI", label="Your Location")
|
96 |
|
97 |
+
# Output components
|
98 |
+
route_info_output = gr.outputs.Textbox(label="Driving Directions")
|
99 |
+
map_output = gr.outputs.HTML(label="Map with Route and Medical Centers")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
# Create Gradio interface
|
102 |
+
iface = gr.Interface(
|
103 |
+
fn=get_route_and_medical_centers, # Function to call
|
104 |
+
inputs=[source_location_input, destination_location_input, medical_center_query_input, location_input], # Inputs
|
105 |
+
outputs=[route_info_output, map_output], # Outputs
|
106 |
+
live=True # Update outputs live as inputs change
|
107 |
+
)
|
108 |
+
|
109 |
+
# Launch Gradio interface
|
110 |
+
iface.launch()
|