Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from transformers import pipeline
|
|
| 3 |
from huggingface_hub import login
|
| 4 |
from openai import OpenAI
|
| 5 |
import os
|
| 6 |
-
import
|
| 7 |
|
| 8 |
# --- 1. SETUP ---
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
|
@@ -33,16 +33,17 @@ def detect_depression_risk(text: str) -> dict:
|
|
| 33 |
"""Analyzes text using Mental-Longformer (eRisk 2025)."""
|
| 34 |
|
| 35 |
# --- THESIS LOGIC: AGGREGATION ---
|
| 36 |
-
#
|
| 37 |
processed_text = text.replace("\n", "\n\n")
|
| 38 |
|
| 39 |
results = classifier(processed_text)[0]
|
| 40 |
prob = next((r['score'] for r in results if r['label'] == 'LABEL_1'), 0)
|
| 41 |
|
|
|
|
| 42 |
if prob < 0.40:
|
| 43 |
level = "Low Risk"
|
| 44 |
biomarker = "Healthy External Focus"
|
| 45 |
-
desc = "Matches 'Isolated Control' group.
|
| 46 |
color = "#10b981" # Green
|
| 47 |
elif 0.40 <= prob < 0.60:
|
| 48 |
level = "Moderate Risk"
|
|
@@ -64,14 +65,7 @@ def detect_depression_risk(text: str) -> dict:
|
|
| 64 |
"word_count": len(processed_text.split())
|
| 65 |
}
|
| 66 |
|
| 67 |
-
# --- 4. AGENT REASONING (
|
| 68 |
-
|
| 69 |
-
def clean_deepseek_output(text):
|
| 70 |
-
"""Removes the <think> tags from DeepSeek R1 models."""
|
| 71 |
-
# Regex to remove <think>...</think> content (dotall to match newlines)
|
| 72 |
-
cleaned = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 73 |
-
return cleaned.strip()
|
| 74 |
-
|
| 75 |
def agent_reasoning(text, risk_data, provider="SambaNova"):
|
| 76 |
"""
|
| 77 |
Uses Sponsor APIs to generate the analysis report.
|
|
@@ -79,39 +73,43 @@ def agent_reasoning(text, risk_data, provider="SambaNova"):
|
|
| 79 |
client = None
|
| 80 |
model_id = None
|
| 81 |
|
| 82 |
-
# PROMPT
|
| 83 |
system_prompt = f"""
|
| 84 |
You are 'Dr. Longformer', an empathetic Clinical AI Research Agent.
|
| 85 |
|
| 86 |
-
CLINICAL DATA
|
| 87 |
-
- Risk Level: {risk_data['risk_level']}
|
| 88 |
-
-
|
| 89 |
-
- Input Length: {risk_data['word_count']} words
|
| 90 |
|
| 91 |
USER TEXT SNIPPET: "{text[:800]}..."
|
| 92 |
|
| 93 |
INSTRUCTIONS:
|
| 94 |
1. Acknowledge the user's situation based on the text.
|
| 95 |
2. Explain the risk level using thesis terms: 'Nocturnal Posting' (High), 'Supportive Responder' (Moderate), or 'Healthy External Focus' (Low).
|
| 96 |
-
3. Be compassionate
|
| 97 |
-
4. Keep it under 100 words.
|
| 98 |
"""
|
| 99 |
|
| 100 |
try:
|
| 101 |
-
# --- SPONSOR 1: NEBIUS (
|
| 102 |
-
if provider == "Nebius (
|
| 103 |
api_key = os.getenv("NEBIUS_API_KEY")
|
| 104 |
if not api_key: return "⚠️ Nebius API Key missing."
|
| 105 |
|
| 106 |
-
client = OpenAI(
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# --- SPONSOR 2: SAMBANOVA (Llama 3.3) ---
|
| 110 |
elif provider == "SambaNova":
|
| 111 |
api_key = os.getenv("SAMBANOVA_API_KEY")
|
| 112 |
if not api_key: return "⚠️ SambaNova API Key missing."
|
| 113 |
|
| 114 |
-
client = OpenAI(
|
|
|
|
|
|
|
|
|
|
| 115 |
model_id = "Meta-Llama-3.3-70B-Instruct"
|
| 116 |
|
| 117 |
# EXECUTE
|
|
@@ -122,23 +120,16 @@ def agent_reasoning(text, risk_data, provider="SambaNova"):
|
|
| 122 |
{"role": "user", "content": "Analyze this."}
|
| 123 |
],
|
| 124 |
temperature=0.6,
|
| 125 |
-
max_tokens=
|
| 126 |
)
|
| 127 |
-
|
| 128 |
-
raw_output = response.choices[0].message.content
|
| 129 |
-
|
| 130 |
-
# CLEANUP: Remove the "Thinking" part if it's DeepSeek
|
| 131 |
-
final_output = clean_deepseek_output(raw_output)
|
| 132 |
-
|
| 133 |
-
return final_output
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
return f"Reasoning Error ({provider}): {str(e)}"
|
| 137 |
|
| 138 |
-
# --- 5. PIPELINE
|
| 139 |
-
|
| 140 |
def full_analysis_pipeline(user_text, location, provider):
|
| 141 |
-
if not user_text.strip(): return "Please enter text."
|
| 142 |
|
| 143 |
# 1. Run Tool
|
| 144 |
risk_data = detect_depression_risk(user_text)
|
|
@@ -152,60 +143,54 @@ def full_analysis_pipeline(user_text, location, provider):
|
|
| 152 |
# 4. Color Logic
|
| 153 |
color = "green" if risk_data['probability'] < 0.4 else "orange" if risk_data['probability'] < 0.6 else "red"
|
| 154 |
|
| 155 |
-
# 5. Build
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
<
|
| 160 |
-
|
| 161 |
-
<div style="background-color: #e5e7eb; border-radius: 9999px; height: 10px; margin-top: 10px; width: 100%;">
|
| 162 |
-
<div style="background-color: {data['color']}; height: 10px; border-radius: 9999px; width: {data['probability']*100}%;"></div>
|
| 163 |
-
</div>
|
| 164 |
-
|
| 165 |
-
<div style="margin-top: 15px; white-space: pre-wrap;">
|
| 166 |
-
<p><strong>🧠 Thesis Biomarker:</strong> {data['biomarker']}</p>
|
| 167 |
-
<p style="font-size: 0.9em; opacity: 0.8;">{data['description']}</p>
|
| 168 |
-
</div>
|
| 169 |
</div>
|
| 170 |
-
"""
|
| 171 |
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
-
### 🤖 Agent Analysis ({provider})
|
| 175 |
{explanation}
|
|
|
|
| 176 |
|
| 177 |
---
|
| 178 |
-
### 🆘 Recommended Resources
|
| 179 |
{resources}
|
| 180 |
"""
|
| 181 |
-
|
| 182 |
-
return html_dashboard, report_markdown
|
| 183 |
|
| 184 |
# --- 6. EXAMPLES ---
|
| 185 |
example_low = """The new update for the Linux kernel (6.8) finally addressed the driver issues I was seeing with Realtek cards. I read the changelog on kernel.org and noticed they patched the module specific to the rtl8821ce chipset.
|
|
|
|
| 186 |
On a different note, the defensive stats for the basketball team have improved significantly. Allowing 15% fewer points per possession is a game changer.
|
|
|
|
| 187 |
I also spent some time woodworking. I'm building a walnut coffee table and the grain is tricky to plane. I might switch to a polyurethane finish for durability."""
|
| 188 |
|
| 189 |
example_mod = """Things have been pretty busy at work. We're in the middle of a big project and deadlines are tight. Staying a bit later than usual but that's just how it goes sometimes. Team is handling it well overall.
|
|
|
|
| 190 |
Sleep could be better. Been staying up too late scrolling my phone. Need to work on that. Usually feel okay once I get moving in the morning though.
|
|
|
|
| 191 |
Went hiking with some friends last weekend which was nice. Good to get outside and move around. We're talking about doing another trip next month. Weather should be better by then."""
|
| 192 |
|
| 193 |
example_high = """I don't know why I even bother getting out of bed anymore. I slept for 12 hours yesterday and I'm still exhausted. It feels like my limbs weigh a thousand pounds.
|
|
|
|
| 194 |
I've been avoiding my friends for weeks. They keep texting me, but I can't bring myself to reply. The thought of socializing is terrifying.
|
|
|
|
| 195 |
Everything feels like a shade of grey. I can't concentrate on my work. I feel like I'm drowning while everyone else is breathing fine. I was up until 4 AM again last night just staring at the ceiling."""
|
| 196 |
|
| 197 |
# --- 7. UI ---
|
| 198 |
with gr.Blocks(title="Depression Risk Agent") as demo:
|
| 199 |
gr.Markdown("# 🧠 Early Depression Detection Agent (MCP)")
|
| 200 |
-
gr.Markdown("Agentic system using **Mental-Longformer** (Tool) + **Multi-Provider Reasoning**.")
|
| 201 |
-
gr.Markdown("⚡ **Powered by:** [SambaNova](https://sambanova.ai/) (Llama 3.3) & [Nebius](https://nebius.com/) (DeepSeek R1)")
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
with gr.Column(scale=1):
|
| 205 |
gr.Markdown("### 1. User History")
|
| 206 |
input_text = gr.Textbox(
|
| 207 |
label="User Timeline",
|
| 208 |
-
lines=
|
| 209 |
placeholder="[Post 1] ...\n\n[Post 2] ...\n\n(Context >100 words recommended)"
|
| 210 |
)
|
| 211 |
|
|
@@ -219,11 +204,14 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
|
|
| 219 |
with gr.Row():
|
| 220 |
# RENAMED to be clearer
|
| 221 |
loc_dropdown = gr.Dropdown(["Global", "US", "Malaysia"], value="Malaysia", label="Crisis Resource Region")
|
| 222 |
-
|
| 223 |
-
provider_dropdown = gr.Dropdown(["SambaNova", "Nebius (DeepSeek R1)"], value="SambaNova", label="Reasoning Brain")
|
| 224 |
|
| 225 |
submit = gr.Button("🚀 Run Analysis Agent", variant="primary", size="lg")
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
with gr.Accordion("🔧 MCP Tools Exposed", open=True):
|
| 228 |
gr.Markdown("""
|
| 229 |
The following functions are exposed to the MCP Client:
|
|
@@ -231,14 +219,7 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
|
|
| 231 |
- `get_crisis_resources(location)`: Returns localized help.
|
| 232 |
""")
|
| 233 |
|
| 234 |
-
|
| 235 |
-
# OUTPUTS: ONE FOR HTML, ONE FOR MARKDOWN
|
| 236 |
-
out_dashboard = gr.HTML(label="Clinical Dashboard")
|
| 237 |
-
out_report = gr.Markdown(label="Agent Report")
|
| 238 |
-
|
| 239 |
-
# WIRING
|
| 240 |
-
# Ensure correct mapping: input_text -> full_analysis -> [dashboard, report]
|
| 241 |
-
submit.click(full_analysis_pipeline, inputs=[input_text, loc_dropdown, provider_dropdown], outputs=[out_dashboard, out_report])
|
| 242 |
|
| 243 |
btn_low.click(lambda: example_low, None, input_text)
|
| 244 |
btn_mod.click(lambda: example_mod, None, input_text)
|
|
|
|
| 3 |
from huggingface_hub import login
|
| 4 |
from openai import OpenAI
|
| 5 |
import os
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
# --- 1. SETUP ---
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 33 |
"""Analyzes text using Mental-Longformer (eRisk 2025)."""
|
| 34 |
|
| 35 |
# --- THESIS LOGIC: AGGREGATION ---
|
| 36 |
+
# 1. We replace single newlines with double newlines for the MODEL
|
| 37 |
processed_text = text.replace("\n", "\n\n")
|
| 38 |
|
| 39 |
results = classifier(processed_text)[0]
|
| 40 |
prob = next((r['score'] for r in results if r['label'] == 'LABEL_1'), 0)
|
| 41 |
|
| 42 |
+
# Thesis Thresholds (Figure 4.15)
|
| 43 |
if prob < 0.40:
|
| 44 |
level = "Low Risk"
|
| 45 |
biomarker = "Healthy External Focus"
|
| 46 |
+
desc = "Matches 'Isolated Control' group. High lexical diversity, focus on hobbies/events."
|
| 47 |
color = "#10b981" # Green
|
| 48 |
elif 0.40 <= prob < 0.60:
|
| 49 |
level = "Moderate Risk"
|
|
|
|
| 65 |
"word_count": len(processed_text.split())
|
| 66 |
}
|
| 67 |
|
| 68 |
+
# --- 4. AGENT REASONING (SambaNova + Nebius Kimi) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def agent_reasoning(text, risk_data, provider="SambaNova"):
|
| 70 |
"""
|
| 71 |
Uses Sponsor APIs to generate the analysis report.
|
|
|
|
| 73 |
client = None
|
| 74 |
model_id = None
|
| 75 |
|
| 76 |
+
# SYSTEM PROMPT
|
| 77 |
system_prompt = f"""
|
| 78 |
You are 'Dr. Longformer', an empathetic Clinical AI Research Agent.
|
| 79 |
|
| 80 |
+
CLINICAL DATA:
|
| 81 |
+
- Risk Level: {risk_data['risk_level']} ({risk_data['probability']:.1%})
|
| 82 |
+
- Biomarker: {risk_data['biomarker']}
|
|
|
|
| 83 |
|
| 84 |
USER TEXT SNIPPET: "{text[:800]}..."
|
| 85 |
|
| 86 |
INSTRUCTIONS:
|
| 87 |
1. Acknowledge the user's situation based on the text.
|
| 88 |
2. Explain the risk level using thesis terms: 'Nocturnal Posting' (High), 'Supportive Responder' (Moderate), or 'Healthy External Focus' (Low).
|
| 89 |
+
3. Be compassionate. Max 100 words.
|
|
|
|
| 90 |
"""
|
| 91 |
|
| 92 |
try:
|
| 93 |
+
# --- SPONSOR 1: NEBIUS (Kimi K2 Thinking) ---
|
| 94 |
+
if provider == "Nebius (Kimi K2)":
|
| 95 |
api_key = os.getenv("NEBIUS_API_KEY")
|
| 96 |
if not api_key: return "⚠️ Nebius API Key missing."
|
| 97 |
|
| 98 |
+
client = OpenAI(
|
| 99 |
+
base_url="https://api.tokenfactory.nebius.com/v1/",
|
| 100 |
+
api_key=api_key
|
| 101 |
+
)
|
| 102 |
+
model_id = "moonshotai/Kimi-K2-Thinking"
|
| 103 |
|
| 104 |
# --- SPONSOR 2: SAMBANOVA (Llama 3.3) ---
|
| 105 |
elif provider == "SambaNova":
|
| 106 |
api_key = os.getenv("SAMBANOVA_API_KEY")
|
| 107 |
if not api_key: return "⚠️ SambaNova API Key missing."
|
| 108 |
|
| 109 |
+
client = OpenAI(
|
| 110 |
+
base_url="https://api.sambanova.ai/v1",
|
| 111 |
+
api_key=api_key
|
| 112 |
+
)
|
| 113 |
model_id = "Meta-Llama-3.3-70B-Instruct"
|
| 114 |
|
| 115 |
# EXECUTE
|
|
|
|
| 120 |
{"role": "user", "content": "Analyze this."}
|
| 121 |
],
|
| 122 |
temperature=0.6,
|
| 123 |
+
max_tokens=300
|
| 124 |
)
|
| 125 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
return f"Reasoning Error ({provider}): {str(e)}"
|
| 129 |
|
| 130 |
+
# --- 5. PIPELINE ---
|
|
|
|
| 131 |
def full_analysis_pipeline(user_text, location, provider):
|
| 132 |
+
if not user_text.strip(): return "Please enter text."
|
| 133 |
|
| 134 |
# 1. Run Tool
|
| 135 |
risk_data = detect_depression_risk(user_text)
|
|
|
|
| 143 |
# 4. Color Logic
|
| 144 |
color = "green" if risk_data['probability'] < 0.4 else "orange" if risk_data['probability'] < 0.6 else "red"
|
| 145 |
|
| 146 |
+
# 5. Build HTML/Markdown Report
|
| 147 |
+
# FIX: Added 'white-space: pre-wrap' to preserve newlines in display
|
| 148 |
+
return f"""
|
| 149 |
+
<div style="border-left: 6px solid {color}; padding-left: 15px; background-color: {color}10; border-radius: 5px; padding: 20px;">
|
| 150 |
+
<h2 style="color:{color}; margin:0;">{risk_data['risk_level']}</h2>
|
| 151 |
+
<h3 style="margin-top:5px;">Probability: {risk_data['probability']*100:.1f}%</h3>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
</div>
|
|
|
|
| 153 |
|
| 154 |
+
### 🧠 Agent Analysis ({provider})
|
| 155 |
+
<div style="white-space: pre-wrap; font-family: sans-serif;">
|
|
|
|
| 156 |
{explanation}
|
| 157 |
+
</div>
|
| 158 |
|
| 159 |
---
|
| 160 |
+
### 🆘 Recommended Resources
|
| 161 |
{resources}
|
| 162 |
"""
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# --- 6. EXAMPLES ---
|
| 165 |
example_low = """The new update for the Linux kernel (6.8) finally addressed the driver issues I was seeing with Realtek cards. I read the changelog on kernel.org and noticed they patched the module specific to the rtl8821ce chipset.
|
| 166 |
+
|
| 167 |
On a different note, the defensive stats for the basketball team have improved significantly. Allowing 15% fewer points per possession is a game changer.
|
| 168 |
+
|
| 169 |
I also spent some time woodworking. I'm building a walnut coffee table and the grain is tricky to plane. I might switch to a polyurethane finish for durability."""
|
| 170 |
|
| 171 |
example_mod = """Things have been pretty busy at work. We're in the middle of a big project and deadlines are tight. Staying a bit later than usual but that's just how it goes sometimes. Team is handling it well overall.
|
| 172 |
+
|
| 173 |
Sleep could be better. Been staying up too late scrolling my phone. Need to work on that. Usually feel okay once I get moving in the morning though.
|
| 174 |
+
|
| 175 |
Went hiking with some friends last weekend which was nice. Good to get outside and move around. We're talking about doing another trip next month. Weather should be better by then."""
|
| 176 |
|
| 177 |
example_high = """I don't know why I even bother getting out of bed anymore. I slept for 12 hours yesterday and I'm still exhausted. It feels like my limbs weigh a thousand pounds.
|
| 178 |
+
|
| 179 |
I've been avoiding my friends for weeks. They keep texting me, but I can't bring myself to reply. The thought of socializing is terrifying.
|
| 180 |
+
|
| 181 |
Everything feels like a shade of grey. I can't concentrate on my work. I feel like I'm drowning while everyone else is breathing fine. I was up until 4 AM again last night just staring at the ceiling."""
|
| 182 |
|
| 183 |
# --- 7. UI ---
|
| 184 |
with gr.Blocks(title="Depression Risk Agent") as demo:
|
| 185 |
gr.Markdown("# 🧠 Early Depression Detection Agent (MCP)")
|
| 186 |
+
gr.Markdown("Agentic system using **Mental-Longformer** (Tool) + **Multi-Provider Reasoning** (Nebius/SambaNova).")
|
|
|
|
| 187 |
|
| 188 |
with gr.Row():
|
| 189 |
with gr.Column(scale=1):
|
| 190 |
gr.Markdown("### 1. User History")
|
| 191 |
input_text = gr.Textbox(
|
| 192 |
label="User Timeline",
|
| 193 |
+
lines=10,
|
| 194 |
placeholder="[Post 1] ...\n\n[Post 2] ...\n\n(Context >100 words recommended)"
|
| 195 |
)
|
| 196 |
|
|
|
|
| 204 |
with gr.Row():
|
| 205 |
# RENAMED to be clearer
|
| 206 |
loc_dropdown = gr.Dropdown(["Global", "US", "Malaysia"], value="Malaysia", label="Crisis Resource Region")
|
| 207 |
+
provider_dropdown = gr.Dropdown(["SambaNova", "Nebius (Kimi K2)"], value="SambaNova", label="Reasoning Brain")
|
|
|
|
| 208 |
|
| 209 |
submit = gr.Button("🚀 Run Analysis Agent", variant="primary", size="lg")
|
| 210 |
|
| 211 |
+
with gr.Column(scale=1):
|
| 212 |
+
gr.Markdown("### 4. Agent Response")
|
| 213 |
+
output = gr.Markdown(label="Response")
|
| 214 |
+
|
| 215 |
with gr.Accordion("🔧 MCP Tools Exposed", open=True):
|
| 216 |
gr.Markdown("""
|
| 217 |
The following functions are exposed to the MCP Client:
|
|
|
|
| 219 |
- `get_crisis_resources(location)`: Returns localized help.
|
| 220 |
""")
|
| 221 |
|
| 222 |
+
submit.click(full_analysis_pipeline, inputs=[input_text, loc_dropdown, provider_dropdown], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
btn_low.click(lambda: example_low, None, input_text)
|
| 225 |
btn_mod.click(lambda: example_mod, None, input_text)
|