File size: 3,440 Bytes
0ed3848
4cfb561
 
 
 
65d5924
4cfb561
d72524f
65d5924
ad85c69
 
 
 
4cfb561
 
0ed3848
ad85c69
34d2572
 
 
16da4f9
ad85c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16da4f9
ad85c69
 
0ed3848
 
ad85c69
65d5924
28bdba9
ad85c69
 
 
65d5924
28bdba9
ad85c69
 
4cfb561
0ed3848
 
ad85c69
 
0ed3848
16da4f9
0ed3848
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()