Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -92,27 +92,36 @@ regional_resources = {
|
|
92 |
]
|
93 |
}
|
94 |
|
95 |
-
# Mock Therapist Database
|
96 |
therapists = {
|
97 |
"Dr. Jane Smith": {
|
98 |
"specialty": "Anxiety and Depression",
|
99 |
"email": "[email protected]",
|
100 |
-
"times": [
|
|
|
|
|
|
|
101 |
},
|
102 |
"Dr. Amit Patel": {
|
103 |
"specialty": "Stress Management",
|
104 |
"email": "[email protected]",
|
105 |
-
"times": [
|
|
|
|
|
|
|
106 |
},
|
107 |
"Dr. Sarah Brown": {
|
108 |
"specialty": "Trauma and PTSD",
|
109 |
"email": "[email protected]",
|
110 |
-
"times": [
|
|
|
|
|
|
|
111 |
}
|
112 |
}
|
113 |
|
114 |
# Initialize Gemini model
|
115 |
-
model = genai.GenerativeModel("
|
116 |
generation_config={"temperature": 0.7, "max_output_tokens": 200})
|
117 |
|
118 |
# Chatbot function
|
@@ -144,7 +153,7 @@ def chatbot_function(message, mood, conversation_mode, region, state):
|
|
144 |
}.get(conversation_mode, "Maintain a balanced, empathetic tone.")
|
145 |
|
146 |
prompt = f"""
|
147 |
-
You are a compassionate
|
148 |
- Empathetic, sensitive responses in the user's language (detected as {lang}).
|
149 |
- {tone_instruction}
|
150 |
- Suggestions for coping strategies if distress is detected, tailored to their mood or input.
|
@@ -176,7 +185,7 @@ def chatbot_function(message, mood, conversation_mode, region, state):
|
|
176 |
if user_msg:
|
177 |
chat_display += f"**You**: {user_msg}\n\n"
|
178 |
if bot_msg:
|
179 |
-
chat_display += f"**
|
180 |
|
181 |
return chat_display, state
|
182 |
|
@@ -190,7 +199,6 @@ def log_mood(mood, state):
|
|
190 |
if mood and mood != "Select mood":
|
191 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
192 |
state["mood_journal"].append({"timestamp": timestamp, "mood": mood.lower()})
|
193 |
-
# Trigger mood trends update
|
194 |
trend_output, state = show_mood_trends(state)
|
195 |
return "Mood logged!", trend_output, state
|
196 |
return "Please select a mood.", None, state
|
@@ -247,7 +255,7 @@ You have a new appointment:
|
|
247 |
Please confirm with the client.
|
248 |
|
249 |
Best,
|
250 |
-
|
251 |
"""
|
252 |
therapist_message = create_message(therapist_email, "New Appointment", therapist_body)
|
253 |
|
@@ -262,7 +270,7 @@ Details:
|
|
262 |
- Time: {time_slot}
|
263 |
|
264 |
Best,
|
265 |
-
|
266 |
"""
|
267 |
user_message = create_message(user_email, "Appointment Confirmation", user_body)
|
268 |
|
@@ -318,15 +326,16 @@ def schedule_appointment(therapist, time_slot, date, user_email, state):
|
|
318 |
return "Invalid date format (use YYYY-MM-DD).", state
|
319 |
|
320 |
try:
|
321 |
-
|
322 |
-
|
323 |
-
|
|
|
324 |
except ValueError:
|
325 |
-
return "Invalid time format
|
326 |
|
327 |
appointment = {
|
328 |
"therapist": therapist,
|
329 |
-
"time":
|
330 |
"date": date,
|
331 |
"user_email": user_email,
|
332 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
@@ -334,30 +343,40 @@ def schedule_appointment(therapist, time_slot, date, user_email, state):
|
|
334 |
state["appointments"].append(appointment)
|
335 |
|
336 |
therapist_email = therapists[therapist]["email"]
|
337 |
-
success, error_msg = send_emails(therapist,
|
338 |
if success:
|
339 |
-
return f"Appointment booked with {therapist} on {date} at {
|
340 |
else:
|
341 |
-
return (f"Appointment booked with {therapist} on {date} at {
|
342 |
f"Email sending failed: {error_msg}. Please contact {therapist_email}."), state
|
343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
344 |
# Gradio Interface
|
345 |
-
with gr.Blocks(title="
|
346 |
state = gr.State({"chat_history": [], "mood_journal": [], "feedback": [], "appointments": [], "failed_emails": []})
|
347 |
|
348 |
-
gr.Markdown("##
|
349 |
-
gr.Markdown("
|
|
|
350 |
|
351 |
with gr.Row():
|
352 |
with gr.Column(scale=1):
|
353 |
mood = gr.Dropdown(
|
354 |
choices=["Select mood", "Happy", "Sad", "Anxious", "Stressed", "Angry", "Overwhelmed", "Hopeful", "Lonely", "Other"],
|
355 |
-
label="
|
356 |
value="Select mood"
|
357 |
)
|
358 |
conversation_mode = gr.Radio(
|
359 |
choices=["Neutral", "Calm", "Motivational", "Empathetic", "Hopeful"],
|
360 |
-
label="
|
361 |
value="Neutral"
|
362 |
)
|
363 |
region = gr.Dropdown(
|
@@ -367,57 +386,59 @@ with gr.Blocks(title="Mental Health Support Chatbot") as demo:
|
|
367 |
)
|
368 |
with gr.Column(scale=2):
|
369 |
chatbot = gr.Textbox(
|
370 |
-
label="
|
371 |
value="",
|
372 |
interactive=False,
|
373 |
-
lines=
|
374 |
show_copy_button=True
|
375 |
)
|
376 |
user_input = gr.Textbox(
|
377 |
-
placeholder="
|
378 |
-
label="Your
|
379 |
lines=3
|
380 |
)
|
381 |
with gr.Row():
|
382 |
-
submit_btn = gr.Button("Send
|
383 |
clear_btn = gr.Button("Clear Chat")
|
384 |
|
|
|
|
|
385 |
emergency_btn = gr.Button("Crisis Support")
|
386 |
emergency_output = gr.Markdown("")
|
387 |
|
388 |
with gr.Accordion("Mood Journal"):
|
389 |
log_mood_btn = gr.Button("Log Mood")
|
390 |
-
mood_log_output = gr.Textbox(label="
|
391 |
-
mood_trend_output = gr.Plot(label="
|
392 |
|
393 |
-
with gr.Accordion("
|
394 |
-
feedback_text = gr.Textbox(label="Your
|
395 |
-
feedback_rating = gr.Slider(minimum=1, maximum=5, step=1, label="
|
396 |
-
feedback_btn = gr.Button("Submit
|
397 |
-
feedback_output = gr.Textbox(label="
|
398 |
|
399 |
-
with gr.Accordion("
|
400 |
therapist = gr.Dropdown(
|
401 |
choices=list(therapists.keys()),
|
402 |
-
label="
|
403 |
)
|
404 |
date = gr.Textbox(
|
405 |
-
label="
|
406 |
-
placeholder="
|
407 |
-
info="
|
408 |
)
|
409 |
time_slot = gr.Dropdown(
|
410 |
choices=["Select time"],
|
411 |
-
label="
|
412 |
interactive=True,
|
413 |
allow_custom_value=True
|
414 |
)
|
415 |
user_email = gr.Textbox(
|
416 |
-
label="
|
417 |
placeholder="e.g., [email protected]"
|
418 |
)
|
419 |
-
schedule_btn = gr.Button("Book Appointment")
|
420 |
-
schedule_output = gr.Textbox(label="
|
421 |
|
422 |
submit_btn.click(
|
423 |
fn=chatbot_function,
|
@@ -460,6 +481,7 @@ with gr.Blocks(title="Mental Health Support Chatbot") as demo:
|
|
460 |
outputs=[schedule_output, state]
|
461 |
)
|
462 |
|
|
|
463 |
gr.Markdown("""
|
464 |
**Resources**:
|
465 |
- [Befrienders Worldwide](https://befrienders.org/)
|
|
|
92 |
]
|
93 |
}
|
94 |
|
95 |
+
# Mock Therapist Database with 12-hour time format
|
96 |
therapists = {
|
97 |
"Dr. Jane Smith": {
|
98 |
"specialty": "Anxiety and Depression",
|
99 |
"email": "[email protected]",
|
100 |
+
"times": [
|
101 |
+
"7:00 AM", "7:30 AM", "8:00 AM", "8:30 AM", "9:00 AM", "9:30 AM",
|
102 |
+
"2:00 PM", "2:30 PM", "3:00 PM", "3:30 PM", "4:00 PM"
|
103 |
+
]
|
104 |
},
|
105 |
"Dr. Amit Patel": {
|
106 |
"specialty": "Stress Management",
|
107 |
"email": "[email protected]",
|
108 |
+
"times": [
|
109 |
+
"8:00 AM", "8:30 AM", "9:00 AM", "9:30 AM", "10:00 AM",
|
110 |
+
"3:00 PM", "3:30 PM", "4:00 PM", "4:30 PM", "5:00 PM"
|
111 |
+
]
|
112 |
},
|
113 |
"Dr. Sarah Brown": {
|
114 |
"specialty": "Trauma and PTSD",
|
115 |
"email": "[email protected]",
|
116 |
+
"times": [
|
117 |
+
"7:00 AM", "7:30 AM", "8:00 AM", "1:00 PM", "1:30 PM",
|
118 |
+
"6:00 PM", "6:30 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM"
|
119 |
+
]
|
120 |
}
|
121 |
}
|
122 |
|
123 |
# Initialize Gemini model
|
124 |
+
model = genai.GenerativeModel("learnlm-1.5-pro-experimental",
|
125 |
generation_config={"temperature": 0.7, "max_output_tokens": 200})
|
126 |
|
127 |
# Chatbot function
|
|
|
153 |
}.get(conversation_mode, "Maintain a balanced, empathetic tone.")
|
154 |
|
155 |
prompt = f"""
|
156 |
+
You are Healora, a compassionate chatbot focused on mental health, self-motivation, and being a supportive friend. Based on the user's input: {message}, provide:
|
157 |
- Empathetic, sensitive responses in the user's language (detected as {lang}).
|
158 |
- {tone_instruction}
|
159 |
- Suggestions for coping strategies if distress is detected, tailored to their mood or input.
|
|
|
185 |
if user_msg:
|
186 |
chat_display += f"**You**: {user_msg}\n\n"
|
187 |
if bot_msg:
|
188 |
+
chat_display += f"**Healora**: {bot_msg}\n\n"
|
189 |
|
190 |
return chat_display, state
|
191 |
|
|
|
199 |
if mood and mood != "Select mood":
|
200 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
201 |
state["mood_journal"].append({"timestamp": timestamp, "mood": mood.lower()})
|
|
|
202 |
trend_output, state = show_mood_trends(state)
|
203 |
return "Mood logged!", trend_output, state
|
204 |
return "Please select a mood.", None, state
|
|
|
255 |
Please confirm with the client.
|
256 |
|
257 |
Best,
|
258 |
+
Healora
|
259 |
"""
|
260 |
therapist_message = create_message(therapist_email, "New Appointment", therapist_body)
|
261 |
|
|
|
270 |
- Time: {time_slot}
|
271 |
|
272 |
Best,
|
273 |
+
Healora
|
274 |
"""
|
275 |
user_message = create_message(user_email, "Appointment Confirmation", user_body)
|
276 |
|
|
|
326 |
return "Invalid date format (use YYYY-MM-DD).", state
|
327 |
|
328 |
try:
|
329 |
+
# Convert 12-hour format to 24-hour for validation
|
330 |
+
appointment_time = datetime.strptime(time_slot, "%I:%M %p").strftime("%H:%M")
|
331 |
+
if time_slot not in therapists[therapist]["times"]:
|
332 |
+
return f"Time {time_slot} is not available for {therapist}.", state
|
333 |
except ValueError:
|
334 |
+
return "Invalid time format.", state
|
335 |
|
336 |
appointment = {
|
337 |
"therapist": therapist,
|
338 |
+
"time": time_slot,
|
339 |
"date": date,
|
340 |
"user_email": user_email,
|
341 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
343 |
state["appointments"].append(appointment)
|
344 |
|
345 |
therapist_email = therapists[therapist]["email"]
|
346 |
+
success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email, state)
|
347 |
if success:
|
348 |
+
return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent!", state
|
349 |
else:
|
350 |
+
return (f"Appointment booked with {therapist} on {date} at {time_slot}. "
|
351 |
f"Email sending failed: {error_msg}. Please contact {therapist_email}."), state
|
352 |
|
353 |
+
# Minimal CSS for UI polish
|
354 |
+
custom_css = """
|
355 |
+
button { padding: 10px 20px; margin: 5px; }
|
356 |
+
input, select { border: 1px solid #ccc; border-radius: 4px; padding: 8px; }
|
357 |
+
.gr-row { gap: 10px; }
|
358 |
+
.gr-column { padding: 10px; }
|
359 |
+
.gr-textbox, .gr-dropdown { margin-bottom: 10px; }
|
360 |
+
"""
|
361 |
+
|
362 |
# Gradio Interface
|
363 |
+
with gr.Blocks(title="Healora Chatbot", css=custom_css) as demo:
|
364 |
state = gr.State({"chat_history": [], "mood_journal": [], "feedback": [], "appointments": [], "failed_emails": []})
|
365 |
|
366 |
+
gr.Markdown("## Healora")
|
367 |
+
gr.Markdown("Your safe space for healing and hope.")
|
368 |
+
gr.Markdown("---")
|
369 |
|
370 |
with gr.Row():
|
371 |
with gr.Column(scale=1):
|
372 |
mood = gr.Dropdown(
|
373 |
choices=["Select mood", "Happy", "Sad", "Anxious", "Stressed", "Angry", "Overwhelmed", "Hopeful", "Lonely", "Other"],
|
374 |
+
label="How Are You Feeling?",
|
375 |
value="Select mood"
|
376 |
)
|
377 |
conversation_mode = gr.Radio(
|
378 |
choices=["Neutral", "Calm", "Motivational", "Empathetic", "Hopeful"],
|
379 |
+
label="Choose a Tone",
|
380 |
value="Neutral"
|
381 |
)
|
382 |
region = gr.Dropdown(
|
|
|
386 |
)
|
387 |
with gr.Column(scale=2):
|
388 |
chatbot = gr.Textbox(
|
389 |
+
label="Chat with Healora",
|
390 |
value="",
|
391 |
interactive=False,
|
392 |
+
lines=14,
|
393 |
show_copy_button=True
|
394 |
)
|
395 |
user_input = gr.Textbox(
|
396 |
+
placeholder="Share your thoughts...",
|
397 |
+
label="Your Thoughts",
|
398 |
lines=3
|
399 |
)
|
400 |
with gr.Row():
|
401 |
+
submit_btn = gr.Button("Send", variant="primary")
|
402 |
clear_btn = gr.Button("Clear Chat")
|
403 |
|
404 |
+
gr.Markdown("---")
|
405 |
+
|
406 |
emergency_btn = gr.Button("Crisis Support")
|
407 |
emergency_output = gr.Markdown("")
|
408 |
|
409 |
with gr.Accordion("Mood Journal"):
|
410 |
log_mood_btn = gr.Button("Log Mood")
|
411 |
+
mood_log_output = gr.Textbox(label="Status", interactive=False)
|
412 |
+
mood_trend_output = gr.Plot(label="Mood Trends")
|
413 |
|
414 |
+
with gr.Accordion("Feedback"):
|
415 |
+
feedback_text = gr.Textbox(label="Your Feedback", placeholder="How can we improve?")
|
416 |
+
feedback_rating = gr.Slider(minimum=1, maximum=5, step=1, label="Rate Your Experience")
|
417 |
+
feedback_btn = gr.Button("Submit")
|
418 |
+
feedback_output = gr.Textbox(label="Status", interactive=False)
|
419 |
|
420 |
+
with gr.Accordion("Appointments"):
|
421 |
therapist = gr.Dropdown(
|
422 |
choices=list(therapists.keys()),
|
423 |
+
label="Select Therapist"
|
424 |
)
|
425 |
date = gr.Textbox(
|
426 |
+
label="Date",
|
427 |
+
placeholder="YYYY-MM-DD (e.g., 2025-04-22)",
|
428 |
+
info="Enter date in YYYY-MM-DD format"
|
429 |
)
|
430 |
time_slot = gr.Dropdown(
|
431 |
choices=["Select time"],
|
432 |
+
label="Time",
|
433 |
interactive=True,
|
434 |
allow_custom_value=True
|
435 |
)
|
436 |
user_email = gr.Textbox(
|
437 |
+
label="Email",
|
438 |
placeholder="e.g., [email protected]"
|
439 |
)
|
440 |
+
schedule_btn = gr.Button("Book Appointment", variant="primary")
|
441 |
+
schedule_output = gr.Textbox(label="Status", interactive=False)
|
442 |
|
443 |
submit_btn.click(
|
444 |
fn=chatbot_function,
|
|
|
481 |
outputs=[schedule_output, state]
|
482 |
)
|
483 |
|
484 |
+
gr.Markdown("---")
|
485 |
gr.Markdown("""
|
486 |
**Resources**:
|
487 |
- [Befrienders Worldwide](https://befrienders.org/)
|