Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,9 @@ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
18 |
GMAIL_ADDRESS = os.getenv("GMAIL_ADDRESS")
|
19 |
GMAIL_APP_PASSWORD = os.getenv("GMAIL_APP_PASSWORD")
|
20 |
|
|
|
|
|
|
|
21 |
# Coping Strategies Library
|
22 |
coping_strategies = {
|
23 |
"happy": [
|
@@ -202,7 +205,7 @@ def get_available_times(therapist):
|
|
202 |
return gr.update(choices=therapists[therapist]["times"], value=None), f"Available times for {therapist} loaded."
|
203 |
return gr.update(choices=[], value=None), "Please select a therapist."
|
204 |
|
205 |
-
# Send emails with
|
206 |
def send_emails(therapist, time_slot, date, user_email, therapist_email, state):
|
207 |
if "failed_emails" not in state:
|
208 |
state["failed_emails"] = []
|
@@ -245,6 +248,17 @@ def send_emails(therapist, time_slot, date, user_email, therapist_email, state):
|
|
245 |
msg_user["From"] = GMAIL_ADDRESS
|
246 |
msg_user["To"] = user_email
|
247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
max_retries = 3
|
249 |
for attempt in range(max_retries):
|
250 |
try:
|
@@ -255,7 +269,6 @@ def send_emails(therapist, time_slot, date, user_email, therapist_email, state):
|
|
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},
|
@@ -298,11 +311,12 @@ def schedule_appointment(therapist, time_slot, date, user_email, state):
|
|
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}
|
304 |
-
f"
|
305 |
-
f"
|
|
|
306 |
|
307 |
# Gradio Interface
|
308 |
with gr.Blocks(title="Mental Health Support Chatbot") as demo:
|
|
|
18 |
GMAIL_ADDRESS = os.getenv("GMAIL_ADDRESS")
|
19 |
GMAIL_APP_PASSWORD = os.getenv("GMAIL_APP_PASSWORD")
|
20 |
|
21 |
+
# Check if running in Hugging Face (set in Space secrets or environment)
|
22 |
+
IS_HUGGINGFACE = os.getenv("IS_HUGGINGFACE", "false").lower() == "true"
|
23 |
+
|
24 |
# Coping Strategies Library
|
25 |
coping_strategies = {
|
26 |
"happy": [
|
|
|
205 |
return gr.update(choices=therapists[therapist]["times"], value=None), f"Available times for {therapist} loaded."
|
206 |
return gr.update(choices=[], value=None), "Please select a therapist."
|
207 |
|
208 |
+
# Send emails with conditional SMTP
|
209 |
def send_emails(therapist, time_slot, date, user_email, therapist_email, state):
|
210 |
if "failed_emails" not in state:
|
211 |
state["failed_emails"] = []
|
|
|
248 |
msg_user["From"] = GMAIL_ADDRESS
|
249 |
msg_user["To"] = user_email
|
250 |
|
251 |
+
if IS_HUGGINGFACE:
|
252 |
+
# Skip SMTP in Hugging Face due to network issues
|
253 |
+
state["failed_emails"].append({
|
254 |
+
"therapist_email": {"to": therapist_email, "subject": "New Appointment", "body": therapist_body},
|
255 |
+
"user_email": {"to": user_email, "subject": "Appointment Confirmation", "body": user_body},
|
256 |
+
"error": "SMTP skipped in Hugging Face environment",
|
257 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
258 |
+
})
|
259 |
+
return False, "SMTP disabled in Hugging Face"
|
260 |
+
|
261 |
+
# Try SMTP for local or other environments
|
262 |
max_retries = 3
|
263 |
for attempt in range(max_retries):
|
264 |
try:
|
|
|
269 |
return True, ""
|
270 |
except (smtplib.SMTPException, ConnectionRefusedError, OSError) as e:
|
271 |
if attempt == max_retries - 1:
|
|
|
272 |
state["failed_emails"].append({
|
273 |
"therapist_email": {"to": therapist_email, "subject": "New Appointment", "body": therapist_body},
|
274 |
"user_email": {"to": user_email, "subject": "Appointment Confirmation", "body": user_body},
|
|
|
311 |
therapist_email = therapists[therapist]["email"]
|
312 |
success, error_msg = send_emails(therapist, time_slot, date, user_email, therapist_email, state)
|
313 |
if success:
|
314 |
+
return f"Appointment booked with {therapist} on {date} at {time_slot}. Emails sent from {GMAIL_ADDRESS} to {therapist_email} and {user_email}!", state
|
315 |
else:
|
316 |
+
return (f"Appointment booked with {therapist} on {date} at {time_slot}. "
|
317 |
+
f"Emails from {GMAIL_ADDRESS} could not be sent. "
|
318 |
+
f"Please email {therapist_email} with your details (date: {date}, time: {time_slot}) "
|
319 |
+
f"and check {user_email} for confirmation (spam/junk)."), state
|
320 |
|
321 |
# Gradio Interface
|
322 |
with gr.Blocks(title="Mental Health Support Chatbot") as demo:
|