Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -49,7 +49,41 @@ ESCALATION_QUESTIONS = [
|
|
49 |
("Violence has increased in frequency or severity", 3),
|
50 |
("Partner monitors your calls/GPS/social media", 2)
|
51 |
]
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def detect_contradiction(message):
|
54 |
patterns = [
|
55 |
(r"\b(i love you).{0,15}(i hate you|you ruin everything)", re.IGNORECASE),
|
@@ -60,6 +94,27 @@ def detect_contradiction(message):
|
|
60 |
(r"\b(i guess i’m just).{0,15}(the bad guy|worthless|never enough)", re.IGNORECASE)
|
61 |
]
|
62 |
return any(re.search(p, message, flags) for p, flags in patterns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
def get_risk_stage(patterns, sentiment):
|
65 |
if "threat" in patterns or "insults" in patterns:
|
@@ -126,7 +181,13 @@ def analyze_single_message(text, thresholds):
|
|
126 |
|
127 |
contradiction_flag = detect_contradiction(text)
|
128 |
motifs = [phrase for _, phrase in matched_phrases]
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
131 |
with torch.no_grad():
|
132 |
outputs = model(**inputs)
|
@@ -171,7 +232,7 @@ def analyze_single_message(text, thresholds):
|
|
171 |
print(f"Contradiction: {contradiction_flag}")
|
172 |
print("------------------\n")
|
173 |
|
174 |
-
return abuse_score, threshold_labels, top_patterns, result, stage
|
175 |
|
176 |
def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
177 |
responses = answers_and_none[:len(ESCALATION_QUESTIONS)]
|
|
|
49 |
("Violence has increased in frequency or severity", 3),
|
50 |
("Partner monitors your calls/GPS/social media", 2)
|
51 |
]
|
52 |
+
DARVO_MOTIFS = [
|
53 |
+
"I never said that.", "You’re imagining things.", "That never happened.",
|
54 |
+
"You’re making a big deal out of nothing.", "It was just a joke.", "You’re too sensitive.",
|
55 |
+
"I don’t know what you’re talking about.", "You’re overreacting.", "I didn’t mean it that way.",
|
56 |
+
"You’re twisting my words.", "You’re remembering it wrong.", "You’re always looking for something to complain about.",
|
57 |
+
"You’re just trying to start a fight.", "I was only trying to help.", "You’re making things up.",
|
58 |
+
"You’re blowing this out of proportion.", "You’re being paranoid.", "You’re too emotional.",
|
59 |
+
"You’re always so dramatic.", "You’re just trying to make me look bad.",
|
60 |
+
|
61 |
+
"You’re crazy.", "You’re the one with the problem.", "You’re always so negative.",
|
62 |
+
"You’re just trying to control me.", "You’re the abusive one.", "You’re trying to ruin my life.",
|
63 |
+
"You’re just jealous.", "You’re the one who needs help.", "You’re always playing the victim.",
|
64 |
+
"You’re the one causing all the problems.", "You’re just trying to make me feel guilty.",
|
65 |
+
"You’re the one who can’t let go of the past.", "You’re the one who’s always angry.",
|
66 |
+
"You’re the one who’s always complaining.", "You’re the one who’s always starting arguments.",
|
67 |
+
"You’re the one who’s always making things worse.", "You’re the one who’s always making me feel bad.",
|
68 |
+
"You’re the one who’s always making me look like the bad guy.",
|
69 |
+
"You’re the one who’s always making me feel like a failure.",
|
70 |
+
"You’re the one who’s always making me feel like I’m not good enough.",
|
71 |
+
|
72 |
+
"I can’t believe you’re doing this to me.", "You’re hurting me.",
|
73 |
+
"You’re making me feel like a terrible person.", "You’re always blaming me for everything.",
|
74 |
+
"You’re the one who’s abusive.", "You’re the one who’s controlling.", "You’re the one who’s manipulative.",
|
75 |
+
"You’re the one who’s toxic.", "You’re the one who’s gaslighting me.",
|
76 |
+
"You’re the one who’s always putting me down.", "You’re the one who’s always making me feel bad.",
|
77 |
+
"You’re the one who’s always making me feel like I’m not good enough.",
|
78 |
+
"You’re the one who’s always making me feel like I’m the problem.",
|
79 |
+
"You’re the one who’s always making me feel like I’m the bad guy.",
|
80 |
+
"You’re the one who’s always making me feel like I’m the villain.",
|
81 |
+
"You’re the one who’s always making me feel like I’m the one who needs to change.",
|
82 |
+
"You’re the one who’s always making me feel like I’m the one who’s wrong.",
|
83 |
+
"You’re the one who’s always making me feel like I’m the one who’s crazy.",
|
84 |
+
"You’re the one who’s always making me feel like I’m the one who’s abusive.",
|
85 |
+
"You’re the one who’s always making me feel like I’m the one who’s toxic."
|
86 |
+
]
|
87 |
def detect_contradiction(message):
|
88 |
patterns = [
|
89 |
(r"\b(i love you).{0,15}(i hate you|you ruin everything)", re.IGNORECASE),
|
|
|
94 |
(r"\b(i guess i’m just).{0,15}(the bad guy|worthless|never enough)", re.IGNORECASE)
|
95 |
]
|
96 |
return any(re.search(p, message, flags) for p, flags in patterns)
|
97 |
+
|
98 |
+
def calculate_darvo_score(patterns, sentiment_before, sentiment_after, motifs_found, contradiction_flag=False):
|
99 |
+
pattern_hits = len([p for p in patterns if p in DARVO_PATTERNS])
|
100 |
+
pattern_score = pattern_hits / len(DARVO_PATTERNS)
|
101 |
+
|
102 |
+
sentiment_shift_score = max(0.0, sentiment_after - sentiment_before)
|
103 |
+
|
104 |
+
motif_hits = len([
|
105 |
+
motif for motif in motifs_found
|
106 |
+
if any(phrase.lower() in motif.lower() for phrase in DARVO_MOTIFS)
|
107 |
+
])
|
108 |
+
motif_score = motif_hits / len(DARVO_MOTIFS)
|
109 |
+
|
110 |
+
contradiction_score = 1.0 if contradiction_flag else 0.0
|
111 |
+
|
112 |
+
return round(min(
|
113 |
+
0.3 * pattern_score +
|
114 |
+
0.3 * sentiment_shift_score +
|
115 |
+
0.25 * motif_score +
|
116 |
+
0.15 * contradiction_score, 1.0
|
117 |
+
), 3)
|
118 |
|
119 |
def get_risk_stage(patterns, sentiment):
|
120 |
if "threat" in patterns or "insults" in patterns:
|
|
|
181 |
|
182 |
contradiction_flag = detect_contradiction(text)
|
183 |
motifs = [phrase for _, phrase in matched_phrases]
|
184 |
+
darvo_score = calculate_darvo_score(
|
185 |
+
pattern_labels,
|
186 |
+
sentiment_before=0.0,
|
187 |
+
sentiment_after=sentiment_score,
|
188 |
+
motifs_found=motifs,
|
189 |
+
contradiction_flag=contradiction_flag
|
190 |
+
)
|
191 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
192 |
with torch.no_grad():
|
193 |
outputs = model(**inputs)
|
|
|
232 |
print(f"Contradiction: {contradiction_flag}")
|
233 |
print("------------------\n")
|
234 |
|
235 |
+
return abuse_score, threshold_labels, top_patterns, result, stage, darvo_score
|
236 |
|
237 |
def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
238 |
responses = answers_and_none[:len(ESCALATION_QUESTIONS)]
|