Tetherworking / app.py
SamanthaStorm's picture
Update app.py
02944ab verified
import gradio as gr
import spaces
import torch
import numpy as np
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline as hf_pipeline
import re
import matplotlib.pyplot as plt
import io
from PIL import Image
from datetime import datetime
from torch.nn.functional import sigmoid
from collections import Counter
import logging
import traceback
import json
from deep_translator import GoogleTranslator
from langdetect import detect as detect_language, LangDetectException
import os
UI_STRINGS = {
"en": {
"tab_analyze": "Analyze Messages",
"share_messages": "Share Your Messages",
"safety_checklist": "Safety Checklist",
"analysis_results": "Analysis Results",
"message1": "Message 1 *",
"message1_required": "Message 1 (required)",
"message2": "Message 2 (optional)",
"message3": "Message 3 (optional)",
"none_apply": "None of the above apply to my situation",
"none_apply_mobile": "None of the above apply",
"analyze_btn": "Analyze Messages",
"analyzing_btn": "⏳ Analyzing...",
"safety_plan_btn": "🛡️ Get Safety Plan",
"safety_plan_btn_mobile": "🛡️ Safety Plan",
"full_analysis_btn": "📊 Show Full Analysis",
"full_analysis_btn_mobile": "📊 Full Analysis",
"download_btn": "📄 Download Report",
"download_btn_mobile": "📄 Download",
"download_file": "Download Report",
"pattern_timeline": "Pattern Timeline",
"results_placeholder": "Results will appear here after analysis...",
"based_on_messages": "Based on the messages you shared",
"key_concerns": "Key Concerns Found",
"additional_analysis": "Additional Analysis",
"personalized_recommendations": "Personalized Recommendations",
"risk_score": "Risk Score",
"no_concerns": "No specific concerns identified in the messages.",
"pattern_detected": "pattern detected",
"patterns_detected": "patterns detected",
"low_risk": "Low Risk",
"moderate_risk": "Moderate Concern",
"high_risk": "High Risk",
"critical_risk": "Critical Risk",
"darvo_moderate": "Moderate",
"darvo_high": "High",
"darvo_low": "Low",
"emotional_tones": "Emotional Tones Detected",
"boundary_violations": "Boundary Violations Detected",
"mixed_boundary": "Mixed Boundary Patterns",
"safety_checklist_optional": "⚠️ Safety Checklist (Optional)",
"concerning_patterns": "Concerning communication patterns",
"checklist_q1": "Partner has access to firearms or weapons",
"checklist_q2": "Partner threatened to kill you",
"checklist_q3": "Partner threatened you with a weapon",
"checklist_q4": "Partner has ever choked you, even if you considered it consensual at the time",
"checklist_q5": "Partner injured or threatened your pet(s)",
"checklist_q6": "Partner has broken your things, punched or kicked walls, or thrown things",
"checklist_q7": "Partner forced or coerced you into unwanted sexual acts",
"checklist_q8": "Partner threatened to take away your children",
"checklist_q9": "Violence has increased in frequency or severity",
"checklist_q10": "Partner monitors your calls/GPS/social media",
"share_messages_instruction": "Enter up to three messages that made you feel uncomfortable, confused, or concerned. For the most accurate analysis, include messages from recent emotionally intense conversations.",
"privacy_title": "Your Privacy Matters",
"privacy_body": "Your messages are analyzed locally and are not stored or shared. This tool is for educational purposes and not a substitute for professional counseling.",
"results_placeholder": "Results will appear here after analysis...",
"checklist_optional": "Optional but recommended. Check any that apply to your situation:",
"darvo_description": "DARVO (Deny, Attack, Reverse Victim & Offender) indicates potential narrative manipulation where the speaker may be deflecting responsibility.",
"emotional_tone_description": "Emotional tone analysis helps identify underlying manipulation tactics or concerning emotional patterns.",
"boundary_footer": "Boundary analysis reflects how the sender relates to limits — yours or their own.",
"manipulative_boundary_desc": "Communication uses tactics designed to influence or control rather than express needs directly.",
"violated_boundary_desc": "Communication crosses stated or implied boundaries without acknowledgment.",
"dismissed_boundary_desc": "Communication minimizes or ignores boundaries that have been expressed.",
"message_label": "Message",
"unknown_concern": "Unknown Concern",
"no_description": "No description available",
# Pattern names
"gaslighting": "Gaslighting",
"control": "Control",
"guilt tripping": "Guilt Tripping",
"blame shifting": "Blame Shifting",
"dismissiveness": "Dismissiveness",
"projection": "Projection",
"insults": "Insults",
"recovery phase": "Recovery Phase",
"contradictory statements": "Contradictory Statements",
"obscure language": "Obscure Language",
"veiled threats": "Veiled Threats",
"stalking language": "Stalking Language",
"false concern": "False Concern",
"false equivalence": "False Equivalence",
"future faking": "Future Faking",
"nonabusive": "Non-Abusive Communication",
# Boundary labels
"Respected Boundary": "Respected Boundary",
"Violated Boundary": "Violated Boundary",
"Dismissed Boundary": "Dismissed Boundary",
"Manipulative Boundary": "Manipulative Boundary",
# Emotional tones
"contradictory gaslight": "Contradictory Gaslight",
"emotional threat": "Emotional Threat",
"cold invalidation": "Cold Invalidation",
"predictive punishment": "Predictive Punishment",
"coercive warmth": "Coercive Warmth",
"weaponized sadness": "Weaponized Sadness",
"forced accountability flip": "Forced Accountability Flip",
"performative regret": "Performative Regret",
"emotional instability": "Emotional Instability",
"menacing calm": "Menacing Calm",
"obsessive fixation": "Obsessive Fixation",
"surveillance intimacy": "Surveillance Intimacy",
"conditional menace": "Conditional Menace",
"predatory concern": "Predatory Concern",
"victim cosplay": "Victim Cosplay",
"entitled rage": "Entitled Rage",
"manipulative hope": "Manipulative Hope",
"false vulnerability": "False Vulnerability",
"calculated coldness": "Calculated Coldness",
"genuine vulnerability": "Genuine Vulnerability",
"neutral": "Neutral",
# Concern descriptions
"concern_gaslighting": "Making you question your memory, perception, or reality",
"concern_control": "Attempts to manage your behavior, decisions, or daily activities",
"concern_guilt_tripping": "Making you feel guilty to influence your behavior",
"concern_blame_shifting": "Placing responsibility for their actions onto you",
"concern_dismissiveness": "Minimizing or invalidating your feelings and experiences",
"concern_projection": "Accusing you of behaviors they themselves exhibit",
"concern_insults": "Name-calling or personal attacks intended to hurt",
"concern_recovery_phase": "A temporary calm period used to reset the abuse cycle",
"concern_contradictory_statements": "Saying things that conflict with previous statements",
"concern_obscure_language": "Using confusing or unclear language to avoid accountability",
"concern_veiled_threats": "Indirect threats or intimidating language",
"concern_stalking_language": "Language indicating monitoring or tracking behavior",
"concern_false_concern": "Expressing concern as a means of control",
"concern_false_equivalence": "Drawing false comparisons to deflect from the issue",
"concern_future_faking": "Making promises about the future to manipulate behavior",
"add_more_messages": "➕ Add More Messages (Optional)",
"safety_checklist_btn": "⚠️ Safety Checklist (Optional)",
"share_messages_mobile_header": "📝 Share Your Messages",
"checklist_accuracy_mobile": "Check any that apply to improve analysis accuracy:",
"tagline": "Share messages that concern you, and we'll help you understand what patterns might be present.",
"safety_resources_header": "🛡️ Safety Planning",
"safety_resources_intro": "If you're concerned about your safety, here are immediate resources and steps you can take.",
"emergency_resources_header": "🚨 Emergency Resources",
"emergency_911": "911 - For immediate danger",
"emergency_dv_hotline": "1-800-799-7233 - National DV Hotline (24/7)",
"emergency_text_line": "Text START to 88788 - Crisis Text Line",
"emergency_suicide_line": "988 - National Suicide Prevention Lifeline",
"support_resources_header": "💚 Support Resources",
"support_hotline_chat": "thehotline.org - Online chat support",
"support_counseling": "Local counseling services - Professional support",
"support_personal": "Trusted friends/family - Personal support network",
"support_legal": "Legal advocacy - Know your rights",
"placeholder_message": "Enter the message here...",
"placeholder_message_mobile": "Enter the concerning message here...",
"placeholder_message2": "Enter another message...",
"placeholder_message3": "Enter a third message...",
}
}
SUPPORTED_LANGUAGES = {
"English": "en",
"Spanish": "es",
"French": "fr",
"Portuguese": "pt",
"Arabic": "ar",
"Hindi": "hi",
"Tagalog": "tl",
"Haitian Creole": "ht",
"Chinese (Simplified)": "zh-CN",
"Korean": "ko",
"Vietnamese": "vi",
"Russian": "ru",
"Somali": "so",
"Amharic": "am",
"Polish": "pl",
"Ukrainian": "uk",
"Hmong": "hmn",
"Khmer": "km",
"Punjabi": "pa",
"Urdu": "ur",
"Swahili": "sw",
"Tigrinya": "ti",
"Burmese": "my",
"Nepali": "ne",
"Japanese": "ja",
"Turkish": "tr",
"Farsi": "fa",
"Romanian": "ro",
"Pashto": "ps",
}
TRANSLATION_CACHE_FILE = "ui_translations.json"
def load_or_build_translation_cache():
if os.path.exists(TRANSLATION_CACHE_FILE):
print("Loading translation cache from disk...")
with open(TRANSLATION_CACHE_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
print("Building translation cache for first time - this will take a minute...")
cache = {"en": UI_STRINGS["en"]}
for lang_name, lang_code in SUPPORTED_LANGUAGES.items():
if lang_code == "en":
continue
print(f"Translating UI strings to {lang_name}...")
translated = {}
for key, value in UI_STRINGS["en"].items():
try:
translated[key] = GoogleTranslator(source='en', target=lang_code).translate(value)
except Exception:
translated[key] = value
cache[lang_code] = translated
with open(TRANSLATION_CACHE_FILE, 'w', encoding='utf-8') as f:
json.dump(cache, f, ensure_ascii=False)
print("Translation cache built and saved.")
return cache
_string_cache = load_or_build_translation_cache()
def get_ui_strings(lang_code):
return _string_cache.get(lang_code, _string_cache["en"])
def translate_dynamic_value(value, lang_code):
"""Translate a dynamic value like pattern name or emotional tone using the cache"""
if lang_code == "en" or not value:
return value
# Terms that should never be translated
no_translate = ["gaslighting", "darvo"]
if value.lower() in no_translate:
return value
s = get_ui_strings(lang_code)
value_lower = value.lower()
for key, english_val in UI_STRINGS["en"].items():
if english_val.lower() == value_lower and key in s:
return s[key]
return value
# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"Using device: {device}")
# Set up custom logging
class CustomFormatter(logging.Formatter):
"""Custom formatter with colors and better formatting"""
grey = "\x1b[38;21m"
blue = "\x1b[38;5;39m"
yellow = "\x1b[38;5;226m"
red = "\x1b[38;5;196m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
def format(self, record):
# Remove the logger name from the output
if record.levelno == logging.DEBUG:
return f"{self.blue}{record.getMessage()}{self.reset}"
elif record.levelno == logging.INFO:
return f"{self.grey}{record.getMessage()}{self.reset}"
elif record.levelno == logging.WARNING:
return f"{self.yellow}{record.getMessage()}{self.reset}"
elif record.levelno == logging.ERROR:
return f"{self.red}{record.getMessage()}{self.reset}"
elif record.levelno == logging.CRITICAL:
return f"{self.bold_red}{record.getMessage()}{self.reset}"
return record.getMessage()
# Setup logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Remove any existing handlers
logger.handlers = []
# Create console handler with custom formatter
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
logger.addHandler(ch)
# Model initialization
model_name = "SamanthaStorm/tether-multilabel-v6"
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
# sentiment model
sentiment_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-sentiment-v3").to(device)
sentiment_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tether-sentiment-v3", use_fast=False)
sentiment_model.eval()
emotion_pipeline = hf_pipeline(
"text-classification",
model="j-hartmann/emotion-english-distilroberta-base",
return_all_scores=True, # Get all emotion scores
top_k=None, # Don't limit to top k predictions
truncation=True,
device=0 if torch.cuda.is_available() else -1
)
# DARVO model
darvo_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1").to(device)
darvo_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1", use_fast=False)
darvo_model.eval()
# Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
boundary_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/boundary_violation_model", use_fast=False)
boundary_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/boundary_violation_model").to(device)
boundary_model.eval()
@spaces.GPU
def predict_boundary_health(text):
"""
Predict boundary health for given text
Returns:
- 0 for Respected (healthy)
- 1 for Violated (unhealthy)
- 2 for Dismissed (unhealthy)
- 3 for Manipulative (unhealthy)
"""
try:
inputs = boundary_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = boundary_model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
# Return the actual prediction (0, 1, 2, or 3)
predicted_class = torch.argmax(predictions, dim=-1).item()
confidence = predictions[0][predicted_class].item()
return predicted_class, confidence
except Exception as e:
logger.error(f"Error in boundary prediction: {e}")
return 1, 1.0 # Return Violated (unhealthy) with full confidence on error
def get_boundary_assessment(text, prediction, confidence=1.0):
"""Get boundary assessment based on the 4-category prediction"""
if prediction == 0: # Respected (healthy)
return {
'assessment': 'healthy',
'label': 'Respected Boundary',
'confidence': confidence,
'description': 'This communication shows healthy boundary setting with mutual respect',
'recommendations': ['Continue this respectful communication approach']
}
elif prediction == 1: # Violated (unhealthy)
return {
'assessment': 'unhealthy',
'label': 'Violated Boundary',
'confidence': confidence,
'description': 'Communication shows boundary violation patterns',
'recommendations': ['Acknowledge the boundary violation', 'Use "I" statements instead of accusations', 'Focus on respectful communication']
}
elif prediction == 2: # Dismissed (unhealthy)
return {
'assessment': 'unhealthy',
'label': 'Dismissed Boundary',
'confidence': confidence,
'description': 'Communication shows boundary dismissal patterns',
'recommendations': ['Recognize and validate boundaries', 'Avoid minimizing others\' concerns', 'Practice active listening']
}
else: # Manipulative (unhealthy) - prediction == 3
return {
'assessment': 'unhealthy',
'label': 'Manipulative Boundary',
'confidence': confidence,
'description': 'Communication shows manipulative boundary patterns',
'recommendations': ['Avoid manipulation tactics', 'Communicate needs directly', 'Respect others\' autonomy']
}
# Constants and Labels
LABELS = [
"recovery phase", "control", "gaslighting", "guilt tripping", "dismissiveness",
"blame shifting", "nonabusive", "projection", "insults",
"contradictory statements", "obscure language",
"veiled threats", "stalking language", "false concern",
"false equivalence", "future faking"
]
SENTIMENT_LABELS = ["supportive", "undermining"]
THRESHOLDS = {
"recovery phase": 0.278,
"control": 0.287,
"gaslighting": 0.144,
"guilt tripping": 0.220,
"dismissiveness": 0.142,
"blame shifting": 0.183,
"projection": 0.253,
"insults": 0.247,
"contradictory statements": 0.200,
"obscure language": 0.455,
"nonabusive": 0.281,
# NEW v6 patterns:
"veiled threats": 0.310,
"stalking language": 0.339,
"false concern": 0.334,
"false equivalence": 0.317,
"future faking": 0.385
}
PATTERN_WEIGHTS = {
"recovery phase": 0.7,
"control": 1.4,
"gaslighting": 1.3,
"guilt tripping": 1.2,
"dismissiveness": 0.9,
"blame shifting": 1.0,
"projection": 0.5,
"insults": 1.4,
"contradictory statements": 1.0,
"obscure language": 0.9,
"nonabusive": 0.0,
# NEW v6 patterns:
"veiled threats": 1.6, # High weight - very dangerous
"stalking language": 1.8, # Highest weight - extremely dangerous
"false concern": 1.1, # Moderate weight - manipulative
"false equivalence": 1.3, # Enhances DARVO detection
"future faking": 0.8 # Lower weight - manipulation tactic
}
ESCALATION_QUESTIONS = [
("Partner has access to firearms or weapons", 4),
("Partner threatened to kill you", 3),
("Partner threatened you with a weapon", 3),
("Partner has ever choked you, even if you considered it consensual at the time", 4),
("Partner injured or threatened your pet(s)", 3),
("Partner has broken your things, punched or kicked walls, or thrown things ", 2),
("Partner forced or coerced you into unwanted sexual acts", 3),
("Partner threatened to take away your children", 2),
("Violence has increased in frequency or severity", 3),
("Partner monitors your calls/GPS/social media", 2)
]
RISK_STAGE_LABELS = {
1: "🌀 Risk Stage: Tension-Building\nThis message reflects rising emotional pressure or subtle control attempts.",
2: "🔥 Risk Stage: Escalation\nThis message includes direct or aggressive patterns, suggesting active harm.",
3: "🌧️ Risk Stage: Reconciliation\nThis message reflects a reset attempt—apologies or emotional repair without accountability.",
4: "🌸 Risk Stage: Calm / Honeymoon\nThis message appears supportive but may follow prior harm, minimizing it."
}
THREAT_MOTIFS = [
"i'll kill you", "i'm going to hurt you", "you're dead", "you won't survive this",
"i'll break your face", "i'll bash your head in", "i'll snap your neck",
"i'll come over there and make you shut up", "i'll knock your teeth out",
"you're going to bleed", "you want me to hit you?", "i won't hold back next time",
"i swear to god i'll beat you", "next time, i won't miss", "i'll make you scream",
"i know where you live", "i'm outside", "i'll be waiting", "i saw you with him",
"you can't hide from me", "i'm coming to get you", "i'll find you", "i know your schedule",
"i watched you leave", "i followed you home", "you'll regret this", "you'll be sorry",
"you're going to wish you hadn't", "you brought this on yourself", "don't push me",
"you have no idea what i'm capable of", "you better watch yourself",
"i don't care what happens to you anymore", "i'll make you suffer", "you'll pay for this",
"i'll never let you go", "you're nothing without me", "if you leave me, i'll kill myself",
"i'll ruin you", "i'll tell everyone what you did", "i'll make sure everyone knows",
"i'm going to destroy your name", "you'll lose everyone", "i'll expose you",
"your friends will hate you", "i'll post everything", "you'll be cancelled",
"you'll lose everything", "i'll take the house", "i'll drain your account",
"you'll never see a dime", "you'll be broke when i'm done", "i'll make sure you lose your job",
"i'll take your kids", "i'll make sure you have nothing", "you can't afford to leave me",
"don't make me do this", "you know what happens when i'm mad", "you're forcing my hand",
"if you just behaved, this wouldn't happen", "this is your fault",
"you're making me hurt you", "i warned you", "you should have listened"
]
# MOVED TO TOP LEVEL - Fixed tone severity mapping
TONE_SEVERITY = {
# Highest danger tones
"obsessive fixation": 4,
"menacing calm": 4,
"conditional menace": 4,
"surveillance intimacy": 4,
# High danger tones
"predatory concern": 3,
"victim cosplay": 3,
"entitled rage": 3,
"direct threat": 3,
# Moderate danger tones
"manipulative hope": 2,
"false vulnerability": 2,
"calculated coldness": 2,
"predictive punishment": 2,
# Existing tones (keep current mappings)
"emotional threat": 3,
"forced accountability flip": 3,
"performative regret": 2,
"coercive warmth": 2,
"cold invalidation": 2,
"weaponized sadness": 2,
"contradictory gaslight": 2,
# Low risk tones
"neutral": 0,
"genuine vulnerability": 0
}
# MOVED TO TOP LEVEL - Helper functions
def log_emotional_tone_usage(tone_tag, patterns):
"""Log tone usage for analytics"""
logger.debug(f"🔍 Detected tone tag: {tone_tag} with patterns: {patterns}")
# Track dangerous tone combinations
dangerous_tones = [
"obsessive fixation", "menacing calm", "predatory concern",
"surveillance intimacy", "conditional menace", "victim cosplay"
]
if tone_tag in dangerous_tones:
logger.warning(f"⚠️ Dangerous emotional tone detected: {tone_tag}")
def calculate_tone_risk_boost(tone_tag):
"""Calculate risk boost based on emotional tone severity"""
return TONE_SEVERITY.get(tone_tag, 0)
def should_show_safety_planning(abuse_score, escalation_risk, detected_patterns):
"""Check if we should show safety planning"""
if escalation_risk in ["High", "Critical"]:
return True
if abuse_score >= 70:
return True
dangerous_patterns = ["stalking language", "veiled threats", "threats"]
if any(pattern in detected_patterns for pattern in dangerous_patterns):
return True
return False
def generate_simple_safety_plan(abuse_score, escalation_risk, detected_patterns, lang_code="en"):
"""Generate a basic safety plan"""
plan = "🛡️ **SAFETY PLANNING RECOMMENDED**\n\n"
if escalation_risk == "Critical" or abuse_score >= 85:
plan += "🚨 **CRITICAL SAFETY SITUATION**\n\n"
plan += "**IMMEDIATE ACTIONS:**\n"
plan += "• Contact domestic violence hotline: **1-800-799-7233** (24/7, free, confidential)\n"
plan += "• Text START to **88788** for crisis text support\n"
plan += "• Consider staying with trusted friends/family tonight\n"
plan += "• Keep phone charged and accessible\n"
plan += "• Have emergency bag ready (documents, medications, cash)\n"
plan += "\n**IF IN IMMEDIATE DANGER: Call 911**\n\n"
elif escalation_risk == "High" or abuse_score >= 70:
plan += "⚠️ **HIGH RISK SITUATION**\n\n"
plan += "**SAFETY STEPS:**\n"
plan += "• Contact domestic violence hotline for safety planning: **1-800-799-7233**\n"
plan += "• Identify 3 trusted people you can contact for help\n"
plan += "• Plan escape routes and transportation options\n"
plan += "• Document concerning behaviors with dates and details\n"
plan += "• Research legal protection options\n\n"
# Add pattern-specific advice
if "stalking language" in detected_patterns:
plan += "🔍 **STALKING BEHAVIORS DETECTED:**\n"
plan += "• Vary your routines and routes\n"
plan += "• Check devices for tracking software\n"
plan += "• Keep record of all stalking incidents\n"
plan += "• Alert neighbors to watch for suspicious activity\n\n"
if "veiled threats" in detected_patterns:
plan += "⚠️ **THREATENING LANGUAGE IDENTIFIED:**\n"
plan += "• Take all threats seriously, even indirect ones\n"
plan += "• Document all threatening communications\n"
plan += "• Inform trusted people about threat patterns\n"
plan += "• Avoid being alone in isolated locations\n\n"
# Always include crisis resources
plan += "🆘 **CRISIS RESOURCES (24/7):**\n"
plan += "• **National DV Hotline:** 1-800-799-7233\n"
plan += "• **Crisis Text Line:** Text START to 88788\n"
plan += "• **Online Chat:** thehotline.org\n"
plan += "• **Emergency:** Call 911\n\n"
plan += "💙 **Remember:** You are not alone. This is not your fault. You deserve to be safe."
if lang_code != "en":
try:
plan = GoogleTranslator(source='en', target=lang_code).translate(plan)
except Exception:
pass # Fall back to English if translation fails
return plan
def detect_rare_threats(text):
rare_threats = ["necktie party", "permanent solution", "final conversation"]
if any(threat in text.lower() for threat in rare_threats):
return [("veiled threats", 0.90, 1.6)]
return []
STRUCTURAL_THREAT_PATTERNS = [
# Conditional threats
(r"you'?ll? (regret|pay for|wish you (hadn'?t|never))", "veiled threats"),
(r"(don'?t|do not) (make me|push me|test me|force me)", "veiled threats"),
(r"you have no idea what i'?m? (capable of|going to do)", "veiled threats"),
(r"(things|accidents|something) (could )?happen(s)? to (people|you)", "veiled threats"),
(r"wouldn'?t want (anything|something) (bad )?to happen", "veiled threats"),
(r"(hope|hopefully) (nothing|no one) (happens?|hurts? you)", "veiled threats"),
# Location/surveillance awareness
(r"i (know|knew|found out) where you (live|work|go|are|were)", "stalking language"),
(r"i'?ve? (been )?(watching|following|tracking) you", "stalking language"),
(r"i (saw|seen|spotted) you (with|at|leaving|coming)", "stalking language"),
(r"i'?ll? (find|come for|come after|get to) you", "stalking language"),
(r"you can'?t (hide|run|escape|get away) from me", "stalking language"),
# Future consequence framing
(r"you'?ll? (end up|be) (alone|miserable|sorry|nothing)", "veiled threats"),
(r"(no one|nobody) (will|would|is going to) (believe|help|want) you", "veiled threats"),
(r"i'?ll? make sure (everyone|everyone knows|you lose|you have nothing)", "veiled threats"),
# Coercive control threats
(r"i (control|own|have) (your|the) (money|account|finances|phone|access)", "control"),
(r"you'?ll? (lose|never see|never have) (everything|the kids|the house|a dime)", "veiled threats"),
]
def detect_structural_threats(text):
"""Detect threat syntax patterns that exact-phrase matching misses"""
text_lower = text.lower()
detected = []
for pattern, label in STRUCTURAL_THREAT_PATTERNS:
if re.search(pattern, text_lower):
detected.append(label)
return list(set(detected)) # deduplicate
def detect_enhanced_threats(text, patterns):
"""Enhanced threat detection for v6 patterns"""
text_lower = text.lower()
enhanced_threats = []
# Stalking language indicators
stalking_phrases = [
"stop at nothing", "will find you", "know where you",
"watching you", "following you", "can't hide",
"i know your", "saw you with", "you belong to me"
]
# Veiled threat indicators
veiled_threat_phrases = [
"some people might", "things happen to people who",
"be careful", "hope nothing happens", "accidents happen",
"necktie party", "permanent solution", "wouldn't want"
]
# False concern indicators
false_concern_phrases = [
"just worried about", "concerned about your",
"someone needs to protect", "for your own good"
]
if any(phrase in text_lower for phrase in stalking_phrases):
enhanced_threats.append("stalking language")
if any(phrase in text_lower for phrase in veiled_threat_phrases):
enhanced_threats.append("veiled threats")
if any(phrase in text_lower for phrase in false_concern_phrases):
enhanced_threats.append("false concern")
return enhanced_threats
def calculate_enhanced_risk_level(abuse_score, detected_patterns, escalation_risk, darvo_score):
"""Enhanced risk calculation that properly weights dangerous patterns"""
# Start with base risk from escalation system
base_risk = escalation_risk
# CRITICAL PATTERNS - Auto-elevate to HIGH risk minimum
critical_patterns = ["stalking language", "veiled threats"]
has_critical = any(pattern in detected_patterns for pattern in critical_patterns)
# DANGEROUS COMBINATIONS - Auto-elevate to CRITICAL
dangerous_combos = [
("stalking language", "control"),
("veiled threats", "stalking language"),
("stalking language", "false concern"),
("veiled threats", "control")
]
has_dangerous_combo = any(
all(pattern in detected_patterns for pattern in combo)
for combo in dangerous_combos
)
# FORCE RISK ELEVATION for dangerous patterns
if has_dangerous_combo:
return "Critical"
elif has_critical and abuse_score >= 30: # Lower threshold for critical patterns
return "High"
elif has_critical:
return "Moderate"
elif abuse_score >= 70:
return "High"
elif abuse_score >= 30:
return "Moderate"
else:
return base_risk
def get_emotion_profile(text):
"""Get emotion profile from text with all scores"""
try:
emotions = emotion_pipeline(text)
if isinstance(emotions, list) and isinstance(emotions[0], list):
# Extract all scores from the first prediction
emotion_scores = emotions[0]
# Convert to dictionary with lowercase emotion names
return {e['label'].lower(): round(e['score'], 3) for e in emotion_scores}
return {}
except Exception as e:
logger.error(f"Error in get_emotion_profile: {e}")
return {
"sadness": 0.0,
"joy": 0.0,
"neutral": 0.0,
"disgust": 0.0,
"anger": 0.0,
"fear": 0.0
}
# FIXED FUNCTION - Added missing "d" and cleaned up structure
def get_emotional_tone_tag(text, sentiment, patterns, abuse_score):
"""Get emotional tone tag based on emotions and patterns"""
emotions = get_emotion_profile(text)
sadness = emotions.get("sadness", 0)
joy = emotions.get("joy", 0)
neutral = emotions.get("neutral", 0)
disgust = emotions.get("disgust", 0)
anger = emotions.get("anger", 0)
fear = emotions.get("fear", 0)
text_lower = text.lower()
# 1. Direct Threat Detection
threat_indicators = [
"if you", "i'll make", "don't forget", "remember", "regret",
"i control", "i'll take", "you'll lose", "make sure",
"never see", "won't let"
]
if (
any(indicator in text_lower for indicator in threat_indicators) and
any(p in patterns for p in ["control", "insults"]) and
(anger > 0.2 or disgust > 0.2 or abuse_score > 70)
):
return "direct threat"
# 2. Obsessive Fixation (for stalking language)
obsessive_indicators = [
"stop at nothing", "most desired", "forever", "always will",
"belong to me", "you're mine", "never let you go", "can't live without"
]
if (
any(indicator in text_lower for indicator in obsessive_indicators) and
"stalking language" in patterns and
(joy > 0.3 or sadness > 0.4 or fear > 0.2)
):
return "obsessive fixation"
# 3. Menacing Calm (for veiled threats)
veiled_threat_indicators = [
"some people might", "accidents happen", "be careful",
"wouldn't want", "things happen", "unfortunate"
]
if (
any(indicator in text_lower for indicator in veiled_threat_indicators) and
"veiled threats" in patterns and
neutral > 0.4 and anger < 0.2
):
return "menacing calm"
# 4. Predatory Concern (for false concern)
concern_indicators = [
"worried about", "concerned about", "for your own good",
"someone needs to", "protect you", "take care of you"
]
if (
any(indicator in text_lower for indicator in concern_indicators) and
"false concern" in patterns and
(joy > 0.2 or neutral > 0.3) and sentiment == "undermining"
):
return "predatory concern"
# 5. Victim Cosplay (for false equivalence/DARVO)
victim_indicators = [
"i'm the victim", "you're abusing me", "i'm being hurt",
"you're attacking me", "i'm innocent", "you're the problem"
]
if (
any(indicator in text_lower for indicator in victim_indicators) and
"false equivalence" in patterns and
sadness > 0.4 and anger > 0.2
):
return "victim cosplay"
# 6. Manipulative Hope (for future faking)
future_indicators = [
"i'll change", "we'll be", "i promise", "things will be different",
"next time", "from now on", "i'll never", "we'll have"
]
if (
any(indicator in text_lower for indicator in future_indicators) and
"future faking" in patterns and
(joy > 0.3 or sadness > 0.3)
):
return "manipulative hope"
# 7. Surveillance Intimacy (for stalking with false intimacy)
surveillance_indicators = [
"i know you", "i saw you", "i watched", "i've been",
"your routine", "where you go", "what you do"
]
if (
any(indicator in text_lower for indicator in surveillance_indicators) and
"stalking language" in patterns and
joy > 0.2 and neutral > 0.2
):
return "surveillance intimacy"
# 8. Conditional Menace (for threats with conditions)
conditional_indicators = [
"if you", "unless you", "you better", "don't make me",
"you wouldn't want", "force me to"
]
if (
any(indicator in text_lower for indicator in conditional_indicators) and
any(p in patterns for p in ["veiled threats", "control"]) and
anger > 0.3 and neutral > 0.2
):
return "conditional menace"
# 9. False Vulnerability (manipulation disguised as weakness)
vulnerability_indicators = [
"i can't help", "i need you", "without you i", "you're all i have",
"i'm lost without", "i don't know what to do"
]
if (
any(indicator in text_lower for indicator in vulnerability_indicators) and
any(p in patterns for p in ["guilt tripping", "future faking", "false concern"]) and
sadness > 0.5 and sentiment == "undermining"
):
return "false vulnerability"
# 10. Entitled Rage (anger with entitlement)
entitlement_indicators = [
"you owe me", "after everything", "how dare you", "you should",
"i deserve", "you have no right"
]
if (
any(indicator in text_lower for indicator in entitlement_indicators) and
anger > 0.5 and
any(p in patterns for p in ["control", "insults", "blame shifting"])
):
return "entitled rage"
# 11. Calculated Coldness (deliberate emotional detachment)
cold_indicators = [
"i don't care", "whatever", "your choice", "suit yourself",
"fine by me", "your loss"
]
calculated_patterns = ["dismissiveness", "obscure language", "control"]
if (
any(indicator in text_lower for indicator in cold_indicators) and
any(p in patterns for p in calculated_patterns) and
neutral > 0.6 and all(e < 0.2 for e in [anger, sadness, joy])
):
return "calculated coldness"
# 12. Predictive Punishment
future_consequences = [
"will end up", "you'll be", "you will be", "going to be",
"will become", "will find yourself", "will realize",
"you'll regret", "you'll see", "will learn", "truly will",
"end up alone", "end up miserable"
]
dismissive_endings = [
"i'm out", "i'm done", "whatever", "good luck",
"your choice", "your problem", "regardless",
"keep", "keep on"
]
if (
(any(phrase in text_lower for phrase in future_consequences) or
any(end in text_lower for end in dismissive_endings)) and
any(p in ["dismissiveness", "control"] for p in patterns) and
(disgust > 0.2 or neutral > 0.3 or anger > 0.2)
):
return "predictive punishment"
# 13. Performative Regret
if (
sadness > 0.3 and
any(p in patterns for p in ["blame shifting", "guilt tripping", "recovery"]) and
(sentiment == "undermining" or abuse_score > 40)
):
return "performative regret"
# 14. Coercive Warmth
if (
(joy > 0.2 or sadness > 0.3) and
any(p in patterns for p in ["control", "gaslighting"]) and
sentiment == "undermining"
):
return "coercive warmth"
# 15. Cold Invalidation
if (
(neutral + disgust) > 0.4 and
any(p in patterns for p in ["dismissiveness", "projection", "obscure language"]) and
sentiment == "undermining"
):
return "cold invalidation"
# 16. Genuine Vulnerability
if (
(sadness + fear) > 0.4 and
sentiment == "supportive" and
all(p in ["recovery"] for p in patterns)
):
return "genuine vulnerability"
# 17. Emotional Threat
if (
(anger + disgust) > 0.4 and
any(p in patterns for p in ["control", "insults", "dismissiveness"]) and
sentiment == "undermining"
):
return "emotional threat"
# 18. Weaponized Sadness
if (
sadness > 0.5 and
any(p in patterns for p in ["guilt tripping", "projection"]) and
sentiment == "undermining"
):
return "weaponized sadness"
# 19. Contradictory Gaslight
if (
(joy + anger + sadness) > 0.4 and
any(p in patterns for p in ["gaslighting", "contradictory statements"]) and
sentiment == "undermining"
):
return "contradictory gaslight"
# 20. Forced Accountability Flip
if (
(anger + disgust) > 0.4 and
any(p in patterns for p in ["blame shifting", "projection"]) and
sentiment == "undermining"
):
return "forced accountability flip"
# Emotional Instability Fallback
if (
(anger + sadness + disgust) > 0.5 and
sentiment == "undermining"
):
return "emotional instability"
return "neutral"
@spaces.GPU
def predict_darvo_score(text):
"""Predict DARVO score for given text"""
try:
inputs = darvo_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
logits = darvo_model(**inputs).logits
return round(sigmoid(logits.cpu()).item(), 4)
except Exception as e:
logger.error(f"Error in DARVO prediction: {e}")
return 0.0
def detect_weapon_language(text):
"""Detect weapon-related language in text"""
weapon_keywords = ["knife", "gun", "bomb", "weapon", "kill", "stab"]
t = text.lower()
return any(w in t for w in weapon_keywords)
# get_risk_stage() removed — stage is determined by weighted voting
# inside analyze_single_message() at the point of scoring.
def detect_threat_pattern(text, patterns):
"""Detect if a message contains threat patterns"""
# Threat indicators in text
threat_words = [
"regret", "sorry", "pay", "hurt", "suffer", "destroy", "ruin",
"expose", "tell everyone", "never see", "take away", "lose",
"control", "make sure", "won't let", "force", "warn", "never",
"punish", "teach you", "learn", "show you", "remember",
"if you", "don't forget", "i control", "i'll make sure", # Added these specific phrases
"bank account", "phone", "money", "access" # Added financial control indicators
]
# Check for conditional threats (if/then structures)
text_lower = text.lower()
conditional_threat = (
"if" in text_lower and
any(word in text_lower for word in ["regret", "make sure", "control"])
)
has_threat_words = any(word in text_lower for word in threat_words)
# Check for threat patterns
threat_patterns = {"control", "gaslighting", "blame shifting", "insults"}
has_threat_patterns = any(p in threat_patterns for p in patterns)
return has_threat_words or has_threat_patterns or conditional_threat
def detect_compound_threat(text, patterns):
"""Detect compound threats in a single message"""
try:
# Rule A: Single Message Multiple Patterns
high_risk_patterns = {"control", "gaslighting", "blame shifting", "insults"}
high_risk_count = sum(1 for p in patterns if p in high_risk_patterns)
has_threat = detect_threat_pattern(text, patterns)
# Special case for control + threats
has_control = "control" in patterns
has_conditional_threat = "if" in text.lower() and any(word in text.lower()
for word in ["regret", "make sure", "control"])
# Single message compound threat
if (has_threat and high_risk_count >= 2) or (has_control and has_conditional_threat):
return True, "single_message"
return False, None
except Exception as e:
logger.error(f"Error in compound threat detection: {e}")
return False, None
def analyze_message_batch_threats(messages, results):
"""Analyze multiple messages for compound threats"""
threat_messages = []
support_messages = []
for i, (msg, (result, _)) in enumerate(zip(messages, results)):
if not msg.strip(): # Skip empty messages
continue
patterns = result[1] # Get detected patterns
# Check for threat in this message
if detect_threat_pattern(msg, patterns):
threat_messages.append(i)
# Check for supporting patterns
if any(p in {"control", "gaslighting", "blame shifting"} for p in patterns):
support_messages.append(i)
# Rule B: Multi-Message Accumulation
if len(threat_messages) >= 2:
return True, "multiple_threats"
elif len(threat_messages) == 1 and len(support_messages) >= 2:
return True, "threat_with_support"
return False, None
@spaces.GPU
def compute_abuse_score(matched_scores, sentiment):
"""Compute abuse score from matched patterns and sentiment"""
try:
if not matched_scores:
logger.debug("No matched scores, returning 0")
return 0.0
# Calculate weighted score
total_weight = sum(weight for _, _, weight in matched_scores)
if total_weight == 0:
logger.debug("Total weight is 0, returning 0")
return 0.0
# Get highest pattern scores
pattern_scores = [(label, score) for label, score, _ in matched_scores]
sorted_scores = sorted(pattern_scores, key=lambda x: x[1], reverse=True)
logger.debug(f"Sorted pattern scores: {sorted_scores}")
# Base score calculation
weighted_sum = sum(score * weight for _, score, weight in matched_scores)
base_score = (weighted_sum / total_weight) * 100
logger.debug(f"Initial base score: {base_score:.1f}")
# Cap maximum score based on pattern severity
max_score = 85.0 # Set maximum possible score
if any(label in {'control', 'gaslighting'} for label, _, _ in matched_scores):
max_score = 90.0
logger.debug(f"Increased max score to {max_score} due to high severity patterns")
# Apply diminishing returns for multiple patterns
if len(matched_scores) > 1:
multiplier = 1 + (0.1 * (len(matched_scores) - 1))
base_score *= multiplier
logger.debug(f"Applied multiplier {multiplier:.2f} for {len(matched_scores)} patterns")
# Apply sentiment modifier
if sentiment == "supportive":
base_score *= 0.85
logger.debug("Applied 15% reduction for supportive sentiment")
final_score = min(round(base_score, 1), max_score)
logger.debug(f"Final abuse score: {final_score}")
return final_score
except Exception as e:
logger.error(f"Error computing abuse score: {e}")
return 0.0
def detect_explicit_abuse(text):
"""Improved explicit abuse detection with word boundary checking"""
import re
explicit_abuse_words = ['fuck', 'bitch', 'shit', 'dick',
'pathetic', 'worthless', 'useless', 'stupid', 'idiot']
abusive_ass_patterns = [
r'\bass\b(?!\s*glass)', # 'ass' not followed by 'glass'
r'\bdumb\s*ass\b',
r'\bkiss\s*my\s*ass\b',
r'\bget\s*your\s*ass\b'
]
text_lower = text.lower()
# Check basic explicit words
for word in explicit_abuse_words:
if re.search(r'\b' + word + r'\b', text_lower):
return True
# Check specific abusive 'ass' patterns
for pattern in abusive_ass_patterns:
if re.search(pattern, text_lower):
return True
return False
@spaces.GPU
def analyze_single_message(text, thresholds):
"""Analyze a single message for abuse patterns with boundary assessment"""
logger.debug("\n=== DEBUG START ===")
logger.debug(f"Input text: {text}")
try:
if not text.strip():
logger.debug("Empty text, returning zeros")
return 0.0, [], [], {"label": "none"}, 1, 0.0, None, {'assessment': 'neutral', 'confidence': 0.5}
# --- Language detection & translation ---
detected_lang = "en"
was_translated = False
try:
detected_lang = detect_language(text)
if detected_lang != "en":
text = GoogleTranslator(source='auto', target='en').translate(text)
was_translated = True
logger.info(f"Translated from {detected_lang} to English")
except LangDetectException:
pass # Too short to detect — just run it as-is
except Exception as e:
logger.warning(f"Translation failed, proceeding in original language: {e}")
# -----------------------------------------
# BOUNDARY HEALTH CHECK - Add this new section
logger.debug("\n🛡️ BOUNDARY HEALTH ANALYSIS")
logger.debug("-" * 40)
boundary_class, boundary_confidence = predict_boundary_health(text) # Unpack the tuple
boundary_assessment = get_boundary_assessment(text, boundary_class, boundary_confidence)
logger.debug(f"Boundary Class: {boundary_class} ({['Respected', 'Violated', 'Dismissed', 'Manipulative'][boundary_class]})")
logger.debug(f"Boundary Confidence: {boundary_confidence:.3f}")
logger.debug(f"Boundary Assessment: {boundary_assessment['label']}")
# Get sentiment EARLY - BEFORE any early returns
sent_inputs = sentiment_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
sent_inputs = {k: v.to(device) for k, v in sent_inputs.items()}
with torch.no_grad():
sent_logits = sentiment_model(**sent_inputs).logits[0]
sent_probs = torch.softmax(sent_logits, dim=-1).cpu().numpy()
# Add detailed logging
logger.debug("\n🎭 SENTIMENT ANALYSIS DETAILS")
logger.debug(f"Raw logits: {sent_logits}")
logger.debug(f"Probabilities: supportive={sent_probs[0]:.3f}, undermining={sent_probs[1]:.3f}")
# Make sure we're using the correct index mapping
sentiment = SENTIMENT_LABELS[int(np.argmax(sent_probs))]
logger.debug(f"Selected sentiment: {sentiment}")
# UPDATE THE OVERRIDE CONDITION:
# Now we need to check if boundary_class == 0 (Respected) instead of checking a probability
if (boundary_class != 0 and # Not "Respected"
boundary_confidence < 0.7 and
sentiment == "supportive" and
len(text.split()) > 50 and
any(phrase in text.lower() for phrase in [
"i need you to", "i want to understand", "this isn't about",
"about accuracy", "willing to do something different"
])):
logger.debug("🔄 Boundary assessment override: Sophisticated healthy boundary detected")
boundary_assessment = {
'assessment': 'healthy',
'label': 'Healthy Boundary (Sophisticated)',
'confidence': 0.85,
'description': 'Complex but healthy boundary-setting communication',
'recommendations': ['Continue this thoughtful, direct approach']
}
# EARLY SUPPORTIVE MESSAGE CHECK
# Only tech/object words — apology language removed (recovery-phase and guilt-tripping risk)
innocent_indicators = [
'broken', 'not working', 'cracked', 'glass', 'screen', 'phone',
'device', 'battery', 'charger', 'wifi', 'internet', 'computer'
]
# Enhanced early return check - now includes boundary health and DARVO gate
if (any(indicator in text.lower() for indicator in innocent_indicators) and
len(text.split()) < 20 and
not any(threat in text.lower() for threat in ['kill', 'hurt', 'destroy', 'hate']) and
boundary_class == 0):
# If sentiment is strongly supportive AND boundary health is good,
# run a DARVO check before returning — catches calm/apologetic control
if sent_probs[0] > 0.8: # 80% supportive
darvo_gate = predict_darvo_score(text)
logger.debug(f"Early return DARVO gate score: {darvo_gate:.4f}")
if darvo_gate <= 0.3:
logger.debug("Early return: Message appears to be innocent/supportive with healthy boundaries")
return 0.0, [], [], {"label": "supportive"}, 1, 0.0, "neutral", boundary_assessment
else:
logger.debug(f"Early return blocked by DARVO gate ({darvo_gate:.4f}) — proceeding to full analysis")
# Check for explicit abuse (moved AFTER early return check)
explicit_abuse = detect_explicit_abuse(text)
logger.debug(f"Explicit abuse detected: {explicit_abuse}")
# Abuse model inference
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
raw_scores = torch.sigmoid(outputs.logits.squeeze(0)).cpu().numpy()
# Log raw model outputs
logger.debug("\nRaw model scores:")
for label, score in zip(LABELS, raw_scores):
logger.debug(f"{label}: {score:.3f}")
# Get predictions and sort them
predictions = list(zip(LABELS, raw_scores))
sorted_predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
logger.debug("\nTop 3 predictions:")
for label, score in sorted_predictions[:3]:
logger.debug(f"{label}: {score:.3f}")
# Apply thresholds
threshold_labels = []
if explicit_abuse:
threshold_labels.append("insults")
logger.debug("\nForced inclusion of 'insults' due to explicit abuse")
for label, score in sorted_predictions:
base_threshold = thresholds.get(label, 0.25)
if explicit_abuse:
base_threshold *= 0.5
if score > base_threshold:
if label not in threshold_labels:
threshold_labels.append(label)
logger.debug(f"\nLabels that passed thresholds: {threshold_labels}")
# Calculate matched scores
matched_scores = []
for label in threshold_labels:
score = raw_scores[LABELS.index(label)]
weight = PATTERN_WEIGHTS.get(label, 1.0)
if explicit_abuse and label == "insults":
weight *= 1.5
matched_scores.append((label, score, weight))
enhanced_patterns = detect_enhanced_threats(text, threshold_labels)
structural_patterns = detect_structural_threats(text)
all_extra_patterns = list(set(enhanced_patterns + structural_patterns))
for pattern in all_extra_patterns:
if pattern not in threshold_labels:
threshold_labels.append(pattern)
weight = PATTERN_WEIGHTS.get(pattern, 1.0)
matched_scores.append((pattern, 0.85, weight))
logger.debug(f"Added structural/enhanced pattern: {pattern}")
# Calculate abuse score
abuse_score = compute_abuse_score(matched_scores, sentiment)
if explicit_abuse:
abuse_score = max(abuse_score, 70.0)
# Apply boundary health modifier to abuse score
# NOTE: Updated to use boundary_class instead of healthy_prob
if boundary_class == 0 and boundary_confidence > 0.8 and not explicit_abuse:
# Very healthy boundaries (Respected) - cap abuse score much lower
abuse_score = min(abuse_score, 20.0)
logger.debug(f"Capped abuse score to {abuse_score} due to very healthy boundaries")
elif boundary_class == 0 and boundary_confidence > 0.6 and sentiment == "supportive":
# Moderately healthy boundaries with supportive sentiment
abuse_score = min(abuse_score, 35.0)
logger.debug(f"Capped abuse score to {abuse_score} due to healthy boundaries")
# Apply sentiment-based score capping BEFORE compound threat check
if sentiment == "supportive" and not explicit_abuse:
# For supportive messages, cap the abuse score much lower
abuse_score = min(abuse_score, 30.0)
logger.debug(f"Capped abuse score to {abuse_score} due to supportive sentiment")
# Check for compound threats
compound_threat_flag, threat_type = detect_compound_threat(text, threshold_labels)
# Apply compound threat override only for non-supportive messages
if compound_threat_flag and sentiment != "supportive":
logger.debug(f"⚠️ Compound threat detected in message: {threat_type}")
abuse_score = max(abuse_score, 85.0)
# Get DARVO score
darvo_score = predict_darvo_score(text)
# Get tone using emotion-based approach
tone_tag = get_emotional_tone_tag(text, sentiment, threshold_labels, abuse_score)
# Log tone usage
log_emotional_tone_usage(tone_tag, threshold_labels)
# Check for the specific combination (final safety check)
# NOTE: Updated to use boundary_class instead of healthy_prob
highest_pattern = max(matched_scores, key=lambda x: x[1])[0] if matched_scores else None
if sentiment == "supportive" and tone_tag == "neutral" and highest_pattern == "obscure language" and boundary_class == 0:
logger.debug("Message classified as likely non-abusive (supportive, neutral, healthy boundaries). Returning low risk.")
return 0.0, [], [], {"label": "supportive"}, 1, 0.0, "neutral", boundary_assessment
# Weighted stage voting — replaces single-condition override
stage_votes = {1: 0, 2: 0, 3: 0, 4: 0}
if explicit_abuse: stage_votes[2] += 3
if abuse_score > 70: stage_votes[2] += 2
if "insults" in threshold_labels: stage_votes[2] += 2
if "veiled threats" in threshold_labels: stage_votes[2] += 2
if "control" in threshold_labels: stage_votes[1] += 2
if "guilt tripping" in threshold_labels: stage_votes[1] += 1
if "gaslighting" in threshold_labels: stage_votes[1] += 1
if "recovery phase" in threshold_labels: stage_votes[3] += 2
if "future faking" in threshold_labels: stage_votes[3] += 1
if any(p in threshold_labels for p in ["false concern", "projection", "dismissiveness", "future faking"]) and "insults" not in threshold_labels and not explicit_abuse:
stage_votes[4] += 2
# Tiebreaker: when Stage 1 and 4 tie, favor Stage 4 if false concern
# is present and no explicit aggression — calm control reads as honeymoon
top_score = max(stage_votes.values())
tied_stages = [s for s, v in stage_votes.items() if v == top_score]
if len(tied_stages) > 1:
if 2 in tied_stages and (explicit_abuse or "insults" in threshold_labels or "veiled threats" in threshold_labels):
stage = 2 # Escalation wins only if there's actual aggression, not just high score
elif 4 in tied_stages and "false concern" in threshold_labels and not explicit_abuse:
stage = 4 # Honeymoon wins when calm control + false concern, no aggression
elif 2 in tied_stages:
stage = 1 # High score alone without aggression → tension-building
else:
stage = min(tied_stages)
else:
stage = tied_stages[0]
logger.debug(f"Stage votes: {stage_votes} → Stage {stage}")
logger.debug("=== DEBUG END ===\n")
# Return with boundary assessment as the 8th element
return abuse_score, threshold_labels, matched_scores, {"label": sentiment}, stage, darvo_score, tone_tag, boundary_assessment
except Exception as e:
logger.error(f"Error in analyze_single_message: {e}")
logger.error(f"Traceback: {traceback.format_exc()}")
return 0.0, [], [], {"label": "error"}, 1, 0.0, None, {'assessment': 'error', 'confidence': 0.0}
def generate_abuse_score_chart(dates, scores, patterns):
"""Generate a timeline chart of abuse scores"""
try:
plt.figure(figsize=(10, 6))
plt.clf()
# Create new figure
fig, ax = plt.subplots(figsize=(10, 6))
# Plot points and lines
x = range(len(scores))
plt.plot(x, scores, 'bo-', linewidth=2, markersize=8)
# Add labels for each point with highest scoring pattern
for i, (score, pattern) in enumerate(zip(scores, patterns)):
# Get the pattern and its score
plt.annotate(
f'{pattern}\n{score:.0f}%',
(i, score),
textcoords="offset points",
xytext=(0, 10),
ha='center',
bbox=dict(
boxstyle='round,pad=0.5',
fc='white',
ec='gray',
alpha=0.8
)
)
# Customize the plot
plt.ylim(-5, 105)
plt.grid(True, linestyle='--', alpha=0.7)
plt.title('Abuse Pattern Timeline', pad=20, fontsize=12)
plt.ylabel('Abuse Score %')
# X-axis labels
plt.xticks(x, dates, rotation=45)
# Risk level bands with better colors
plt.axhspan(0, 50, color='#90EE90', alpha=0.2) # light green - Low Risk
plt.axhspan(50, 70, color='#FFD700', alpha=0.2) # gold - Moderate Risk
plt.axhspan(70, 85, color='#FFA500', alpha=0.2) # orange - High Risk
plt.axhspan(85, 100, color='#FF6B6B', alpha=0.2) # light red - Critical Risk
# Add risk level labels
plt.text(-0.2, 25, 'Low Risk', rotation=90, va='center')
plt.text(-0.2, 60, 'Moderate Risk', rotation=90, va='center')
plt.text(-0.2, 77.5, 'High Risk', rotation=90, va='center')
plt.text(-0.2, 92.5, 'Critical Risk', rotation=90, va='center')
# Adjust layout
plt.tight_layout()
# Convert plot to image
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight')
buf.seek(0)
plt.close('all') # Close all figures to prevent memory leaks
return Image.open(buf)
except Exception as e:
logger.error(f"Error generating abuse score chart: {e}")
return None
def analyze_composite(msg1, msg2, msg3, *answers_and_none, lang_code="en"):
"""Analyze multiple messages and checklist responses"""
logger.debug("\n🔄 STARTING NEW ANALYSIS")
logger.debug("=" * 50)
high = {'control', 'veiled threats', 'stalking language'}
moderate = {'gaslighting', 'dismissiveness', 'obscure language', 'insults',
'contradictory statements', 'guilt tripping', 'false concern', 'false equivalence'}
low = {'blame shifting', 'projection', 'recovery phase', 'future faking'}
try:
# Process checklist responses
logger.debug("\n📋 CHECKLIST PROCESSING")
logger.debug("=" * 50)
none_selected_checked = answers_and_none[-1]
responses_checked = any(answers_and_none[:-1])
none_selected = not responses_checked and none_selected_checked
logger.debug("Checklist Status:")
logger.debug(f" • None Selected Box: {'✓' if none_selected_checked else '✗'}")
logger.debug(f" • Has Responses: {'✓' if responses_checked else '✗'}")
logger.debug(f" • Final Status: {'None Selected' if none_selected else 'Has Selections'}")
if none_selected:
escalation_score = 0
escalation_note = "Checklist completed: no danger items reported."
escalation_completed = True
logger.debug("\n✓ Checklist: No items selected")
elif responses_checked:
escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, answers_and_none[:-1]) if a)
escalation_note = "Checklist completed."
escalation_completed = True
logger.debug(f"\n📊 Checklist Score: {escalation_score}")
# Log checked items
logger.debug("\n⚠️ Selected Risk Factors:")
for (q, w), a in zip(ESCALATION_QUESTIONS, answers_and_none[:-1]):
if a:
logger.debug(f" • [{w} points] {q}")
else:
escalation_score = None
escalation_note = "Checklist not completed."
escalation_completed = False
logger.debug("\n❗ Checklist: Not completed")
# Process messages
logger.debug("\n📝 MESSAGE PROCESSING")
logger.debug("=" * 50)
messages = [msg1, msg2, msg3]
active = [(m, f"Message {i+1}") for i, m in enumerate(messages) if m.strip()]
logger.debug(f"Active Messages: {len(active)} of 3")
if not active:
logger.debug("❌ Error: No messages provided")
return "Please enter at least one message.", None
# Detect threats
logger.debug("\n🚨 THREAT DETECTION")
logger.debug("=" * 50)
def normalize(text):
import unicodedata
text = text.lower().strip()
text = unicodedata.normalize("NFKD", text)
text = text.replace("'", "'")
return re.sub(r"[^a-z0-9 ]", "", text)
def detect_threat_motifs(message, motif_list):
norm_msg = normalize(message)
return [motif for motif in motif_list if normalize(motif) in norm_msg]
# Analyze threats and patterns
immediate_threats = [detect_threat_motifs(m, THREAT_MOTIFS) for m, _ in active]
flat_threats = [t for sublist in immediate_threats for t in sublist]
threat_risk = "Yes" if flat_threats else "No"
# Analyze each message
logger.debug("\n🔍 INDIVIDUAL MESSAGE ANALYSIS")
logger.debug("=" * 50)
results = []
for m, d in active:
logger.debug(f"\n📝 ANALYZING {d}")
logger.debug("-" * 40)
result = analyze_single_message(m, THRESHOLDS.copy())
# Check for non-abusive classification and skip further analysis
if result[0] == 0.0 and result[1] == [] and result[3] == {"label": "supportive"} and result[4] == 1 and result[5] == 0.0 and result[6] == "neutral":
logger.debug(f"✓ {d} classified as non-abusive, skipping further analysis.")
continue # This MUST be inside the for loop
results.append((result, d))
# UNPACK RESULT FIRST - INSIDE the for loop
abuse_score, patterns, matched_scores, sentiment, stage, darvo_score, tone, boundary_assessment = result
# NOW you can log these variables safely
logger.debug("\n📊 CORE METRICS")
logger.debug(f" • Abuse Score: {abuse_score:.1f}%")
logger.debug(f" • DARVO Score: {darvo_score:.3f}")
logger.debug(f" • Risk Stage: {stage}")
logger.debug(f" • Sentiment: {sentiment['label']}")
logger.debug(f" • Tone: {tone}")
# Log detected patterns with scores
if patterns:
logger.debug("\n🎯 DETECTED PATTERNS")
for label, score, weight in matched_scores:
severity = "❗HIGH" if label in high else "⚠️ MODERATE" if label in moderate else "📝 LOW"
logger.debug(f" • {severity} | {label}: {score:.3f} (weight: {weight})")
else:
logger.debug("\n✓ No abuse patterns detected")
# Check if we have any results to process
if not results:
logger.debug("No valid results to analyze - all messages were classified as non-abusive")
return "All messages appear to be non-abusive based on the analysis.", None
# Extract scores and metadata
abuse_scores = [r[0][0] for r in results]
stages = [r[0][4] for r in results]
darvo_scores = [r[0][5] for r in results]
tone_tags = [r[0][6] for r in results]
dates_used = [r[1] for r in results]
# Pattern Analysis Summary
logger.debug("\n📈 PATTERN ANALYSIS SUMMARY")
logger.debug("=" * 50)
predicted_labels = [label for r in results for label in r[0][1]]
if predicted_labels:
logger.debug("Detected Patterns Across All Messages:")
pattern_counts = Counter(predicted_labels)
# Log high severity patterns first
high_patterns = [p for p in pattern_counts if p in high]
if high_patterns:
logger.debug("\n❗ HIGH SEVERITY PATTERNS:")
for p in high_patterns:
logger.debug(f" • {p}{pattern_counts[p]})")
# Then moderate
moderate_patterns = [p for p in pattern_counts if p in moderate]
if moderate_patterns:
logger.debug("\n⚠️ MODERATE SEVERITY PATTERNS:")
for p in moderate_patterns:
logger.debug(f" • {p}{pattern_counts[p]})")
# Then low
low_patterns = [p for p in pattern_counts if p in low]
if low_patterns:
logger.debug("\n📝 LOW SEVERITY PATTERNS:")
for p in low_patterns:
logger.debug(f" • {p}{pattern_counts[p]})")
else:
logger.debug("✓ No patterns detected across messages")
# Pattern Severity Analysis
logger.debug("\n⚖️ SEVERITY ANALYSIS")
logger.debug("=" * 50)
counts = {'high': 0, 'moderate': 0, 'low': 0}
for label in predicted_labels:
if label in high:
counts['high'] += 1
elif label in moderate:
counts['moderate'] += 1
elif label in low:
counts['low'] += 1
logger.debug("Pattern Distribution:")
if counts['high'] > 0:
logger.debug(f" ❗ High Severity: {counts['high']}")
if counts['moderate'] > 0:
logger.debug(f" ⚠️ Moderate Severity: {counts['moderate']}")
if counts['low'] > 0:
logger.debug(f" 📝 Low Severity: {counts['low']}")
total_patterns = sum(counts.values())
if total_patterns > 0:
logger.debug(f"\nSeverity Percentages:")
logger.debug(f" • High: {(counts['high']/total_patterns)*100:.1f}%")
logger.debug(f" • Moderate: {(counts['moderate']/total_patterns)*100:.1f}%")
logger.debug(f" • Low: {(counts['low']/total_patterns)*100:.1f}%")
# Risk Assessment
logger.debug("\n🎯 RISK ASSESSMENT")
logger.debug("=" * 50)
if counts['high'] >= 2 and counts['moderate'] >= 2:
pattern_escalation_risk = "Critical"
logger.debug("❗❗ CRITICAL RISK")
logger.debug(" • Multiple high and moderate patterns detected")
logger.debug(f" • High patterns: {counts['high']}")
logger.debug(f" • Moderate patterns: {counts['moderate']}")
elif (counts['high'] >= 2 and counts['moderate'] >= 1) or \
(counts['moderate'] >= 3) or \
(counts['high'] >= 1 and counts['moderate'] >= 2):
pattern_escalation_risk = "High"
logger.debug("❗ HIGH RISK")
logger.debug(" • Significant pattern combination detected")
logger.debug(f" • High patterns: {counts['high']}")
logger.debug(f" • Moderate patterns: {counts['moderate']}")
elif (counts['moderate'] == 2) or \
(counts['high'] == 1 and counts['moderate'] == 1) or \
(counts['moderate'] == 1 and counts['low'] >= 2) or \
(counts['high'] == 1 and sum(counts.values()) == 1):
pattern_escalation_risk = "Moderate"
logger.debug("⚠️ MODERATE RISK")
logger.debug(" • Concerning pattern combination detected")
logger.debug(f" • Pattern distribution: H:{counts['high']}, M:{counts['moderate']}, L:{counts['low']}")
else:
pattern_escalation_risk = "Low"
logger.debug("📝 LOW RISK")
logger.debug(" • Limited pattern severity detected")
logger.debug(f" • Pattern distribution: H:{counts['high']}, M:{counts['moderate']}, L:{counts['low']}")
# Checklist Risk Assessment
logger.debug("\n📋 CHECKLIST RISK ASSESSMENT")
logger.debug("=" * 50)
checklist_escalation_risk = "Unknown" if escalation_score is None else (
"Critical" if escalation_score >= 20 else
"High" if escalation_score >= 15 else
"Moderate" if escalation_score >= 10 else
"Low"
)
if escalation_score is not None:
logger.debug(f"Score: {escalation_score}/29")
logger.debug(f"Risk Level: {checklist_escalation_risk}")
if escalation_score >= 20:
logger.debug("❗❗ CRITICAL: Score indicates severe risk")
elif escalation_score >= 15:
logger.debug("❗ HIGH: Score indicates high risk")
elif escalation_score >= 10:
logger.debug("⚠️ MODERATE: Score indicates concerning risk")
else:
logger.debug("📝 LOW: Score indicates limited risk")
else:
logger.debug("❓ Risk Level: Unknown (checklist not completed)")
# Escalation Analysis
logger.debug("\n📈 ESCALATION ANALYSIS")
logger.debug("=" * 50)
escalation_bump = 0
for result, msg_id in results:
abuse_score, patterns, matched_scores, sentiment, stage, darvo_score, tone_tag, boundary_assessment = result
logger.debug(f"\n🔍 Message {msg_id} Risk Factors:")
factors = []
if darvo_score > 0.65:
escalation_bump += 3
factors.append(f"▲ +3: High DARVO score ({darvo_score:.3f})")
if tone_tag in ["forced accountability flip", "emotional threat"]:
escalation_bump += 2
factors.append(f"▲ +2: Concerning tone ({tone_tag})")
if abuse_score > 80:
escalation_bump += 2
factors.append(f"▲ +2: High abuse score ({abuse_score:.1f}%)")
if stage == 2:
escalation_bump += 3
factors.append("▲ +3: Escalation stage")
if factors:
for factor in factors:
logger.debug(f" {factor}")
else:
logger.debug(" ✓ No escalation factors")
logger.debug(f"\n📊 Total Escalation Bump: +{escalation_bump}")
# Check for compound threats across messages
compound_threat_flag, threat_type = analyze_message_batch_threats(
[msg1, msg2, msg3], results
)
if compound_threat_flag:
logger.debug(f"⚠️ Compound threat detected across messages: {threat_type}")
pattern_escalation_risk = "Critical" # Override risk level
logger.debug("Risk level elevated to CRITICAL due to compound threats")
# Combined Risk Calculation
logger.debug("\n🎯 FINAL RISK CALCULATION")
logger.debug("=" * 50)
def rank(label):
return {"Low": 0, "Moderate": 1, "High": 2, "Critical": 3, "Unknown": 0}.get(label, 0)
combined_score = rank(pattern_escalation_risk) + rank(checklist_escalation_risk) + escalation_bump
logger.debug("Risk Components:")
logger.debug(f" • Pattern Risk ({pattern_escalation_risk}): +{rank(pattern_escalation_risk)}")
logger.debug(f" • Checklist Risk ({checklist_escalation_risk}): +{rank(checklist_escalation_risk)}")
logger.debug(f" • Escalation Bump: +{escalation_bump}")
logger.debug(f" = Combined Score: {combined_score}")
escalation_risk = (
"Critical" if combined_score >= 6 else
"High" if combined_score >= 4 else
"Moderate" if combined_score >= 2 else
"Low"
)
logger.debug(f"\n⚠️ Final Escalation Risk: {escalation_risk}")
# Generate Output Text
logger.debug("\n📝 GENERATING OUTPUT")
logger.debug("=" * 50)
if escalation_score is None:
escalation_text = (
"🚫 **Escalation Potential: Unknown** (Checklist not completed)\n"
"⚠️ This section was not completed. Escalation potential is estimated using message data only.\n"
)
hybrid_score = 0
logger.debug("Generated output for incomplete checklist")
elif escalation_score == 0:
escalation_text = (
"✅ **Escalation Checklist Completed:** No danger items reported.\n"
"🧭 **Escalation potential estimated from detected message patterns only.**\n"
f"• Pattern Risk: {pattern_escalation_risk}\n"
f"• Checklist Risk: None reported\n"
f"• Escalation Bump: +{escalation_bump} (from DARVO, tone, intensity, etc.)"
)
hybrid_score = escalation_bump
logger.debug("Generated output for no-risk checklist")
else:
hybrid_score = escalation_score + escalation_bump
escalation_text = (
f"📈 **Escalation Potential: {escalation_risk} ({hybrid_score}/29)**\n"
"📋 This score combines your safety checklist answers *and* detected high-risk behavior.\n"
f"• Pattern Risk: {pattern_escalation_risk}\n"
f"• Checklist Risk: {checklist_escalation_risk}\n"
f"• Escalation Bump: +{escalation_bump} (from DARVO, tone, intensity, etc.)"
)
logger.debug(f"Generated output with hybrid score: {hybrid_score}/29")
# Final Metrics
logger.debug("\n📊 FINAL METRICS")
logger.debug("=" * 50)
composite_abuse = int(round(sum(abuse_scores) / len(abuse_scores)))
logger.debug(f"Composite Abuse Score: {composite_abuse}%")
most_common_stage = max(set(stages), key=stages.count)
logger.debug(f"Most Common Stage: {most_common_stage}")
avg_darvo = round(sum(darvo_scores) / len(darvo_scores), 3)
logger.debug(f"Average DARVO Score: {avg_darvo}")
final_risk_level = calculate_enhanced_risk_level(
composite_abuse,
predicted_labels,
escalation_risk,
avg_darvo
)
# Override escalation_risk with the enhanced version
escalation_risk = final_risk_level
# Add this to your analyze_composite function, right after the Final Report section
# Generate Final Report (existing code)
logger.debug("\n📄 GENERATING FINAL REPORT")
logger.debug("=" * 50)
out = f"Abuse Intensity: {composite_abuse}%\n"
# ADD HEALTHY BOUNDARY DETECTION - NEW SECTION
logger.debug("\n🛡️ CHECKING FOR HEALTHY BOUNDARIES")
logger.debug("=" * 50)
# Check if any messages were identified as having healthy boundaries
healthy_boundaries_detected = []
for result, msg_id in results:
abuse_score, patterns, matched_scores, sentiment, stage, darvo_score, tone_tag, boundary_assessment = result
# Check if this message has healthy boundaries
if boundary_assessment.get('assessment') == 'healthy':
healthy_boundaries_detected.append({
'message_id': msg_id,
'label': boundary_assessment.get('label', 'Healthy Boundary'),
'confidence': boundary_assessment.get('confidence', 1.0),
'description': boundary_assessment.get('description', 'Healthy boundary communication detected')
})
logger.debug(f"✅ {msg_id}: {boundary_assessment.get('label')}")
# Add healthy boundary section to output if any detected
if healthy_boundaries_detected:
logger.debug(f"Found {len(healthy_boundaries_detected)} healthy boundary messages")
out += "\n🛡️ **HEALTHY BOUNDARIES DETECTED**\n"
out += "=" * 50 + "\n"
if len(healthy_boundaries_detected) == 1:
boundary = healthy_boundaries_detected[0]
out += f"✅ **{boundary['message_id']}**: This is a healthily phrased boundary\n"
out += f" • **Type**: {boundary['label']}\n"
out += f" • **Analysis**: {boundary['description']}\n"
out += " • **Recommendation**: Continue this respectful, direct communication approach\n"
else:
out += f"✅ **Multiple healthy boundaries detected** ({len(healthy_boundaries_detected)} messages)\n"
for boundary in healthy_boundaries_detected:
out += f" • **{boundary['message_id']}**: {boundary['label']}\n"
out += " • **Overall**: These messages demonstrate healthy boundary-setting skills\n"
out += "\n💡 **About Healthy Boundaries**: Even when addressing difficult topics, "
out += "these messages use respectful language, focus on specific behaviors rather than "
out += "character attacks, and communicate needs clearly without manipulation.\n"
logger.debug("Added healthy boundary section to output")
else:
logger.debug("No healthy boundaries detected in messages")
# Add detected patterns to output
if predicted_labels:
out += "🔍 Detected Patterns:\n"
pattern_counts = Counter(predicted_labels) # Re-define here for safety
high_patterns = [p for p in pattern_counts if p in high]
moderate_patterns = [p for p in pattern_counts if p in moderate]
low_patterns = [p for p in pattern_counts if p in low]
if high_patterns:
patterns_str = ", ".join(f"{p} ({pattern_counts[p]}x)" for p in high_patterns)
out += f"❗ High Severity: {patterns_str}\n"
if moderate_patterns:
patterns_str = ", ".join(f"{p} ({pattern_counts[p]}x)" for p in moderate_patterns)
out += f"⚠️ Moderate Severity: {patterns_str}\n"
if low_patterns:
patterns_str = ", ".join(f"{p} ({pattern_counts[p]}x)" for p in low_patterns)
out += f"📝 Low Severity: {patterns_str}\n"
out += "\n"
out += "📊 This reflects the strength and severity of detected abuse patterns in the message(s).\n\n"
# Risk Level Assessment
risk_level = final_risk_level
logger.debug(f"Final Risk Level: {risk_level}")
# Add Risk Description
risk_descriptions = {
"Critical": (
"🚨 **Risk Level: Critical**\n"
"Multiple severe abuse patterns detected. This situation shows signs of "
"dangerous escalation and immediate intervention may be needed."
),
"High": (
"⚠️ **Risk Level: High**\n"
"Strong abuse patterns detected. This situation shows concerning "
"signs of manipulation and control."
),
"Moderate": (
"⚡ **Risk Level: Moderate**\n"
"Concerning patterns detected. While not severe, these behaviors "
"indicate unhealthy relationship dynamics."
),
"Low": (
"📝 **Risk Level: Low**\n"
"Minor concerning patterns detected. While present, the detected "
"behaviors are subtle or infrequent."
)
}
out += risk_descriptions[risk_level]
out += f"\n\n{RISK_STAGE_LABELS[most_common_stage]}"
logger.debug("Added risk description and stage information")
# Add DARVO Analysis
if avg_darvo > 0.25:
level = "moderate" if avg_darvo < 0.65 else "high"
out += f"\n\n🎭 **DARVO Score: {avg_darvo}** → This indicates a **{level} likelihood** of narrative reversal (DARVO), where the speaker may be denying, attacking, or reversing blame."
logger.debug(f"Added DARVO analysis ({level} level)")
# Add Emotional Tones
logger.debug("\n🎭 Adding Emotional Tones")
out += "\n\n🎭 **Emotional Tones Detected:**\n"
for i, tone in enumerate(tone_tags):
out += f"• Message {i+1}: *{tone or 'none'}*\n"
logger.debug(f"Message {i+1} tone: {tone}")
# Add Threats Section
logger.debug("\n⚠️ Adding Threat Analysis")
if flat_threats:
out += "\n\n🚨 **Immediate Danger Threats Detected:**\n"
for t in set(flat_threats):
out += f"• \"{t}\"\n"
out += "\n⚠️ These phrases may indicate an imminent risk to physical safety."
logger.debug(f"Added {len(set(flat_threats))} unique threat warnings")
else:
out += "\n\n🧩 **Immediate Danger Threats:** None explicitly detected.\n"
out += "This does *not* rule out risk, but no direct threat phrases were matched."
logger.debug("No threats to add")
# Generate Timeline
logger.debug("\n📈 Generating Timeline")
pattern_labels = []
for result, _ in results:
matched_scores = result[2] # Get the matched_scores from the result tuple
if matched_scores:
# Sort matched_scores by score and get the highest scoring pattern
highest_pattern = max(matched_scores, key=lambda x: x[1])
pattern_labels.append(highest_pattern[0]) # Add the pattern name
else:
pattern_labels.append("none")
logger.debug("Pattern labels for timeline:")
for i, (pattern, score) in enumerate(zip(pattern_labels, abuse_scores)):
logger.debug(f"Message {i+1}: {pattern} ({score:.1f}%)")
timeline_image = generate_abuse_score_chart(dates_used, abuse_scores, pattern_labels)
logger.debug("Timeline generated successfully")
# Add Escalation Text
out += "\n\n" + escalation_text
logger.debug("Added escalation text to output")
logger.debug("\n✅ ANALYSIS COMPLETE")
logger.debug("=" * 50)
# SAFETY PLANNING CHECK
# Check if safety planning should be offered
show_safety = should_show_safety_planning(
composite_abuse,
escalation_risk,
predicted_labels
)
safety_plan = ""
if show_safety:
# Generate safety plan
safety_plan = generate_simple_safety_plan(
composite_abuse,
escalation_risk,
predicted_labels,
lang_code=lang_code
)
# Add notice to main results
out += "\n\n" + "🛡️ " + "="*48
out += "\n**SAFETY PLANNING AVAILABLE**"
out += "\n" + "="*50
out += "\n\nBased on your analysis results, we've generated a safety plan."
out += "\nCheck the 'Safety Plan' output below for personalized guidance."
# Build structured data dict — travels alongside text output
# so format_results_for_new_ui doesn't need to regex-parse the string
pattern_counts = Counter(predicted_labels)
structured_patterns = []
for label, count in pattern_counts.items():
if label == 'nonabusive':
continue
if label in high:
severity = 'high'
elif label in moderate:
severity = 'moderate'
else:
severity = 'low'
structured_patterns.append({
'name': label.replace('_', ' ').title(),
'severity': severity,
'description': get_pattern_description(label)
})
# Sort by severity
severity_order = {'high': 0, 'moderate': 1, 'low': 2}
structured_patterns.sort(key=lambda x: severity_order.get(x['severity'], 3))
# Build boundary health from per-message assessments
boundary_assessments = [r[0][7] for r in results]
overall_boundary = 'healthy' if all(
b.get('assessment') == 'healthy' for b in boundary_assessments
) else 'unhealthy' if all(
b.get('assessment') != 'healthy' for b in boundary_assessments
) else 'mixed'
structured_data = {
'riskLevel': final_risk_level.lower(),
'riskScore': composite_abuse,
'primaryConcerns': structured_patterns[:3],
'allPatterns': structured_patterns,
'riskStage': {1: 'tension-building', 2: 'escalation', 3: 'reconciliation', 4: 'honeymoon'}.get(most_common_stage, 'unknown'),
'emotionalTones': [t or 'neutral' for t in tone_tags],
'darvoScore': avg_darvo,
'boundaryHealth': {
'overall_health': overall_boundary,
'message_assessments': boundary_assessments,
'recommendations': []
},
'hasSafetyPlan': bool(safety_plan),
'safetyPlan': safety_plan,
'rawAnalysis': out
}
return out, timeline_image, safety_plan, structured_data
except Exception as e:
logger.error("\n❌ ERROR IN ANALYSIS")
logger.error("=" * 50)
logger.error(f"Error type: {type(e).__name__}")
logger.error(f"Error message: {str(e)}")
logger.error(f"Traceback:\n{traceback.format_exc()}")
return "An error occurred during analysis.", None, "", {}
except Exception as e:
logger.error("\n❌ ERROR IN ANALYSIS")
logger.error("=" * 50)
logger.error(f"Error type: {type(e).__name__}")
logger.error(f"Error message: {str(e)}")
logger.error(f"Traceback:\n{traceback.format_exc()}")
return "An error occurred during analysis.", None, "", {}
def format_results_for_new_ui(structured_data, timeline_image, safety_plan):
"""
Receives structured dict directly from analyze_composite.
No regex parsing needed.
"""
try:
recommendations = generate_personalized_recommendations(
structured_data.get('riskScore', 0),
structured_data.get('allPatterns', []),
safety_plan
)
structured_data['personalizedRecommendations'] = recommendations
return structured_data
except Exception as e:
logger.error(f"Error formatting results: {e}")
return {
'riskLevel': 'low',
'riskScore': 0,
'primaryConcerns': [],
'allPatterns': [],
'riskStage': 'unknown',
'emotionalTones': [],
'darvoScore': 0.0,
'boundaryHealth': {'overall_health': 'unknown', 'message_assessments': [], 'recommendations': []},
'personalizedRecommendations': ['Consider speaking with a counselor about your relationship concerns'],
'hasSafetyPlan': False,
'safetyPlan': '',
'rawAnalysis': ''
}
def get_pattern_description(pattern_name):
"""Get human-readable descriptions for patterns"""
descriptions = {
'control': 'Attempts to manage your behavior, decisions, or daily activities',
'gaslighting': 'Making you question your memory, perception, or reality',
'dismissiveness': 'Minimizing or invalidating your feelings and experiences',
'guilt tripping': 'Making you feel guilty to influence your behavior',
'blame shifting': 'Placing responsibility for their actions onto you',
'projection': 'Accusing you of behaviors they themselves exhibit',
'insults': 'Name-calling or personal attacks intended to hurt',
'contradictory statements': 'Saying things that conflict with previous statements',
'obscure language': 'Using vague or confusing language to avoid accountability',
'veiled threats': 'Indirect threats or intimidating language',
'stalking language': 'Monitoring, tracking, or obsessive behaviors',
'false concern': 'Expressing fake worry to manipulate or control',
'false equivalence': 'Comparing incomparable situations to justify behavior',
'future faking': 'Making promises about future behavior that are unlikely to be kept'
}
return descriptions.get(pattern_name.lower(), 'Concerning communication pattern detected')
def generate_personalized_recommendations(abuse_score, patterns, safety_plan):
"""Generate recommendations based on specific findings"""
recommendations = []
if abuse_score >= 70:
recommendations.extend([
'Document these conversations with dates and times',
'Reach out to a trusted friend or family member about your concerns',
'Consider contacting the National Domestic Violence Hotline for guidance'
])
elif abuse_score >= 40:
recommendations.extend([
'Keep a private journal of concerning interactions',
'Talk to someone you trust about these communication patterns',
'Consider counseling to explore healthy relationship dynamics'
])
else:
recommendations.extend([
'Continue monitoring communication patterns that concern you',
'Consider discussing communication styles with your partner when you feel safe to do so'
])
pattern_names = [p['name'].lower() for p in patterns]
if 'control' in pattern_names:
recommendations.append('Maintain your independence and decision-making autonomy')
if 'gaslighting' in pattern_names:
recommendations.append('Trust your memory and perceptions - consider keeping notes')
if any(p in pattern_names for p in ['stalking language', 'veiled threats']):
recommendations.append('Vary your routines and inform trusted people of your whereabouts')
if safety_plan:
recommendations.append('Review your personalized safety plan regularly')
return recommendations[:4]
def extract_risk_stage(analysis_output):
"""Fallback stage extractor — only used if structured data is unavailable"""
if 'Risk Stage: Calm / Honeymoon' in analysis_output:
return 'honeymoon'
elif 'Risk Stage: Reconciliation' in analysis_output:
return 'reconciliation'
elif 'Risk Stage: Escalation' in analysis_output:
return 'escalation'
elif 'Risk Stage: Tension-Building' in analysis_output:
return 'tension-building'
else:
return 'unknown'
def create_boundary_health_display(boundary_assessment):
"""Create HTML display for boundary health assessment"""
color_map = {
'healthy': '#10b981',
'mostly_healthy': '#3b82f6',
'concerning': '#f59e0b',
'unhealthy': '#ef4444',
'neutral': '#6b7280'
}
bg_map = {
'healthy': '#f0fdf4',
'mostly_healthy': '#eff6ff',
'concerning': '#fffbeb',
'unhealthy': '#fef2f2',
'neutral': '#f9fafb'
}
assessment = boundary_assessment.get('assessment', 'neutral')
color = color_map.get(assessment, '#6b7280')
bg_color = bg_map.get(assessment, '#f9fafb')
html = '<div style="background: ' + bg_color + '; border-left: 4px solid ' + color + '; border-radius: 8px; padding: 16px; margin: 16px 0;">'
html += '<h4 style="margin: 0 0 8px 0; color: #1f2937;">&#128737; Boundary Health: ' + boundary_assessment.get('label', 'Unknown') + '</h4>'
html += '<p style="margin: 0 0 8px 0; color: #6b7280;">' + boundary_assessment.get('description', 'No assessment available') + '</p>'
html += '<p style="margin: 0; color: #6b7280; font-size: 14px;"><strong>Confidence:</strong> ' + str(round(boundary_assessment.get('confidence', 0) * 100, 1)) + '%</p>'
html += '<div style="margin-top: 12px;"><h5 style="margin: 0 0 4px 0; color: #1f2937;">Recommendations:</h5>'
html += '<ul style="margin: 0; padding-left: 20px; color: #6b7280;">'
for rec in boundary_assessment.get('recommendations', []):
html += '<li style="margin: 2px 0; color: #6b7280;">' + rec + '</li>'
html += '</ul></div></div>'
return html
def analyze_composite_with_ui_format(msg1, msg2, msg3, *answers_and_none, lang_code="en"):
"""
Runs analysis and returns structured data for the UI.
structured_data travels directly from analyze_composite — no text parsing.
"""
analysis_output, timeline_image, safety_plan, structured_data = analyze_composite(
msg1, msg2, msg3, *answers_and_none, lang_code=lang_code
)
if not structured_data:
structured_data = {
'riskLevel': 'low',
'riskScore': 0,
'primaryConcerns': [],
'allPatterns': [],
'riskStage': 'unknown',
'emotionalTones': [],
'darvoScore': 0.0,
'boundaryHealth': {'overall_health': 'unknown', 'message_assessments': [], 'recommendations': []},
'personalizedRecommendations': [],
'hasSafetyPlan': False,
'safetyPlan': '',
'rawAnalysis': analysis_output
}
structured_results = format_results_for_new_ui(structured_data, timeline_image, safety_plan)
return json.dumps(structured_results), timeline_image, safety_plan
def create_mobile_friendly_interface():
"""Create a responsive interface that works well on both mobile and desktop with full functionality"""
css = """
/* Base responsive layout */
.gradio-container {
max-width: 100% !important;
padding: 12px !important;
}
/* Desktop: side-by-side columns */
@media (min-width: 1024px) {
.desktop-row {
display: flex !important;
gap: 20px !important;
}
.desktop-col-messages {
flex: 2 !important;
min-width: 400px !important;
}
.desktop-col-checklist {
flex: 1 !important;
min-width: 300px !important;
}
.desktop-col-results {
flex: 2 !important;
min-width: 400px !important;
}
.mobile-only {
display: none !important;
}
.mobile-expandable-btn {
display: none !important;
}
}
/* Mobile/Tablet: stack everything */
@media (max-width: 1023px) {
.gradio-row {
flex-direction: column !important;
}
.gradio-column {
width: 100% !important;
margin-bottom: 20px !important;
}
.desktop-only {
display: none !important;
}
/* Mobile expandable sections */
.mobile-expandable-content {
display: none;
}
.mobile-expandable-content.show {
display: block;
}
}
/* Button styling */
.gradio-button {
margin-bottom: 8px !important;
}
@media (max-width: 1023px) {
.gradio-button {
width: 100% !important;
padding: 16px !important;
font-size: 16px !important;
}
.mobile-expand-btn {
background: #f9fafb !important;
border: 1px solid #e5e7eb !important;
color: #374151 !important;
padding: 12px 16px !important;
margin: 8px 0 !important;
border-radius: 8px !important;
font-weight: 500 !important;
}
.mobile-expand-btn:hover {
background: #f3f4f6 !important;
}
}
/* Results styling */
.risk-low { border-left: 4px solid #10b981; background: #f0fdf4; }
.risk-moderate { border-left: 4px solid #f59e0b; background: #fffbeb; }
.risk-high { border-left: 4px solid #f97316; background: #fff7ed; }
.risk-critical { border-left: 4px solid #ef4444; background: #fef2f2; }
/* Clean group styling */
.gradio-group {
border: none !important;
background: none !important;
padding: 0 !important;
margin: 0 !important;
box-shadow: none !important;
}
/* Force readable text colors */
.gradio-html * {
color: #1f2937 !important;
}
.gradio-html p, .gradio-html div, .gradio-html span, .gradio-html li, .gradio-html ul, .gradio-html h1, .gradio-html h2, .gradio-html h3, .gradio-html h4 {
color: #1f2937 !important;
}
/* Form spacing */
.gradio-textbox {
margin-bottom: 12px !important;
}
.gradio-checkbox {
margin-bottom: 6px !important;
font-size: 14px !important;
}
/* Compact checklist */
.compact-checklist .gradio-checkbox {
margin-bottom: 4px !important;
}
/* Specific overrides for safety plan and analysis displays */
.gradio-html pre {
color: #1f2937 !important;
background: #f9fafb !important;
padding: 12px !important;
border-radius: 8px !important;
}
/* Prevent dark theme from overriding italic text in HTML components */
.gradio-html em, .gradio-html i {
color: #111827 !important;
}
"""
with gr.Blocks(css=css, title="Tether | Relationship Pattern Analyzer") as demo:
tagline_html = gr.HTML("""
<div style="text-align: center; padding: 30px 20px;">
<h1 style="font-size: 2.5rem; font-weight: bold; color: #1f2937; margin-bottom: 16px;">
Tether
</h1>
<p style="font-size: 1.25rem; color: #6b7280; max-width: 600px; margin: 0 auto;">
Share messages that concern you, and we'll help you understand what patterns might be present.
</p>
</div>
""")
with gr.Tab("Analyze Messages"):
lang_selector = gr.Dropdown(
choices=list(SUPPORTED_LANGUAGES.keys()),
value="English",
label="🌐 Select Language",
interactive=True,
scale=1
)
lang_state = gr.State("en")
# Privacy notice
privacy_banner = gr.HTML("""
<div style="background: #1e40af; border-radius: 12px; padding: 24px; margin-bottom: 24px; width: 100%; box-shadow: 0 4px 12px rgba(30, 64, 175, 0.3);">
<div style="display: flex; align-items: center; margin-bottom: 12px;">
<span style="font-size: 1.5rem; margin-right: 12px;">🛡️</span>
<h3 style="color: white; margin: 0; font-size: 1.25rem; font-weight: 600;">Your Privacy Matters</h3>
</div>
<p style="color: #e0e7ff; margin: 0; font-size: 1rem; line-height: 1.5;">
Your messages are analyzed locally and are not stored or shared.
This tool is for educational purposes and not a substitute for professional counseling.
</p>
</div>
""")
# Desktop layout
with gr.Row(elem_classes=["desktop-row", "desktop-only"], equal_height=True):
# Messages column
with gr.Column(elem_classes=["desktop-col-messages"], scale=4, min_width=400):
share_messages_header = gr.HTML("<h3 style='margin-bottom: 16px;'>Share Your Messages</h3>")
share_messages_instruction = gr.HTML("""
<p style="color: #6b7280; margin-bottom: 20px;">
Enter up to three messages that made you feel uncomfortable, confused, or concerned.
For the most accurate analysis, include messages from recent emotionally intense conversations.
</p>
""")
msg1_desktop = gr.Textbox(
label="Message 1 *",
placeholder="Enter the message here...",
lines=4
)
msg2_desktop = gr.Textbox(
label="Message 2 (optional)",
placeholder="Enter the message here...",
lines=4
)
msg3_desktop = gr.Textbox(
label="Message 3 (optional)",
placeholder="Enter the message here...",
lines=4
)
# Checklist column
with gr.Column(elem_classes=["desktop-col-checklist"], scale=3, min_width=300):
safety_checklist_header = gr.HTML("<h3 style='margin-bottom: 16px;'>Safety Checklist</h3>")
checklist_instruction = gr.HTML("""
<p style="color: #6b7280; margin-bottom: 20px; font-size: 14px;">
Optional but recommended. Check any that apply to your situation:
</p>
""")
checklist_items_desktop = []
with gr.Column(elem_classes=["compact-checklist"]):
for question, weight in ESCALATION_QUESTIONS:
checklist_items_desktop.append(gr.Checkbox(label=question, elem_classes=["compact-checkbox"]))
none_selected_desktop = gr.Checkbox(
label="None of the above apply to my situation",
elem_classes=["none-checkbox"]
)
analyze_btn_desktop = gr.Button(
UI_STRINGS["en"]["analyze_btn"],
variant="primary",
size="lg",
elem_id="analyze-btn-desktop"
)
# Results column
with gr.Column(elem_classes=["desktop-col-results"], scale=5, min_width=400):
analysis_results_header = gr.HTML("<h3 style='margin-bottom: 16px;'>Analysis Results</h3>")
results_placeholder_html = gr.HTML("""
<p style="color: #6b7280; margin-bottom: 20px; font-style: italic;">
Results will appear here after analysis...
</p>
""")
# Desktop results components
results_json_desktop = gr.JSON(visible=False)
risk_summary_desktop = gr.HTML(visible=False)
concerns_display_desktop = gr.HTML(visible=False)
additional_metrics_desktop = gr.HTML(visible=False)
recommendations_display_desktop = gr.HTML(visible=False)
with gr.Row(visible=False) as action_buttons_desktop:
safety_plan_btn_desktop = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["safety_plan_btn"],
variant="secondary"
)
full_analysis_btn_desktop = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["full_analysis_btn"],
variant="secondary"
)
download_btn_desktop = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["download_btn"],
variant="secondary"
)
full_analysis_display_desktop = gr.HTML(visible=False)
timeline_chart_desktop = gr.Image(visible=False, label="Pattern Timeline")
download_file_desktop = gr.File(label="Download Report", visible=False)
# Mobile layout
with gr.Column(elem_classes=["mobile-only"]):
# Message input - always visible
share_messages_header_mobile = gr.HTML("<h3>📝 Share Your Messages</h3>")
share_messages_instruction_mobile = gr.HTML("""
<p style="color: #6b7280; margin-bottom: 20px; font-size: 14px;">
Optional but recommended. Check any that apply to your situation:
</p>
""")
msg1_mobile = gr.Textbox(
label="Message 1 (required)",
placeholder="Enter the concerning message here...",
lines=3
)
# Button to show additional messages
show_more_msgs_btn = gr.Button(
"➕ Add More Messages (Optional)",
elem_classes=["mobile-expand-btn", "mobile-expandable-btn"],
variant="secondary"
)
# Additional messages (hidden by default)
with gr.Column(visible=False) as additional_messages_mobile:
msg2_mobile = gr.Textbox(
label="Message 2 (optional)",
placeholder="Enter another message...",
lines=3
)
msg3_mobile = gr.Textbox(
label="Message 3 (optional)",
placeholder="Enter a third message...",
lines=3
)
# Button to show safety checklist
show_checklist_btn = gr.Button(
"⚠️ Safety Checklist (Optional)",
elem_classes=["mobile-expand-btn", "mobile-expandable-btn"],
variant="secondary"
)
# Safety checklist (hidden by default)
with gr.Column(visible=False) as safety_checklist_mobile:
checklist_accuracy_mobile = gr.HTML("""
<p style="color: #6b7280; margin-bottom: 16px; font-size: 14px;">
Check any that apply to improve analysis accuracy:
</p>
""")
checklist_items_mobile = []
for question, weight in ESCALATION_QUESTIONS:
checklist_items_mobile.append(gr.Checkbox(label=question, elem_classes=["compact-checkbox"]))
none_selected_mobile = gr.Checkbox(
label="None of the above apply",
elem_classes=["none-checkbox"]
)
analyze_btn_mobile = gr.Button(
UI_STRINGS["en"]["analyze_btn"],
variant="primary",
size="lg",
elem_id="analyze-btn-mobile"
)
# Mobile results components
results_json_mobile = gr.JSON(visible=False)
risk_summary_mobile = gr.HTML(visible=False)
concerns_display_mobile = gr.HTML(visible=False)
additional_metrics_mobile = gr.HTML(visible=False)
recommendations_display_mobile = gr.HTML(visible=False)
with gr.Row(visible=False) as action_buttons_mobile:
safety_plan_btn_mobile = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["safety_plan_btn_mobile"],
variant="secondary"
)
full_analysis_btn_mobile = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["full_analysis_btn_mobile"],
variant="secondary"
)
download_btn_mobile = gr.Button(
_string_cache.get("en", UI_STRINGS["en"])["download_btn_mobile"],
variant="secondary"
)
full_analysis_display_mobile = gr.HTML(visible=False)
timeline_chart_mobile = gr.Image(visible=False, label="Pattern Timeline")
download_file_mobile = gr.File(label="Download Report", visible=False)
with gr.Tab("Safety Resources"):
safety_resources_banner = gr.HTML("""
<div style="background: #dcfce7; border-radius: 12px; padding: 24px; margin-bottom: 20px;">
<h2 style="color: #166534; margin-bottom: 16px;">🛡️ Safety Planning</h2>
<p style="color: #166534;">
If you're concerned about your safety, here are immediate resources and steps you can take.
</p>
</div>
""")
with gr.Row():
with gr.Column():
emergency_resources_html = gr.HTML("""
<div class="risk-card" style="background: #fef2f2; border-left: 4px solid #ef4444;">
<h3 style="color: #991b1b;">🚨 Emergency Resources</h3>
<div style="margin: 16px 0;">
<p><strong>911</strong> - For immediate danger</p>
<p><strong>1-800-799-7233</strong> - National DV Hotline (24/7)</p>
<p><strong>Text START to 88788</strong> - Crisis Text Line</p>
<p><strong>988</strong> - National Suicide Prevention Lifeline</p>
</div>
</div>
""")
with gr.Column():
support_resources_html = gr.HTML("""
<div class="risk-card" style="background: #f0fdf4; border-left: 4px solid #10b981;">
<h3 style="color: #065f46;">💚 Support Resources</h3>
<div style="margin: 16px 0;">
<p><strong>thehotline.org</strong> - Online chat support</p>
<p><strong>Local counseling services</strong> - Professional support</p>
<p><strong>Trusted friends/family</strong> - Personal support network</p>
<p><strong>Legal advocacy</strong> - Know your rights</p>
</div>
</div>
""")
safety_plan_display = gr.HTML()
# Mobile expandable button handlers
more_msgs_visible = gr.State(False)
checklist_visible = gr.State(False)
def toggle_additional_messages(is_visible):
new_state = not is_visible
return new_state, gr.update(visible=new_state)
def toggle_safety_checklist(is_visible):
new_state = not is_visible
return new_state, gr.update(visible=new_state)
show_more_msgs_btn.click(
toggle_additional_messages,
inputs=[more_msgs_visible],
outputs=[more_msgs_visible, additional_messages_mobile]
)
show_checklist_btn.click(
toggle_safety_checklist,
inputs=[checklist_visible],
outputs=[checklist_visible, safety_checklist_mobile]
)
# Full analysis processing function
def process_analysis(*inputs):
"""Process the analysis and format for display - FULL FUNCTIONALITY"""
lang_code = inputs[-1]
s = get_ui_strings(lang_code)
inputs = inputs[:-1]
msgs = inputs[:3]
checklist_responses = inputs[3:]
# Run analysis
analysis_result, timeline_img, safety_plan = analyze_composite_with_ui_format(*inputs, lang_code=lang_code)
# Parse results
try:
results = json.loads(analysis_result)
except:
results = {'riskLevel': 'low', 'riskScore': 0, 'primaryConcerns': [], 'emotionalTones': [], 'darvoScore': 0, 'personalizedRecommendations': []}
# Translate dynamic values if not English
if lang_code != "en":
# Translate pattern names and descriptions in concerns
if results.get('primaryConcerns'):
for concern in results['primaryConcerns']:
concern['name'] = translate_dynamic_value(concern['name'], lang_code)
concern['description'] = translate_dynamic_value(concern['description'], lang_code)
# Translate emotional tones
if results.get('emotionalTones'):
results['emotionalTones'] = [translate_dynamic_value(t, lang_code) for t in results['emotionalTones']]
# Translate boundary labels
if results.get('boundaryHealth', {}).get('message_assessments'):
for assessment in results['boundaryHealth']['message_assessments']:
assessment['label'] = translate_dynamic_value(assessment['label'], lang_code)
# Translate recommendations
translated_recs = []
for rec in results.get('personalizedRecommendations', []):
try:
translated_recs.append(GoogleTranslator(source='en', target=lang_code).translate(rec))
except Exception:
translated_recs.append(rec)
results['personalizedRecommendations'] = translated_recs
# Format risk summary
risk_config = {
'low': {'color': '#10b981', 'bg': '#f0fdf4', 'icon': '🟢', 'label': s['low_risk']},
'moderate': {'color': '#f59e0b', 'bg': '#fffbeb', 'icon': '🟡', 'label': s['moderate_risk']},
'high': {'color': '#f97316', 'bg': '#fff7ed', 'icon': '🟠', 'label': s['high_risk']},
'critical': {'color': '#ef4444', 'bg': '#fef2f2', 'icon': '🔴', 'label': s['critical_risk']}
}
config = risk_config.get(results['riskLevel'], risk_config['low'])
pattern_summary = ""
if results.get('primaryConcerns'):
actual_concerns = [concern for concern in results['primaryConcerns']
if 'escalation potential' not in concern['name'].lower()]
if actual_concerns:
pattern_names = [concern['name'] for concern in actual_concerns]
if len(pattern_names) == 1:
pattern_summary = f"<span style='color: #1f2937 !important;'><strong style='color: #1f2937 !important;'>{pattern_names[0]}</strong> {s['pattern_detected']}</span>"
elif len(pattern_names) == 2:
pattern_summary = f"<span style='color: #1f2937 !important;'><strong style='color: #1f2937 !important;'>{pattern_names[0]}</strong> and <strong style='color: #1f2937 !important;'>{pattern_names[1]}</strong> {s['patterns_detected']}</span>"
else:
pattern_summary = f"<span style='color: #1f2937 !important;'><strong style='color: #1f2937 !important;'>{', '.join(pattern_names[:-1])}</strong> and <strong style='color: #1f2937 !important;'>{pattern_names[-1]}</strong> {s['patterns_detected']}</span>"
else:
pattern_summary = f"<span style='color: #1f2937 !important;'><strong style='color: #1f2937 !important;'>{s['concerning_patterns']}</strong> {s['pattern_detected']}</span>"
else:
pattern_summary = f"<span style='color: #1f2937 !important;'><strong style='color: #1f2937 !important;'>{s['concerning_patterns']}</strong> {s['pattern_detected']}</span>"
risk_html = f"""
<div style="background: {config['bg']}; border-left: 4px solid {config['color']}; border-radius: 12px; padding: 24px; margin-bottom: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
<div style="display: flex; align-items: center; margin-bottom: 16px;">
<span style="font-size: 2rem; margin-right: 12px;">{config['icon']}</span>
<div>
<h2 style="font-size: 1.5rem; font-weight: bold; color: #1f2937; margin: 0;">{config['label']}</h2>
<p style="color: #374151; margin: 0; font-weight: 500;">{s['based_on_messages']}</p>
</div>
</div>
<div style="background: rgba(0,0,0,0.05); border-radius: 8px; padding: 16px;">
<div style="color: #1f2937 !important; margin: 0 0 8px 0; font-size: 1rem;">
<span style="color: #1f2937 !important;">{pattern_summary}</span>
</div>
<p style="color: #374151 !important; margin: 0; font-weight: 600;">
{s['risk_score']}: {results['riskScore']}%
</p>
</div>
</div>
"""
# Format concerns
concerns_html = f"<h3 style='margin-top: 24px;'>{s['key_concerns']}</h3>"
if results.get('primaryConcerns'):
for concern in results['primaryConcerns']:
severity_colors = {
'high': '#fee2e2',
'moderate': '#fef3c7',
'low': '#dbeafe'
}
bg_color = severity_colors.get(concern.get('severity', 'low'), '#f3f4f6')
concerns_html += f"""
<div style="background: {bg_color}; border-radius: 8px; padding: 16px; margin: 8px 0;">
<h4 style="margin: 0 0 8px 0; color: #1f2937;">{concern.get('name', s['unknown_concern'])}</h4>
<p style="margin: 0; color: #6b7280;">{concern.get('description', s['no_description'])}</p>
</div>
"""
else:
concerns_html += f"<p style='color: #6b7280; font-style: italic;'>{s['no_concerns']}</p>"
# Additional Metrics Section
metrics_html = f"<h3 style='margin-top: 24px;'>{s['additional_analysis']}</h3>"
# DARVO Score
darvo_score = results.get('darvoScore', 0)
if darvo_score > 0.25:
darvo_level = s['darvo_high'] if darvo_score >= 0.65 else s['darvo_moderate']
darvo_color = "#fee2e2" if darvo_score >= 0.65 else "#fef3c7"
metrics_html += f"""
<div style="background: {darvo_color}; border-radius: 8px; padding: 16px; margin: 8px 0;">
<h4 style="margin: 0 0 8px 0; color: #1f2937;">🎭 DARVO Score: {darvo_score:.3f} ({darvo_level})</h4>
<p style="margin: 0; color: #6b7280;">
{s['darvo_description']}
</p>
</div>
"""
# Emotional Tones
emotional_tones = results.get('emotionalTones', [])
if emotional_tones and any(tone != 'neutral' for tone in emotional_tones):
metrics_html += '<div style="background: #f8fafc; border-radius: 8px; padding: 16px; margin: 8px 0;">'
metrics_html += f'<h4 style="margin: 0 0 8px 0; color: #1f2937;">&#127917; {s["emotional_tones"]}</h4>'
metrics_html += '<div style="margin: 8px 0;">'
for i, tone in enumerate(emotional_tones):
if tone and tone != 'neutral':
metrics_html += '<p style="margin: 4px 0; color: #1f2937;">&#8226; ' + s['message_label'] + ' ' + str(i+1) + ': <em style="color: #1f2937; font-style: italic;">' + tone + '</em></p>'
metrics_html += '</div>'
metrics_html += f'<p style="margin: 8px 0 0 0; color: #374151; font-size: 14px;">{s["emotional_tone_description"]}</p>'
metrics_html += '</div>'
# Boundary Health — only show when unhealthy patterns detected
boundary_health = results.get('boundaryHealth', {})
overall_health = boundary_health.get('overall_health', 'unknown')
message_assessments = boundary_health.get('message_assessments', [])
if overall_health in ['unhealthy', 'mixed'] and message_assessments:
# Count boundary types across messages
type_counts = {}
for assessment in message_assessments:
label = assessment.get('label', 'Unknown')
if label != 'Respected Boundary':
type_counts[label] = type_counts.get(label, 0) + 1
if type_counts:
border_color = '#ef4444' if overall_health == 'unhealthy' else '#f59e0b'
bg_color = '#fef2f2' if overall_health == 'unhealthy' else '#fffbeb'
header = s['boundary_violations'] if overall_health == 'unhealthy' else s['mixed_boundary']
metrics_html += '<div style="background: ' + bg_color + '; border-left: 4px solid ' + border_color + '; border-radius: 8px; padding: 16px; margin: 8px 0;">'
metrics_html += '<h4 style="margin: 0 0 12px 0; color: #1f2937;">&#128737; ' + header + '</h4>'
for label, count in type_counts.items():
suffix = ' (' + str(count) + ' messages)' if count > 1 else ''
metrics_html += '<p style="margin: 4px 0; color: #1f2937; font-weight: 600;">&#8226; ' + label + suffix + '</p>'
# Add plain-language explanation per type
if 'Manipulative' in label:
metrics_html += f'<p style="margin: 2px 0 8px 20px; color: #374151; font-size: 14px;">{s["manipulative_boundary_desc"]}</p>'
elif 'Violated' in label:
metrics_html += f'<p style="margin: 2px 0 8px 20px; color: #374151; font-size: 14px;">{s["violated_boundary_desc"]}</p>'
elif 'Dismissed' in label:
metrics_html += f'<p style="margin: 2px 0 8px 20px; color: #374151; font-size: 14px;">{s["dismissed_boundary_desc"]}</p>'
metrics_html += f'<p style="margin: 12px 0 0 0; color: #374151; font-size: 13px;">{s["boundary_footer"]}</p>'
metrics_html += '</div>'
# Format recommendations
rec_html = f"<h3 style='margin-top: 24px;'>{s['personalized_recommendations']}</h3>"
# Format recommendations
rec_html = f"<h3 style='margin-top: 24px;'>{s['personalized_recommendations']}</h3>"
recommendations = results.get('personalizedRecommendations', [])
for rec in recommendations:
rec_html += '<div style="background: #f8fafc; border-left: 3px solid #3b82f6; border-radius: 8px; padding: 12px; margin: 8px 0;">'
rec_html += '<p style="margin: 0; color: #374151;">&#8226; ' + rec + '</p>'
rec_html += '</div>'
return (
gr.update(value=analysis_result, visible=False), # results_json
gr.update(value=risk_html, visible=True), # risk_summary
gr.update(value=concerns_html, visible=True), # concerns_display
gr.update(value=metrics_html, visible=results.get('riskScore', 0) > 25), # additional_metrics
gr.update(value=rec_html, visible=True), # recommendations_display
gr.update(visible=True), # action_buttons
gr.update(visible=False), # full_analysis_display
gr.update(value=timeline_img, visible=True), # timeline_chart
gr.update(visible=False), # download_file
gr.update(value=safety_plan) # safety_plan_display
)
def show_full_analysis(results_json_str, lang_code="en"):
"""Show the full technical analysis"""
try:
if not results_json_str:
return gr.update(value="<p>No analysis data available. Please run the analysis first.</p>", visible=True)
# Handle both JSON string and dict inputs
if isinstance(results_json_str, str):
results = json.loads(results_json_str)
elif isinstance(results_json_str, dict):
results = results_json_str
else:
return gr.update(value="<p>Invalid data format. Please run the analysis again.</p>", visible=True)
# Create comprehensive full analysis display
full_html = f"""
<div style="background: white; border-radius: 12px; padding: 24px; border: 1px solid #e5e7eb; margin-top: 20px;">
<h3 style="color: #1f2937 !important;">Complete Technical Analysis</h3>
<div style="background: #f9fafb; border-radius: 8px; padding: 16px; margin: 16px 0;">
<h4 style="color: #1f2937 !important;">Risk Assessment Summary</h4>
<p style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">Risk Level:</strong> {results.get('riskLevel', 'Unknown').title()}</p>
<p style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">Risk Score:</strong> {results.get('riskScore', 'N/A')}%</p>
<p style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">Risk Stage:</strong> {results.get('riskStage', 'Unknown').replace('-', ' ').title()}</p>
</div>
<div style="background: #f9fafb; border-radius: 8px; padding: 16px; margin: 16px 0;">
<h4 style="color: #1f2937 !important;">Behavioral Analysis</h4>
<p style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">DARVO Score:</strong> {results.get('darvoScore', 0):.3f}</p>
<p style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">Emotional Tones:</strong> <span style="color: #1f2937 !important;">{', '.join(results.get('emotionalTones', ['None detected']))}</span></p>
</div>
<div style="background: #f9fafb; border-radius: 8px; padding: 16px; margin: 16px 0;">
<h4 style="color: #1f2937 !important;">Detected Patterns</h4>
"""
if results.get('allPatterns'):
for pattern in results['allPatterns']:
severity_badge = {
'high': '🔴',
'moderate': '🟡',
'low': '🟢'
}.get(pattern.get('severity', 'low'), '⚪')
full_html += f"""
<div style="margin: 8px 0; padding: 8px; background: white; border-radius: 4px;">
<p style="margin: 0; color: #1f2937 !important;"><strong style="color: #1f2937 !important;">{severity_badge} {pattern.get('name', 'Unknown')}</strong></p>
<p style="margin: 4px 0 0 0; font-size: 14px; color: #6b7280 !important;">{pattern.get('description', 'No description available')}</p>
</div>
"""
else:
full_html += "<p style='color: #1f2937 !important;'>No specific patterns detected.</p>"
full_html += """
</div>
<div style="background: #f9fafb; border-radius: 8px; padding: 16px; margin: 16px 0;">
<h4 style="color: #1f2937 !important;">📝 Complete Analysis Output</h4>
<div style="max-height: 400px; overflow-y: auto; background: white; padding: 12px; border-radius: 4px; font-family: monospace; font-size: 14px; white-space: pre-wrap; color: #1f2937 !important;">"""
full_html += results.get('rawAnalysis', 'No detailed analysis available')
full_html += """
</div>
</div>
</div>
"""
if lang_code != "en":
try:
full_html = GoogleTranslator(source='en', target=lang_code).translate(full_html)
except Exception:
pass
return gr.update(value=full_html, visible=True)
except Exception as e:
error_html = f"""
<div style="background: #fee2e2; border-radius: 8px; padding: 16px; margin-top: 20px;">
<h4>❌ Error Loading Analysis</h4>
<p>Unable to parse analysis results: {str(e)}</p>
<p>Please try running the analysis again.</p>
</div>
"""
return gr.update(value=error_html, visible=True)
def generate_report(results_json_str, timeline_img, lang_code="en"):
"""Generate a downloadable report with all analysis information"""
import tempfile
import os
from datetime import datetime
try:
if not results_json_str:
return None
# Handle both JSON string and dict inputs
if isinstance(results_json_str, str):
results = json.loads(results_json_str)
elif isinstance(results_json_str, dict):
results = results_json_str
else:
return None
current_date = datetime.now().strftime("%Y-%m-%d")
current_time = datetime.now().strftime("%I:%M %p")
# Create comprehensive report
report = f"""RELATIONSHIP PATTERN ANALYSIS REPORT
Generated: {current_date} at {current_time}
═══════════════════════════════════════════════════════════════════
EXECUTIVE SUMMARY
═══════════════════════════════════════════════════════════════════
Risk Level: {results.get('riskLevel', 'Unknown').upper()}
Risk Score: {results.get('riskScore', 'N/A')}%
Risk Stage: {results.get('riskStage', 'Unknown').replace('-', ' ').title()}
═══════════════════════════════════════════════════════════════════
DETECTED PATTERNS
═══════════════════════════════════════════════════════════════════"""
# Add detected patterns
if results.get('allPatterns'):
for pattern in results['allPatterns']:
severity_symbol = {
'high': '🔴 HIGH',
'moderate': '🟡 MODERATE',
'low': '🟢 LOW'
}.get(pattern.get('severity', 'low'), '⚪ UNKNOWN')
report += f"""
{severity_symbol} SEVERITY: {pattern.get('name', 'Unknown Pattern')}
Description: {pattern.get('description', 'No description available')}"""
else:
report += "\n\nNo specific patterns detected in the analysis."
# Add behavioral analysis
report += f"""
═══════════════════════════════════════════════════════════════════
BEHAVIORAL ANALYSIS
═══════════════════════════════════════════════════════════════════
DARVO Score: {results.get('darvoScore', 0):.3f}"""
darvo_score = results.get('darvoScore', 0)
if darvo_score > 0.65:
report += "\nDARVO Level: HIGH - Strong indication of narrative manipulation"
elif darvo_score > 0.25:
report += "\nDARVO Level: MODERATE - Some indication of narrative manipulation"
else:
report += "\nDARVO Level: LOW - Limited indication of narrative manipulation"
report += """\n
DARVO Definition: Deny, Attack, Reverse Victim & Offender - a manipulation
tactic where the perpetrator denies wrongdoing, attacks the victim, and
positions themselves as the victim.
Emotional Tone Analysis:"""
# Add emotional tones
emotional_tones = results.get('emotionalTones', [])
if emotional_tones:
for i, tone in enumerate(emotional_tones):
if tone and tone != 'neutral':
report += f"\nMessage {i+1}: {tone}"
if not any(tone != 'neutral' for tone in emotional_tones):
report += "\nNo concerning emotional tones detected."
else:
report += "\nNo emotional tone data available."
# Add recommendations
report += f"""
═══════════════════════════════════════════════════════════════════
PERSONALIZED RECOMMENDATIONS
═══════════════════════════════════════════════════════════════════"""
recommendations = results.get('personalizedRecommendations', [])
for i, rec in enumerate(recommendations, 1):
report += f"\n{i}. {rec}"
# Add safety planning
safety_plan = results.get('safetyPlan', '')
if safety_plan:
report += f"""
═══════════════════════════════════════════════════════════════════
SAFETY PLANNING
═══════════════════════════════════════════════════════════════════
{safety_plan}"""
# Add emergency resources
report += """
═══════════════════════════════════════════════════════════════════
EMERGENCY RESOURCES
═══════════════════════════════════════════════════════════════════
🚨 IMMEDIATE EMERGENCY: Call 911
24/7 CRISIS SUPPORT:
• National Domestic Violence Hotline: 1-800-799-7233
• Crisis Text Line: Text START to 88788
• National Suicide Prevention Lifeline: 988
• Online Chat Support: thehotline.org
ADDITIONAL SUPPORT:
• Local counseling services
• Legal advocacy organizations
• Trusted friends and family
• Employee assistance programs (if available)
═══════════════════════════════════════════════════════════════════
IMPORTANT DISCLAIMERS
═══════════════════════════════════════════════════════════════════
• This analysis is for educational purposes only
• It is not a substitute for professional counseling or legal advice
• Trust your instincts about your safety
• Consider sharing this report with a trusted counselor or advocate
• Your messages were analyzed locally and not stored or shared
Report Generated by: Tether | usetetherai.com
Analysis Date: {current_date}
Report Version: 2.0
═══════════════════════════════════════════════════════════════════"""
# Create temporary file
temp_file = tempfile.NamedTemporaryFile(
mode='w',
suffix='.txt',
prefix=f'relationship_analysis_report_{current_date.replace("-", "_")}_',
delete=False,
encoding='utf-8'
)
if lang_code != "en":
try:
report = GoogleTranslator(source='en', target=lang_code).translate(report)
except Exception:
pass
temp_file.write(report)
temp_file.close()
return temp_file.name
except Exception as e:
# Create error report
error_report = f"""RELATIONSHIP PATTERN ANALYSIS REPORT - ERROR
Generated: {datetime.now().strftime("%Y-%m-%d at %I:%M %p")}
An error occurred while generating the full report: {str(e)}
Please try running the analysis again or contact support if the issue persists."""
temp_file = tempfile.NamedTemporaryFile(
mode='w',
suffix='.txt',
prefix='error_report_',
delete=False,
encoding='utf-8'
)
temp_file.write(error_report)
temp_file.close()
return temp_file.name
def show_safety_plan_content(safety_plan_content):
"""Display the personalized safety plan"""
if safety_plan_content:
safety_plan_html = f"""
<div style="background: white; border-radius: 12px; padding: 24px; border: 1px solid #e5e7eb; margin-top: 20px;">
<h3 style="color: #1f2937 !important;">🛡️ Your Personalized Safety Plan</h3>
<div style="background: #f0fdf4; border-radius: 8px; padding: 16px; margin: 16px 0;">
<div style="white-space: pre-wrap; font-family: inherit; font-size: 14px; line-height: 1.5; color: #1f2937 !important;">{safety_plan_content}</div>
</div>
</div>
"""
return gr.update(value=safety_plan_html, visible=True)
else:
# Fallback to general safety information
general_safety = """
<div style="background: white; border-radius: 12px; padding: 24px; border: 1px solid #e5e7eb; margin-top: 20px;">
<h3 style="color: #1f2937 !important;">🛡️ Safety Planning</h3>
<div style="background: #f0fdf4; border-radius: 8px; padding: 16px; margin: 16px 0;">
<h4 style="color: #1f2937 !important;">Immediate Safety Steps:</h4>
<ul style="color: #1f2937 !important;">
<li style="color: #1f2937 !important;">Trust your instincts - if something feels wrong, it probably is</li>
<li style="color: #1f2937 !important;">Document concerning incidents with dates and details</li>
<li style="color: #1f2937 !important;">Identify safe people you can reach out to</li>
<li style="color: #1f2937 !important;">Keep important documents and emergency contacts accessible</li>
<li style="color: #1f2937 !important;">Consider speaking with a counselor or trusted friend</li>
</ul>
<h4 style="color: #1f2937 !important;">Emergency Resources:</h4>
<ul style="color: #1f2937 !important;">
<li style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">911</strong> - For immediate danger</li>
<li style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">1-800-799-7233</strong> - National DV Hotline (24/7)</li>
<li style="color: #1f2937 !important;"><strong style="color: #1f2937 !important;">Text START to 88788</strong> - Crisis Text Line</li>
</ul>
</div>
</div>
"""
return gr.update(value=general_safety, visible=True)
def on_language_change(lang_name):
print(f"Language changed to: {lang_name}")
lang_code = SUPPORTED_LANGUAGES.get(lang_name, "en")
s = get_ui_strings(lang_code)
return (
lang_code,
gr.update(value=s["analyze_btn"]),
gr.update(value=s["analyze_btn"]),
gr.update(label=s["message1"], placeholder=s["placeholder_message"]),
gr.update(label=s["message2"], placeholder=s["placeholder_message"]),
gr.update(label=s["message3"], placeholder=s["placeholder_message"]),
gr.update(label=s["message1_required"], placeholder=s["placeholder_message_mobile"]),
gr.update(label=s["message2"], placeholder=s["placeholder_message2"]),
gr.update(label=s["message3"], placeholder=s["placeholder_message3"]),
gr.update(value=s["safety_plan_btn"]),
gr.update(value=s["safety_plan_btn_mobile"]),
gr.update(value=s["full_analysis_btn"]),
gr.update(value=s["full_analysis_btn_mobile"]),
gr.update(value=s["download_btn"]),
gr.update(value=s["download_btn_mobile"]),
gr.update(label=s["checklist_q1"]),
gr.update(label=s["checklist_q2"]),
gr.update(label=s["checklist_q3"]),
gr.update(label=s["checklist_q4"]),
gr.update(label=s["checklist_q5"]),
gr.update(label=s["checklist_q6"]),
gr.update(label=s["checklist_q7"]),
gr.update(label=s["checklist_q8"]),
gr.update(label=s["checklist_q9"]),
gr.update(label=s["checklist_q10"]),
gr.update(label=s["checklist_q1"]),
gr.update(label=s["checklist_q2"]),
gr.update(label=s["checklist_q3"]),
gr.update(label=s["checklist_q4"]),
gr.update(label=s["checklist_q5"]),
gr.update(label=s["checklist_q6"]),
gr.update(label=s["checklist_q7"]),
gr.update(label=s["checklist_q8"]),
gr.update(label=s["checklist_q9"]),
gr.update(label=s["checklist_q10"]),
gr.update(label=s["none_apply"]),
gr.update(label=s["none_apply_mobile"]),
gr.update(value=f"<h3 style='margin-bottom: 16px;'>{s['share_messages']}</h3>"),
gr.update(value=f"<p style='color: #6b7280; margin-bottom: 20px;'>{s['share_messages_instruction']}</p>"),
gr.update(value=f"<div style='background: #1e40af; border-radius: 12px; padding: 24px; margin-bottom: 24px; width: 100%;'><div style='display: flex; align-items: center; margin-bottom: 12px;'><span style='font-size: 1.5rem; margin-right: 12px;'>🛡</span><h3 style='color: white; margin: 0; font-size: 1.25rem; font-weight: 600;'>{s['privacy_title']}</h3></div><p style='color: #e0e7ff; margin: 0; font-size: 1rem; line-height: 1.5;'>{s['privacy_body']}</p></div>"),
gr.update(value=f"<h3 style='margin-bottom: 16px;'>{s['safety_checklist']}</h3>"),
gr.update(value=f"<p style='color: #6b7280; margin-bottom: 20px; font-size: 14px;'>{s['checklist_optional']}</p>"),
gr.update(value=f"<h3 style='margin-bottom: 16px;'>{s['analysis_results']}</h3>"),
gr.update(value=f"<p style='color: #6b7280; margin-bottom: 20px; font-style: italic;'>{s['results_placeholder']}</p>"),
gr.update(value=f"<h3>📝 {s['share_messages']}</h3>"),
gr.update(value=f"<p style='color: #6b7280; margin-bottom: 20px; font-size: 14px;'>{s['checklist_optional']}</p>"),
gr.update(value=s["add_more_messages"]),
gr.update(value=s["safety_checklist_btn"]),
gr.update(value=f"<p style='color: #6b7280; margin-bottom: 16px; font-size: 14px;'>{s['checklist_accuracy_mobile']}</p>"),
gr.update(value=f"<div style='text-align: center; padding: 30px 20px;'><h1 style='font-size: 2.5rem; font-weight: bold; color: #1f2937; margin-bottom: 16px;'>Tether</h1><p style='font-size: 1.25rem; color: #6b7280; max-width: 600px; margin: 0 auto;'>{s['tagline']}</p></div>"),
gr.update(value=f"<div style='background: #dcfce7; border-radius: 12px; padding: 24px; margin-bottom: 20px;'><h2 style='color: #166534; margin-bottom: 16px;'>🛡️ {s['safety_resources_header']}</h2><p style='color: #166534;'>{s['safety_resources_intro']}</p></div>"),
gr.update(value=f"<div class='risk-card' style='background: #fef2f2; border-left: 4px solid #ef4444;'><h3 style='color: #991b1b;'>🚨 {s['emergency_resources_header']}</h3><div style='margin: 16px 0;'><p><strong>911</strong> - {s['emergency_911']}</p><p><strong>1-800-799-7233</strong> - {s['emergency_dv_hotline']}</p><p><strong>Text START to 88788</strong> - {s['emergency_text_line']}</p><p><strong>988</strong> - {s['emergency_suicide_line']}</p></div></div>"),
gr.update(value=f"<div class='risk-card' style='background: #f0fdf4; border-left: 4px solid #10b981;'><h3 style='color: #065f46;'>💚 {s['support_resources_header']}</h3><div style='margin: 16px 0;'><p><strong>thehotline.org</strong> - {s['support_hotline_chat']}</p><p><strong>{s['support_counseling']}</strong></p><p><strong>{s['support_personal']}</strong></p><p><strong>{s['support_legal']}</strong></p></div></div>"),
)
lang_selector.change(
on_language_change,
inputs=[lang_selector],
outputs=[
lang_state,
analyze_btn_desktop,
analyze_btn_mobile,
msg1_desktop,
msg2_desktop,
msg3_desktop,
msg1_mobile,
msg2_mobile,
msg3_mobile,
safety_plan_btn_desktop,
safety_plan_btn_mobile,
full_analysis_btn_desktop,
full_analysis_btn_mobile,
download_btn_desktop,
download_btn_mobile,
checklist_items_desktop[0],
checklist_items_desktop[1],
checklist_items_desktop[2],
checklist_items_desktop[3],
checklist_items_desktop[4],
checklist_items_desktop[5],
checklist_items_desktop[6],
checklist_items_desktop[7],
checklist_items_desktop[8],
checklist_items_desktop[9],
checklist_items_mobile[0],
checklist_items_mobile[1],
checklist_items_mobile[2],
checklist_items_mobile[3],
checklist_items_mobile[4],
checklist_items_mobile[5],
checklist_items_mobile[6],
checklist_items_mobile[7],
checklist_items_mobile[8],
checklist_items_mobile[9],
none_selected_desktop,
none_selected_mobile,
share_messages_header,
share_messages_instruction,
privacy_banner,
safety_checklist_header,
checklist_instruction,
analysis_results_header,
results_placeholder_html,
share_messages_header_mobile,
share_messages_instruction_mobile,
show_more_msgs_btn,
show_checklist_btn,
checklist_accuracy_mobile,
tagline_html,
safety_resources_banner,
emergency_resources_html,
support_resources_html,
]
)
# Connect desktop event handlers
analyze_btn_desktop.click(
lambda: gr.update(value="⏳ Analyzing...", interactive=False),
outputs=[analyze_btn_desktop]
).then(
process_analysis,
inputs=[msg1_desktop, msg2_desktop, msg3_desktop] + checklist_items_desktop + [none_selected_desktop, lang_state],
outputs=[
results_json_desktop, risk_summary_desktop, concerns_display_desktop,
additional_metrics_desktop, recommendations_display_desktop, action_buttons_desktop,
full_analysis_display_desktop, timeline_chart_desktop, download_file_desktop, safety_plan_display
]
).then(
lambda: gr.update(value="Analyze Messages", interactive=True),
outputs=[analyze_btn_desktop]
).then(
lambda lang: (
gr.update(value=get_ui_strings(lang)["safety_plan_btn"]),
gr.update(value=get_ui_strings(lang)["full_analysis_btn"]),
gr.update(value=get_ui_strings(lang)["download_btn"]),
),
inputs=[lang_state],
outputs=[safety_plan_btn_desktop, full_analysis_btn_desktop, download_btn_desktop]
)
full_analysis_btn_desktop.click(
show_full_analysis,
inputs=[results_json_desktop, lang_state],
outputs=[full_analysis_display_desktop]
)
download_btn_desktop.click(
generate_report,
inputs=[results_json_desktop, timeline_chart_desktop, lang_state],
outputs=[download_file_desktop]
).then(
lambda: gr.update(visible=True),
outputs=[download_file_desktop]
)
safety_plan_btn_desktop.click(
show_safety_plan_content,
inputs=[safety_plan_display],
outputs=[full_analysis_display_desktop]
)
# Connect mobile event handlers
analyze_btn_mobile.click(
lambda: gr.update(value="⏳ Analyzing...", interactive=False),
outputs=[analyze_btn_mobile]
).then(
process_analysis,
inputs=[msg1_mobile, msg2_mobile, msg3_mobile] + checklist_items_mobile + [none_selected_mobile, lang_state],
outputs=[
results_json_mobile, risk_summary_mobile, concerns_display_mobile,
additional_metrics_mobile, recommendations_display_mobile, action_buttons_mobile,
full_analysis_display_mobile, timeline_chart_mobile, download_file_mobile, safety_plan_display
]
).then(
lambda: gr.update(value="Analyze Messages", interactive=True),
outputs=[analyze_btn_mobile]
).then(
lambda lang: (
gr.update(value=get_ui_strings(lang)["safety_plan_btn_mobile"]),
gr.update(value=get_ui_strings(lang)["full_analysis_btn_mobile"]),
gr.update(value=get_ui_strings(lang)["download_btn_mobile"]),
),
inputs=[lang_state],
outputs=[safety_plan_btn_mobile, full_analysis_btn_mobile, download_btn_mobile]
)
full_analysis_btn_mobile.click(
show_full_analysis,
inputs=[results_json_mobile, lang_state],
outputs=[full_analysis_display_mobile]
)
download_btn_mobile.click(
generate_report,
inputs=[results_json_mobile, timeline_chart_mobile,lang_state],
outputs=[download_file_mobile]
).then(
lambda: gr.update(visible=True),
outputs=[download_file_mobile]
)
safety_plan_btn_mobile.click(
show_safety_plan_content,
inputs=[safety_plan_display],
outputs=[full_analysis_display_mobile]
)
return demo
if __name__ == "__main__":
try:
print("📱 Creating interface...")
demo = create_mobile_friendly_interface()
print("✅ Interface created successfully")
print("🌐 Launching demo...")
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
print("🎉 App launched!")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
raise