Spaces:
Sleeping
Sleeping
| 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 search for health professionals using Google Places API | |
| def search_health_professionals(query, location, radius=10000): | |
| # Use the query to search for places like doctors, psychiatrists, psychologists, etc., within the specified radius. | |
| places_result = gmaps.places_nearby(location, radius=radius, type='doctor', keyword=query) | |
| return places_result.get('results', []) | |
| # Function to get directions and display on Gradio UI | |
| def get_health_professionals_and_map(current_location, health_professional_query): | |
| route_info = "" | |
| m = None # Default to None | |
| try: | |
| # Geocode the current location (i.e., convert it to latitude and longitude) | |
| geocode_result = gmaps.geocode(current_location) | |
| if not geocode_result: | |
| route_info = "Could not retrieve location coordinates. Please enter a valid location." | |
| return route_info, m | |
| location_coords = geocode_result[0]['geometry']['location'] | |
| lat, lon = location_coords['lat'], location_coords['lng'] | |
| # Search for health professionals based on the user's query near the current location | |
| health_professionals = search_health_professionals(health_professional_query, (lat, lon)) | |
| if health_professionals: | |
| route_info = "Health professionals found:\n" | |
| m = folium.Map(location=[lat, lon], zoom_start=12) | |
| for professional in health_professionals: | |
| name = professional['name'] | |
| vicinity = professional.get('vicinity', 'N/A') | |
| rating = professional.get('rating', 'N/A') | |
| folium.Marker([professional['geometry']['location']['lat'], professional['geometry']['location']['lng']], | |
| popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m) | |
| route_info += f"- {name} ({rating} stars): {vicinity}\n" | |
| else: | |
| route_info = "No health professionals found matching your query." | |
| m = folium.Map(location=[lat, lon], zoom_start=12) # Default map if no professionals are found | |
| except Exception as e: | |
| route_info = f"Error: {str(e)}" | |
| m = folium.Map(location=[20, 0], zoom_start=2) # Default map if any error occurs | |
| # Return both the route info and the map | |
| return route_info, m._repr_html_() | |
| # Gradio UI components (Updated for Gradio v3.x) | |
| current_location_input = gr.Textbox(value="Honolulu, HI", label="Current Location (Hawaii)") | |
| health_professional_query_input = gr.Textbox(value="doctor", label="Health Professional Query (e.g., doctor, psychiatrist, psychologist)") | |
| # Output components (Updated for Gradio v3.x) | |
| route_info_output = gr.Textbox(label="Health Professionals Information") | |
| map_output = gr.HTML(label="Map with Health Professionals") | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=get_health_professionals_and_map, # Function to call | |
| inputs=[current_location_input, health_professional_query_input], # Inputs | |
| outputs=[route_info_output, map_output], # Outputs | |
| live=False # Disable live updates, use button click to trigger the function | |
| ) | |
| # Launch Gradio interface | |
| iface.launch() | |