Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -103,6 +103,10 @@ model = genai.GenerativeModel("gemini-1.5-flash",
|
|
103 |
def chatbot_function(message, mood, conversation_mode, region, state):
|
104 |
if "chat_history" not in state:
|
105 |
state["chat_history"] = []
|
|
|
|
|
|
|
|
|
106 |
|
107 |
history = state["chat_history"]
|
108 |
|
@@ -150,20 +154,64 @@ def chatbot_function(message, mood, conversation_mode, region, state):
|
|
150 |
history[-1][1] = response_text
|
151 |
state["chat_history"] = history
|
152 |
|
153 |
-
chat_display =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
for user_msg, bot_msg in history:
|
155 |
if user_msg:
|
156 |
-
chat_display += f"
|
157 |
if bot_msg:
|
158 |
-
chat_display += f"
|
159 |
-
|
160 |
-
return chat_display
|
161 |
|
162 |
# Clear chat history
|
163 |
def clear_chat(state):
|
164 |
state["chat_history"] = []
|
165 |
return "", state
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
# Mood journal function
|
168 |
def log_mood(mood, state):
|
169 |
if mood and mood != "Select mood (optional)":
|
@@ -207,10 +255,18 @@ def create_message(to, subject, message_text):
|
|
207 |
return {"raw": raw}
|
208 |
|
209 |
# Send emails using Gmail API
|
210 |
-
def send_emails(therapist, time_slot, date, user_email, therapist_email, appointment_note, state):
|
211 |
if "failed_emails" not in state:
|
212 |
state["failed_emails"] = []
|
213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
therapist_body = f"""
|
215 |
Dear {therapist},
|
216 |
|
@@ -222,6 +278,7 @@ You have a new appointment:
|
|
222 |
- Therapist Details:
|
223 |
- Specialty: {therapists[therapist]["specialty"]}
|
224 |
- Email: {therapist_email}
|
|
|
225 |
|
226 |
Please confirm with the client.
|
227 |
|
@@ -301,7 +358,7 @@ def schedule_appointment(therapist, time_slot, date, user_email, appointment_not
|
|
301 |
state["appointments"].append(appointment)
|
302 |
|
303 |
therapist_email = therapists[therapist]["email"]
|
304 |
-
success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email, appointment_note, state)
|
305 |
if success:
|
306 |
return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent from {GMAIL_ADDRESS} to {therapist_email} and {user_email}!", state
|
307 |
else:
|
@@ -390,7 +447,7 @@ Healora
|
|
390 |
|
391 |
# Gradio Interface
|
392 |
with gr.Blocks(title="Healora: Mental Health Support Chatbot") as demo:
|
393 |
-
state = gr.State({"chat_history": [], "mood_journal": [], "appointments": [], "failed_emails": []})
|
394 |
|
395 |
gr.Markdown("# Healora: Your Safe Space for Healing and Hope")
|
396 |
gr.Markdown("I'm here to listen and support you. Feel free to share how you're feeling.")
|
@@ -412,11 +469,17 @@ with gr.Blocks(title="Healora: Mental Health Support Chatbot") as demo:
|
|
412 |
value="Global"
|
413 |
)
|
414 |
|
415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
label="Conversation",
|
417 |
-
value=""
|
418 |
-
interactive=False,
|
419 |
-
lines=10
|
420 |
)
|
421 |
user_input = gr.Textbox(
|
422 |
placeholder="Type your message here...",
|
@@ -507,6 +570,16 @@ with gr.Blocks(title="Healora: Mental Health Support Chatbot") as demo:
|
|
507 |
inputs=[state],
|
508 |
outputs=[chatbot, state]
|
509 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
emergency_btn.click(
|
511 |
fn=show_emergency_resources,
|
512 |
inputs=region,
|
|
|
103 |
def chatbot_function(message, mood, conversation_mode, region, state):
|
104 |
if "chat_history" not in state:
|
105 |
state["chat_history"] = []
|
106 |
+
if "conversation_archive" not in state:
|
107 |
+
state["conversation_archive"] = []
|
108 |
+
if "current_conversation_id" not in state:
|
109 |
+
state["current_conversation_id"] = 0
|
110 |
|
111 |
history = state["chat_history"]
|
112 |
|
|
|
154 |
history[-1][1] = response_text
|
155 |
state["chat_history"] = history
|
156 |
|
157 |
+
chat_display = generate_chat_display(history)
|
158 |
+
|
159 |
+
return chat_display, state
|
160 |
+
|
161 |
+
# Generate chat display with left/right alignment
|
162 |
+
def generate_chat_display(history):
|
163 |
+
chat_display = """
|
164 |
+
<style>
|
165 |
+
.chat-container { max-width: 600px; margin: auto; }
|
166 |
+
.message { margin: 10px 0; padding: 10px; border-radius: 10px; width: 80%; }
|
167 |
+
.bot-message { background-color: #e6f3ff; float: left; clear: both; }
|
168 |
+
.user-message { background-color: #d4edda; float: right; clear: both; }
|
169 |
+
</style>
|
170 |
+
<div class='chat-container'>
|
171 |
+
"""
|
172 |
for user_msg, bot_msg in history:
|
173 |
if user_msg:
|
174 |
+
chat_display += f"<div class='message user-message'><strong>You</strong>: {user_msg}</div>"
|
175 |
if bot_msg:
|
176 |
+
chat_display += f"<div class='message bot-message'><strong>Healora</strong>: {bot_msg}</div>"
|
177 |
+
chat_display += "</div>"
|
178 |
+
return chat_display
|
179 |
|
180 |
# Clear chat history
|
181 |
def clear_chat(state):
|
182 |
state["chat_history"] = []
|
183 |
return "", state
|
184 |
|
185 |
+
# Start new conversation
|
186 |
+
def new_conversation(state):
|
187 |
+
if state["chat_history"]:
|
188 |
+
state["conversation_archive"].append({
|
189 |
+
"id": state["current_conversation_id"],
|
190 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
191 |
+
"history": state["chat_history"]
|
192 |
+
})
|
193 |
+
state["current_conversation_id"] += 1
|
194 |
+
state["chat_history"] = []
|
195 |
+
return "", update_conversation_dropdown(state), state
|
196 |
+
|
197 |
+
# Update conversation dropdown
|
198 |
+
def update_conversation_dropdown(state):
|
199 |
+
choices = ["Current Conversation"] + [
|
200 |
+
f"Conversation {conv['id']} ({conv['timestamp']})"
|
201 |
+
for conv in state["conversation_archive"]
|
202 |
+
]
|
203 |
+
return gr.update(choices=choices, value="Current Conversation")
|
204 |
+
|
205 |
+
# Load selected conversation
|
206 |
+
def load_conversation(selected_conversation, state):
|
207 |
+
if selected_conversation == "Current Conversation":
|
208 |
+
return generate_chat_display(state["chat_history"]), state
|
209 |
+
else:
|
210 |
+
for conv in state["conversation_archive"]:
|
211 |
+
if f"Conversation {conv['id']} ({conv['timestamp']})" == selected_conversation:
|
212 |
+
return generate_chat_display(conv["history"]), state
|
213 |
+
return generate_chat_display(state["chat_history"]), state
|
214 |
+
|
215 |
# Mood journal function
|
216 |
def log_mood(mood, state):
|
217 |
if mood and mood != "Select mood (optional)":
|
|
|
255 |
return {"raw": raw}
|
256 |
|
257 |
# Send emails using Gmail API
|
258 |
+
def send_emails(therapist, time_slot, date, user_email, therapist_email, appointment_note, chat_history, state):
|
259 |
if "failed_emails" not in state:
|
260 |
state["failed_emails"] = []
|
261 |
|
262 |
+
# Format chat history for email
|
263 |
+
chat_history_text = "Chat History:\n"
|
264 |
+
for user_msg, bot_msg in chat_history:
|
265 |
+
if user_msg:
|
266 |
+
chat_history_text += f"You: {user_msg}\n"
|
267 |
+
if bot_msg:
|
268 |
+
chat_history_text += f"Healora: {bot_msg}\n"
|
269 |
+
|
270 |
therapist_body = f"""
|
271 |
Dear {therapist},
|
272 |
|
|
|
278 |
- Therapist Details:
|
279 |
- Specialty: {therapists[therapist]["specialty"]}
|
280 |
- Email: {therapist_email}
|
281 |
+
- {chat_history_text}
|
282 |
|
283 |
Please confirm with the client.
|
284 |
|
|
|
358 |
state["appointments"].append(appointment)
|
359 |
|
360 |
therapist_email = therapists[therapist]["email"]
|
361 |
+
success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email, appointment_note, state["chat_history"], state)
|
362 |
if success:
|
363 |
return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent from {GMAIL_ADDRESS} to {therapist_email} and {user_email}!", state
|
364 |
else:
|
|
|
447 |
|
448 |
# Gradio Interface
|
449 |
with gr.Blocks(title="Healora: Mental Health Support Chatbot") as demo:
|
450 |
+
state = gr.State({"chat_history": [], "conversation_archive": [], "current_conversation_id": 0, "mood_journal": [], "appointments": [], "failed_emails": []})
|
451 |
|
452 |
gr.Markdown("# Healora: Your Safe Space for Healing and Hope")
|
453 |
gr.Markdown("I'm here to listen and support you. Feel free to share how you're feeling.")
|
|
|
469 |
value="Global"
|
470 |
)
|
471 |
|
472 |
+
with gr.Row():
|
473 |
+
conversation_dropdown = gr.Dropdown(
|
474 |
+
choices=["Current Conversation"],
|
475 |
+
label="Select Conversation",
|
476 |
+
value="Current Conversation"
|
477 |
+
)
|
478 |
+
new_conversation_btn = gr.Button("New Conversation")
|
479 |
+
|
480 |
+
chatbot = gr.HTML(
|
481 |
label="Conversation",
|
482 |
+
value=""
|
|
|
|
|
483 |
)
|
484 |
user_input = gr.Textbox(
|
485 |
placeholder="Type your message here...",
|
|
|
570 |
inputs=[state],
|
571 |
outputs=[chatbot, state]
|
572 |
)
|
573 |
+
new_conversation_btn.click(
|
574 |
+
fn=new_conversation,
|
575 |
+
inputs=[state],
|
576 |
+
outputs=[chatbot, conversation_dropdown, state]
|
577 |
+
)
|
578 |
+
conversation_dropdown.change(
|
579 |
+
fn=load_conversation,
|
580 |
+
inputs=[conversation_dropdown, state],
|
581 |
+
outputs=[chatbot, state]
|
582 |
+
)
|
583 |
emergency_btn.click(
|
584 |
fn=show_emergency_resources,
|
585 |
inputs=region,
|