File size: 4,263 Bytes
0ed3848
4cfb561
 
 
 
65d5924
4cfb561
d72524f
65d5924
4cfb561
 
 
 
 
 
 
 
65d5924
4cfb561
65d5924
4cfb561
 
 
 
 
65d5924
4cfb561
 
 
 
 
0ed3848
34d2572
 
 
 
0ed3848
16da4f9
 
 
 
0ed3848
4cfb561
0ed3848
 
1852538
0ed3848
 
 
 
34d2572
16da4f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ed3848
16da4f9
 
 
0ed3848
 
28bdba9
65d5924
28bdba9
16da4f9
 
 
65d5924
28bdba9
 
34d2572
4cfb561
0ed3848
 
 
d28c3b2
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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):
    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
    try:
        steps, coords = get_directions_and_coords(current_location, destination_location)
    except Exception as e:
        return f"Error fetching directions: {str(e)}", None
    
    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:
        try:
            geocode_result = gmaps.geocode(current_location)
            if geocode_result:
                location_coords = geocode_result[0]['geometry']['location']
                lat, lon = location_coords['lat'], location_coords['lng']
                
                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."
        except Exception as e:
            route_info += f"\nError fetching medical centers: {str(e)}"
    
    # 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 (Plain Text)")
destination_location_input = gr.Textbox(value="Maui, HI", label="Destination Location (Plain Text)")
health_professional_query_input = gr.Textbox(value="Hawaii Medical Center", label="Health Professional Query (Plain Text)")

# 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=False  # Disable live updates, use button click to trigger the function
)

# Launch Gradio interface
iface.launch()