Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ import folium
|
|
| 14 |
import os
|
| 15 |
import base64
|
| 16 |
import torch # Added missing import for torch
|
|
|
|
| 17 |
|
| 18 |
# Disable GPU usage for TensorFlow
|
| 19 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
@@ -101,106 +102,75 @@ def detect_emotion(user_input):
|
|
| 101 |
emotion = result[0]['label']
|
| 102 |
return f"Emotion Detected: {emotion}"
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY'))
|
| 106 |
-
|
| 107 |
-
# Function to search for health professionals
|
| 108 |
-
def search_health_professionals(query, location, radius=10000):
|
| 109 |
-
places_result = gmaps.places_nearby(location, radius=radius, type='doctor', keyword=query)
|
| 110 |
-
return places_result.get('results', [])
|
| 111 |
-
|
| 112 |
-
# Function to get directions and display on Gradio UI
|
| 113 |
-
def get_health_professionals_and_map(current_location, health_professional_query):
|
| 114 |
-
try:
|
| 115 |
-
location = gmaps.geocode(current_location)
|
| 116 |
-
if not location:
|
| 117 |
-
return "Location not found. Please try again with a valid location.", ""
|
| 118 |
-
lat = location[0]["geometry"]["location"]["lat"]
|
| 119 |
-
lng = location[0]["geometry"]["location"]["lng"]
|
| 120 |
-
location = (lat, lng)
|
| 121 |
-
|
| 122 |
-
professionals = search_health_professionals(health_professional_query, location)
|
| 123 |
-
|
| 124 |
-
# Generate map
|
| 125 |
-
map_center = location
|
| 126 |
-
m = folium.Map(location=map_center, zoom_start=13)
|
| 127 |
-
|
| 128 |
-
# Add markers to the map
|
| 129 |
-
for place in professionals:
|
| 130 |
-
folium.Marker(
|
| 131 |
-
location=[place['geometry']['location']['lat'], place['geometry']['location']['lng']],
|
| 132 |
-
popup=place['name']
|
| 133 |
-
).add_to(m)
|
| 134 |
-
|
| 135 |
-
# Convert map to HTML for Gradio display
|
| 136 |
-
map_html = m._repr_html_()
|
| 137 |
-
|
| 138 |
-
# Route information
|
| 139 |
-
route_info = "\n".join([f"{place['name']} - {place['vicinity']}" for place in professionals])
|
| 140 |
-
|
| 141 |
-
return route_info, map_html
|
| 142 |
-
except googlemaps.exceptions.HTTPError as e:
|
| 143 |
-
return f"Error occurred with Google Maps API: {e}", ""
|
| 144 |
-
except Exception as e:
|
| 145 |
-
return f"Unexpected error: {str(e)}", ""
|
| 146 |
-
|
| 147 |
-
# Function to generate suggestions based on the detected emotion
|
| 148 |
def generate_suggestions(emotion):
|
| 149 |
suggestions = {
|
| 150 |
-
'joy': [
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
]
|
| 156 |
-
'anger': [
|
| 157 |
-
{"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Emotional Wellness Toolkit</a>'},
|
| 158 |
-
{"Title": "Stress Management Tips", "Subject": "Stress Management", "Link": '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Harvard Health: Stress Management</a>'},
|
| 159 |
-
{"Title": "Dealing with Anger", "Subject": "Anger Management", "Link": '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Tips for Dealing with Anger</a>'},
|
| 160 |
-
{"Title": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch Video</a>'}
|
| 161 |
-
],
|
| 162 |
-
'fear': [
|
| 163 |
-
{"Title": "Mindfulness Practices", "Subject": "Mindfulness", "Link": '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Mindful Breathing Meditation</a>'},
|
| 164 |
-
{"Title": "Coping with Anxiety", "Subject": "Anxiety Management", "Link": '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Tips for Dealing with Anxiety</a>'},
|
| 165 |
-
{"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Emotional Wellness Toolkit</a>'},
|
| 166 |
-
{"Title": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch Video</a>'}
|
| 167 |
-
]
|
| 168 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# Custom CSS styling for Gradio interface
|
| 174 |
css = """
|
| 175 |
body {
|
| 176 |
-
font-family: '
|
| 177 |
}
|
| 178 |
.gradio-container {
|
| 179 |
-
background-color: #
|
|
|
|
| 180 |
}
|
| 181 |
.gradio-input, .gradio-output {
|
| 182 |
-
padding:
|
| 183 |
-
border-radius:
|
| 184 |
-
background-color: #
|
| 185 |
-
border:
|
| 186 |
}
|
| 187 |
.gradio-container .gradio-button {
|
| 188 |
-
background-color: #
|
| 189 |
color: white;
|
| 190 |
border-radius: 5px;
|
|
|
|
| 191 |
}
|
| 192 |
.gradio-container .gradio-button:hover {
|
| 193 |
-
background-color: #
|
| 194 |
-
}
|
| 195 |
-
.gradio-output {
|
| 196 |
-
font-size: 16px;
|
| 197 |
-
color: #333;
|
| 198 |
}
|
| 199 |
.gradio-container h3 {
|
| 200 |
color: #333;
|
| 201 |
}
|
| 202 |
.gradio-output .output {
|
| 203 |
-
border-top:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}
|
| 205 |
"""
|
| 206 |
|
|
@@ -216,7 +186,18 @@ def gradio_app(message, current_location, health_professional_query, history):
|
|
| 216 |
# Get health professionals details and map
|
| 217 |
route_info, map_html = get_health_professionals_and_map(current_location, health_professional_query)
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
# Gradio interface setup
|
| 222 |
iface = gr.Interface(
|
|
@@ -242,4 +223,4 @@ iface = gr.Interface(
|
|
| 242 |
)
|
| 243 |
|
| 244 |
# Launch Gradio interface
|
| 245 |
-
iface.launch(share=True)
|
|
|
|
| 14 |
import os
|
| 15 |
import base64
|
| 16 |
import torch # Added missing import for torch
|
| 17 |
+
from PIL import Image
|
| 18 |
|
| 19 |
# Disable GPU usage for TensorFlow
|
| 20 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
| 102 |
emotion = result[0]['label']
|
| 103 |
return f"Emotion Detected: {emotion}"
|
| 104 |
|
| 105 |
+
# Function to generate suggestions based on detected emotion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def generate_suggestions(emotion):
|
| 107 |
suggestions = {
|
| 108 |
+
'joy': ["Stay positive! Keep up the good mood.", "Try some relaxing activities like meditation."],
|
| 109 |
+
'anger': ["It's okay to be angry, try to breathe and relax.", "Exercise can help release tension."],
|
| 110 |
+
'fear': ["Take deep breaths, you are in control.", "Try mindfulness exercises to calm your mind."],
|
| 111 |
+
'sadness': ["Take a break, it's okay to feel down sometimes.", "Consider reaching out to a friend or loved one."],
|
| 112 |
+
'surprise': ["Take a moment to reflect, things might seem overwhelming.", "Practice mindfulness to regain balance."],
|
| 113 |
+
'disgust': ["It's okay to feel disgust, try to identify the cause.", "Taking a short walk might help clear your mind."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
+
return pd.DataFrame(suggestions.get(emotion, ["Stay positive!"]))
|
| 116 |
+
|
| 117 |
+
# Function to get nearby health professionals and create a map
|
| 118 |
+
def get_health_professionals_and_map(location, health_professional_query):
|
| 119 |
+
# Use Google Maps API to get health professionals (example setup)
|
| 120 |
+
gmaps = googlemaps.Client(key="YOUR_GOOGLE_API_KEY")
|
| 121 |
+
places = gmaps.places(query=health_professional_query, location=location)
|
| 122 |
|
| 123 |
+
if places['status'] == 'OK':
|
| 124 |
+
results = places['results']
|
| 125 |
+
route_info = "\n".join([place['name'] for place in results])
|
| 126 |
+
map_html = create_map(results)
|
| 127 |
+
return route_info, map_html
|
| 128 |
+
return "No professionals found.", None
|
| 129 |
+
|
| 130 |
+
def create_map(places):
|
| 131 |
+
m = folium.Map(location=[places[0]['geometry']['location']['lat'], places[0]['geometry']['location']['lng']], zoom_start=13)
|
| 132 |
+
for place in places:
|
| 133 |
+
folium.Marker([place['geometry']['location']['lat'], place['geometry']['location']['lng']],
|
| 134 |
+
popup=place['name']).add_to(m)
|
| 135 |
+
map_html = m._repr_html_()
|
| 136 |
+
return map_html
|
| 137 |
|
| 138 |
# Custom CSS styling for Gradio interface
|
| 139 |
css = """
|
| 140 |
body {
|
| 141 |
+
font-family: 'Roboto', sans-serif;
|
| 142 |
}
|
| 143 |
.gradio-container {
|
| 144 |
+
background-color: #f0f0f0;
|
| 145 |
+
font-size: 16px;
|
| 146 |
}
|
| 147 |
.gradio-input, .gradio-output {
|
| 148 |
+
padding: 15px;
|
| 149 |
+
border-radius: 10px;
|
| 150 |
+
background-color: #ffffff;
|
| 151 |
+
border: 2px solid #ccc;
|
| 152 |
}
|
| 153 |
.gradio-container .gradio-button {
|
| 154 |
+
background-color: #007BFF;
|
| 155 |
color: white;
|
| 156 |
border-radius: 5px;
|
| 157 |
+
padding: 10px 15px;
|
| 158 |
}
|
| 159 |
.gradio-container .gradio-button:hover {
|
| 160 |
+
background-color: #0056b3;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
.gradio-container h3 {
|
| 163 |
color: #333;
|
| 164 |
}
|
| 165 |
.gradio-output .output {
|
| 166 |
+
border-top: 3px solid #ddd;
|
| 167 |
+
padding-top: 10px;
|
| 168 |
+
}
|
| 169 |
+
.gradio-input input {
|
| 170 |
+
color: #333;
|
| 171 |
+
}
|
| 172 |
+
.gradio-input textarea {
|
| 173 |
+
color: #333;
|
| 174 |
}
|
| 175 |
"""
|
| 176 |
|
|
|
|
| 186 |
# Get health professionals details and map
|
| 187 |
route_info, map_html = get_health_professionals_and_map(current_location, health_professional_query)
|
| 188 |
|
| 189 |
+
# Add emoticon for emotion
|
| 190 |
+
emotion_emoticons = {
|
| 191 |
+
'joy': '😊',
|
| 192 |
+
'anger': '😡',
|
| 193 |
+
'fear': '😨',
|
| 194 |
+
'sadness': '😢',
|
| 195 |
+
'surprise': '😲',
|
| 196 |
+
'disgust': '🤢'
|
| 197 |
+
}
|
| 198 |
+
emotion_icon = emotion_emoticons.get(emotion, '🙂')
|
| 199 |
+
|
| 200 |
+
return sentiment, f"{emotion_icon} {emotion}", suggestions_df, route_info, map_html, history
|
| 201 |
|
| 202 |
# Gradio interface setup
|
| 203 |
iface = gr.Interface(
|
|
|
|
| 223 |
)
|
| 224 |
|
| 225 |
# Launch Gradio interface
|
| 226 |
+
iface.launch(share=True)
|