Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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
|
| 57 |
logits = outputs.logits.squeeze(0)
|
| 58 |
|
| 59 |
-
# Convert
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
for i, s in enumerate(scores):
|
| 78 |
print(LABELS[i], "=", round(s, 3))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
danger_assessment = (
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
| 86 |
-
|
|
|
|
| 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
|
| 91 |
-
"**Legal Assistance:** WomensLaw β Legal Help for Survivors: [womenslaw.org](https://www.womenslaw.org
|
| 92 |
-
"**Specialized Support:** For LGBTQ+, immigrants, and neurodivergent survivors, please consult local
|
| 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
|
| 97 |
-
"**Relationship Health:** One Love Foundation β Digital Relationship Health: [joinonelove.org](https://www.joinonelove.org
|
| 98 |
-
"**Support Chat:** National Domestic Violence Hotline Chat: [thehotline.org](https://www.thehotline.org
|
| 99 |
"**Specialized Groups:** Look for support groups tailored for LGBTQ+, immigrant, and neurodivergent communities."
|
| 100 |
)
|
| 101 |
-
else:
|
| 102 |
resources = (
|
| 103 |
-
"**Educational Resources:** Love Is Respect β Healthy Relationships: [loveisrespect.org](https://www.loveisrespect.org
|
| 104 |
-
"**Therapy Finder:** Psychology Today β Find a Therapist: [psychologytoday.com](https://www.psychologytoday.com
|
| 105 |
-
"**Relationship Tools:** Relate β Relationship Health Tools: [relate.org.uk](https://www.relate.org.uk
|
| 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
|