Spaces:
Sleeping
Sleeping
import gradio as gr | |
import folium | |
from folium.plugins import MarkerCluster | |
import googlemaps | |
from datetime import datetime | |
import os | |
# Initialize Google Maps API client securely | |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY')) # Fetch API key from Hugging Face secrets | |
# Function to fetch directions | |
def get_directions_and_coords(source, destination): | |
now = datetime.now() | |
directions_info = gmaps.directions(source, destination, mode='driving', departure_time=now) | |
if directions_info: | |
steps = directions_info[0]['legs'][0]['steps'] | |
coords = [(step['start_location']['lat'], step['start_location']['lng']) for step in steps] | |
return steps, coords | |
else: | |
return None, None | |
# Function to render map with directions | |
def render_folium_map(coords): | |
m = folium.Map(location=[coords[0][0], coords[0][1]], zoom_start=13) | |
folium.PolyLine(coords, color="blue", weight=2.5, opacity=1).add_to(m) | |
return m | |
# Function to add medical center paths and annotate distance | |
def add_medical_center_paths(m, source, med_centers): | |
for name, lat, lon, specialty, city in med_centers: | |
_, coords = get_directions_and_coords(source, (lat, lon)) | |
if coords: | |
folium.PolyLine(coords, color="red", weight=2.5, opacity=1).add_to(m) | |
folium.Marker([lat, lon], popup=name).add_to(m) | |
distance_info = gmaps.distance_matrix(source, (lat, lon), mode='driving') | |
distance = distance_info['rows'][0]['elements'][0]['distance']['text'] | |
folium.PolyLine(coords, color='red').add_to(m) | |
folium.map.Marker( | |
[coords[-1][0], coords[-1][1]], | |
icon=folium.DivIcon( | |
icon_size=(150, 36), | |
icon_anchor=(0, 0), | |
html=f'<div style="font-size: 10pt; color : red;">{distance}</div>', | |
) | |
).add_to(m) | |
# Function to search nearby medical centers using Google Places API | |
def search_medical_centers(query, location, radius=10000): | |
# Search for places like hospitals or medical centers nearby | |
places_result = gmaps.places_nearby(location, radius=radius, type='hospital', keyword=query) | |
return places_result.get('results', []) | |
# Function to get directions and display on Gradio UI | |
def get_route_and_medical_centers(source_location, destination_location, medical_center_query, location_input): | |
# Fetch the directions for the route | |
steps, coords = get_directions_and_coords(source_location, destination_location) | |
if steps and coords: | |
route_info = [f"{i+1}. {step['html_instructions']}" for i, step in enumerate(steps)] | |
m = render_folium_map(coords) | |
else: | |
route_info = "No available routes." | |
m = folium.Map(location=[20, 0], zoom_start=2) # Default map if no route | |
# Fetch nearby medical centers if the location input is provided | |
if location_input and medical_center_query: | |
# Fetch the coordinates of the source location using Google Geocoding | |
geocode_result = gmaps.geocode(location_input) | |
if geocode_result: | |
location_coords = geocode_result[0]['geometry']['location'] | |
lat, lon = location_coords['lat'], location_coords['lng'] | |
# Fetch nearby medical centers in Hawaii | |
medical_centers = search_medical_centers(medical_center_query, (lat, lon)) | |
if medical_centers: | |
for center in medical_centers: | |
name = center['name'] | |
vicinity = center.get('vicinity', 'N/A') | |
rating = center.get('rating', 'N/A') | |
folium.Marker([center['geometry']['location']['lat'], center['geometry']['location']['lng']], | |
popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m) | |
else: | |
route_info.append("No medical centers found matching your query.") | |
else: | |
route_info.append("Could not retrieve location coordinates.") | |
# Return both the route info and the map | |
return "\n".join(route_info), m | |
# Gradio UI components | |
source_location_input = gr.inputs.Textbox(default="Honolulu, HI", label="Source Location") | |
destination_location_input = gr.inputs.Textbox(default="Maui, HI", label="Destination Location") | |
medical_center_query_input = gr.inputs.Textbox(default="Hawaii Medical Center", label="Medical Center Query") | |
location_input = gr.inputs.Textbox(default="Honolulu, HI", label="Your Location") | |
# Output components | |
route_info_output = gr.outputs.Textbox(label="Driving Directions") | |
map_output = gr.outputs.HTML(label="Map with Route and Medical Centers") | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=get_route_and_medical_centers, # Function to call | |
inputs=[source_location_input, destination_location_input, medical_center_query_input, location_input], # Inputs | |
outputs=[route_info_output, map_output], # Outputs | |
live=True # Update outputs live as inputs change | |
) | |
# Launch Gradio interface | |
iface.launch() | |