avtak commited on
Commit
926e080
Β·
verified Β·
1 Parent(s): 39f914f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -42
app.py CHANGED
@@ -62,9 +62,9 @@ def detect_depression_risk(text: str) -> dict:
62
  "word_count": len(processed_text.split()),
63
  }
64
 
65
- # --- 3. AGENT LOGIC (API uses dicts, UI uses tuples) ---
66
 
67
- def generate_response(api_history, risk_context, provider):
68
  if not risk_context:
69
  risk_context = {
70
  "risk_level": "Unknown",
@@ -96,7 +96,7 @@ YOUR GOAL: Provide supportive, scientifically-grounded chat. Max 2 sentences. Do
96
  """.strip()
97
 
98
  messages = [{"role": "system", "content": system_prompt}]
99
- messages.extend(api_history)
100
 
101
  try:
102
  resp = client.chat.completions.create(
@@ -109,11 +109,11 @@ YOUR GOAL: Provide supportive, scientifically-grounded chat. Max 2 sentences. Do
109
  except Exception as e:
110
  return f"⚠️ Error with {provider}: {e}"
111
 
112
- # --- 4. UI ORCHESTRATION ---
113
 
114
  def run_analysis(text, location, provider):
115
  if not text.strip():
116
- return None, [], [], None
117
 
118
  data = detect_depression_risk(text)
119
  resources = get_crisis_resources(location)
@@ -138,31 +138,26 @@ def run_analysis(text, location, provider):
138
  </div>
139
  """
140
 
141
- api_history = [{"role": "user", "content": "I just ran the analysis. Please explain my results."}]
142
- ai_msg = generate_response(api_history, data, provider)
143
- api_history.append({"role": "assistant", "content": ai_msg})
 
 
 
 
 
144
 
145
- # Chatbot expects list of tuples: [(user, bot), ...]
146
- ui_history = [(None, ai_msg)]
147
 
148
- return html_dashboard, ui_history, api_history, data
149
-
150
- def user_chat(user_message, ui_history, api_history, risk_data, provider):
151
  if not user_message:
152
- return ui_history, api_history, ""
153
-
154
- # For API
155
- api_history.append({"role": "user", "content": user_message})
156
-
157
- # For UI (append placeholder, then fill reply)
158
- ui_history.append((user_message, None))
159
-
160
- ai_msg = generate_response(api_history, risk_data, provider)
161
 
162
- api_history.append({"role": "assistant", "content": ai_msg})
163
- ui_history[-1] = (user_message, ai_msg)
164
-
165
- return ui_history, api_history, ""
166
 
167
  # --- 5. EXAMPLES ---
168
 
@@ -186,7 +181,6 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
186
  gr.Markdown("⚑ Powered by: SambaNova (Llama 3.3) & Nebius (Kimi K2)")
187
 
188
  risk_state = gr.State(None)
189
- api_state = gr.State([])
190
 
191
  with gr.Row():
192
  with gr.Column(scale=1):
@@ -199,9 +193,9 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
199
 
200
  gr.Markdown("### πŸ” Try Thesis Patterns")
201
  with gr.Row():
202
- btn_low = gr.Button("🟒 Low Risk", size="sm")
203
- btn_mod = gr.Button("🟑 Moderate", size="sm")
204
- btn_high = gr.Button("πŸ”΄ High Risk", size="sm")
205
 
206
  gr.Markdown("### βš™οΈ Settings")
207
  with gr.Row():
@@ -218,22 +212,17 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
218
 
219
  analyze_btn = gr.Button("πŸš€ Run Clinical Analysis", variant="primary", size="lg")
220
 
221
- with gr.Accordion("πŸ”§ MCP Tools Exposed", open=False):
222
- gr.Markdown("- `detect_depression_risk`\n- `get_crisis_resources`")
223
-
224
  with gr.Column(scale=1):
225
  dashboard = gr.HTML(label="Clinical Dashboard")
226
- # Chatbot uses list of (user, bot) tuples
227
- chatbot = gr.Chatbot(label="Agent Chat", height=400)
228
  msg_input = gr.Textbox(label="Chat with Agent", placeholder="Ask about your results or get advice...")
229
  send_btn = gr.Button("Send Message")
230
 
231
- # --- Wiring ---
232
-
233
  analyze_btn.click(
234
  run_analysis,
235
  inputs=[input_text, loc_drop, prov_drop],
236
- outputs=[dashboard, chatbot, api_state, risk_state],
237
  )
238
 
239
  btn_low.click(lambda: example_low, None, input_text)
@@ -242,13 +231,13 @@ with gr.Blocks(title="Depression Risk Agent") as demo:
242
 
243
  send_btn.click(
244
  user_chat,
245
- inputs=[msg_input, chatbot, api_state, risk_state, prov_drop],
246
- outputs=[chatbot, api_state, msg_input],
247
  )
248
  msg_input.submit(
249
  user_chat,
250
- inputs=[msg_input, chatbot, api_state, risk_state, prov_drop],
251
- outputs=[chatbot, api_state, msg_input],
252
  )
253
 
254
  if __name__ == "__main__":
 
62
  "word_count": len(processed_text.split()),
63
  }
