Spaces:
Sleeping

File size: 2,684 Bytes
73ecc58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45aee2a
73ecc58
 
 
 
45aee2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73ecc58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45aee2a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Crisis detection that does not depend on the model following its prompt."""

from __future__ import annotations

import re

_PATTERNS = [
    r"\bsuicid\w*",
    r"\bkill(ing)?\s+my\s*self\b",
    r"\bend(ing)?\s+(it\s+all|my\s+(own\s+)?life)\b",
    r"\btake\s+my\s+(own\s+)?life\b",
    r"\bwant(s|ed)?\s+to\s+die\b",
    r"\bwish\s+i\s+(was|were)\s+dead\b",
    r"\bbetter\s+off\s+dead\b",
    r"\bno\s+(reason|point)\s+(to|in)\s+(live|living|being\s+alive)\b",
    r"\bdon'?t\s+want\s+to\s+(be\s+alive|live|exist)\b",
    r"\bself[\s-]?harm\w*",
    r"\b(hurt|hurting|cut|cutting|harm|harming)\s+my\s*self\b",
    r"\boverdos\w*",
]
_RE = re.compile("|".join(_PATTERNS), re.IGNORECASE)

_INTRO = (
    "If you're thinking about hurting yourself or you feel unsafe, please reach out "
    "to someone now:\n"
)

# Keyed by ISO country code from the phone's region setting.
_LINES: dict[str, list[str]] = {
    "AU": ["Emergency: 000", "Lifeline (24/7): 13 11 14", "Beyond Blue: 1300 22 4636"],
    "NZ": ["Emergency: 111", "Need to talk? Call or text 1737 (free, 24/7)"],
    "US": ["Emergency: 911", "988 Suicide & Crisis Lifeline: call or text 988"],
    "CA": ["Emergency: 911", "Suicide Crisis Helpline: call or text 988"],
    "GB": ["Emergency: 999", "Samaritans (free, 24/7): 116 123"],
    "IE": ["Emergency: 112 or 999", "Samaritans (free, 24/7): 116 123"],
}
_DEFAULT_REGION = "AU"
_OTHER = (
    "Call your local emergency number, or find a free helpline near you at "
    "findahelpline.com."
)


def crisis_resources(region: str | None = None) -> str:
    code = (region or "").strip().upper()
    if code == "UK":
        code = "GB"
    if code and code not in _LINES:
        return _INTRO + _OTHER
    lines = _LINES[code or _DEFAULT_REGION]
    return _INTRO + "\n".join(f"• {line}" for line in lines)


# Australian default, kept for callers that don't know the region.
CRISIS_RESOURCES = crisis_resources(_DEFAULT_REGION)

CRISIS_INSTRUCTION = (
    "Safety note: the user's latest message may mean they are at risk of harming "
    "themselves. Put confidence coaching aside. Respond briefly and warmly, take "
    "what they said seriously, and encourage them to contact a crisis line or "
    "someone they trust right now. Support details are added after your reply, "
    "so do not list phone numbers yourself."
)

FALLBACK_REPLY = (
    "I'm really glad you told me. What you're feeling matters, and you don't have "
    "to carry it alone."
)


def is_crisis(text: str) -> bool:
    return bool(_RE.search(text or ""))


def with_resources(reply: str, region: str | None = None) -> str:
    return f"{reply.rstrip()}\n\n{crisis_resources(region)}"