SamanthaStorm commited on
Commit
a0d733c
·
verified ·
1 Parent(s): f7b5f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -160
app.py CHANGED
@@ -2,31 +2,58 @@ import gradio as gr
2
  import torch
3
  import numpy as np
4
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
 
 
5
  import matplotlib.pyplot as plt
6
  import io
7
  from PIL import Image
8
  from datetime import datetime
9
 
10
- # --- Load models ---
11
- model_name = "SamanthaStorm/tether-multilabel-v2" # UPDATE if needed
12
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
- tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
14
-
15
- healthy_detector = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
16
- sst_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
17
-
18
- LABELS = [
19
- "blame shifting", "contradictory statements", "control", "dismissiveness",
20
- "gaslighting", "guilt tripping", "insults", "obscure language",
21
- "projection", "recovery phase", "threat"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  ]
23
-
24
- THRESHOLDS = {
25
- "blame shifting": 0.3, "contradictory statements": 0.3, "control": 0.35, "dismissiveness": 0.4,
26
- "gaslighting": 0.3, "guilt tripping": 0.3, "insults": 0.3, "obscure language": 0.4,
27
- "projection": 0.4, "recovery phase": 0.35, "threat": 0.3
 
 
 
 
 
 
 
 
28
  }
29
-
30
  ESCALATION_QUESTIONS = [
31
  ("Partner has access to firearms or weapons", 4),
32
  ("Partner threatened to kill you", 3),
@@ -34,155 +61,145 @@ ESCALATION_QUESTIONS = [
34
  ("Partner has ever choked you", 4),
35
  ("Partner injured or threatened your pet(s)", 3),
36
  ("Partner has broken your things, punched walls, or thrown objects", 2),
37
- ("Partner forced you into unwanted sexual acts", 3),
38
  ("Partner threatened to take away your children", 2),
39
  ("Violence has increased in frequency or severity", 3),
40
- ("Partner monitors your calls, GPS, or social media", 2)
41
  ]
42
 
43
- # --- Functions ---
44
-
45
- def is_healthy_message(text, threshold=0.9):
46
- result = healthy_detector(text)[0]
47
- return result['label'] == "POSITIVE" and result['score'] > threshold
48
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def generate_abuse_score_chart(dates, scores, labels):
50
  try:
51
- parsed_dates = [datetime.strptime(d, "%Y-%m-%d") for d in dates]
52
- except Exception:
53
- parsed_dates = list(range(len(dates)))
54
-
55
- fig, ax = plt.subplots(figsize=(8, 3))
56
- ax.plot(parsed_dates, scores, marker='o', linestyle='-', color='darkred', linewidth=2)
57
-
58
- for i, (x, y) in enumerate(zip(parsed_dates, scores)):
59
- label = labels[i]
60
- ax.text(x, y + 2, f"{label}\n{int(y)}%", ha='center', fontsize=8, color='black')
61
-
62
- ax.set_title("Abuse Intensity Over Time")
63
- ax.set_xlabel("Date")
64
- ax.set_ylabel("Abuse Score (%)")
65
- ax.set_ylim(0, 105)
66
- ax.grid(True)
67
- plt.tight_layout()
68
-
69
- buf = io.BytesIO()
70
- plt.savefig(buf, format='png')
71
- buf.seek(0)
72
  return Image.open(buf)
73
 
74
- def analyze_single_message(text):
75
- if is_healthy_message(text):
76
- return {
77
- "abuse_score": 0,
78
- "labels": [],
79
- "sentiment": "supportive",
80
- "stage": 4,
81
- "top_patterns": [],
82
- }
83
-
84
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
85
- with torch.no_grad():
86
- outputs = model(**inputs)
87
- logits = outputs.logits.squeeze(0)
88
- probs = torch.sigmoid(logits).numpy()
89
-
90
- detected_labels = [
91
- label for label, prob in zip(LABELS, probs)
92
- if prob > THRESHOLDS.get(label, 0.3)
93
- ]
94
-
95
- abuse_score = (sum(probs[i] for i, label in enumerate(LABELS) if label in detected_labels) / len(LABELS)) * 100
96
-
97
- sentiment_result = sst_pipeline(text)[0]
98
- sentiment = "supportive" if sentiment_result['label'] == "POSITIVE" else "undermining"
99
-
100
- if "threat" in detected_labels or "insults" in detected_labels:
101
- stage = 2 # Escalation
102
- elif "control" in detected_labels or "guilt tripping" in detected_labels:
103
- stage = 1 # Tension building
104
- elif "recovery phase" in detected_labels:
105
- stage = 3 # Reconciliation
106
- else:
107
- stage = 1
108
-
109
- top_patterns = sorted(
110
- [(label, prob) for label, prob in zip(LABELS, probs)],
111
- key=lambda x: x[1],
112
- reverse=True
113
- )[:2]
114
-
115
- return {
116
- "abuse_score": int(abuse_score),
117
- "labels": detected_labels,
118
- "sentiment": sentiment,
119
- "stage": stage,
120
- "top_patterns": top_patterns,
121
- }
122
-
123
- def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
124
- none_selected_checked = answers_and_none[-1]
125
- responses_checked = any(answers_and_none[:-1])
126
- none_selected = not responses_checked and none_selected_checked
127
-
128
- if none_selected:
129
- escalation_score = None
130
- risk_level = "unknown"
131
- else:
132
- escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, answers_and_none[:-1]) if a)
133
- risk_level = (
134
- "High" if escalation_score >= 16 else
135
- "Moderate" if escalation_score >= 8 else
136
- "Low"
137
- )
138
-
139
- messages = [msg1, msg2, msg3]
140
- dates = [date1, date2, date3]
141
- active = [(m, d) for m, d in zip(messages, dates) if m.strip()]
142
- if not active:
143
- return "Please enter at least one message."
144
-
145
- results = [(analyze_single_message(m), d) for m, d in active]
146
- abuse_scores = [r[0]["abuse_score"] for r in results]
147
- top_labels = [r[0]["top_patterns"][0][0] if r[0]["top_patterns"] else "None" for r in results]
148
- dates_used = [r[1] or "Undated" for r in results]
149
- stage_list = [r[0]["stage"] for r in results]
150
-
151
- most_common_stage = max(set(stage_list), key=stage_list.count)
152
- # compute the average abuse score across all active messages
153
- composite_abuse = int(round(sum(abuse_scores) / len(abuse_scores)))
154
- out = f"Abuse Intensity: {composite_abuse}%\n"
155
- if escalation_score is None:
156
- out += "Escalation Potential: Unknown (Checklist not completed)\n"
157
- else:
158
- out += f"Escalation Potential: {risk_level} ({escalation_score}/{sum(w for _, w in ESCALATION_QUESTIONS)})\n"
159
-
160
- timeline_image = generate_abuse_score_chart(dates_used, abuse_scores, top_labels)
161
- return out, timeline_image
162
-
163
- # --- Gradio Interface ---
164
-
165
- message_date_pairs = [
166
- (
167
- gr.Textbox(label=f"Message {i+1}"),
168
- gr.Textbox(label=f"Date {i+1} (optional)", placeholder="YYYY-MM-DD")
169
- )
170
- for i in range(3)
171
  ]
