Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -52,8 +52,7 @@ def generate_abuse_score_chart(dates, scores, labels):
|
|
52 |
plt.savefig(buf, format='png')
|
53 |
buf.seek(0)
|
54 |
return Image.open(buf)
|
55 |
-
|
56 |
-
sst_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
57 |
|
58 |
# --- Abuse Model ---
|
59 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
@@ -275,9 +274,12 @@ def compute_abuse_score(matched_scores, sentiment):
|
|
275 |
return min(adjusted_score, 100)
|
276 |
def analyze_single_message(text, thresholds):
|
277 |
motif_hits, matched_phrases = detect_motifs(text)
|
278 |
-
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
281 |
weapon_flag = detect_weapon_language(text)
|
282 |
|
283 |
adjusted_thresholds = {
|
@@ -307,14 +309,12 @@ def analyze_single_message(text, thresholds):
|
|
307 |
contradiction_flag=contradiction_flag
|
308 |
)
|
309 |
|
310 |
-
# Top 2 patterns for display (raw scores)
|
311 |
top_patterns = sorted(
|
312 |
[(label, score) for label, score in zip(LABELS, scores)],
|
313 |
key=lambda x: x[1],
|
314 |
reverse=True
|
315 |
)[:2]
|
316 |
|
317 |
-
# β
Final abuse score: only use patterns that passed threshold
|
318 |
matched_scores = [
|
319 |
(label, score, PATTERN_WEIGHTS.get(label, 1.0))
|
320 |
for label, score in zip(LABELS, scores)
|
@@ -324,7 +324,6 @@ def analyze_single_message(text, thresholds):
|
|
324 |
abuse_score_raw = compute_abuse_score(matched_scores, sentiment)
|
325 |
abuse_score = abuse_score_raw
|
326 |
|
327 |
-
# β
Always assign stage
|
328 |
stage = get_risk_stage(threshold_labels, sentiment) if threshold_labels else 1
|
329 |
if weapon_flag and stage < 2:
|
330 |
stage = 2
|
@@ -333,14 +332,14 @@ def analyze_single_message(text, thresholds):
|
|
333 |
abuse_score_raw = min(abuse_score_raw + 25, 100)
|
334 |
|
335 |
abuse_score = min(abuse_score_raw, 100 if "threat" in threshold_labels or "control" in threshold_labels else 95)
|
336 |
-
|
337 |
-
|
338 |
print("Emotion Profile:")
|
339 |
for emotion, score in emotion_profile.items():
|
340 |
print(f" {emotion.capitalize():10}: {score}")
|
341 |
print("\n--- Debug Info ---")
|
342 |
print(f"Text: {text}")
|
343 |
-
print(f"Sentiment: {sentiment} (
|
344 |
print("Abuse Pattern Scores:")
|
345 |
for label, score in zip(LABELS, scores):
|
346 |
passed = "β
" if score > adjusted_thresholds[label] else "β"
|
@@ -351,7 +350,7 @@ def analyze_single_message(text, thresholds):
|
|
351 |
print(f"Contradiction: {contradiction_flag}")
|
352 |
print("------------------\n")
|
353 |
|
354 |
-
return abuse_score, threshold_labels, top_patterns,
|
355 |
|
356 |
def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
357 |
none_selected_checked = answers_and_none[-1]
|
|
|
52 |
plt.savefig(buf, format='png')
|
53 |
buf.seek(0)
|
54 |
return Image.open(buf)
|
55 |
+
|
|
|
56 |
|
57 |
# --- Abuse Model ---
|
58 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
274 |
return min(adjusted_score, 100)
|
275 |
def analyze_single_message(text, thresholds):
|
276 |
motif_hits, matched_phrases = detect_motifs(text)
|
277 |
+
emotion_profile = get_emotion_profile(text)
|
278 |
+
|
279 |
+
# Derive sentiment from emotion intensity
|
280 |
+
sentiment_score = emotion_profile.get("anger", 0) + emotion_profile.get("disgust", 0)
|
281 |
+
sentiment = "undermining" if sentiment_score > 0.5 else "supportive"
|
282 |
+
|
283 |
weapon_flag = detect_weapon_language(text)
|
284 |
|
285 |
adjusted_thresholds = {
|
|
|
309 |
contradiction_flag=contradiction_flag
|
310 |
)
|
311 |
|
|
|
312 |
top_patterns = sorted(
|
313 |
[(label, score) for label, score in zip(LABELS, scores)],
|
314 |
key=lambda x: x[1],
|
315 |
reverse=True
|
316 |
)[:2]
|
317 |
|
|
|
318 |
matched_scores = [
|
319 |
(label, score, PATTERN_WEIGHTS.get(label, 1.0))
|
320 |
for label, score in zip(LABELS, scores)
|
|
|
324 |
abuse_score_raw = compute_abuse_score(matched_scores, sentiment)
|
325 |
abuse_score = abuse_score_raw
|
326 |
|
|
|
327 |
stage = get_risk_stage(threshold_labels, sentiment) if threshold_labels else 1
|
328 |
if weapon_flag and stage < 2:
|
329 |
stage = 2
|
|
|
332 |
abuse_score_raw = min(abuse_score_raw + 25, 100)
|
333 |
|
334 |
abuse_score = min(abuse_score_raw, 100 if "threat" in threshold_labels or "control" in threshold_labels else 95)
|
335 |
+
|
336 |
+
# Debug logging
|
337 |
print("Emotion Profile:")
|
338 |
for emotion, score in emotion_profile.items():
|
339 |
print(f" {emotion.capitalize():10}: {score}")
|
340 |
print("\n--- Debug Info ---")
|
341 |
print(f"Text: {text}")
|
342 |
+
print(f"Sentiment (via emotion): {sentiment} (score: {round(sentiment_score, 3)})")
|
343 |
print("Abuse Pattern Scores:")
|
344 |
for label, score in zip(LABELS, scores):
|
345 |
passed = "β
" if score > adjusted_thresholds[label] else "β"
|
|
|
350 |
print(f"Contradiction: {contradiction_flag}")
|
351 |
print("------------------\n")
|
352 |
|
353 |
+
return abuse_score, threshold_labels, top_patterns, {"label": sentiment}, stage, darvo_score
|
354 |
|
355 |
def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
356 |
none_selected_checked = answers_and_none[-1]
|