Ujeshhh commited on
Commit
999e1f6
·
verified ·
1 Parent(s): 536ea02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -203,9 +203,12 @@ def get_available_times(therapist):
203
  return gr.update(choices=[], value=None), "Please select a therapist."
204
 
205
  # Send emails with retries
206
- def send_emails(therapist, time_slot, date, user_email, therapist_email):
 
 
 
207
  msg_therapist = EmailMessage()
208
- msg_therapist.set_content(f"""
209
  Dear {therapist},
210
 
211
  You have a new appointment:
@@ -217,13 +220,14 @@ def send_emails(therapist, time_slot, date, user_email, therapist_email):
217
 
218
  Best,
219
  Mental Health Chatbot
220
- """)
 
221
  msg_therapist["Subject"] = "New Appointment"
222
  msg_therapist["From"] = GMAIL_ADDRESS
223
  msg_therapist["To"] = therapist_email
224
 
225
  msg_user = EmailMessage()
226
- msg_user.set_content(f"""
227
  Dear User,
228
 
229
  Your appointment is booked:
@@ -235,7 +239,8 @@ def send_emails(therapist, time_slot, date, user_email, therapist_email):
235
 
236
  Best,
237
  Mental Health Chatbot
238
- """)
 
239
  msg_user["Subject"] = "Appointment Confirmation"
240
  msg_user["From"] = GMAIL_ADDRESS
241
  msg_user["To"] = user_email
@@ -250,8 +255,15 @@ def send_emails(therapist, time_slot, date, user_email, therapist_email):
250
  return True, ""
251
  except (smtplib.SMTPException, ConnectionRefusedError, OSError) as e:
252
  if attempt == max_retries - 1:
 
 
 
 
 
 
 
253
  return False, str(e)
254
- time.sleep(2) # Wait before retry
255
  return False, "Max retries exceeded"
256
 
257
  # Schedule appointment
@@ -284,17 +296,18 @@ def schedule_appointment(therapist, time_slot, date, user_email, state):
284
 
285
  # Send emails
286
  therapist_email = therapists[therapist]["email"]
287
- success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email)
288
  if success:
289
- return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent!", state
290
  else:
291
  return (f"Appointment booked with {therapist} on {date} at {time_slot}, but emails failed: {error_msg}. "
292
- f"Please contact {therapist_email} manually with your appointment details."), state
 
293
 
294
  # Gradio Interface
295
  with gr.Blocks(title="Mental Health Support Chatbot") as demo:
296
  # Initialize state
297
- state = gr.State({"chat_history": [], "mood_journal": [], "feedback": [], "appointments": []})
298
 
299
  gr.Markdown("# Mental Health Support Chatbot")
300
  gr.Markdown("I'm here to listen and support you. Feel free to share how you're feeling.")
 
203
  return gr.update(choices=[], value=None), "Please select a therapist."
204
 
205
  # Send emails with retries
206
+ def send_emails(therapist, time_slot, date, user_email, therapist_email, state):
207
+ if "failed_emails" not in state:
208
+ state["failed_emails"] = []
209
+
210
  msg_therapist = EmailMessage()
211
+ therapist_body = f"""
212
  Dear {therapist},
213
 
214
  You have a new appointment:
 
220
 
221
  Best,
222
  Mental Health Chatbot
223
+ """
224
+ msg_therapist.set_content(therapist_body)
225
  msg_therapist["Subject"] = "New Appointment"
226
  msg_therapist["From"] = GMAIL_ADDRESS
227
  msg_therapist["To"] = therapist_email
228
 
229
  msg_user = EmailMessage()
230
+ user_body = f"""
231
  Dear User,
232
 
233
  Your appointment is booked:
 
239
 
240
  Best,
241
  Mental Health Chatbot
242
+ """
243
+ msg_user.set_content(user_body)
244
  msg_user["Subject"] = "Appointment Confirmation"
245
  msg_user["From"] = GMAIL_ADDRESS
246
  msg_user["To"] = user_email
 
255
  return True, ""
256
  except (smtplib.SMTPException, ConnectionRefusedError, OSError) as e:
257
  if attempt == max_retries - 1:
258
+ # Store failed email content
259
+ state["failed_emails"].append({
260
+ "therapist_email": {"to": therapist_email, "subject": "New Appointment", "body": therapist_body},
261
+ "user_email": {"to": user_email, "subject": "Appointment Confirmation", "body": user_body},
262
+ "error": str(e),
263
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
264
+ })
265
  return False, str(e)
266
+ time.sleep(2)
267
  return False, "Max retries exceeded"
268
 
269
  # Schedule appointment
 
296
 
297
  # Send emails
298
  therapist_email = therapists[therapist]["email"]
299
+ success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email, state)
300
  if success:
301
+ return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent to {therapist_email} and {user_email}!", state
302
  else:
303
  return (f"Appointment booked with {therapist} on {date} at {time_slot}, but emails failed: {error_msg}. "
304
+ f"Please email {therapist_email} with your details (date: {date}, time: {time_slot}) and check {user_email} for confirmation. "
305
+ f"Emails may be in spam/junk."), state
306
 
307
  # Gradio Interface
308
  with gr.Blocks(title="Mental Health Support Chatbot") as demo:
309
  # Initialize state
310
+ state = gr.State({"chat_history": [], "mood_journal": [], "feedback": [], "appointments": [], "failed_emails": []})
311
 
312
  gr.Markdown("# Mental Health Support Chatbot")
313
  gr.Markdown("I'm here to listen and support you. Feel free to share how you're feeling.")