172
- textbox_inputs = [item for pair in message_date_pairs for item in pair]
173
- quiz_boxes = [gr.Checkbox(label=q) for q, _ in ESCALATION_QUESTIONS]
174
- none_box = gr.Checkbox(label="None of the above")
175
-
176
- iface = gr.Interface(
177
- fn=analyze_composite,
178
- inputs=textbox_inputs + quiz_boxes + [none_box],
179
- outputs=[
180
- gr.Textbox(label="Results"),
181
- gr.Image(label="Risk Stage Timeline", type="pil")
182
- ],
183
- title="Tether Abuse Pattern Detector v2",
184
- allow_flagging="manual"
185
- )
186
 
187
- if __name__ == "__main__":
188
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
3
  import numpy as np
4
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
5
+ from motif_tagging import detect_motifs
6
+ import re
7
  import matplotlib.pyplot as plt
8
  import io
9
  from PIL import Image
10
  from datetime import datetime
11
 
12
+ # ——— DARVO & Risk Utilities ———
13
+ DARVO_PATTERNS = {"blame shifting", "projection", "dismissiveness", "guilt tripping", "contradictory statements"}
14
+ DARVO_MOTIFS = [
15
+ "I never said that.", "You’re imagining things.", "That never happened.",
16
+ "You’re making a big deal out of nothing.", "It was just a joke.", "You’re too sensitive.",
17
+ "I don’t know what you’re talking about.", "You’re overreacting.", "I didn’t mean it that way.",
18
+ "You’re twisting my words.", "You’re remembering it wrong.", "You’re always looking for something to complain about.",
19
+ "You’re just trying to start a fight.", "I was only trying to help.", "You’re making things up.",
20
+ "You’re blowing this out of proportion.", "You’re being paranoid.", "You’re too emotional.",
21
+ "You’re always so dramatic.", "You’re just trying to make me look bad.",
22
+ "You’re crazy.", "You’re the one with the problem.", "You’re always so negative.",
23
+ "You’re just trying to control me.", "You’re the abusive one.", "You’re trying to ruin my life.",
24
+ "You’re just jealous.", "You’re the one who needs help.", "You’re always playing the victim.",
25
+ "You’re the one causing all the problems.", "You’re just trying to make me feel guilty.",
26
+ "You’re the one who can’t let go of the past.", "You’re the one who’s always angry.",
27
+ "You’re the one who’s always complaining.", "You’re the one who’s always starting arguments.",
28
+ "You’re the one who’s always making things worse.", "You’re the one who’s always making me feel bad.",
29
+ "You’re the one who’s always making me look like the bad guy.",
30
+ "You’re the one who’s always making me feel like a failure.", "You’re the one who’s always making me feel like I’m not good enough.",
31
+ "I can’t believe you’re doing this to me.", "You’re hurting me.", "You’re making me feel like a terrible person.",
32
+ "You’re always blaming me for everything.", "You’re the one who’s abusive.", "You’re the one who’s controlling.",
33
+ "You’re the one who’s manipulative.", "You’re the one who’s toxic.", "You’re the one who’s gaslighting me.",
34
+ "You’re the one who’s always putting me down.", "You’re the one who’s always making me feel bad.",
35
+ "You’re the one who’s always making me feel like I’m not good enough.", "You’re the one who’s always making me feel like I’m the problem.",
36
+ "You’re the one who’s always making me feel like I’m the bad guy.", "You’re the one who’s always making me feel like I’m the villain.",
37
+ "You’re the one who’s always making me feel like I’m the one who needs to change.",
38
+ "You’re the one who’s always making me feel like I’m the one who’s wrong.",
39
+ "You’re the one who’s always making me feel like I’m the one who’s crazy.",
40
+ "You’re the one who’s always making me feel like I’m the one who’s abusive.",
41
+ "You’re the one who’s always making me feel like I’m the one who’s toxic."
42
  ]
