avtak's picture
Create app.py
c766646 verified
raw
history blame
3.24 kB
import gradio as gr
from transformers import pipeline
# Load your model
model_name = "avtak/erisk-longformer-depression-v1"
classifier = pipeline("text-classification", model=model_name, truncation=True, max_length=4096, top_k=None)
def analyze_text(user_text):
"""
Analyzes text for linguistic markers of depression using Mental-Longformer.
This MCP tool provides depression risk assessment based on long-form user histories.
Trained on eRisk datasets (2017-2022) with F1-score of 0.7668.
"""
if not user_text.strip():
return "⚠️ Please provide text to analyze."
results = classifier(user_text)[0]
# Get top prediction
top = max(results, key=lambda x: x['score'])
label = "πŸ”΄ Depressed Patterns Detected" if top['label'] == 'LABEL_1' else "βœ… Non-Depressed Patterns"
confidence = top['score'] * 100
output = f"""# {label}
**Confidence:** {confidence:.1f}%
## Detailed Scores:
"""
for r in sorted(results, key=lambda x: x['score'], reverse=True):
pattern = "Depressed" if r['label'] == 'LABEL_1' else "Non-Depressed"
output += f"- {pattern}: {r['score']*100:.1f}%\n"
output += """
---
⚠️ **DISCLAIMER:** Research tool only - not for medical diagnosis. Consult a healthcare professional for mental health concerns.
πŸ“ž **Crisis Resources:** Text HOME to 741741 (US) | Visit befrienders.org (International)
"""
return output
# Examples
example_depressed = """Post 1: Another sleepless night. Can't stop my mind from racing through every mistake I've made this week. I feel like such a failure.
Post 2: My friend invited me out but I cancelled. The thought of pretending to be okay is exhausting. I just want to be alone.
Post 3: I used to love gaming but now I can't even focus. Everything feels pointless. What's wrong with me?"""
example_normal = """Post 1: Started a new herb garden today! My basil is already sprouting. Feeling proud of this little project.
Post 2: Had a great workout this morning. The weather was perfect for a run. Ready to tackle the day!
Post 3: Dinner with friends tonight was so much fun. We tried that new Thai place and the food was amazing!"""
# Build interface
demo = gr.Interface(
fn=analyze_text,
inputs=gr.Textbox(
label="πŸ“ Enter User Text History",
placeholder="Paste multiple posts or long-form text (minimum 200 words recommended)...",
lines=10
),
outputs=gr.Markdown(label="πŸ“Š Analysis Results"),
title="🧠 Early Depression Detection via Mental-Longformer",
description="""
MCP-enabled agent for detecting depression risk from social media text.
**Model:** erisk-longformer-depression-v1 | **F1-Score:** 0.7668 | **Context:** 4,096 tokens
Built on Master's thesis research at University of Malaya using Gemini 2.5 Flash data augmentation.
πŸ”— [Model Card](https://huggingface.co/avtak/erisk-longformer-depression-v1) | πŸ‘€ [Author LinkedIn](https://www.linkedin.com/in/hassanzh/)
""",
examples=[
[example_depressed],
[example_normal]
],
theme=gr.themes.Soft()
)
# πŸ”₯ Enable MCP Server
if __name__ == "__main__":
demo.launch(mcp_server=True)