Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -69,22 +69,12 @@ def calculate_abuse_level(scores, thresholds, motif_hits=None):
|
|
69 |
]
|
70 |
base_score = round(np.mean(triggered_scores) * 100, 2) if triggered_scores else 0.0
|
71 |
|
72 |
-
# Boost score if high-risk motifs were detected
|
73 |
motif_hits = motif_hits or []
|
74 |
if any(label in motif_hits for label in {"physical_threat", "suicidal_threat", "extreme_control"}):
|
75 |
-
base_score = max(base_score, 75.0)
|
76 |
|
77 |
return base_score
|
78 |
|
79 |
-
# Boost score if high-risk motifs were detected
|
80 |
-
motif_hits = motif_hits or []
|
81 |
-
if any(label in motif_hits for label in {"physical_threat", "suicidal_threat", "extreme_control"}):
|
82 |
-
base_score = max(base_score, 75.0) # Push to "Severe / Harmful Pattern Present"
|
83 |
-
if matched_phrases:
|
84 |
-
pattern_labels_used.extend([label for label, _ in matched_phrases])
|
85 |
-
abuse_level = calculate_abuse_level(scores, adjusted_thresholds, motif_hits=[label for label, _ in matched_phrases])
|
86 |
-
return base_score
|
87 |
-
|
88 |
def interpret_abuse_level(score):
|
89 |
if score > 80:
|
90 |
return "Extreme / High Risk"
|
@@ -115,9 +105,17 @@ def analyze_messages(input_text, risk_flags):
|
|
115 |
outputs = model(**inputs)
|
116 |
scores = torch.sigmoid(outputs.logits.squeeze(0)).numpy()
|
117 |
|
118 |
-
|
119 |
-
|
|
|
|
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
contextual_flags = risk_flags if risk_flags else []
|
122 |
if len(contextual_flags) >= 2:
|
123 |
danger_flag_count += 1
|
@@ -132,24 +130,9 @@ def analyze_messages(input_text, risk_flags):
|
|
132 |
return "This message is classified as non-abusive."
|
133 |
|
134 |
scored_patterns = [
|
135 |
-
(label, score) for label, score in zip(PATTERN_LABELS, scores[:15]) if label != "non_abusive"
|
136 |
-
|
137 |
-
pattern_labels_used = list(set(
|
138 |
-
[label for label, score in scored_patterns if score > adjusted_thresholds[label]] +
|
139 |
-
[label for label, _ in matched_phrases]
|
140 |
-
))
|
141 |
-
|
142 |
-
abuse_level = calculate_abuse_level(scores, adjusted_thresholds, motif_hits=[label for label, _ in matched_phrases])
|
143 |
-
abuse_description = interpret_abuse_level(abuse_level)
|
144 |
-
|
145 |
-
abuse_type, abuser_profile, advice = determine_abuse_type(pattern_labels_used)
|
146 |
-
|
147 |
-
if danger_flag_count >= 2:
|
148 |
-
resources = "Immediate assistance recommended. Please seek professional help or contact emergency services."
|
149 |
-
else:
|
150 |
-
resources = "For more information on abuse patterns, consider reaching out to support groups or professional counselors."
|
151 |
|
152 |
-
# Override top patterns if a high-risk motif was detected
|
153 |
override_labels = {"physical_threat", "suicidal_threat", "extreme_control"}
|
154 |
override_matches = [label for label, _ in matched_phrases if label in override_labels]
|
155 |
|
@@ -161,33 +144,17 @@ def analyze_messages(input_text, risk_flags):
|
|
161 |
top_pattern_explanations = "\n".join([
|
162 |
f"• {label.replace('_', ' ').title()}: {EXPLANATIONS.get(label, 'No explanation available.')}"
|
163 |
for label, _ in top_patterns
|
164 |
-
])
|
165 |
|
166 |
-
|
167 |
|
|
|
168 |
if abuse_level >= 15:
|
169 |
result += f"Most Likely Patterns:\n{top_pattern_explanations}\n\n"
|
170 |
-
|
171 |
result += f"⚠️ Critical Danger Flags Detected: {danger_flag_count} of 3\n"
|
172 |
result += f"Resources: {resources}\n"
|
173 |
result += f"🧠 Sentiment: {sentiment_label.title()} (Confidence: {sentiment_score*100:.2f}%)\n"
|
174 |
|
175 |
-
# Always include these if available:
|
176 |
-
if contextual_flags:
|
177 |
-
result += "\n\n⚠️ You indicated the following:\n" + "\n".join([f"• {flag.replace('_', ' ').title()}" for flag in contextual_flags])
|
178 |
-
|
179 |
-
if high_risk_context:
|
180 |
-
result += "\n\n🚨 These responses suggest a high-risk situation. Consider seeking immediate help or safety planning resources."
|
181 |
-
|
182 |
-
if matched_phrases:
|
183 |
-
result += "\n\n🚨 Detected High-Risk Phrases:\n"
|
184 |
-
for label, phrase in matched_phrases:
|
185 |
-
phrase_clean = phrase.replace('"', "'").strip()
|
186 |
-
result += f"• {label.replace('_', ' ').title()}: “{phrase_clean}”\n"
|
187 |
-
|
188 |
-
# Always show abuse type if available
|
189 |
-
|
190 |
-
# THEN immediately follow with this:
|
191 |
if contextual_flags:
|
192 |
result += "\n\n⚠️ You indicated the following:\n" + "\n".join([f"• {flag.replace('_', ' ').title()}" for flag in contextual_flags])
|
193 |
|
@@ -222,4 +189,4 @@ iface = gr.Interface(
|
|
222 |
)
|
223 |
|
224 |
if __name__ == "__main__":
|
225 |
-
iface.queue().launch()
|
|
|
69 |
]
|
70 |
base_score = round(np.mean(triggered_scores) * 100, 2) if triggered_scores else 0.0
|
71 |
|
|
|
72 |
motif_hits = motif_hits or []
|
73 |
if any(label in motif_hits for label in {"physical_threat", "suicidal_threat", "extreme_control"}):
|
74 |
+
base_score = max(base_score, 75.0)
|
75 |
|
76 |
return base_score
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
def interpret_abuse_level(score):
|
79 |
if score > 80:
|
80 |
return "Extreme / High Risk"
|
|
|
105 |
outputs = model(**inputs)
|
106 |
scores = torch.sigmoid(outputs.logits.squeeze(0)).numpy()
|
107 |
|
108 |
+
pattern_labels_used = list(set(
|
109 |
+
[label for label, score in zip(PATTERN_LABELS, scores[:15]) if score > adjusted_thresholds[label]] +
|
110 |
+
[label for label, _ in matched_phrases]
|
111 |
+
))
|
112 |
|
113 |
+
abuse_level = calculate_abuse_level(scores, adjusted_thresholds, motif_hits=[label for label, _ in matched_phrases])
|
114 |
+
abuse_description = interpret_abuse_level(abuse_level)
|
115 |
+
|
116 |
+
abuse_type, abuser_profile, advice = determine_abuse_type(pattern_labels_used)
|
117 |
+
|
118 |
+
danger_flag_count = sum(score > adjusted_thresholds[label] for label, score in zip(DANGER_LABELS, scores[15:18]))
|
119 |
contextual_flags = risk_flags if risk_flags else []
|
120 |
if len(contextual_flags) >= 2:
|
121 |
danger_flag_count += 1
|
|
|
130 |
return "This message is classified as non-abusive."
|
131 |
|
132 |
scored_patterns = [
|
133 |
+
(label, score) for label, score in zip(PATTERN_LABELS, scores[:15]) if label != "non_abusive"
|
134 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
|
|
136 |
override_labels = {"physical_threat", "suicidal_threat", "extreme_control"}
|
137 |
override_matches = [label for label, _ in matched_phrases if label in override_labels]
|
138 |
|
|
|
144 |
top_pattern_explanations = "\n".join([
|
145 |
f"• {label.replace('_', ' ').title()}: {EXPLANATIONS.get(label, 'No explanation available.')}"
|
146 |
for label, _ in top_patterns
|
147 |
+
])
|
148 |
|
149 |
+
resources = "Immediate assistance recommended. Please seek professional help or contact emergency services." if danger_flag_count >= 2 else "For more information on abuse patterns, consider reaching out to support groups or professional counselors."
|
150 |
|
151 |
+
result = f"Abuse Risk Score: {abuse_level}% – {abuse_description}\n\n"
|
152 |
if abuse_level >= 15:
|
153 |
result += f"Most Likely Patterns:\n{top_pattern_explanations}\n\n"
|
|
|
154 |
result += f"⚠️ Critical Danger Flags Detected: {danger_flag_count} of 3\n"
|
155 |
result += f"Resources: {resources}\n"
|
156 |
result += f"🧠 Sentiment: {sentiment_label.title()} (Confidence: {sentiment_score*100:.2f}%)\n"
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
if contextual_flags:
|
159 |
result += "\n\n⚠️ You indicated the following:\n" + "\n".join([f"• {flag.replace('_', ' ').title()}" for flag in contextual_flags])
|
160 |
|
|
|
189 |
)
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
+
iface.queue().launch()
|