43
+ PATTERN_WEIGHTS = {
44
+ "gaslighting": 1.3,
45
+ "control": 1.2,
46
+ "dismissiveness": 0.8,
47
+ "blame shifting": 0.8,
48
+ "contradictory statements": 0.75,
49
+ "threat": 1.5,
50
+ }
51
+ RISK_STAGE_LABELS = {
52
+ 1: "🌀 Risk Stage: Tension-Building\nThis message reflects rising emotional pressure or subtle control attempts.",
53
+ 2: "🔥 Risk Stage: Escalation\nThis message includes direct or aggressive patterns, suggesting active harm.",
54
+ 3: "🌧️ Risk Stage: Reconciliation\nThis message reflects a reset attempt—apologies or emotional repair without accountability.",
55
+ 4: "🌸 Risk Stage: Calm / Honeymoon\nThis message appears supportive but may follow prior harm, minimizing it."
56
  }
 
57
  ESCALATION_QUESTIONS = [
58
  ("Partner has access to firearms or weapons", 4),
59
  ("Partner threatened to kill you", 3),
 
61
  ("Partner has ever choked you", 4),
62
  ("Partner injured or threatened your pet(s)", 3),
63
  ("Partner has broken your things, punched walls, or thrown objects", 2),
64
+ ("Partner forced or coerced you into unwanted sexual acts", 3),
65
  ("Partner threatened to take away your children", 2),
66
  ("Violence has increased in frequency or severity", 3),
67
+ ("Partner monitors your calls/GPS/social media", 2)
68
  ]
69
 
