Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,31 +66,44 @@ def detect_depression_risk(text: str) -> dict:
|
|
| 66 |
}
|
| 67 |
|
| 68 |
# --- 4. AGENT REASONING (SambaNova + Nebius Kimi) ---
|
| 69 |
-
|
|
|
|
| 70 |
"""
|
| 71 |
Uses Sponsor APIs to generate the analysis report.
|
| 72 |
"""
|
| 73 |
client = None
|
| 74 |
model_id = None
|
| 75 |
|
| 76 |
-
# SYSTEM PROMPT
|
| 77 |
system_prompt = f"""
|
| 78 |
-
You are '
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
- Risk
|
| 82 |
-
- Biomarker: {risk_data['biomarker']}
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
"""
|
| 91 |
|
| 92 |
try:
|
| 93 |
-
# --- SPONSOR 1: NEBIUS (Kimi K2
|
| 94 |
if provider == "Nebius (Kimi K2)":
|
| 95 |
api_key = os.getenv("NEBIUS_API_KEY")
|
| 96 |
if not api_key: return "⚠️ Nebius API Key missing."
|
|
@@ -102,7 +115,7 @@ def agent_reasoning(text, risk_data, provider="SambaNova"):
|
|
| 102 |
model_id = "moonshotai/Kimi-K2-Instruct"
|
| 103 |
|
| 104 |
# --- SPONSOR 2: SAMBANOVA (Llama 3.3) ---
|
| 105 |
-
|
| 106 |
api_key = os.getenv("SAMBANOVA_API_KEY")
|
| 107 |
if not api_key: return "⚠️ SambaNova API Key missing."
|
| 108 |
|
|
@@ -140,19 +153,18 @@ def full_analysis_pipeline(user_text, location, provider):
|
|
| 140 |
# 3. Agent Reasoning
|
| 141 |
explanation = agent_reasoning(user_text, risk_data, provider)
|
| 142 |
|
| 143 |
-
# 4. Color Logic
|
| 144 |
-
color =
|
| 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 |
-
###
|
| 155 |
-
<div style="white-space: pre-wrap; font-family: sans-serif;">
|
| 156 |
{explanation}
|
| 157 |
</div>
|
| 158 |
|
|
@@ -204,7 +216,7 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
|
|
| 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 |
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
# --- 4. AGENT REASONING (SambaNova + Nebius Kimi) ---
|
| 69 |
+
# --- 4. AGENT REASONING (SambaNova + Nebius Kimi) ---
|
| 70 |
+
def agent_reasoning(text, risk_data, provider="SambaNova (Llama 3.3)"):
|
| 71 |
"""
|
| 72 |
Uses Sponsor APIs to generate the analysis report.
|
| 73 |
"""
|
| 74 |
client = None
|
| 75 |
model_id = None
|
| 76 |
|
| 77 |
+
# IMPROVED SYSTEM PROMPT
|
| 78 |
system_prompt = f"""
|
| 79 |
+
You are 'MindSight', a Clinical Research Agent specialized in linguistic biomarker analysis.
|
| 80 |
+
|
| 81 |
+
CONTEXTUAL DATA:
|
| 82 |
+
- Detected Risk: {risk_data['risk_level']} ({risk_data['probability']:.1%})
|
| 83 |
+
- Key Biomarker: {risk_data['biomarker']}
|
| 84 |
+
- Dataset Context: Based on eRisk longitudinal analysis (2017-2022).
|
| 85 |
+
|
| 86 |
+
USER TEXT SNIPPET: "{text[:1000]}..."
|
| 87 |
+
|
| 88 |
+
YOUR MISSION:
|
| 89 |
+
Synthesize the clinical data with the user's personal narrative to provide a supportive, research-backed insight.
|
| 90 |
+
|
| 91 |
+
GUIDELINES:
|
| 92 |
+
1. **Validate:** Start by acknowledging the specific struggles or sentiments expressed in the text (e.g., "I hear your exhaustion...").
|
| 93 |
+
2. **Connect:** Explain *why* the risk level was triggered using the Biomarker.
|
| 94 |
+
- If High Risk: Link sleep/energy complaints to the "Nocturnal Posting" pattern (activity spikes 00:00-05:00 UTC).
|
| 95 |
+
- If Moderate Risk: Link their focus on others to the "Supportive Responder" effect found in online echo chambers.
|
| 96 |
+
- If Low Risk: Affirm their "Healthy External Focus" (hobbies, work, events).
|
| 97 |
+
3. **Bridge:** Offer a gentle, non-medical bridge to the resources below.
|
| 98 |
+
|
| 99 |
+
CONSTRAINTS:
|
| 100 |
+
- Be warm but objective.
|
| 101 |
+
- NO medical diagnosis (use "suggests alignment with...").
|
| 102 |
+
- Max 80 words.
|
| 103 |
"""
|
| 104 |
|
| 105 |
try:
|
| 106 |
+
# --- SPONSOR 1: NEBIUS (Kimi K2) ---
|
| 107 |
if provider == "Nebius (Kimi K2)":
|
| 108 |
api_key = os.getenv("NEBIUS_API_KEY")
|
| 109 |
if not api_key: return "⚠️ Nebius API Key missing."
|
|
|
|
| 115 |
model_id = "moonshotai/Kimi-K2-Instruct"
|
| 116 |
|
| 117 |
# --- SPONSOR 2: SAMBANOVA (Llama 3.3) ---
|
| 118 |
+
else: # Default to SambaNova
|
| 119 |
api_key = os.getenv("SAMBANOVA_API_KEY")
|
| 120 |
if not api_key: return "⚠️ SambaNova API Key missing."
|
| 121 |
|
|
|
|
| 153 |
# 3. Agent Reasoning
|
| 154 |
explanation = agent_reasoning(user_text, risk_data, provider)
|
| 155 |
|
| 156 |
+
# 4. Color Logic (Fixed)
|
| 157 |
+
color = risk_data['color']
|
| 158 |
|
| 159 |
# 5. Build HTML/Markdown Report
|
|
|
|
| 160 |
return f"""
|
| 161 |
<div style="border-left: 6px solid {color}; padding-left: 15px; background-color: {color}10; border-radius: 5px; padding: 20px;">
|
| 162 |
<h2 style="color:{color}; margin:0;">{risk_data['risk_level']}</h2>
|
| 163 |
<h3 style="margin-top:5px;">Probability: {risk_data['probability']*100:.1f}%</h3>
|
| 164 |
</div>
|
| 165 |
|
| 166 |
+
### 👁️ MindSight Analysis ({provider})
|
| 167 |
+
<div style="white-space: pre-wrap; font-family: sans-serif; font-size: 1.1em;">
|
| 168 |
{explanation}
|
| 169 |
</div>
|
| 170 |
|
|
|
|
| 216 |
with gr.Row():
|
| 217 |
# RENAMED to be clearer
|
| 218 |
loc_dropdown = gr.Dropdown(["Global", "US", "Malaysia"], value="Malaysia", label="Crisis Resource Region")
|
| 219 |
+
provider_dropdown = gr.Dropdown(["SambaNova (Llama 3.3)", "Nebius (Kimi K2)"], value="SambaNova (Llama 3.3)", label="Reasoning Brain")
|
| 220 |
|
| 221 |
submit = gr.Button("🚀 Run Analysis Agent", variant="primary", size="lg")
|
| 222 |
|