SamanthaStorm commited on
Commit
5d7c4ba
Β·
verified Β·
1 Parent(s): ea30a69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -53,71 +53,67 @@ def analyze_messages(input_text):
53
  with torch.no_grad():
54
  outputs = model(**inputs)
55
 
56
- # Squeeze out batch dimension: shape should be [17]
57
  logits = outputs.logits.squeeze(0)
58
 
59
- # Convert logits to probabilities
60
  scores = torch.sigmoid(logits).numpy()
61
  print("Scores:", scores)
62
  print("Danger Scores:", scores[14:]) # suicidal, physical, extreme
63
 
64
- # Debug printing (remove once you're confident everything works)
65
- print("Scores:", scores)
66
-
67
  pattern_count = 0
68
  danger_flag_count = 0
69
 
70
- for i, (label, score) in enumerate(zip(LABELS, scores)):
71
- if score > THRESHOLDS[label]:
72
- if i < 14:
73
- pattern_count += 1
74
- else:
75
- danger_flag_count += 1
76
- # (Optional) Print label-by-label for debugging
 
77
  for i, s in enumerate(scores):
78
  print(LABELS[i], "=", round(s, 3))
79
-
80
-
81
  danger_assessment = (
82
- "High" if danger_flag_count >= 2 else
83
- "Moderate" if danger_flag_count == 1 else
84
- "Low"
85
- )
86
- # Customize resource links based on Danger Assessment Score (with additional niche support)
 
87
  if danger_assessment == "High":
88
  resources = (
89
  "**Immediate Help:** If you are in immediate danger, please call 911.\n\n"
90
- "**Crisis Support:** National DV Hotline – Safety Planning: [thehotline.org/plan-for-safety](https://www.thehotline.org/plan-for-safety/)\n"
91
- "**Legal Assistance:** WomensLaw – Legal Help for Survivors: [womenslaw.org](https://www.womenslaw.org/)\n"
92
- "**Specialized Support:** For LGBTQ+, immigrants, and neurodivergent survivors, please consult local specialized services or visit RAINN: [rainn.org](https://www.rainn.org/)"
93
  )
94
  elif danger_assessment == "Moderate":
95
  resources = (
96
- "**Safety Planning:** The Hotline – What Is Emotional Abuse?: [thehotline.org/resources](https://www.thehotline.org/resources/what-is-emotional-abuse/)\n"
97
- "**Relationship Health:** One Love Foundation – Digital Relationship Health: [joinonelove.org](https://www.joinonelove.org/)\n"
98
- "**Support Chat:** National Domestic Violence Hotline Chat: [thehotline.org](https://www.thehotline.org/)\n"
99
  "**Specialized Groups:** Look for support groups tailored for LGBTQ+, immigrant, and neurodivergent communities."
100
  )
101
- else: # Low risk
102
  resources = (
103
- "**Educational Resources:** Love Is Respect – Healthy Relationships: [loveisrespect.org](https://www.loveisrespect.org/)\n"
104
- "**Therapy Finder:** Psychology Today – Find a Therapist: [psychologytoday.com](https://www.psychologytoday.com/us/therapists)\n"
105
- "**Relationship Tools:** Relate – Relationship Health Tools: [relate.org.uk](https://www.relate.org.uk/)\n"
106
  "**Community Support:** Consider community-based and online support groups, especially those focused on LGBTQ+, immigrant, and neurodivergent survivors."
107
  )
108
-
109
-
110
- # Prepare the output result with just pattern count and dynamic resources
111
  result_md = (
112
  f"**Abuse Pattern Count:** {pattern_count}\n\n"
113
  f"**Support Resources:**\n{resources}"
114
  )
115
-
116
- # Save the result to a temporary text file for download
117
  with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w") as f:
118
  f.write(result_md)
119
  report_path = f.name
120
-
121
  return result_md, report_path
122
 
123
  # Build the Gradio interface
 
53
  with torch.no_grad():
54
  outputs = model(**inputs)
55
 
56
+ # Squeeze out batch dimension
57
  logits = outputs.logits.squeeze(0)
58
 
59
+ # Convert to probabilities
60
  scores = torch.sigmoid(logits).numpy()
61
  print("Scores:", scores)
62
  print("Danger Scores:", scores[14:]) # suicidal, physical, extreme
63
 
 
 
 
64
  pattern_count = 0
65
  danger_flag_count = 0
66
 
67
+ for i, (label, score) in enumerate(zip(LABELS, scores)):
68
+ if score > THRESHOLDS[label]:
69
+ if i < 14:
70
+ pattern_count += 1
71
+ else:
72
+ danger_flag_count += 1
73
+
74
+ # Optional debug print
75
  for i, s in enumerate(scores):
76
  print(LABELS[i], "=", round(s, 3))
77
+
 
78
  danger_assessment = (
79
+ "High" if danger_flag_count >= 2 else
80
+ "Moderate" if danger_flag_count == 1 else
81
+ "Low"
82
+ )
83
+
84
+ # Set resources
85
  if danger_assessment == "High":
86
  resources = (
87
  "**Immediate Help:** If you are in immediate danger, please call 911.\n\n"
88
+ "**Crisis Support:** National DV Hotline – Safety Planning: [thehotline.org/plan-for-safety](https://www.thehotline.org/plan-for-safety)\n"
89
+ "**Legal Assistance:** WomensLaw – Legal Help for Survivors: [womenslaw.org](https://www.womenslaw.org)\n"
90
+ "**Specialized Support:** For LGBTQ+, immigrants, and neurodivergent survivors, please consult local services."
91
  )
92
  elif danger_assessment == "Moderate":
93
  resources = (
94
+ "**Safety Planning:** The Hotline – What Is Emotional Abuse?: [thehotline.org/resources](https://www.thehotline.org/resources)\n"
95
+ "**Relationship Health:** One Love Foundation – Digital Relationship Health: [joinonelove.org](https://www.joinonelove.org)\n"
96
+ "**Support Chat:** National Domestic Violence Hotline Chat: [thehotline.org](https://www.thehotline.org)\n"
97
  "**Specialized Groups:** Look for support groups tailored for LGBTQ+, immigrant, and neurodivergent communities."
98
  )
99
+ else:
100
  resources = (
101
+ "**Educational Resources:** Love Is Respect – Healthy Relationships: [loveisrespect.org](https://www.loveisrespect.org)\n"
102
+ "**Therapy Finder:** Psychology Today – Find a Therapist: [psychologytoday.com](https://www.psychologytoday.com)\n"
103
+ "**Relationship Tools:** Relate – Relationship Health Tools: [relate.org.uk](https://www.relate.org.uk)\n"
104
  "**Community Support:** Consider community-based and online support groups, especially those focused on LGBTQ+, immigrant, and neurodivergent survivors."
105
  )
106
+
107
+ # Output
 
108
  result_md = (
109
  f"**Abuse Pattern Count:** {pattern_count}\n\n"
110
  f"**Support Resources:**\n{resources}"
111
  )
112
+
 
113
  with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w") as f:
114
  f.write(result_md)
115
  report_path = f.name
116
+
117
  return result_md, report_path
118
 
119
  # Build the Gradio interface