Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -394,11 +394,27 @@ def analyze_single_message(text, thresholds):
|
|
394 |
key=lambda x: x[1],
|
395 |
reverse=True
|
396 |
)[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
]
|
403 |
|
404 |
abuse_score_raw = compute_abuse_score(matched_scores, sentiment)
|
@@ -434,7 +450,7 @@ def analyze_single_message(text, thresholds):
|
|
434 |
print(f"Contradiction: {contradiction_flag}")
|
435 |
print("------------------\n")
|
436 |
|
437 |
-
return abuse_score, threshold_labels, top_patterns, {"label": sentiment}, stage, darvo_score
|
438 |
|
439 |
def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
440 |
none_selected_checked = answers_and_none[-1]
|
@@ -459,10 +475,10 @@ def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
|
459 |
return "Please enter at least one message."
|
460 |
|
461 |
results = [(analyze_single_message(m, THRESHOLDS.copy()), d) for m, d in active]
|
|
|
462 |
for result, date in results:
|
463 |
assert len(result) == 6, "Unexpected output from analyze_single_message"
|
464 |
abuse_scores = [r[0][0] for r in results]
|
465 |
-
top_labels = [r[0][1][0] if r[0][1] else r[0][2][0][0] for r in results]
|
466 |
top_scores = [r[0][2][0][1] for r in results]
|
467 |
sentiments = [r[0][3]['label'] for r in results]
|
468 |
stages = [r[0][4] for r in results]
|
|
|
394 |
key=lambda x: x[1],
|
395 |
reverse=True
|
396 |
)[:2]
|
397 |
+
# Escalation-based top label override
|
398 |
+
ESCALATION_HIERARCHY = [
|
399 |
+
"threat", "insults", "control", "blame shifting", "gaslighting",
|
400 |
+
"guilt tripping", "projection", "dismissiveness", "contradictory statements",
|
401 |
+
"recovery phase", "obscure language"
|
402 |
+
]
|
403 |
|
404 |
+
label_scores = {label: score for label, score in zip(LABELS, scores)}
|
405 |
+
passed = {label: score for label in threshold_labels if label in label_scores}
|
406 |
+
|
407 |
+
if passed:
|
408 |
+
top_score = max(passed.values())
|
409 |
+
close_matches = {label: score for label, score in passed.items() if (top_score - score) <= 0.05}
|
410 |
+
sorted_close = sorted(close_matches.items(), key=lambda x: ESCALATION_HIERARCHY.index(x[0]))
|
411 |
+
top_pattern_label, top_pattern_score = sorted_close[0]
|
412 |
+
else:
|
413 |
+
top_pattern_label, top_pattern_score = top_patterns[0]
|
414 |
+
matched_scores = [
|
415 |
+
(label, score, PATTERN_WEIGHTS.get(label, 1.0))
|
416 |
+
for label, score in zip(LABELS, scores)
|
417 |
+
if score > adjusted_thresholds[label]
|
418 |
]
|
419 |
|
420 |
abuse_score_raw = compute_abuse_score(matched_scores, sentiment)
|
|
|
450 |
print(f"Contradiction: {contradiction_flag}")
|
451 |
print("------------------\n")
|
452 |
|
453 |
+
return abuse_score, threshold_labels, top_patterns, {"label": sentiment}, stage, darvo_score, top_pattern_label
|
454 |
|
455 |
def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
456 |
none_selected_checked = answers_and_none[-1]
|
|
|
475 |
return "Please enter at least one message."
|
476 |
|
477 |
results = [(analyze_single_message(m, THRESHOLDS.copy()), d) for m, d in active]
|
478 |
+
top_labels = [r[0][6] for r in results]
|
479 |
for result, date in results:
|
480 |
assert len(result) == 6, "Unexpected output from analyze_single_message"
|
481 |
abuse_scores = [r[0][0] for r in results]
|
|
|
482 |
top_scores = [r[0][2][0][1] for r in results]
|
483 |
sentiments = [r[0][3]['label'] for r in results]
|
484 |
stages = [r[0][4] for r in results]
|