SamanthaStorm commited on
Commit
fbb63df
·
verified ·
1 Parent(s): a239b18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
app.py CHANGED
@@ -898,47 +898,51 @@ def analyze_single_message(text, thresholds):
898
  logger.debug("Empty text, returning zeros")
899
  return 0.0, [], [], {"label": "none"}, 1, 0.0, None, {'assessment': 'neutral', 'confidence': 0.5}
900
 
901
- # BOUNDARY HEALTH CHECK - Add this new section
902
- logger.debug("\n🛡️ BOUNDARY HEALTH ANALYSIS")
903
- logger.debug("-" * 40)
904
- healthy_prob = predict_boundary_health(text)
905
- boundary_assessment = get_boundary_assessment(text, healthy_prob)
906
- logger.debug(f"Boundary Health Score: {healthy_prob:.3f}")
907
- logger.debug(f"Boundary Assessment: {boundary_assessment['label']}")
908
-
909
- # Get sentiment EARLY - BEFORE any early returns
910
- sent_inputs = sentiment_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
911
- sent_inputs = {k: v.to(device) for k, v in sent_inputs.items()}
912
- with torch.no_grad():
913
- sent_logits = sentiment_model(**sent_inputs).logits[0]
914
- sent_probs = torch.softmax(sent_logits, dim=-1).cpu().numpy()
915
-
916
- # Add detailed logging
917
- logger.debug("\n🎭 SENTIMENT ANALYSIS DETAILS")
918
- logger.debug(f"Raw logits: {sent_logits}")
919
- logger.debug(f"Probabilities: supportive={sent_probs[0]:.3f}, undermining={sent_probs[1]:.3f}")
920
-
921
- # Make sure we're using the correct index mapping
922
- sentiment = SENTIMENT_LABELS[int(np.argmax(sent_probs))]
923
- logger.debug(f"Selected sentiment: {sentiment}")
924
-
925
- # ADD THE OVERRIDE HERE - RIGHT AFTER SENTIMENT IS DEFINED:
926
- if (healthy_prob < 0.7 and
927
- sentiment == "supportive" and
928
- len(text.split()) > 50 and
929
- any(phrase in text.lower() for phrase in [
930
- "i need you to", "i want to understand", "this isn't about",
931
- "about accuracy", "willing to do something different"
932
- ])):
933
-
934
- logger.debug("🔄 Boundary assessment override: Sophisticated healthy boundary detected")
935
- boundary_assessment = {
936
- 'assessment': 'healthy',
937
- 'label': 'Healthy Boundary (Sophisticated)',
938
- 'confidence': 0.85,
939
- 'description': 'Complex but healthy boundary-setting communication',
940
- 'recommendations': ['Continue this thoughtful, direct approach']
941
- }
 
 
 
 
942
 
943
  # EARLY SUPPORTIVE MESSAGE CHECK
944
  innocent_indicators = [
 
898
  logger.debug("Empty text, returning zeros")
899
  return 0.0, [], [], {"label": "none"}, 1, 0.0, None, {'assessment': 'neutral', 'confidence': 0.5}
900
 
901
+ # BOUNDARY HEALTH CHECK - Add this new section
902
+ logger.debug("\n🛡️ BOUNDARY HEALTH ANALYSIS")
903
+ logger.debug("-" * 40)
904
+ boundary_class, boundary_confidence = predict_boundary_health(text) # Unpack the tuple
905
+ boundary_assessment = get_boundary_assessment(text, boundary_class, boundary_confidence)
906
+ logger.debug(f"Boundary Class: {boundary_class} ({['Respected', 'Violated', 'Dismissed', 'Manipulative'][boundary_class]})")
907
+ logger.debug(f"Boundary Confidence: {boundary_confidence:.3f}")
908
+ logger.debug(f"Boundary Assessment: {boundary_assessment['label']}")
909
+
910
+ # Get sentiment EARLY - BEFORE any early returns
911
+ sent_inputs = sentiment_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
912
+ sent_inputs = {k: v.to(device) for k, v in sent_inputs.items()}
913
+ with torch.no_grad():
914
+ sent_logits = sentiment_model(**sent_inputs).logits[0]
915
+ sent_probs = torch.softmax(sent_logits, dim=-1).cpu().numpy()
916
+
917
+ # Add detailed logging
918
+ logger.debug("\n🎭 SENTIMENT ANALYSIS DETAILS")
919
+ logger.debug(f"Raw logits: {sent_logits}")
920
+ logger.debug(f"Probabilities: supportive={sent_probs[0]:.3f}, undermining={sent_probs[1]:.3f}")
921
+
922
+ # Make sure we're using the correct index mapping
923
+ sentiment = SENTIMENT_LABELS[int(np.argmax(sent_probs))]
924
+ logger.debug(f"Selected sentiment: {sentiment}")
925
+
926
+ # UPDATE THE OVERRIDE CONDITION:
927
+ # Now we need to check if boundary_class == 0 (Respected) instead of checking a probability
928
+ if (boundary_class != 0 and # Not "Respected"
929
+ boundary_confidence < 0.7 and
930
+ sentiment == "supportive" and
931
+ len(text.split()) > 50 and
932
+ any(phrase in text.lower() for phrase in [
933
+ "i need you to", "i want to understand", "this isn't about",
934
+ "about accuracy", "willing to do something different"
935
+ ])):
936
+
937
+ logger.debug("🔄 Boundary assessment override: Sophisticated healthy boundary detected")
938
+ boundary_assessment = {
939
+ 'assessment': 'healthy',
940
+ 'label': 'Healthy Boundary (Sophisticated)',
941
+ 'confidence': 0.85,
942
+ 'description': 'Complex but healthy boundary-setting communication',
943
+ 'recommendations': ['Continue this thoughtful, direct approach']
944
+ }
945
+
946
 
947
  # EARLY SUPPORTIVE MESSAGE CHECK
948
  innocent_indicators = [