Spaces:
Sleeping
Sleeping
Download safety.py from scchess/confidence-api: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/spaces/scchess/confidence-api/resolve/main/safety.py
- Command line
-
hf download hf://spaces/scchess/confidence-api/safety.py
-
curl -L -o safety.py https://huggingface.co/spaces/scchess/confidence-api/resolve/main/safety.py
2.68 kB
| """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)}" | |