64
 
65
+ # --- 3. AGENT LOGIC (messages format only) ---
66
 
67
+ def generate_response(history, risk_context, provider):
68
  if not risk_context:
69
  risk_context = {
70
  "risk_level": "Unknown",
 
96
  """.strip()
97
 
98
  messages = [{"role": "system", "content": system_prompt}]
99
+ messages.extend(history) # history is already [{"role": ..., "content": ...}]
100
 
101
  try:
102
  resp = client.chat.completions.create(
 
109
  except Exception as e:
110
  return f"⚠️ Error with {provider}: {e}"
111
 
112
+ # --- 4. UI ORCHESTRATION (Chatbot uses messages format) ---
113
 
114
  def run_analysis(text, location, provider):
115
  if not text.strip():
116
+ return None, [], None
117
 
118
  data = detect_depression_risk(text)
119
  resources = get_crisis_resources(location)
 
138
  </div>
139
  """
140
 
141
+ # Initial assistant message
142
+ history = [
143
+ {"role": "assistant", "content": generate_response(
144
+ [{"role": "user", "content": "I just ran the analysis. Please explain my results."}],
145
+ data,
146
+ provider
147
+ )}
148
+ ]
149
 
150
+ # dashboard HTML, chatbot messages (list[dict]), risk_data
151
+ return html_dashboard, history, data
152
 
153
+ def user_chat(user_message, history, risk_data, provider):
 
 
154
  if not user_message:
155
+ return history, ""
 
 
 
 
 
 
 
 
156
 
157
+ history.append({"role": "user", "content": user_message})
158
+ ai_msg = generate_response(history, risk_data, provider)
159
+ history.append({"role": "assistant", "content": ai_msg})
160
+ return history, ""
161
 
162
  # --- 5. EXAMPLES ---
163
 
 
181
  gr.Markdown("⚑ Powered by: SambaNova (Llama 3.3) & Nebius (Kimi K2)")
182
 
183
  risk_state = gr.State(None)
 
184
 
185
  with gr.Row():
186
  with gr.Column(scale=1):
 
193
 
194
  gr.Markdown("### πŸ” Try Thesis Patterns")
195
  with gr.Row():
196
+ btn_low = gr.Button("🟒 Low Risk", size="sm", variant="secondary")
197
+ btn_mod = gr.Button("🟑 Moderate", size="sm", variant="secondary")
198
+ btn_high = gr.Button("πŸ”΄ High Risk", size="sm", variant="secondary")
199
 
200
  gr.Markdown("### βš™οΈ Settings")
201
  with gr.Row():
 
212
 
213
  analyze_btn = gr.Button("πŸš€ Run Clinical Analysis", variant="primary", size="lg")
214
 
 
 
 
215
  with gr.Column(scale=1):
216
  dashboard = gr.HTML(label="Clinical Dashboard")
217
+ chatbot = gr.Chatbot(label="Agent Chat", height=400, type="messages")
 
218
  msg_input = gr.Textbox(label="Chat with Agent", placeholder="Ask about your results or get advice...")
219
  send_btn = gr.Button("Send Message")
220
 
221
+ # Wiring
 
222
  analyze_btn.click(
223
  run_analysis,
224
  inputs=[input_text, loc_drop, prov_drop],
225
+ outputs=[dashboard, chatbot, risk_state],
226
  )
227
 
228
  btn_low.click(lambda: example_low, None, input_text)
 
231
 
232
  send_btn.click(
233
  user_chat,
234
+ inputs=[msg_input, chatbot, risk_state, prov_drop],
235
+ outputs=[chatbot, msg_input],
236
  )
237
  msg_input.submit(
238
  user_chat,
239
+ inputs=[msg_input, chatbot, risk_state, prov_drop],
240
+ outputs=[chatbot, msg_input],
241
  )
242
 
243
  if __name__ == "__main__":