70
+ def detect_contradiction(message):
71
+ patterns = [
72
+ (r"\b(i love you).{0,15}(i hate you|you ruin everything)", re.IGNORECASE),
73
+ (r"\b(i’m sorry).{0,15}(but you|if you hadn’t)", re.IGNORECASE),
74
+ # ... other patterns ...
75
+ ]
76
+ return any(re.search(pat, message, flags) for pat, flags in patterns)
77
+
78
+ def calculate_darvo_score(patterns, sentiment_before, sentiment_after, motifs_found, contradiction_flag=False):
79
+ hits = len([p for p in patterns if p in DARVO_PATTERNS])
80
+ p_score = hits / len(DARVO_PATTERNS)
81
+ s_shift = max(0.0, sentiment_after - sentiment_before)
82
+ m_hits = len([m for m in motifs_found if any(f.lower() in m.lower() for f in DARVO_MOTIFS)])
83
+ m_score = m_hits / len(DARVO_MOTIFS)
84
+ c_score = 1.0 if contradiction_flag else 0.0
85
+ raw = 0.3*p_score + 0.3*s_shift + 0.25*m_score + 0.15*c_score
86
+ return round(min(raw,1.0),3)
87
+
88
+ def generate_risk_snippet(abuse_score, top_label, escalation_score, stage):
89
+ label = top_label.split(" – ")[0]
90
+ why = {
91
+ "control": "This message may reflect efforts to restrict someone’s autonomy.",
92
+ "gaslighting": "This message could be manipulating perception.",
93
+ # ... other explanations ...
94
+ }.get(label, "This message contains language patterns that may affect safety.")
95
+ if abuse_score>=85 or escalation_score>=16:
96
+ lvl="high"
97
+ elif abuse_score>=60 or escalation_score>=8:
98
+ lvl="moderate"
99
+ else:
100
+ lvl="low"
101
+ return f"\n\n🛑 Risk Level: {lvl.capitalize()}\nThis message shows **{label}**.\n💡 Why: {why}\n"
102
+
103
+ def detect_weapon_language(text):
104
+ kws=["knife","gun","bomb","kill you","shoot","explode"]
105
+ t=text.lower()
106
+ return any(k in t for k in kws)
107
+
108
+ def get_risk_stage(patterns, sentiment):
109
+ if "threat" in patterns or "insults" in patterns: return 2
110
+ if "control" in patterns or "guilt tripping" in patterns: return 1
111
+ if "recovery phase" in patterns: return 3
112
+ if sentiment=="supportive" and any(p in patterns for p in ["projection","dismissiveness"]): return 4
113
+ return 1
114
+
115
+ # --- Timeline Visualization ---
116
  def generate_abuse_score_chart(dates, scores, labels):
117
  try:
118
+ parsed=[datetime.strptime(d,"%Y-%m-%d") for d in dates]
119
+ except:
120
+ parsed=list(range(len(dates)))
121
+ fig,ax=plt.subplots(figsize=(8,3))
122
+ ax.plot(parsed,scores,marker='o',linestyle='-',color='darkred',linewidth=2)
123
+ for i,(x,y) in enumerate(zip(parsed,scores)):
124
+ ax.text(x,y+2,f"{labels[i]}\n{int(y)}%",ha='center',fontsize=8)
125
+ ax.set(title="Abuse Intensity Over Time",xlabel="Date",ylabel="Abuse Score (%)")
126
+ ax.set_ylim(0,105);ax.grid(True);plt.tight_layout()
127
+ buf=io.BytesIO();plt.savefig(buf,format='png');buf.seek(0)
 
 
 
 
 
 
 
 
 
 
 
128
  return Image.open(buf)
129
 
