import gradio as gr import folium 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 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(current_location, destination_location, health_professional_query): route_info = "" m = None # Default to None # Fetch the directions for the route steps, coords = get_directions_and_coords(current_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 current_location and health_professional_query: # Fetch the coordinates of the current location using Google Geocoding geocode_result = gmaps.geocode(current_location) if geocode_result: location_coords = geocode_result[0]['geometry']['location'] lat, lon = location_coords['lat'], location_coords['lng'] # Fetch nearby medical centers based on the query medical_centers = search_medical_centers(health_professional_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 += "\nNo medical centers found matching your query." else: route_info += "\nCould not retrieve location coordinates." # Return both the route info and the map return "\n".join(route_info), m._repr_html_() # Gradio UI components (Updated for Gradio v3.x) current_location_input = gr.Textbox(value="Honolulu, HI", label="Current Location") destination_location_input = gr.Textbox(value="Maui, HI", label="Destination Location") health_professional_query_input = gr.Textbox(value="Hawaii Medical Center", label="Health Professional Query") # Output components (Updated for Gradio v3.x) route_info_output = gr.Textbox(label="Driving Directions") map_output = gr.HTML(label="Map with Route and Health Professionals") # Create Gradio interface iface = gr.Interface( fn=get_route_and_medical_centers, # Function to call inputs=[current_location_input, destination_location_input, health_professional_query_input], # Inputs outputs=[route_info_output, map_output], # Outputs live=True # Update outputs live as inputs change ) # Launch Gradio interface iface.launch()