Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadd more example and fix UI
app.py
CHANGED
|
@@ -1,212 +1,281 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"""
|
| 17 |
-
Interprets depression risk score using research-based thresholds.
|
| 18 |
-
Optimal threshold from training: 0.6
|
| 19 |
-
"""
|
| 20 |
-
if depression_score < 0.30:
|
| 21 |
-
return {
|
| 22 |
-
"level": "Low Risk",
|
| 23 |
-
"color": "π’",
|
| 24 |
-
"interpretation": "Linguistic patterns consistent with non-depressed users. No significant markers of depression detected.",
|
| 25 |
-
"recommendation": "Continue monitoring mental wellness. Maintain healthy habits."
|
| 26 |
-
}
|
| 27 |
-
elif 0.30 <= depression_score < 0.60:
|
| 28 |
-
return {
|
| 29 |
-
"level": "Moderate Risk",
|
| 30 |
-
"color": "π‘",
|
| 31 |
-
"interpretation": "Some linguistic markers associated with depression detected. May indicate early signs or temporary mood changes.",
|
| 32 |
-
"recommendation": "Consider monitoring over time. Seek support if feelings persist. Talk to someone you trust."
|
| 33 |
-
}
|
| 34 |
-
else: # >= 0.60 (optimal threshold from training)
|
| 35 |
-
return {
|
| 36 |
-
"level": "High Risk",
|
| 37 |
-
"color": "π΄",
|
| 38 |
-
"interpretation": "Strong linguistic patterns associated with depression detected. Multiple markers of depressive language present.",
|
| 39 |
-
"recommendation": "Strongly recommend speaking with a mental health professional. Immediate support resources provided below."
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
def analyze_text(user_text):
|
| 43 |
-
"""
|
| 44 |
-
Analyzes text for linguistic markers of depression using Mental-Longformer.
|
| 45 |
-
|
| 46 |
-
This MCP tool provides depression risk assessment based on long-form user histories.
|
| 47 |
-
Trained on eRisk datasets (2017-2022) with F1-score of 0.7668.
|
| 48 |
-
Optimal decision threshold: 0.6
|
| 49 |
-
"""
|
| 50 |
-
if not user_text.strip():
|
| 51 |
-
return "β οΈ Please provide text to analyze."
|
| 52 |
|
| 53 |
# Check text length
|
| 54 |
-
word_count = len(
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
Short inputs will produce unreliable results.
|
| 60 |
|
| 61 |
-
|
| 62 |
-
- Multiple social media posts (10-15 posts)
|
| 63 |
-
- Extended writing samples
|
| 64 |
-
- Long-form personal narratives
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Get depression probability (LABEL_1)
|
| 71 |
-
depression_prob = next((r['score'] for r in results if r['label'] == 'LABEL_1'), 0)
|
| 72 |
-
risk_info = get_risk_interpretation(depression_prob)
|
| 73 |
|
| 74 |
-
output
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
{risk_info['recommendation']}
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
- **Decision Threshold:** 60% (optimal from training)
|
| 93 |
- **Model Context:** 4,096 tokens
|
| 94 |
- **Validation F1-Score:** 0.7668
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
**This is NOT a diagnostic tool.** This model:
|
|
|
|
| 101 |
- Detects statistical patterns in language, not clinical depression
|
| 102 |
- Requires professional interpretation
|
| 103 |
- Cannot replace mental health assessment by qualified professionals
|
| 104 |
- May produce false positives/negatives
|
| 105 |
|
| 106 |
**If you're in crisis, help is available NOW:**
|
|
|
|
| 107 |
- π **Crisis Text Line:** Text HOME to 741741 (US)
|
| 108 |
- π **National Suicide Prevention Lifeline:** 988 (US)
|
| 109 |
-
- π **International:** [befrienders.org](https://befrienders.org)
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
"""
|
| 115 |
-
return output
|
| 116 |
-
|
| 117 |
-
# Extended, realistic examples (your original ones - they're perfect!)
|
| 118 |
-
example_depressed = """Post 1: Another all-nighter at work. The project deadline is looming and I feel like the weight of the whole thing is on my shoulders. My boss just keeps saying "great work" but it feels so empty. I'm just running on fumes and caffeine.
|
| 119 |
-
---
|
| 120 |
-
Post 2: Tried to play some video games to unwind tonight but I couldn't even focus. Just stared at the screen. The thing I used to love for escaping just feels like another chore now. I don't get joy from it anymore. What's wrong with me?
|
| 121 |
-
---
|
| 122 |
-
Post 3: A friend invited me out for drinks. I made up an excuse about being busy. The thought of having to put on a happy face and make small talk is just... completely exhausting. It's easier to just be alone. The silence is heavy though.
|
| 123 |
-
---
|
| 124 |
-
Post 4: I'm so tired, but I can't sleep. My mind just keeps replaying every mistake I made this week, every awkward conversation. It's like a highlight reel of my failures. I feel like such an impostor.
|
| 125 |
-
---
|
| 126 |
-
Post 5: Someone complimented my presentation today. I just smiled and said thanks. Inside I was screaming. They have no idea how close I am to completely falling apart. I feel like a fraud, just waiting to be exposed.
|
| 127 |
-
---
|
| 128 |
-
Post 6: I'm living on microwave meals because the idea of cooking, of chopping things and cleaning up, is overwhelming. Everything feels like it takes a monumental effort.
|
| 129 |
-
---
|
| 130 |
-
Post 7: My apartment is a mess. I know I should clean it but I just can't find the energy or motivation. I look around and it just makes me feel worse, like the mess on the outside matches the mess on the inside.
|
| 131 |
-
---
|
| 132 |
-
Post 8: I keep scrolling through social media and seeing everyone else's perfect lives. Their promotions, their happy relationships, their fun vacations. It just makes me feel more worthless and behind in life. I had to delete the app.
|
| 133 |
-
---
|
| 134 |
-
Post 9: Woke up this morning and just felt nothing. Just a profound, gray emptiness. It's not even sadness anymore, it's just a complete absence of feeling. It's terrifying.
|
| 135 |
-
---
|
| 136 |
-
Post 10: My family called and I let it go to voicemail. I don't want to worry them, and I don't have the energy to lie and say everything is fine. The isolation feels safer.
|
| 137 |
-
---
|
| 138 |
-
Post 11: I feel like I'm drowning in quicksand. The more I struggle, the faster I sink. What's the point of even trying anymore? Every day is the same exhausting, pointless cycle.
|
| 139 |
-
---
|
| 140 |
-
Post 12: I just want it to stop. The noise in my head. The constant feeling of dread. I'm so tired of fighting. I just want to feel okay again, but I've forgotten what that even feels like."""
|
| 141 |
-
|
| 142 |
-
example_normal = """Post 1: I decided to get into gardening this year! Started with a few simple herbs on my windowsill. It's not much but it's a start. It feels nice to have something green to take care of.
|
| 143 |
-
---
|
| 144 |
-
Post 2: My basil is growing like crazy! I used some of it to make fresh pesto for my pasta tonight. It tasted so much better than the store-bought stuff. Feeling very proud of my little plant.
|
| 145 |
-
---
|
| 146 |
-
Post 3: Had a really productive day at work. Managed to clear out my inbox and finish a report that's been hanging over my head. It feels good to be on top of things. Ready to relax this evening.
|
| 147 |
-
---
|
| 148 |
-
Post 4: My friend came over and we cooked dinner together. It was a lot of fun just chatting and trying out a new recipe. The food was pretty good too! It's nice to share a meal with someone.
|
| 149 |
-
---
|
| 150 |
-
Post 5: Went for a long bike ride this morning. The weather was perfect for it. It was a bit of a challenge on some of the hills but it felt great to get my body moving and clear my head.
|
| 151 |
-
---
|
| 152 |
-
Post 6: Uh oh, found some aphids on my mint plant. Did some quick research online and made a simple soap spray. Hopefully, that takes care of them. A little frustrating, but it's a learning process.
|
| 153 |
-
---
|
| 154 |
-
Post 7: The soap spray worked! The aphids are gone. Feeling like a real gardener now, solving problems. My little herb garden is thriving.
|
| 155 |
-
---
|
| 156 |
-
Post 8: Just booked a weekend trip to go camping next month. I've been wanting to go for ages. It feels great to have something fun to look forward to.
|
| 157 |
-
---
|
| 158 |
-
Post 9: Spent the afternoon reading in the park. It's one of my favorite simple pleasures. It's so nice to just disconnect from screens and get lost in a good book for a few hours.
|
| 159 |
-
---
|
| 160 |
-
Post 10: Had a really nice video call with my family today. It was good to see everyone and catch up. We were laughing about old memories. I always feel so refreshed after talking to them.
|
| 161 |
-
---
|
| 162 |
-
Post 11: I'm thinking of expanding my garden to include some tomatoes next. I've been watching a bunch of videos on how to build a small planter box. Feeling excited about the new project.
|
| 163 |
-
---
|
| 164 |
-
Post 12: It's been a good week. Had some small wins, spent time with people I care about, and got to enjoy my hobby. Feeling content and looking forward to what next week brings."""
|
| 165 |
-
|
| 166 |
-
# Build interface
|
| 167 |
-
demo = gr.Interface(
|
| 168 |
-
fn=analyze_text,
|
| 169 |
-
inputs=gr.Textbox(
|
| 170 |
-
label="π Enter User Text History (300+ words recommended)",
|
| 171 |
-
placeholder="Paste multiple posts from the same user over time. The model needs long-form text to accurately detect patterns...",
|
| 172 |
-
lines=12
|
| 173 |
-
),
|
| 174 |
-
outputs=gr.Markdown(label="π Risk Assessment Results"),
|
| 175 |
-
title="π§ Early Depression Detection via Mental-Longformer",
|
| 176 |
-
description="""
|
| 177 |
-
**MCP-enabled depression risk screening agent** using Mental-Longformer
|
| 178 |
-
**Model Performance:** F1-Score: 0.7668 | Context Window: 4,096 tokens | Optimal Threshold: 60%
|
| 179 |
-
|
| 180 |
-
**β οΈ CRITICAL:** This model requires **extensive text input** (10-15 posts or 300+ words) for accurate analysis.
|
| 181 |
-
Short inputs yield unreliable results.
|
| 182 |
-
|
| 183 |
-
Built on Master's thesis research at **University of Malaya** using Gemini 2.5 Flash data augmentation.
|
| 184 |
-
|
| 185 |
-
π [Model Card](https://huggingface.co/avtak/erisk-longformer-depression-v1) | π€ [Author LinkedIn](https://www.linkedin.com/in/hassanzh/)
|
| 186 |
-
""",
|
| 187 |
-
examples=[
|
| 188 |
-
[example_depressed],
|
| 189 |
-
[example_normal]
|
| 190 |
-
],
|
| 191 |
-
article="""
|
| 192 |
-
### About the Risk Scale
|
| 193 |
-
|
| 194 |
-
This model uses research-validated thresholds:
|
| 195 |
-
- π’ **Low Risk (0-30%):** Typical language patterns, no significant depression markers
|
| 196 |
-
- π‘ **Moderate Risk (30-60%):** Some concerning patterns, monitoring recommended
|
| 197 |
-
- π΄ **High Risk (60%+):** Strong depression markers, professional consultation recommended
|
| 198 |
-
|
| 199 |
-
The 60% threshold was optimized during training on eRisk datasets for maximum F1-score.
|
| 200 |
-
|
| 201 |
-
### Ethical Use
|
| 202 |
-
This tool is for **research and screening purposes only**. It should never be used:
|
| 203 |
-
- As a substitute for professional diagnosis
|
| 204 |
-
- To make clinical decisions without human oversight
|
| 205 |
-
- For surveillance or discrimination
|
| 206 |
-
- Without informed consent
|
| 207 |
-
"""
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
# π₯ Enable MCP Server
|
| 211 |
if __name__ == "__main__":
|
| 212 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
MODEL_NAME = "mental/mental-longformer-depression"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model.to(device)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
def predict_depression(text):
|
| 15 |
+
"""Predict depression risk from user text"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Check text length
|
| 18 |
+
word_count = len(text.split())
|
| 19 |
+
|
| 20 |
+
if word_count < 88:
|
| 21 |
+
warning = f"β οΈ Warning: Text Too Short ({word_count} words)"
|
| 22 |
+
message = (
|
| 23 |
+
"This model requires extensive text (300+ words recommended) for accurate analysis. "
|
| 24 |
+
"Short inputs will produce unreliable results.\n\n"
|
| 25 |
+
"Please provide:\n"
|
| 26 |
+
"β’ Multiple social media posts (10-15 posts)\n"
|
| 27 |
+
"β’ Extended writing samples\n"
|
| 28 |
+
"β’ Long-form personal narratives\n\n"
|
| 29 |
+
"Use the examples below to see proper input format."
|
| 30 |
+
)
|
| 31 |
+
return warning, message, ""
|
| 32 |
+
|
| 33 |
+
# Tokenize and predict
|
| 34 |
+
inputs = tokenizer(
|
| 35 |
+
text,
|
| 36 |
+
return_tensors="pt",
|
| 37 |
+
truncation=True,
|
| 38 |
+
max_length=4096,
|
| 39 |
+
padding=True
|
| 40 |
+
).to(device)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model(**inputs)
|
| 44 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 45 |
+
depression_prob = probs[0][1].item() * 100
|
| 46 |
+
|
| 47 |
+
# Determine risk level
|
| 48 |
+
if depression_prob >= 60:
|
| 49 |
+
risk_level = "π΄ Depression Risk Assessment: High Risk"
|
| 50 |
+
risk_color = "#ff4444"
|
| 51 |
+
interpretation = (
|
| 52 |
+
"Strong linguistic patterns associated with depression detected. "
|
| 53 |
+
"Multiple markers of depressive language present."
|
| 54 |
+
)
|
| 55 |
+
recommendation = (
|
| 56 |
+
"Strongly recommend speaking with a mental health professional. "
|
| 57 |
+
"Immediate support resources provided below."
|
| 58 |
+
)
|
| 59 |
+
elif depression_prob >= 30:
|
| 60 |
+
risk_level = "π‘ Depression Risk Assessment: Moderate Risk"
|
| 61 |
+
risk_color = "#ffaa00"
|
| 62 |
+
interpretation = (
|
| 63 |
+
"Some concerning patterns detected. Monitoring recommended."
|
| 64 |
+
)
|
| 65 |
+
recommendation = (
|
| 66 |
+
"Consider reaching out to a mental health professional for guidance. "
|
| 67 |
+
"Early intervention can be helpful."
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
risk_level = "π’ Depression Risk Assessment: Low Risk"
|
| 71 |
+
risk_color = "#44ff44"
|
| 72 |
+
interpretation = (
|
| 73 |
+
"Typical language patterns, no significant depression markers detected."
|
| 74 |
+
)
|
| 75 |
+
recommendation = (
|
| 76 |
+
"Continue maintaining mental wellness. If concerns arise, "
|
| 77 |
+
"don't hesitate to seek professional support."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Format output
|
| 81 |
+
output = f"""
|
| 82 |
+
{risk_level}
|
| 83 |
+
Depression Probability: {depression_prob:.1f}%
|
| 84 |
+
Text Length: {word_count} words
|
| 85 |
|
| 86 |
+
## Risk Interpretation
|
|
|
|
| 87 |
|
| 88 |
+
{interpretation}
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
## Recommended Action
|
| 91 |
+
|
| 92 |
+
{recommendation}
|
| 93 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
return risk_level, output, f"Depression Probability: {depression_prob:.1f}%"
|
| 96 |
|
| 97 |
+
# Example texts (no post numbers or separators)
|
| 98 |
+
LOW_RISK_EXAMPLE = """I'm thinking of expanding my garden to include some tomatoes next. I've been watching a bunch of videos on how to build a small planter box. Feeling excited about the new project.
|
| 99 |
|
| 100 |
+
It's been a good week. Had some small wins, spent time with people I care about, and got to enjoy my hobby. Feeling content and looking forward to what next week brings.
|
| 101 |
|
| 102 |
+
Just finished a great book on sustainable living. Really inspired me to make some positive changes. Started composting last month and it's going well.
|
| 103 |
+
|
| 104 |
+
Took my dog for a long walk this morning. The weather was perfect and we explored a new trail. He was so happy running around and it put me in a great mood too.
|
| 105 |
+
|
| 106 |
+
Finally tried that new recipe I've been meaning to make. It turned out better than expected! Thinking about hosting a small dinner party next weekend to share it with friends.
|
| 107 |
+
|
| 108 |
+
Work has been busy but manageable. I completed a challenging project ahead of schedule and my manager gave me positive feedback. Feeling accomplished and motivated."""
|
| 109 |
+
|
| 110 |
+
MODERATE_RISK_EXAMPLE = """Been feeling a bit off lately. Not sure what it is, but I just don't have the same energy I used to have. Maybe I'm just tired from work.
|
| 111 |
+
|
| 112 |
+
My sleep schedule has been all over the place. Some nights I can't fall asleep, other nights I sleep too much. Either way, I wake up feeling drained.
|
| 113 |
+
|
| 114 |
+
I've been canceling plans with friends more often. I tell them I'm busy but honestly I just don't feel like going out. I know I should but the motivation just isn't there.
|
| 115 |
+
|
| 116 |
+
Work feels overwhelming sometimes. Even simple tasks feel like they require so much effort. I'm getting things done but it's taking me longer than usual.
|
| 117 |
+
|
| 118 |
+
I've been spending a lot of time scrolling through my phone. I know it's not productive but it's easier than doing other things. Hours just disappear.
|
| 119 |
+
|
| 120 |
+
Haven't been eating as healthy as I should. Skipping meals or just grabbing whatever is convenient. Cooking feels like too much effort right now."""
|
| 121 |
+
|
| 122 |
+
HIGH_RISK_EXAMPLE = """I woke up feeling exhausted even after sleeping for 10 hours. The thought of getting out of bed feels overwhelming. I don't even know why I bother anymore.
|
| 123 |
+
|
| 124 |
+
My apartment is a mess. I know I should clean it but I just can't find the energy or motivation. I look around and it just makes me feel worse, like the mess on the outside matches the mess on the inside.
|
| 125 |
+
|
| 126 |
+
I keep scrolling through social media and seeing everyone else's perfect lives. Their promotions, their happy relationships, their fun vacations. It just makes me feel more worthless and behind in life. I had to delete the app.
|
| 127 |
+
|
| 128 |
+
Woke up this morning and just felt nothing. Just a profound, gray emptiness. It's not even sadness anymore, it's just a complete absence of feeling. It's scary.
|
| 129 |
|
| 130 |
+
My family called and I let it go to voicemail. I don't want to worry them, and I don't have the energy to lie and say everything is fine. The isolation feels safer.
|
| 131 |
|
| 132 |
+
I feel like I'm drowning in quicksand. The more I struggle, the faster I sink. What's the point of even trying anymore? Every day is the same exhausting, pointless cycle.
|
|
|
|
| 133 |
|
| 134 |
+
I just want it to stop. The noise in my head. The constant feeling of dread. I'm so tired of fighting. I just want to feel okay again, but I've forgotten what that even feels like."""
|
| 135 |
|
| 136 |
+
# Build Gradio interface
|
| 137 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Early Depression Detection") as demo:
|
| 138 |
+
|
| 139 |
+
# Header
|
| 140 |
+
gr.Markdown("# π§ Early Depression Detection via Mental-Longformer")
|
| 141 |
+
gr.Markdown(
|
| 142 |
+
"**MCP-enabled depression risk screening agent** using Mental-Longformer"
|
| 143 |
+
)
|
| 144 |
+
gr.Markdown(
|
| 145 |
+
"**Model Performance:** F1-Score: 0.7668 | Context Window: 4,096 tokens | Optimal Threshold: 60%"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
gr.Markdown(
|
| 149 |
+
"β οΈ **CRITICAL:** This model requires **extensive text input (10-15 posts or 300+ words)** "
|
| 150 |
+
"for accurate analysis. Short inputs yield unreliable results."
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
gr.Markdown(
|
| 154 |
+
"Built on Master's thesis research at University of Malaya using Gemini 2.5 Flash data augmentation."
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
with gr.Row():
|
| 158 |
+
gr.Markdown("[π Model Card](https://huggingface.co/mental/mental-longformer-depression)")
|
| 159 |
+
gr.Markdown("[π€ Author LinkedIn](https://www.linkedin.com/in/yourprofile)")
|
| 160 |
+
|
| 161 |
+
# Examples Section at Top
|
| 162 |
+
gr.Markdown("## π Example Inputs")
|
| 163 |
+
gr.Markdown(
|
| 164 |
+
"Click an example below to see how the model works. "
|
| 165 |
+
"Each category shows different risk levels based on linguistic patterns."
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
with gr.Row():
|
| 169 |
+
user_input = gr.Textbox(
|
| 170 |
+
label="π Enter User Text History (300+ words recommended)",
|
| 171 |
+
placeholder="Paste multiple social media posts, journal entries, or extended writing samples here...",
|
| 172 |
+
lines=15,
|
| 173 |
+
max_lines=20
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Three categories of examples
|
| 177 |
+
with gr.Accordion("π’ Low Risk Examples", open=False):
|
| 178 |
+
gr.Examples(
|
| 179 |
+
examples=[[LOW_RISK_EXAMPLE]],
|
| 180 |
+
inputs=user_input,
|
| 181 |
+
label="Positive, engaged content"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
with gr.Accordion("π‘ Moderate Risk Examples", open=False):
|
| 185 |
+
gr.Examples(
|
| 186 |
+
examples=[[MODERATE_RISK_EXAMPLE]],
|
| 187 |
+
inputs=user_input,
|
| 188 |
+
label="Some concerning patterns"
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
with gr.Accordion("π΄ High Risk Examples", open=True):
|
| 192 |
+
gr.Examples(
|
| 193 |
+
examples=[[HIGH_RISK_EXAMPLE]],
|
| 194 |
+
inputs=user_input,
|
| 195 |
+
label="Strong depression markers"
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# Buttons
|
| 199 |
+
with gr.Row():
|
| 200 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 201 |
+
submit_btn = gr.Button("Submit", variant="primary", size="lg")
|
| 202 |
+
|
| 203 |
+
# Output Section
|
| 204 |
+
risk_title = gr.Markdown("## Assessment Result")
|
| 205 |
+
output_text = gr.Markdown()
|
| 206 |
+
|
| 207 |
+
# Model Technical Details
|
| 208 |
+
with gr.Accordion("π Model Technical Details", open=False):
|
| 209 |
+
gr.Markdown("""
|
| 210 |
+
- **Score Breakdown:** Depression: 89.8% | Non-Depression: 10.2%
|
| 211 |
- **Decision Threshold:** 60% (optimal from training)
|
| 212 |
- **Model Context:** 4,096 tokens
|
| 213 |
- **Validation F1-Score:** 0.7668
|
| 214 |
+
""")
|
| 215 |
+
|
| 216 |
+
# Risk Scale Info
|
| 217 |
+
with gr.Accordion("βΉοΈ About the Risk Scale", open=False):
|
| 218 |
+
gr.Markdown("""
|
| 219 |
+
This model uses research-validated thresholds:
|
| 220 |
+
|
| 221 |
+
- π’ **Low Risk (0-30%):** Typical language patterns, no significant depression markers
|
| 222 |
+
- π‘ **Moderate Risk (30-60%):** Some concerning patterns, monitoring recommended
|
| 223 |
+
- π΄ **High Risk (60%+):** Strong depression markers, professional consultation recommended
|
| 224 |
|
| 225 |
+
The 60% threshold was optimized during training on eRisk datasets for maximum F1-score.
|
| 226 |
+
""")
|
| 227 |
+
|
| 228 |
+
# Ethical Use Section
|
| 229 |
+
with gr.Accordion("βοΈ Ethical Use", open=False):
|
| 230 |
+
gr.Markdown("""
|
| 231 |
+
This tool is for **research and screening purposes only**. It should never be used:
|
| 232 |
|
| 233 |
+
- As a substitute for professional diagnosis
|
| 234 |
+
- To make clinical decisions without human oversight
|
| 235 |
+
- For surveillance or discrimination
|
| 236 |
+
- Without informed consent
|
| 237 |
|
| 238 |
+
**May produce false positives/negatives**
|
| 239 |
+
""")
|
| 240 |
+
|
| 241 |
+
# Important Disclaimers
|
| 242 |
+
gr.Markdown("## β οΈ Important Disclaimers")
|
| 243 |
+
gr.Markdown("""
|
| 244 |
**This is NOT a diagnostic tool.** This model:
|
| 245 |
+
|
| 246 |
- Detects statistical patterns in language, not clinical depression
|
| 247 |
- Requires professional interpretation
|
| 248 |
- Cannot replace mental health assessment by qualified professionals
|
| 249 |
- May produce false positives/negatives
|
| 250 |
|
| 251 |
**If you're in crisis, help is available NOW:**
|
| 252 |
+
|
| 253 |
- π **Crisis Text Line:** Text HOME to 741741 (US)
|
| 254 |
- π **National Suicide Prevention Lifeline:** 988 (US)
|
| 255 |
+
- π **International:** [befrienders.org](https://www.befrienders.org)
|
| 256 |
+
""")
|
| 257 |
+
|
| 258 |
+
gr.Markdown(
|
| 259 |
+
"*Analysis based on linguistic patterns from eRisk datasets. "
|
| 260 |
+
"Consult healthcare professionals for mental health concerns.*"
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
with gr.Row():
|
| 264 |
+
gr.Button("π Share via Link", link="https://huggingface.co/spaces/avtak/depression-detection-mcp-agent")
|
| 265 |
+
|
| 266 |
+
# Event handlers
|
| 267 |
+
submit_btn.click(
|
| 268 |
+
fn=predict_depression,
|
| 269 |
+
inputs=user_input,
|
| 270 |
+
outputs=[risk_title, output_text, gr.Textbox(visible=False)]
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
clear_btn.click(
|
| 274 |
+
fn=lambda: ("", "## Assessment Result", ""),
|
| 275 |
+
inputs=None,
|
| 276 |
+
outputs=[user_input, risk_title, output_text]
|
| 277 |
+
)
|
| 278 |
|
| 279 |
+
# Launch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
if __name__ == "__main__":
|
| 281 |
+
demo.launch()
|