avtak commited on
Commit
2838408
·
verified ·
1 Parent(s): 9bde0b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -68
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 re
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
- # Strictly preserving your requirement for double newlines
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. Focus on hobbies/events."
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 (With DeepSeek Cleaning) ---
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 (Mental-Longformer):
87
- - Risk Level: {risk_data['risk_level']}
88
- - Probability: {risk_data['probability']:.1%}
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 but professional.
97
- 4. Keep it under 100 words.
98
  """
99
 
100
  try:
101
- # --- SPONSOR 1: NEBIUS (DeepSeek R1) ---
102
- if provider == "Nebius (DeepSeek R1)":
103
  api_key = os.getenv("NEBIUS_API_KEY")
104
  if not api_key: return "⚠️ Nebius API Key missing."
105
 
106
- client = OpenAI(base_url="https://api.tokenfactory.nebius.com/v1/", api_key=api_key)
107
- model_id = "deepseek-ai/DeepSeek-R1" # Standard R1 ID
 
 
 
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(base_url="https://api.sambanova.ai/v1", api_key=api_key)
 
 
 
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=500 # Needs more tokens for thinking
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 (Visual Report) ---
139
-
140
  def full_analysis_pipeline(user_text, location, provider):
141
- if not user_text.strip(): return "Please enter text.", "No data"
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 Dashboard (HTML) - Fixed Spacing
156
- html_dashboard = f"""
157
- <div style="padding: 20px; border-radius: 12px; background-color: {data['color']}15; border: 1px solid {data['color']};">
158
- <h2 style="color: {data['color']}; margin: 0;">{data['risk_level']}</h2>
159
- <h3 style="margin: 0;">{data['probability']*100:.1f}% Match</h3>
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
- # 6. Build Text Report (Markdown)
173
- report_markdown = f"""
174
- ### 🤖 Agent Analysis ({provider})
175
  {explanation}
 
176
 
177
  ---
178
- ### 🆘 Recommended Resources ({location})
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=8,
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
- # UPDATED PROVIDER LABELS
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
- with gr.Column(scale=1):
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)