SamanthaStorm commited on
Commit
e345a71
Β·
verified Β·
1 Parent(s): cf29b7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -22
app.py CHANGED
@@ -14,7 +14,7 @@ from collections import Counter
14
 
15
 
16
  # ─── Abuse Model ─────────────────────────────────────────────────
17
- model_name = "SamanthaStorm/tether-multilabel-v3"
18
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
19
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
20
 
@@ -352,37 +352,68 @@ def analyze_single_message(text, thresholds):
352
  return abuse_score, threshold_labels, matched_scores, {"label": sentiment}, stage, darvo_score, tone_tag
353
 
354
 
355
- def generate_risk_snippet(abuse_score, top_label, hybrid_score, stage):
356
  """
357
- Generate risk assessment snippet based on abuse score and other factors
 
 
 
 
 
 
358
  """
359
- risk_level = (
360
- "Critical" if abuse_score >= 85 or hybrid_score >= 20 else
361
- "High" if abuse_score >= 70 or hybrid_score >= 15 else
362
- "Moderate" if abuse_score >= 50 or hybrid_score >= 10 else
363
- "Low"
364
- )
365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  risk_descriptions = {
367
  "Critical": (
368
  "🚨 **Risk Level: Critical**\n"
369
- "Multiple severe abuse patterns detected. This situation shows signs of "
370
- "dangerous escalation and immediate intervention may be needed."
371
  ),
372
  "High": (
373
  "⚠️ **Risk Level: High**\n"
374
- "Strong abuse patterns detected. This situation shows concerning "
375
- "signs of manipulation and control."
376
  ),
377
  "Moderate": (
378
  "⚑ **Risk Level: Moderate**\n"
379
- "Concerning patterns detected. While not severe, these behaviors "
380
- "indicate unhealthy relationship dynamics."
381
  ),
382
  "Low": (
383
  "πŸ“ **Risk Level: Low**\n"
384
- "Minor concerning patterns detected. While present, the detected "
385
- "behaviors are subtle or infrequent."
386
  )
387
  }
388
 
@@ -394,11 +425,28 @@ def generate_risk_snippet(abuse_score, top_label, hybrid_score, stage):
394
  4: "Surface calm may mask underlying issues."
395
  }
396
 
397
- snippet = risk_descriptions[risk_level]
 
398
  if stage in stage_context:
399
- snippet += f"\n{stage_context[stage]}"
400
-
401
- return snippet
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  def generate_abuse_score_chart(dates, scores, patterns):
403
  """
404
  Generate a timeline chart of abuse scores
@@ -683,4 +731,100 @@ demo = gr.Interface(
683
 
684
  # This single call will start the server and block,
685
  # keeping the container alive on Spaces.
686
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  # ─── Abuse Model ─────────────────────────────────────────────────
17
+ model_name = "SamanthaStorm/tether-multilabel-v4"
18
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
19
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
20
 
 
352
  return abuse_score, threshold_labels, matched_scores, {"label": sentiment}, stage, darvo_score, tone_tag
353
 
354
 
355
+ def generate_risk_snippet(abuse_score, patterns, hybrid_score, stage):
356
  """
357
+ Enhanced risk assessment generator with more nuanced scoring and pattern analysis
358
+
359
+ Parameters:
360
+ - abuse_score: float (0-100)
361
+ - patterns: list of detected abuse patterns
362
+ - hybrid_score: float (combined escalation/risk score)
363
+ - stage: int (1-4 representing relationship stages)
364
  """
 
 
 
 
 
 
365
 
366
+ # Define risk thresholds with more granular levels
367
+ def get_risk_level(abuse_score, hybrid_score, patterns):
368
+ if abuse_score >= 85 or hybrid_score >= 20:
369
+ return "Critical"
370
+ elif abuse_score >= 70 or hybrid_score >= 15:
371
+ return "High"
372
+ elif abuse_score >= 50 or hybrid_score >= 10:
373
+ return "Moderate"
374
+ return "Low"
375
+
376
+ # Pattern severity weights
377
+ PATTERN_SEVERITY = {
378
+ "control": 3,
379
+ "gaslighting": 3,
380
+ "insults": 2,
381
+ "blame shifting": 2,
382
+ "guilt tripping": 2,
383
+ "dismissiveness": 1,
384
+ "projection": 1,
385
+ "contradictory statements": 1,
386
+ "obscure language": 1,
387
+ "recovery": 1
388
+ }
389
+
390
+ # Calculate weighted pattern severity
391
+ pattern_severity = sum(PATTERN_SEVERITY.get(p, 0) for p in patterns)
392
+
393
+ # Get base risk level
394
+ risk_level = get_risk_level(abuse_score, hybrid_score, patterns)
395
+
396
+ # Generate risk descriptions with more detailed context
397
  risk_descriptions = {
398
  "Critical": (
399
  "🚨 **Risk Level: Critical**\n"
400
+ f"Multiple severe abuse patterns detected (Score: {abuse_score:.1f}%). "
401
+ "This situation shows signs of dangerous escalation and immediate intervention may be needed."
402
  ),
403
  "High": (
404
  "⚠️ **Risk Level: High**\n"
405
+ f"Strong abuse patterns detected (Score: {abuse_score:.1f}%). "
406
+ "This situation shows concerning signs of manipulation and control."
407
  ),
408
  "Moderate": (
409
  "⚑ **Risk Level: Moderate**\n"
410
+ f"Concerning patterns detected (Score: {abuse_score:.1f}%). "
411
+ "While not severe, these behaviors indicate unhealthy relationship dynamics."
412
  ),
413
  "Low": (
414
  "πŸ“ **Risk Level: Low**\n"
415
+ f"Minor concerning patterns detected (Score: {abuse_score:.1f}%). "
416
+ "While present, the detected behaviors are subtle or infrequent."
417
  )
418
  }
419
 
 
425
  4: "Surface calm may mask underlying issues."
426
  }
427
 
428
+ # Build output
429
+ output = risk_descriptions[risk_level]
430
  if stage in stage_context:
431
+ output += f"\n{stage_context[stage]}"
432
+
433
+ # Add pattern analysis if patterns detected
434
+ if patterns:
435
+ output += "\n\nπŸ” **Detected Patterns:**"
436
+ for pattern in patterns:
437
+ severity = PATTERN_SEVERITY.get(pattern, 0)
438
+ output += f"\nβ€’ {pattern.title()} (Severity: {'❗' * severity})"
439
+
440
+ # Add safety recommendations based on risk level
441
+ if risk_level in ["Critical", "High"]:
442
+ output += "\n\n⚠️ **Safety Recommendations:**"
443
+ output += "\nβ€’ Consider reaching out to a domestic violence hotline"
444
+ output += "\nβ€’ Document all concerning interactions"
445
+ output += "\nβ€’ Have a safety plan in place"
446
+
447
+ return output
448
+
449
+
450
  def generate_abuse_score_chart(dates, scores, patterns):
451
  """
