Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -532,34 +532,20 @@ def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
|
532 |
risk_level = "unknown"
|
533 |
else:
|
534 |
escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, answers_and_none[:-1]) if a)
|
535 |
-
escalation_bump = 0 # Will be computed later
|
536 |
-
hybrid_score = escalation_score + escalation_bump
|
537 |
-
risk_level = (
|
538 |
-
"High" if hybrid_score >= 16 else
|
539 |
-
"Moderate" if hybrid_score >= 8 else
|
540 |
-
"Low"
|
541 |
-
)
|
542 |
-
|
543 |
-
# Now it's safe to assign these:
|
544 |
-
abuse_scores = [r[0][0] for r in results]
|
545 |
-
stages = [r[0][4] for r in results]
|
546 |
|
547 |
-
# Post-check override to catch hidden risks
|
548 |
-
if any(score > 70 for score in abuse_scores) or any(stage == 2 for stage in stages):
|
549 |
-
if risk_level == "Low":
|
550 |
-
risk_level = "Moderate"
|
551 |
messages = [msg1, msg2, msg3]
|
552 |
dates = [date1, date2, date3]
|
553 |
active = [(m, d) for m, d in zip(messages, dates) if m.strip()]
|
554 |
if not active:
|
555 |
return "Please enter at least one message."
|
556 |
|
|
|
557 |
results = [(analyze_single_message(m, THRESHOLDS.copy()), d) for m, d in active]
|
558 |
-
escalation_bump = 0
|
559 |
|
|
|
|
|
560 |
for result, _ in results:
|
561 |
abuse_score, threshold_labels, top_patterns, sentiment, stage, darvo_score, tone_tag = result
|
562 |
-
|
563 |
if darvo_score > 0.65:
|
564 |
escalation_bump += 3
|
565 |
if tone_tag in ["forced accountability flip", "emotional threat"]:
|
@@ -568,12 +554,24 @@ def analyze_composite(msg1, date1, msg2, date2, msg3, date3, *answers_and_none):
|
|
568 |
escalation_bump += 2
|
569 |
if stage == 2:
|
570 |
escalation_bump += 3
|
571 |
-
|
|
|
|
|
572 |
risk_level = (
|
573 |
"High" if hybrid_score >= 16 else
|
574 |
"Moderate" if hybrid_score >= 8 else
|
575 |
"Low"
|
576 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
577 |
for result, date in results:
|
578 |
assert len(result) == 7, "Unexpected output from analyze_single_message"
|
579 |
abuse_scores = [r[0][0] for r in results]
|
|
|
532 |
risk_level = "unknown"
|
533 |
else:
|
534 |
escalation_score = sum(w for (_, w), a in zip(ESCALATION_QUESTIONS, answers_and_none[:-1]) if a)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
535 |
|
|
|
|
|
|
|
|
|
536 |
messages = [msg1, msg2, msg3]
|
537 |
dates = [date1, date2, date3]
|
538 |
active = [(m, d) for m, d in zip(messages, dates) if m.strip()]
|
539 |
if not active:
|
540 |
return "Please enter at least one message."
|
541 |
|
542 |
+
# Run model on messages
|
543 |
results = [(analyze_single_message(m, THRESHOLDS.copy()), d) for m, d in active]
|
|
|
544 |
|
545 |
+
# Calculate escalation bump *after* model results exist
|
546 |
+
escalation_bump = 0
|
547 |
for result, _ in results:
|
548 |
abuse_score, threshold_labels, top_patterns, sentiment, stage, darvo_score, tone_tag = result
|
|
|
549 |
if darvo_score > 0.65:
|
550 |
escalation_bump += 3
|
551 |
if tone_tag in ["forced accountability flip", "emotional threat"]:
|
|
|
554 |
escalation_bump += 2
|
555 |
if stage == 2:
|
556 |
escalation_bump += 3
|
557 |
+
|
558 |
+
# Now we can safely calculate hybrid_score
|
559 |
+
hybrid_score = escalation_score + escalation_bump if escalation_score is not None else 0
|
560 |
risk_level = (
|
561 |
"High" if hybrid_score >= 16 else
|
562 |
"Moderate" if hybrid_score >= 8 else
|
563 |
"Low"
|
564 |
)
|
565 |
+
|
566 |
+
# Now compute scores and allow override
|
567 |
+
abuse_scores = [r[0][0] for r in results]
|
568 |
+
stages = [r[0][4] for r in results]
|
569 |
+
|
570 |
+
# Post-check override (e.g. stage 2 or high abuse score forces Moderate risk)
|
571 |
+
if any(score > 70 for score in abuse_scores) or any(stage == 2 for stage in stages):
|
572 |
+
if risk_level == "Low":
|
573 |
+
risk_level = "Moderate"
|
574 |
+
)
|
575 |
for result, date in results:
|
576 |
assert len(result) == 7, "Unexpected output from analyze_single_message"
|
577 |
abuse_scores = [r[0][0] for r in results]
|