Spaces:
Runtime error
Runtime error
Commit
Β·
474ac56
1
Parent(s):
03c8c47
testing conversational abilities
Browse files
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 |
-
"[
|
| 106 |
response,
|
| 107 |
"β",
|
| 108 |
"[THINKING] thinking:",
|
|
@@ -157,91 +157,142 @@ diagnosis = ""
|
|
| 157 |
|
| 158 |
|
| 159 |
# === Gradio Inference ===
|
| 160 |
-
def simulate_interaction(user_input,
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
summary = sum_out["output"]
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
history.append(f"Doctor: {q_out['output']}")
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
return {
|
| 184 |
"summary": sum_out,
|
| 185 |
"diagnosis": diag_out,
|
| 186 |
"question": q_out,
|
| 187 |
"treatment": treatment_out,
|
| 188 |
-
"conversation":
|
| 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 |
-
|
| 198 |
|
| 199 |
π©Ί Diagnosis:
|
| 200 |
π THINKING: {res['diagnosis']['thinking']}
|
| 201 |
-
|
| 202 |
-
|
| 203 |
β Follow-up Question:
|
| 204 |
π THINKING: {res['question']['thinking']}
|
| 205 |
-
|
| 206 |
|
| 207 |
π Treatment Plan:
|
| 208 |
-
{res['treatment']['
|
|
|
|
| 209 |
|
| 210 |
-
π¬ Conversation:
|
| 211 |
-
{res['conversation']}
|
| 212 |
"""
|
| 213 |
|
| 214 |
|
| 215 |
# === Stateful Gradio UI ===
|
| 216 |
def stateful_ui_fn(user_input, history):
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
#
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
fn=stateful_ui_fn,
|
| 235 |
inputs=[
|
| 236 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 237 |
gr.State(), # holds the conversation history
|
| 238 |
],
|
| 239 |
outputs=[
|
| 240 |
-
gr.Textbox(label="
|
| 241 |
gr.State(), # returns the updated history
|
| 242 |
],
|
| 243 |
-
title="π§ AI Doctor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
)
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|