130
+ # --- Load and initialize models ---
131
+ model_name="SamanthaStorm/tether-multilabel-v2"
132
+ model=AutoModelForSequenceClassification.from_pretrained(model_name)
133
+ tokenizer=AutoTokenizer.from_pretrained(model_name, use_fast=False)
134
+ healthy_detector=pipeline("text-classification",model="distilbert-base-uncased-finetuned-sst-2-english")
135
+ sst_pipeline=pipeline("sentiment-analysis",model="distilbert-base-uncased-finetuned-sst-2-english")
136
+
137
+ LABELS=[
138
+ "blame shifting","contradictory statements","control","dismissiveness",
139
+ "gaslighting","guilt tripping","insults","obscure language",
140
+ "projection","recovery phase","threat"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  ]
142
+ THRESHOLDS={l:THRESHOLDS.get(l,0.3) for l in LABELS}
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
+ # --- Single Message Analysis ---
145
+ def analyze_single_message(text):
146
+ if healthy_detector(text)[0]['label']=="POSITIVE" and healthy_detector(text)[0]['score']>0.9:
147
+ return {"abuse_score":0,"labels":[],"sentiment":"supportive","stage":4,"darvo_score":0.0,"top_patterns":[]}
148
+ inputs=tokenizer(text,return_tensors='pt',padding=True,truncation=True)
149
+ with torch.no_grad(): outputs=model(**inputs).logits.squeeze(0)
150
+ probs=torch.sigmoid(outputs).numpy()
151
+ labels=[lab for lab,p in zip(LABELS,probs) if p>THRESHOLDS[lab]]
152
+ # weighted score
153
+ total_w=sum(PATTERN_WEIGHTS.get(l,1.0) for l in LABELS)
154
+ abuse_score=int(round(sum(probs[i]*PATTERN_WEIGHTS.get(l,1.0) for i,l in enumerate(LABELS))/total_w*100))
155
+ # sentiment
156
+ sst=sst_pipeline(text)[0]
157
+ sentiment='supportive' if sst['label']=='POSITIVE' else 'undermining'
158
+ sent_score=sst['score'] if sentiment=='undermining' else 0.0
159
+ # DARVO
160
+ motifs,matched=detect_motifs(text)
161
+ contradiction=detect_contradiction(text)
162
+ darvo=calculate_darvo_score(labels,0.0,sent_score,matched,contradiction)
163
+ # stage + weapon
164
+ stage=get_risk_stage(labels,sentiment)
165
+ if detect_weapon_language(text): abuse_score=min(abuse_score+25,100); stage=max(stage,2)
166
+ # top patterns
167
+ top_patterns=sorted(zip(LABELS,probs),key=lambda x:x[1],reverse=True)[:2]
168
+ return {"abuse_score":abuse_score,"labels":labels,"sentiment":sentiment,
169
+ "stage":stage,"darvo_score":darvo,"top_patterns":top_patterns}
170
+
171
+ # --- Composite Analysis ---
172
+ def analyze_composite(m1,d1,m2,d2,m3,d3,*answers):
173
+ none_sel=answers[-1] and not any(answers[:-1])
174
+ if none_sel: esc=None; risk='unknown'
175
+ else:
176
+ esc=sum(w for (_,w),a in zip(ESCALATION_QUESTIONS,answers[:-1]) if a)
177
+ risk='High' if esc>=16 else 'Moderate' if esc>=8 else 'Low'
178
+ msgs=[m1,m2,m3]; dates=[d1,d2,d3]
179
+ active=[(m,d) for m,d in zip(msgs,dates) if m.strip()]
180
+ if not active: return "Please enter at least one message."
181
+ results=[(analyze_single_message(m),d) for m,d in active]
182
+ abuse_scores=[r[0]['abuse_score'] for r in results]
183
+ top_lbls=[r[0]['top_patterns'][0][0] if r[0]['top_patterns'] else 'None' for r in results]
184
+ dates_used=[d or 'Undated' for (_,d) in results]
185
+ stage_list=[r[0]['stage'] for r,_ in results]
186
+ most_common_stage=max(set(stage_list),key=stage_list.count)
187
+ composite_abuse=int(round(sum(abuse_scores)/len(abuse_scores)))
188
+ out=f"Abuse Intensity: {composite_abuse}%\n"
189
+ if esc is None: out+="Escalation Potential: Unknown (Checklist not completed)\n"
190
+ else: out+=f"Escalation Potential: {risk} ({esc}/{sum(w for _,w in ESCALATION_QUESTIONS)})\n"
191
+ # DARVO summary
192
+ darvos=[r[0]['darvo_score'] for r,_ in results]
193
+ avg_d=sum(darvos)/len(darvos)
194
+ if avg_d>0.25: lvl='moderate' if avg_d<0.65 else 'high'; out+=f"\n🎭 DARVO Score: {round(avg_d,3)} ({lvl})\n"
195
+ out+=generate_risk_snippet(composite_abuse,f"{top_lbls[0]} – {int(top_patterns[0][1]*100)}%",esc or 0,most_common_stage)
196
+ img=generate_abuse_score_chart(dates_used,abuse_scores,top_lbls)
197
+ return out,img
198
+
199
+ # --- UI ---
200
+ message_date_pairs=[(gr.Textbox(label=f"Message {i+1}"),gr.Textbox(label=f"Date {i+1} (optional)",placeholder="YYYY-MM-DD")) for i in range(3)]
201
+ quiz_boxes=[gr.Checkbox(label=q) for q,_ in ESCALATION_QUESTIONS]; none_box=gr.Checkbox(label="None of the above")
202
+ iface=gr.Interface(fn=analyze_composite,inputs=[item for pair in message_date_pairs for item in pair]+quiz_boxes+[none_box],outputs=[gr.Textbox(label="Results"),gr.Image(label="Risk Stage Timeline",type="pil")],
203
+ title="Tether Abuse Pattern Detector v2",allow_flagging="manual")
204
+
205
+ if __name__=="__main__": iface.launch()