SamanthaStorm commited on
Commit
6d45d2a
·
verified ·
1 Parent(s): d0e2f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -42
app.py CHANGED
@@ -446,22 +446,6 @@ def analyze_single_message(text, thresholds):
446
  for label, score in zip(LABELS, raw_scores):
447
  logger.debug(f"{label}: {score:.3f}")
448
 
449
- # Initialize lists before checking control
450
- threshold_labels = []
451
- matched_scores = []
452
-
453
- # Add control check
454
- control_score = raw_scores[LABELS.index("control")]
455
- if control_score > 0.3: # Lower threshold for control
456
- if "control" not in threshold_labels:
457
- threshold_labels.append("control")
458
- matched_scores.append(("control", control_score, PATTERN_WEIGHTS["control"]))
459
-
460
- # Get predictions and sort them (continue with existing code)
461
- predictions = list(zip(LABELS, raw_scores))
462
- sorted_predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
463
-
464
-
465
  # Get predictions and sort them
466
  predictions = list(zip(LABELS, raw_scores))
467
  sorted_predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
@@ -474,31 +458,28 @@ def analyze_single_message(text, thresholds):
474
  if explicit_abuse:
475
  threshold_labels.append("insults")
476
  logger.debug("\nForced inclusion of 'insults' due to explicit abuse")
477
-
478
- for label, score in sorted_predictions:
479
- if label == "nonabusive":
480
- continue # Skip nonabusive label
481
- base_threshold = thresholds.get(label, 0.25)
482
- if explicit_abuse:
483
- base_threshold *= 0.5
484
- if score > base_threshold:
485
- if label not in threshold_labels:
486
- threshold_labels.append(label)
487
-
488
- # Calculate matched scores (exclude nonabusive)
489
- matched_scores = []
490
- for label in threshold_labels:
491
- if label == "nonabusive":
492
- continue
493
- score = raw_scores[LABELS.index(label)]
494
- weight = PATTERN_WEIGHTS.get(label, 1.0)
495
- if explicit_abuse and label == "insults":
496
- weight *= 1.5
497
- matched_scores.append((label, score, weight))
498
-
499
- # Calculate matched scores
500
  matched_scores = []
501
  for label in threshold_labels:
 
 
502
  score = raw_scores[LABELS.index(label)]
503
  weight = PATTERN_WEIGHTS.get(label, 1.0)
504
  if explicit_abuse and label == "insults":
@@ -530,18 +511,19 @@ def analyze_single_message(text, thresholds):
530
  abuse_score = max(abuse_score, 85.0) # force high score if compound risk detected
531
  if "control" in [label for label, _, _ in matched_scores]:
532
  abuse_score = max(abuse_score, 70.0) # Minimum score for control patterns
 
533
  # Get DARVO score
534
  darvo_score = predict_darvo_score(text)
535
 
536
  # Get tone using emotion-based approach
537
  tone_tag = get_emotional_tone_tag(text, sentiment, threshold_labels, abuse_score)
538
- # Check for the specific combination
539
- highest_pattern = max(matched_scores, key=lambda x: x[1])[0] if matched_scores else None # Get highest pattern
 
540
  if sentiment == "supportive" and tone_tag == "neutral" and highest_pattern == "obscure language":
541
  logger.debug("Message classified as likely non-abusive (supportive, neutral, and obscure language). Returning low risk.")
542
  return 0.0, [], [], {"label": "supportive"}, 1, 0.0, "neutral" # Return non-abusive values
543
 
544
-
545
  # Set stage
546
  stage = 2 if explicit_abuse or abuse_score > 70 or compound_threat_boost else 1
547
 
@@ -552,6 +534,7 @@ def analyze_single_message(text, thresholds):
552
  except Exception as e:
553
  logger.error(f"Error in analyze_single_message: {e}")
554
  return 0.0, [], [], {"label": "error"}, 1, 0.0, None
 
555
  def generate_abuse_score_chart(dates, scores, patterns):
556
  """Generate a timeline chart of abuse scores"""
557
  try:
 
446
  for label, score in zip(LABELS, raw_scores):
447
  logger.debug(f"{label}: {score:.3f}")
448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  # Get predictions and sort them
450
  predictions = list(zip(LABELS, raw_scores))
451
  sorted_predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
 
458
  if explicit_abuse:
459
  threshold_labels.append("insults")
460
  logger.debug("\nForced inclusion of 'insults' due to explicit abuse")
461
+
462
+ for label, score in sorted_predictions:
463
+ if label == "nonabusive":
464
+ continue # Skip nonabusive label
465
+ base_threshold = thresholds.get(label, 0.25)
466
+ if explicit_abuse:
467
+ base_threshold *= 0.5
468
+ if score > base_threshold:
469
+ if label not in threshold_labels:
470
+ threshold_labels.append(label)
471
+
472
+ # Check for control pattern
473
+ control_score = raw_scores[LABELS.index("control")]
474
+ if control_score > 0.3 and "control" not in threshold_labels: # Lower threshold for control
475
+ threshold_labels.append("control")
476
+ logger.debug("\nAdded control pattern due to high control score")
477
+
478
+ # Calculate matched scores (exclude nonabusive)
 
 
 
 
 
479
  matched_scores = []
480
  for label in threshold_labels:
481
+ if label == "nonabusive":
482
+ continue
483
  score = raw_scores[LABELS.index(label)]
484
  weight = PATTERN_WEIGHTS.get(label, 1.0)
485
  if explicit_abuse and label == "insults":
 
511
  abuse_score = max(abuse_score, 85.0) # force high score if compound risk detected
512
  if "control" in [label for label, _, _ in matched_scores]:
513
  abuse_score = max(abuse_score, 70.0) # Minimum score for control patterns
514
+
515
  # Get DARVO score
516
  darvo_score = predict_darvo_score(text)
517
 
518
  # Get tone using emotion-based approach
519
  tone_tag = get_emotional_tone_tag(text, sentiment, threshold_labels, abuse_score)
520
+
521
+ # Check for the specific combination
522
+ highest_pattern = max(matched_scores, key=lambda x: x[1])[0] if matched_scores else None
523
  if sentiment == "supportive" and tone_tag == "neutral" and highest_pattern == "obscure language":
524
  logger.debug("Message classified as likely non-abusive (supportive, neutral, and obscure language). Returning low risk.")
525
  return 0.0, [], [], {"label": "supportive"}, 1, 0.0, "neutral" # Return non-abusive values
526
 
 
527
  # Set stage
528
  stage = 2 if explicit_abuse or abuse_score > 70 or compound_threat_boost else 1
529
 
 
534
  except Exception as e:
535
  logger.error(f"Error in analyze_single_message: {e}")
536
  return 0.0, [], [], {"label": "error"}, 1, 0.0, None
537
+
538
  def generate_abuse_score_chart(dates, scores, patterns):
539
  """Generate a timeline chart of abuse scores"""
540
  try: