CodCodingCode commited on
Commit
474ac56
Β·
1 Parent(s): 03c8c47

testing conversational abilities

Browse files
Files changed (1) hide show
  1. app.py +97 -46
app.py CHANGED
@@ -102,7 +102,7 @@ class RoleAgent:
102
  thinking = block.split("ANSWER:", 1)[0].strip()
103
  answer = block.split("ANSWER:", 1)[1].strip()
104
  print(
105
- "[DEBUG] thinking/answer split:",
106
  response,
107
  "β†’",
108
  "[THINKING] thinking:",
@@ -157,91 +157,142 @@ diagnosis = ""
157
 
158
 
159
  # === Gradio Inference ===
160
- def simulate_interaction(user_input, iterations=1):
161
- history = [f"Doctor: What brings you in today?", f"Patient: {user_input}"]
162
- summary, diagnosis = "", ""
163
- treatment_out = None
 
 
 
164
 
165
- for i in range(iterations):
166
- # Summarize
167
- sum_in = "\n".join(history) + f"\nPrevious Vignette: {summary}"
168
- sum_out = summarizer.act(sum_in)
169
- summary = sum_out["output"]
170
 
171
- # Diagnose
172
- diag_out = diagnoser.act(summary)
173
- diagnosis = diag_out["output"]
174
 
175
- # Question
176
- q_in = f"Vignette: {summary}\n" f"Current Estimated Diagnosis:\n" f"{diagnosis}"
177
- q_out = questioner.act(q_in)
178
- history.append(f"Doctor: {q_out['output']}")
179
 
180
- # Treatment
181
- treatment_out = treatment_agent.act(f"Diagnosis: {diagnosis}\n")
 
 
 
182
 
183
  return {
184
  "summary": sum_out,
185
  "diagnosis": diag_out,
186
  "question": q_out,
187
  "treatment": treatment_out,
188
- "conversation": "\n".join(history),
189
  }
190
 
191
 
192
  # === Gradio UI ===
193
  def ui_fn(user_input):
 
194
  res = simulate_interaction(user_input)
195
  return f"""πŸ“‹ Vignette Summary:
196
  πŸ’­ THINKING: {res['summary']['thinking']}
197
- ANSWER: {res['summary']['output']}
198
 
199
  🩺 Diagnosis:
200
  πŸ’­ THINKING: {res['diagnosis']['thinking']}
201
- ANSWER: {res['diagnosis']['output']}
202
- T
203
  ❓ Follow-up Question:
204
  πŸ’­ THINKING: {res['question']['thinking']}
205
- ANSWER: {res['question']['output']}
206
 
207
  πŸ’Š Treatment Plan:
208
- {res['treatment']['output']}
 
209
 
210
- πŸ’¬ Conversation:
211
- {res['conversation']}
212
  """
213
 
214
 
215
  # === Stateful Gradio UI ===
216
  def stateful_ui_fn(user_input, history):
217
- # Initialize or retrieve history list
218
- history = history or []
219
- # Append the new patient utterance
220
- history.append(f"Patient: {user_input}")
221
- # Run one round of simulation
222
- res = simulate_interaction(user_input)
223
- # Extract last doctor line from the fresh conversation
224
- last_line = res["question"]["output"]
225
- # Append the doctor's new line
226
- history.append(last_line)
227
- # Build the displayed conversation
228
- convo = "\n".join(history)
229
- # Return both the display text and updated history
230
- return convo, history
 
 
 
 
 
 
 
 
 
231
 
 
 
 
 
232
 
233
- demo = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
234
  fn=stateful_ui_fn,
235
  inputs=[
236
- gr.Textbox(label="Patient Response"),
 
 
 
237
  gr.State(), # holds the conversation history
238
  ],
239
  outputs=[
240
- gr.Textbox(label="Doctor Simulation Output"),
241
  gr.State(), # returns the updated history
242
  ],
243
- title="🧠 AI Doctor Multi-Agent Reasoning",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  )
245
 
246
  if __name__ == "__main__":
247
- demo.launch(share=True)
 
 
 
 
102
  thinking = block.split("ANSWER:", 1)[0].strip()
103
  answer = block.split("ANSWER:", 1)[1].strip()
104
  print(
105
+ "[THINKING ANSWER SPLIT] thinking/answer split:",
106
  response,
107
  "β†’",
108
  "[THINKING] thinking:",
 
157
 
158
 
159
  # === Gradio Inference ===
160
+ def simulate_interaction(user_input, conversation_history=None):
161
+ """Single turn interaction - no iterations, uses accumulated history"""
162
+ if conversation_history is None:
163
+ history = [f"Doctor: What brings you in today?", f"Patient: {user_input}"]
164
+ else:
165
+ history = conversation_history.copy()
166
+ history.append(f"Patient: {user_input}")
167
 
168
+ # Summarize the full conversation history
169
+ sum_in = "\n".join(history)
170
+ sum_out = summarizer.act(sum_in)
171
+ summary = sum_out["output"]
 
172
 
173
+ # Diagnose based on summary
174
+ diag_out = diagnoser.act(summary)
175
+ diagnosis = diag_out["output"]
176
 
177
+ # Generate next question based on current understanding
178
+ q_in = f"Vignette: {summary}\nCurrent Estimated Diagnosis: {diagnosis}"
179
+ q_out = questioner.act(q_in)
 
180
 
181
+ # Add doctor's response to history
182
+ history.append(f"Doctor: {q_out['output']}")
183
+
184
+ # Generate treatment plan (but don't end conversation)
185
+ treatment_out = treatment_agent.act(f"Diagnosis: {diagnosis}\nVignette: {summary}")
186
 
187
  return {
188
  "summary": sum_out,
189
  "diagnosis": diag_out,
190
  "question": q_out,
191
  "treatment": treatment_out,
192
+ "conversation": history, # Return full history list
193
  }
194
 
195
 
196
  # === Gradio UI ===
197
  def ui_fn(user_input):
198
+ """Non-stateful version for testing"""
199
  res = simulate_interaction(user_input)
200
  return f"""πŸ“‹ Vignette Summary:
201
  πŸ’­ THINKING: {res['summary']['thinking']}
202
+ πŸ“ SUMMARY: {res['summary']['output']}
203
 
204
  🩺 Diagnosis:
205
  πŸ’­ THINKING: {res['diagnosis']['thinking']}
206
+ πŸ” DIAGNOSIS: {res['diagnosis']['output']}
207
+
208
  ❓ Follow-up Question:
209
  πŸ’­ THINKING: {res['question']['thinking']}
210
+ πŸ‘¨β€βš•οΈ DOCTOR: {res['question']['output']}
211
 
212
  πŸ’Š Treatment Plan:
213
+ πŸ’­ THINKING: {res['treatment']['thinking']}
214
+ πŸ“‹ TREATMENT: {res['treatment']['output']}
215
 
216
+ πŸ’¬ Full Conversation:
217
+ {chr(10).join(res['conversation'])}
218
  """
219
 
220
 
221
  # === Stateful Gradio UI ===
222
  def stateful_ui_fn(user_input, history):
223
+ """Proper stateful conversation handler"""
224
+ # Initialize history if first interaction
225
+ if history is None:
226
+ history = []
227
+
228
+ # Run one turn of interaction with accumulated history
229
+ res = simulate_interaction(user_input, history)
230
+
231
+ # Get the updated conversation history
232
+ updated_history = res["conversation"]
233
+
234
+ # Format the display output
235
+ display_output = f"""πŸ’¬ Conversation:
236
+ {chr(10).join(updated_history)}
237
+
238
+ πŸ“‹ Current Assessment:
239
+ πŸ” Diagnosis: {res['diagnosis']['output']}
240
+ πŸ’Š Treatment Plan: {res['treatment']['output']}
241
+ """
242
+
243
+ # Return display text and updated history for next turn
244
+ return display_output, updated_history
245
+
246
 
247
+ def chat_interface(user_input, history):
248
+ """Alternative chat-style interface"""
249
+ if history is None:
250
+ history = []
251
 
252
+ # Run interaction
253
+ res = simulate_interaction(user_input, history)
254
+ updated_history = res["conversation"]
255
+
256
+ # Return just the doctor's latest response and updated history
257
+ doctor_response = res["question"]["output"]
258
+
259
+ return doctor_response, updated_history
260
+
261
+
262
+ # Create two different interfaces
263
+ demo_stateful = gr.Interface(
264
  fn=stateful_ui_fn,
265
  inputs=[
266
+ gr.Textbox(
267
+ label="Patient Response",
268
+ placeholder="Describe your symptoms or answer the doctor's question...",
269
+ ),
270
  gr.State(), # holds the conversation history
271
  ],
272
  outputs=[
273
+ gr.Textbox(label="Medical Consultation", lines=15),
274
  gr.State(), # returns the updated history
275
  ],
276
+ title="🧠 AI Doctor - Full Medical Consultation",
277
+ description="Have a conversation with an AI doctor. Each response builds on the previous conversation.",
278
+ )
279
+
280
+ demo_chat = gr.Interface(
281
+ fn=chat_interface,
282
+ inputs=[
283
+ gr.Textbox(label="Your Response", placeholder="Tell me about your symptoms..."),
284
+ gr.State(),
285
+ ],
286
+ outputs=[
287
+ gr.Textbox(label="Doctor", lines=5),
288
+ gr.State(),
289
+ ],
290
+ title="🩺 AI Doctor Chat",
291
+ description="Simple chat interface with the AI doctor.",
292
  )
293
 
294
  if __name__ == "__main__":
295
+ # Launch the stateful version by default
296
+ demo_stateful.launch(share=True)
297
+ # Uncomment the line below to use the chat version instead:
298
+ # demo_chat.launch(share=True)