DreamStream-1 commited on
Commit
ad85c69
·
verified ·
1 Parent(s): d28c3b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -63
app.py CHANGED
@@ -7,86 +7,64 @@ import os
7
  # Initialize Google Maps API client securely
8
  gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY')) # Fetch API key from Hugging Face secrets
9
 
10
- # Function to fetch directions
11
- def get_directions_and_coords(source, destination):
12
- now = datetime.now()
13
- directions_info = gmaps.directions(source, destination, mode='driving', departure_time=now)
14
- if directions_info:
15
- steps = directions_info[0]['legs'][0]['steps']
16
- coords = [(step['start_location']['lat'], step['start_location']['lng']) for step in steps]
17
- return steps, coords
18
- else:
19
- return None, None
20
-
21
- # Function to render map with directions
22
- def render_folium_map(coords):
23
- m = folium.Map(location=[coords[0][0], coords[0][1]], zoom_start=13)
24
- folium.PolyLine(coords, color="blue", weight=2.5, opacity=1).add_to(m)
25
- return m
26
-
27
- # Function to search nearby medical centers using Google Places API
28
- def search_medical_centers(query, location, radius=10000):
29
- places_result = gmaps.places_nearby(location, radius=radius, type='hospital', keyword=query)
30
  return places_result.get('results', [])
31
 
32
  # Function to get directions and display on Gradio UI
33
- def get_route_and_medical_centers(current_location, destination_location, health_professional_query):
34
  route_info = ""
35
  m = None # Default to None
36
 
37
- # Fetch the directions for the route
38
  try:
39
- steps, coords = get_directions_and_coords(current_location, destination_location)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
- return f"Error fetching directions: {str(e)}", None
42
-
43
- if steps and coords:
44
- route_info = [f"{i+1}. {step['html_instructions']}" for i, step in enumerate(steps)]
45
- m = render_folium_map(coords)
46
- else:
47
- route_info = "No available routes."
48
- m = folium.Map(location=[20, 0], zoom_start=2) # Default map if no route
49
-
50
- # Fetch nearby medical centers if the location input is provided
51
- if current_location and health_professional_query:
52
- try:
53
- geocode_result = gmaps.geocode(current_location)
54
- if geocode_result:
55
- location_coords = geocode_result[0]['geometry']['location']
56
- lat, lon = location_coords['lat'], location_coords['lng']
57
-
58
- medical_centers = search_medical_centers(health_professional_query, (lat, lon))
59
-
60
- if medical_centers:
61
- for center in medical_centers:
62
- name = center['name']
63
- vicinity = center.get('vicinity', 'N/A')
64
- rating = center.get('rating', 'N/A')
65
- folium.Marker([center['geometry']['location']['lat'], center['geometry']['location']['lng']],
66
- popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m)
67
- else:
68
- route_info += "\nNo medical centers found matching your query."
69
- else:
70
- route_info += "\nCould not retrieve location coordinates."
71
- except Exception as e:
72
- route_info += f"\nError fetching medical centers: {str(e)}"
73
 
74
  # Return both the route info and the map
75
- return "\n".join(route_info), m._repr_html_()
76
 
77
  # Gradio UI components (Updated for Gradio v3.x)
78
- current_location_input = gr.Textbox(value="Honolulu, HI", label="Current Location (Plain Text)")
79
- destination_location_input = gr.Textbox(value="Maui, HI", label="Destination Location (Plain Text)")
80
- health_professional_query_input = gr.Textbox(value="Hawaii Medical Center", label="Health Professional Query (Plain Text)")
81
 
82
  # Output components (Updated for Gradio v3.x)
83
- route_info_output = gr.Textbox(label="Driving Directions")
84
- map_output = gr.HTML(label="Map with Route and Health Professionals")
85
 
86
  # Create Gradio interface
87
  iface = gr.Interface(
88
- fn=get_route_and_medical_centers, # Function to call
89
- inputs=[current_location_input, destination_location_input, health_professional_query_input], # Inputs
90
  outputs=[route_info_output, map_output], # Outputs
91
  live=False # Disable live updates, use button click to trigger the function
92
  )
 
7
  # Initialize Google Maps API client securely
8
  gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY')) # Fetch API key from Hugging Face secrets
9
 
10
+ # Function to search for health professionals using Google Places API
11
+ def search_health_professionals(query, location, radius=10000):
12
+ # Use the query to search for places like doctors, psychiatrists, psychologists, etc., within the specified radius.
13
+ places_result = gmaps.places_nearby(location, radius=radius, type='doctor', keyword=query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return places_result.get('results', [])
15
 
16
  # Function to get directions and display on Gradio UI
17
+ def get_health_professionals_and_map(current_location, health_professional_query):
18
  route_info = ""
19
  m = None # Default to None
20
 
 
21
  try:
22
+ # Geocode the current location (i.e., convert it to latitude and longitude)
23
+ geocode_result = gmaps.geocode(current_location)
24
+ if not geocode_result:
25
+ route_info = "Could not retrieve location coordinates. Please enter a valid location."
26
+ return route_info, m
27
+
28
+ location_coords = geocode_result[0]['geometry']['location']
29
+ lat, lon = location_coords['lat'], location_coords['lng']
30
+
31
+ # Search for health professionals based on the user's query near the current location
32
+ health_professionals = search_health_professionals(health_professional_query, (lat, lon))
33
+
34
+ if health_professionals:
35
+ route_info = "Health professionals found:\n"
36
+ m = folium.Map(location=[lat, lon], zoom_start=12)
37
+ for professional in health_professionals:
38
+ name = professional['name']
39
+ vicinity = professional.get('vicinity', 'N/A')
40
+ rating = professional.get('rating', 'N/A')
41
+ folium.Marker([professional['geometry']['location']['lat'], professional['geometry']['location']['lng']],
42
+ popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m)
43
+ route_info += f"- {name} ({rating} stars): {vicinity}\n"
44
+ else:
45
+ route_info = "No health professionals found matching your query."
46
+ m = folium.Map(location=[lat, lon], zoom_start=12) # Default map if no professionals are found
47
+
48
  except Exception as e:
49
+ route_info = f"Error: {str(e)}"
50
+ m = folium.Map(location=[20, 0], zoom_start=2) # Default map if any error occurs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Return both the route info and the map
53
+ return route_info, m._repr_html_()
54
 
55
  # Gradio UI components (Updated for Gradio v3.x)
56
+ current_location_input = gr.Textbox(value="Honolulu, HI", label="Current Location (Hawaii)")
57
+
58
+ health_professional_query_input = gr.Textbox(value="doctor", label="Health Professional Query (e.g., doctor, psychiatrist, psychologist)")
59
 
60
  # Output components (Updated for Gradio v3.x)
61
+ route_info_output = gr.Textbox(label="Health Professionals Information")
62
+ map_output = gr.HTML(label="Map with Health Professionals")
63
 
64
  # Create Gradio interface
65
  iface = gr.Interface(
66
+ fn=get_health_professionals_and_map, # Function to call
67
+ inputs=[current_location_input, health_professional_query_input], # Inputs
68
  outputs=[route_info_output, map_output], # Outputs
69
  live=False # Disable live updates, use button click to trigger the function
70
  )