Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import googlemaps
|
|
13 |
import folium
|
14 |
import os
|
15 |
import base64
|
|
|
16 |
|
17 |
# Disable GPU usage for TensorFlow
|
18 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
@@ -71,6 +72,7 @@ def chatbot(message, history):
|
|
71 |
else:
|
72 |
response = "I'm sorry, I didn't understand that. Could you please rephrase?"
|
73 |
except Exception as e:
|
|
|
74 |
response = f"An error occurred: {str(e)}"
|
75 |
# Convert the new message and response to the 'messages' format
|
76 |
history.append({"role": "user", "content": message})
|
@@ -98,7 +100,6 @@ def detect_emotion(user_input):
|
|
98 |
result = pipe(user_input)
|
99 |
emotion = result[0]['label']
|
100 |
return f"Emotion Detected: {emotion}"
|
101 |
-
|
102 |
# Initialize Google Maps API client securely
|
103 |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY'))
|
104 |
|
@@ -158,169 +159,81 @@ def generate_suggestions(emotion):
|
|
158 |
{"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>'},
|
159 |
{"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Emotional Wellness Toolkit</a>'},
|
160 |
{"Title": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch Video</a>'}
|
161 |
-
],
|
162 |
-
'sadness': [
|
163 |
-
{"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Emotional Wellness Toolkit</a>'},
|
164 |
-
{"Title": "Dealing 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": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/-e-4Kx5px_I" target="_blank">Watch Video</a>'}
|
166 |
-
],
|
167 |
-
'surprise': [
|
168 |
-
{"Title": "Managing Stress", "Subject": "Stress Management", "Link": '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Harvard Health: Stress Management</a>'},
|
169 |
-
{"Title": "Coping Strategies", "Subject": "Coping", "Link": '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Coping with Anxiety</a>'},
|
170 |
-
{"Title": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch Video</a>'}
|
171 |
]
|
172 |
}
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
#
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
'anger': 'anger.png',
|
209 |
-
'fear': 'fear.png',
|
210 |
-
'sadness': 'sadness.png',
|
211 |
-
'surprise': 'surprise.png'
|
212 |
-
}
|
213 |
-
|
214 |
-
# Convert image to base64 for embedding in HTML
|
215 |
-
if info_graphics.get(emotion_response.split(': ')[1]):
|
216 |
-
with open(info_graphics[emotion_response.split(': ')[1]], "rb") as image_file:
|
217 |
-
encoded_string = base64.b64encode(image_file.read()).decode()
|
218 |
-
info_graphic_html = f'<img src="data:image/png;base64,{encoded_string}" alt="{emotion_response.split(": ")[1]}">'
|
219 |
-
else:
|
220 |
-
info_graphic_html = ''
|
221 |
-
|
222 |
-
return (
|
223 |
-
history,
|
224 |
-
sentiment_response,
|
225 |
-
f"{emotion_response} {emoticon.get(emotion_response.split(': ')[1], '')}",
|
226 |
-
route_info,
|
227 |
-
map_html,
|
228 |
-
gr.DataFrame(suggestions_df, headers=["Title", "Subject", "Link"]),
|
229 |
-
info_graphic_html,
|
230 |
-
state
|
231 |
-
)
|
232 |
-
else:
|
233 |
-
return history, "", "", "", "", gr.DataFrame([], headers=["Title", "Subject", "Link"]), "", state
|
234 |
-
|
235 |
-
# Gradio UI components
|
236 |
-
message_input = gr.Textbox(lines=1, label="Message", placeholder="Type your message here...")
|
237 |
-
location_input = gr.Textbox(value="Honolulu, HI", label="Current Location", placeholder="Enter your current location...")
|
238 |
-
health_query_input = gr.Textbox(value="doctor", label="Health Professional Query (e.g., doctor, health professional, well-being professional)", placeholder="Search for health professionals...")
|
239 |
-
submit_button = gr.Button("Submit")
|
240 |
-
|
241 |
-
# Updated chat history component with 'messages' type
|
242 |
-
chat_history = gr.Chatbot(label="Well-Being Chat History", type='messages')
|
243 |
-
|
244 |
-
# Outputs
|
245 |
-
sentiment_output = gr.Textbox(label="Sentiment Analysis Result")
|
246 |
-
emotion_output = gr.Textbox(label="Emotion Detection Result")
|
247 |
-
route_info_output = gr.Textbox(label="Health Professionals Information")
|
248 |
-
map_output = gr.HTML(label="Map with Health Professionals")
|
249 |
-
suggestions_output = gr.DataFrame(label="Well-Being Suggestions", headers=["Title", "Subject", "Link"])
|
250 |
-
info_graphic_output = gr.HTML(label="Info Graphic")
|
251 |
-
|
252 |
-
# Custom CSS for styling
|
253 |
-
custom_css = """
|
254 |
-
<style>
|
255 |
-
body {
|
256 |
-
background-color: #f0f8ff;
|
257 |
-
color: #333;
|
258 |
-
font-family: Arial, sans-serif;
|
259 |
-
}
|
260 |
-
h1, h2, h3, h4, h5, h6 {
|
261 |
-
color: #0056b3;
|
262 |
-
}
|
263 |
-
.gradio-app {
|
264 |
-
max-width: 800px;
|
265 |
-
margin: 0 auto;
|
266 |
-
padding: 20px;
|
267 |
-
background-color: #fff;
|
268 |
-
border-radius: 10px;
|
269 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
270 |
-
}
|
271 |
-
.gradio-input, .gradio-output {
|
272 |
-
margin-bottom: 15px;
|
273 |
-
}
|
274 |
-
.gradio-button {
|
275 |
-
background-color: #0056b3;
|
276 |
-
color: #fff;
|
277 |
-
border: none;
|
278 |
-
padding: 10px 20px;
|
279 |
-
border-radius: 5px;
|
280 |
-
cursor: pointer;
|
281 |
-
}
|
282 |
-
.gradio-button:hover {
|
283 |
-
background-color: #004080;
|
284 |
-
}
|
285 |
-
.gradio-dataframe {
|
286 |
-
border: 1px solid #ddd;
|
287 |
-
border-radius: 5px;
|
288 |
-
overflow: hidden;
|
289 |
-
}
|
290 |
-
.gradio-dataframe th, .gradio-dataframe td {
|
291 |
-
padding: 10px;
|
292 |
-
text-align: left;
|
293 |
-
}
|
294 |
-
.gradio-dataframe th {
|
295 |
-
background-color: #0056b3;
|
296 |
-
color: #fff;
|
297 |
-
}
|
298 |
-
.gradio-dataframe a {
|
299 |
-
color: #0056b3;
|
300 |
-
text-decoration: none;
|
301 |
-
}
|
302 |
-
.gradio-dataframe a:hover {
|
303 |
-
text-decoration: underline;
|
304 |
-
}
|
305 |
-
.info-graphic img {
|
306 |
-
max-width: 100%;
|
307 |
-
height: auto;
|
308 |
-
}
|
309 |
-
</style>
|
310 |
"""
|
311 |
|
312 |
-
#
|
313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
fn=gradio_app,
|
315 |
-
inputs=[
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
allow_flagging="never",
|
318 |
-
|
319 |
-
|
320 |
-
css=custom_css
|
321 |
)
|
322 |
|
323 |
-
# Launch
|
324 |
-
|
325 |
-
demo.launch()
|
326 |
-
|
|
|
13 |
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'
|
|
|
72 |
else:
|
73 |
response = "I'm sorry, I didn't understand that. Could you please rephrase?"
|
74 |
except Exception as e:
|
75 |
+
print(f"Error in chatbot: {e}") # For debugging
|
76 |
response = f"An error occurred: {str(e)}"
|
77 |
# Convert the new message and response to the 'messages' format
|
78 |
history.append({"role": "user", "content": message})
|
|
|
100 |
result = pipe(user_input)
|
101 |
emotion = result[0]['label']
|
102 |
return f"Emotion Detected: {emotion}"
|
|
|
103 |
# Initialize Google Maps API client securely
|
104 |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY'))
|
105 |
|
|
|
159 |
{"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>'},
|
160 |
{"Title": "Emotional Wellness Toolkit", "Subject": "Wellness", "Link": '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Emotional Wellness Toolkit</a>'},
|
161 |
{"Title": "Relaxation Video", "Subject": "Video", "Link": '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch Video</a>'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
]
|
163 |
}
|
164 |
+
|
165 |
+
emotion_suggestions = suggestions.get(emotion, [])
|
166 |
+
return pd.DataFrame(emotion_suggestions)
|
167 |
+
# Custom CSS styling for Gradio interface
|
168 |
+
css = """
|
169 |
+
body {
|
170 |
+
font-family: 'Arial', sans-serif;
|
171 |
+
}
|
172 |
+
.gradio-container {
|
173 |
+
background-color: #f7f7f7;
|
174 |
+
}
|
175 |
+
.gradio-input, .gradio-output {
|
176 |
+
padding: 10px;
|
177 |
+
border-radius: 5px;
|
178 |
+
background-color: #fff;
|
179 |
+
border: 1px solid #ccc;
|
180 |
+
}
|
181 |
+
.gradio-container .gradio-button {
|
182 |
+
background-color: #4CAF50;
|
183 |
+
color: white;
|
184 |
+
border-radius: 5px;
|
185 |
+
}
|
186 |
+
.gradio-container .gradio-button:hover {
|
187 |
+
background-color: #45a049;
|
188 |
+
}
|
189 |
+
.gradio-output {
|
190 |
+
font-size: 16px;
|
191 |
+
color: #333;
|
192 |
+
}
|
193 |
+
.gradio-container h3 {
|
194 |
+
color: #333;
|
195 |
+
}
|
196 |
+
.gradio-output .output {
|
197 |
+
border-top: 2px solid #ddd;
|
198 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
"""
|
200 |
|
201 |
+
# Gradio interface components
|
202 |
+
def gradio_app(message, current_location, health_professional_query, history):
|
203 |
+
# Detect sentiment and emotion
|
204 |
+
sentiment = analyze_sentiment(message)
|
205 |
+
emotion = detect_emotion(message)
|
206 |
+
|
207 |
+
# Generate suggestions based on emotion
|
208 |
+
suggestions_df = generate_suggestions(emotion)
|
209 |
+
|
210 |
+
# Get health professionals details and map
|
211 |
+
route_info, map_html = get_health_professionals_and_map(current_location, health_professional_query)
|
212 |
+
|
213 |
+
return sentiment, emotion, suggestions_df, route_info, map_html, history
|
214 |
+
|
215 |
+
# Gradio interface setup
|
216 |
+
iface = gr.Interface(
|
217 |
fn=gradio_app,
|
218 |
+
inputs=[
|
219 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter your message..."),
|
220 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter your current location..."),
|
221 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter health professional query..."),
|
222 |
+
gr.State(value=[])
|
223 |
+
],
|
224 |
+
outputs=[
|
225 |
+
gr.outputs.Textbox(label="Sentiment Analysis"),
|
226 |
+
gr.outputs.Textbox(label="Detected Emotion"),
|
227 |
+
gr.outputs.Dataframe(label="Suggestions"),
|
228 |
+
gr.outputs.Textbox(label="Nearby Health Professionals"),
|
229 |
+
gr.outputs.HTML(label="Map of Health Professionals"),
|
230 |
+
gr.State(value=[])
|
231 |
+
],
|
232 |
+
live=True,
|
233 |
allow_flagging="never",
|
234 |
+
theme="huggingface",
|
235 |
+
css=css # Apply custom CSS styling
|
|
|
236 |
)
|
237 |
|
238 |
+
# Launch Gradio interface
|
239 |
+
iface.launch(share=True)
|
|
|
|