452
  Generate a timeline chart of abuse scores
 
731
 
732
  # This single call will start the server and block,
733
  # keeping the container alive on Spaces.
734
+ demo.launch()
735
+
736
+ def generate_risk_snippet(abuse_score, patterns, hybrid_score, stage):
737
+ """
738
+ Enhanced risk assessment generator with more nuanced scoring and pattern analysis
739
+
740
+ Parameters:
741
+ - abuse_score: float (0-100)
742
+ - patterns: list of detected abuse patterns
743
+ - hybrid_score: float (combined escalation/risk score)
744
+ - stage: int (1-4 representing relationship stages)
745
+ """
746
+
747
+ # Define risk thresholds with more granular levels
748
+ def get_risk_level(abuse_score, hybrid_score, patterns):
749
+ if abuse_score >= 85 or hybrid_score >= 20:
750
+ return "Critical"
751
+ elif abuse_score >= 70 or hybrid_score >= 15:
752
+ return "High"
753
+ elif abuse_score >= 50 or hybrid_score >= 10:
754
+ return "Moderate"
755
+ return "Low"
756
+
757
+ # Pattern severity weights
758
+ PATTERN_SEVERITY = {
759
+ "control": 3,
760
+ "gaslighting": 3,
761
+ "insults": 2,
762
+ "blame shifting": 2,
763
+ "guilt tripping": 2,
764
+ "dismissiveness": 1,
765
+ "projection": 1,
766
+ "contradictory statements": 1,
767
+ "obscure language": 1,
768
+ "recovery": 1
769
+ }
770
+
771
+ # Calculate weighted pattern severity
772
+ pattern_severity = sum(PATTERN_SEVERITY.get(p, 0) for p in patterns)
773
+
774
+ # Get base risk level
775
+ risk_level = get_risk_level(abuse_score, hybrid_score, patterns)
776
+
777
+ # Generate risk descriptions with more detailed context
778
+ risk_descriptions = {
779
+ "Critical": (
780
+ "🚨 **Risk Level: Critical**\n"
781
+ f"Multiple severe abuse patterns detected (Score: {abuse_score:.1f}%). "
782
+ "This situation shows signs of dangerous escalation and immediate intervention may be needed."
783
+ ),
784
+ "High": (
785
+ "⚠️ **Risk Level: High**\n"
786
+ f"Strong abuse patterns detected (Score: {abuse_score:.1f}%). "
787
+ "This situation shows concerning signs of manipulation and control."
788
+ ),
789
+ "Moderate": (
790
+ "⚑ **Risk Level: Moderate**\n"
791
+ f"Concerning patterns detected (Score: {abuse_score:.1f}%). "
792
+ "While not severe, these behaviors indicate unhealthy relationship dynamics."
793
+ ),
794
+ "Low": (
795
+ "πŸ“ **Risk Level: Low**\n"
796
+ f"Minor concerning patterns detected (Score: {abuse_score:.1f}%). "
797
+ "While present, the detected behaviors are subtle or infrequent."
798
+ )
799
+ }
800
+
801
+ # Add stage-specific context
802
+ stage_context = {
803
+ 1: "Current patterns suggest a tension-building phase.",
804
+ 2: "Messages show signs of active escalation.",
805
+ 3: "Patterns indicate attempted reconciliation without real change.",
806
+ 4: "Surface calm may mask underlying issues."
807
+ }
808
+
809
+ # Build output
810
+ output = risk_descriptions[risk_level]
811
+ if stage in stage_context:
812
+ output += f"\n{stage_context[stage]}"
813
+
814
+ # Add pattern analysis if patterns detected
815
+ if patterns:
816
+ output += "\n\nπŸ” **Detected Patterns:**"
817
+ for pattern in patterns:
818
+ severity = PATTERN_SEVERITY.get(pattern, 0)
819
+ output += f"\nβ€’ {pattern.title()} (Severity: {'❗' * severity})"
820
+
821
+ # Add safety recommendations based on risk level
822
+ if risk_level in ["Critical", "High"]:
823
+ output += "\n\n⚠️ **Safety Recommendations:**"
824
+ output += "\nβ€’ Consider reaching out to a domestic violence hotline"
825
+ output += "\nβ€’ Document all concerning interactions"
826
+ output += "\nβ€’ Have a safety plan in place"
827
+
828
+ return output
829
+
830
+