Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -103,30 +103,32 @@ ESCALATION_QUESTIONS = [
|
|
103 |
("Partner has access to firearms or weapons", 4),
|
104 |
("Partner threatened to kill you", 3),
|
105 |
("Partner threatened you with a weapon", 3),
|
106 |
-
("Partner ever choked
|
107 |
("Partner injured or threatened your pet(s)", 3),
|
108 |
-
("Partner
|
109 |
-
("Partner forced you into unwanted sexual acts", 3),
|
110 |
("Partner threatened to take away your children", 2),
|
111 |
("Violence has increased in frequency or severity", 3),
|
112 |
("Partner monitors your calls/GPS/social media", 2)
|
113 |
]
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
# --- Composite Analysis with Escalation Quiz ---
|
119 |
def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
120 |
-
# split args: first len(ESCALATION_QUESTIONS) are checkboxes, last is none_of_above
|
121 |
responses = answers_and_none[:len(ESCALATION_QUESTIONS)]
|
122 |
none_selected = answers_and_none[-1]
|
123 |
-
|
124 |
-
# compute escalation score
|
125 |
if none_selected:
|
126 |
escalation_score = 0
|
127 |
else:
|
128 |
escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, responses) if a)
|
129 |
-
# bucket
|
130 |
if escalation_score >= 16:
|
131 |
escalation_level = "High"
|
132 |
elif escalation_score >= 8:
|
@@ -134,7 +136,6 @@ def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
|
134 |
else:
|
135 |
escalation_level = "Low"
|
136 |
|
137 |
-
# existing abuse analysis
|
138 |
thresholds = THRESHOLDS.copy()
|
139 |
messages = [msg1, msg2, msg3]
|
140 |
active = [m for m in messages if m.strip()]
|
@@ -146,21 +147,17 @@ def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
|
146 |
top_pattern = max({label for r in results for label in r[2]}, key=lambda l: abuse_scores[0])
|
147 |
composite_abuse = round(sum(abuse_scores)/len(abuse_scores),2)
|
148 |
|
149 |
-
# build output
|
150 |
out = f"Abuse Intensity: {composite_abuse}%\n"
|
151 |
out += f"Escalation Potential: {escalation_level} ({escalation_score}/{sum(w for _,w in ESCALATION_QUESTIONS)})"
|
152 |
-
# abuse snippet
|
153 |
out += generate_risk_snippet(composite_abuse, top_pattern)
|
154 |
return out
|
155 |
|
156 |
-
# --- Gradio Interface ---
|
157 |
textbox_inputs = [
|
158 |
gr.Textbox(label="Message 1"),
|
159 |
gr.Textbox(label="Message 2"),
|
160 |
gr.Textbox(label="Message 3")
|
161 |
]
|
162 |
|
163 |
-
# Escalation quiz inputs
|
164 |
quiz_boxes = [gr.Checkbox(label=q) for q, _ in ESCALATION_QUESTIONS]
|
165 |
none_box = gr.Checkbox(label="None of the above")
|
166 |
|
@@ -173,4 +170,4 @@ iface = gr.Interface(
|
|
173 |
)
|
174 |
|
175 |
if __name__ == "__main__":
|
176 |
-
iface.launch()
|
|
|
103 |
("Partner has access to firearms or weapons", 4),
|
104 |
("Partner threatened to kill you", 3),
|
105 |
("Partner threatened you with a weapon", 3),
|
106 |
+
("Partner has ever choked you, even if you considered it consensual at the time", 4),
|
107 |
("Partner injured or threatened your pet(s)", 3),
|
108 |
+
("Partner has broken your things, punched or kicked walls, or thrown things ", 2),
|
109 |
+
("Partner forced or coerced you into unwanted sexual acts", 3),
|
110 |
("Partner threatened to take away your children", 2),
|
111 |
("Violence has increased in frequency or severity", 3),
|
112 |
("Partner monitors your calls/GPS/social media", 2)
|
113 |
]
|
114 |
|
115 |
+
def analyze_single_message(text, thresholds, motif_flags):
|
116 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
117 |
+
with torch.no_grad():
|
118 |
+
outputs = model(**inputs)
|
119 |
+
scores = torch.sigmoid(outputs.logits.squeeze(0)).numpy()
|
120 |
+
threshold_labels = [label for label, score in zip(LABELS, scores) if score > thresholds[label]]
|
121 |
+
top_patterns = sorted([(label, score) for label, score in zip(LABELS, scores)], key=lambda x: x[1], reverse=True)[:2]
|
122 |
+
return (np.mean([score for _, score in top_patterns]) * 100, threshold_labels, top_patterns, 0.0, {"label": "supportive"})
|
123 |
|
124 |
# --- Composite Analysis with Escalation Quiz ---
|
125 |
def analyze_composite(msg1, msg2, msg3, *answers_and_none):
|
|
|
126 |
responses = answers_and_none[:len(ESCALATION_QUESTIONS)]
|
127 |
none_selected = answers_and_none[-1]
|
|
|
|
|
128 |
if none_selected:
|
129 |
escalation_score = 0
|
130 |
else:
|
131 |
escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, responses) if a)
|
|
|
132 |
if escalation_score >= 16:
|
133 |
escalation_level = "High"
|
134 |
elif escalation_score >= 8:
|
|
|
136 |
else:
|
137 |
escalation_level = "Low"
|
138 |
|
|
|
139 |
thresholds = THRESHOLDS.copy()
|
140 |
messages = [msg1, msg2, msg3]
|
141 |
active = [m for m in messages if m.strip()]
|
|
|
147 |
top_pattern = max({label for r in results for label in r[2]}, key=lambda l: abuse_scores[0])
|
148 |
composite_abuse = round(sum(abuse_scores)/len(abuse_scores),2)
|
149 |
|
|
|
150 |
out = f"Abuse Intensity: {composite_abuse}%\n"
|
151 |
out += f"Escalation Potential: {escalation_level} ({escalation_score}/{sum(w for _,w in ESCALATION_QUESTIONS)})"
|
|
|
152 |
out += generate_risk_snippet(composite_abuse, top_pattern)
|
153 |
return out
|
154 |
|
|
|
155 |
textbox_inputs = [
|
156 |
gr.Textbox(label="Message 1"),
|
157 |
gr.Textbox(label="Message 2"),
|
158 |
gr.Textbox(label="Message 3")
|
159 |
]
|
160 |
|
|
|
161 |
quiz_boxes = [gr.Checkbox(label=q) for q, _ in ESCALATION_QUESTIONS]
|
162 |
none_box = gr.Checkbox(label="None of the above")
|
163 |
|
|
|
170 |
)
|
171 |
|
172 |
if __name__ == "__main__":
|
173 |
+
iface.launch()
|