Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import re | |
| from typing import Any | |
| CRISIS_PATTERNS = [ | |
| r"\bkill myself\b", | |
| r"\bend my life\b", | |
| r"\bsuicide\b", | |
| r"\bsuicidal\b", | |
| r"\bself[-\s]?harm\b", | |
| r"\bhurt myself\b", | |
| r"\bi do not want to live\b", | |
| r"\bi don't want to live\b", | |
| r"\bلا اريد ان اعيش\b", | |
| r"\bانتحار\b", | |
| r"\bأنتحر\b", | |
| r"\bأقتل نفسي\b", | |
| ] | |
| CRISIS_RESPONSES = { | |
| "en": ( | |
| "I'm really sorry you're feeling this much pain. If you might hurt yourself or feel in immediate danger, " | |
| "please call your local emergency number now or go to the nearest emergency department. If you are in the " | |
| "US or Canada, call or text 988 for immediate crisis support. If you can, stay near another person and " | |
| "move away from anything you could use to hurt yourself." | |
| ), | |
| "fr": ( | |
| "Je suis vraiment désolé que tu ressentes autant de douleur. Si tu risques de te faire du mal ou si tu es " | |
| "en danger immédiat, appelle maintenant les urgences locales ou va au service d'urgence le plus proche. " | |
| "Si tu es aux États-Unis ou au Canada, appelle ou envoie un message au 988. Si possible, reste près d'une " | |
| "autre personne et éloigne-toi de tout objet dangereux." | |
| ), | |
| "ar": ( | |
| "أنا آسف جدًا لأنك تمر بهذا الألم. إذا كنت قد تؤذي نفسك أو تشعر أنك في خطر فوري، اتصل برقم الطوارئ المحلي " | |
| "الآن أو اذهب إلى أقرب قسم طوارئ. إذا كنت في الولايات المتحدة أو كندا يمكنك الاتصال أو إرسال رسالة إلى 988. " | |
| "حاول أن تبقى بالقرب من شخص تثق به وابتعد عن أي شيء قد تستخدمه لإيذاء نفسك." | |
| ), | |
| } | |
| GENERIC_REPLIES = { | |
| "greeting": { | |
| "en": "Hi, I'm here with you. Tell me what has been going on.", | |
| "fr": "Bonjour, je suis là avec toi. Dis-moi ce qui se passe.", | |
| "ar": "مرحبًا، أنا معك. أخبرني بما يحدث.", | |
| }, | |
| "gratitude": { | |
| "en": "You're welcome. I'm glad to keep helping if you want to talk more.", | |
| "fr": "Avec plaisir. Je peux continuer à t'aider si tu veux en parler davantage.", | |
| "ar": "على الرحب والسعة. يمكنني الاستمرار في مساعدتك إذا أردت التحدث أكثر.", | |
| }, | |
| "goodbye": { | |
| "en": "Take care of yourself. I'm here if you need to come back later.", | |
| "fr": "Prends soin de toi. Je serai là si tu veux revenir plus tard.", | |
| "ar": "اعتنِ بنفسك. سأكون هنا إذا احتجت للعودة لاحقًا.", | |
| }, | |
| "out_of_scope": { | |
| "en": "I can help best with mental-health and emotional support questions.", | |
| "fr": "Je peux surtout aider avec les questions de santé mentale et de soutien émotionnel.", | |
| "ar": "يمكنني المساعدة بشكل أفضل في أسئلة الصحة النفسية والدعم العاطفي.", | |
| }, | |
| } | |
| def detect_crisis(text: str) -> dict[str, Any]: | |
| clean_text = text.lower() | |
| matched = [pattern for pattern in CRISIS_PATTERNS if re.search(pattern, clean_text)] | |
| return { | |
| "is_crisis": bool(matched), | |
| "matched_patterns": matched, | |
| } | |
| def crisis_reply(language_code: str) -> str: | |
| return CRISIS_RESPONSES.get(language_code, CRISIS_RESPONSES["en"]) | |
| def simple_reply(intent: str, language_code: str) -> str: | |
| replies = GENERIC_REPLIES.get(intent, GENERIC_REPLIES["out_of_scope"]) | |
| return replies.get(language_code, replies["en"]) | |