Spaces:
Paused
Paused
fix: Persist uploaded image across interactions to keep visualization visible
Browse filesProblem: Visualization panel was empty after initial analysis because the
uploaded image path wasn't persisted across multiple chat interactions.
Solution:
- Added gr.State to track current_image across interactions
- When no visualization is generated (e.g., grounding fails), show the
uploaded image as a fallback reference
- Image now stays visible throughout the conversation until replaced
This ensures users can always see their uploaded X-ray as reference.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -309,10 +309,17 @@ with gr.Blocks() as demo:
|
|
| 309 |
with gr.Column(scale=1):
|
| 310 |
viz_output = gr.Image(label="Visualization", height=600)
|
| 311 |
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
| 313 |
# Convert mode selection to internal mode string
|
| 314 |
mode = "socratic" if mode_selection == "Tutor Mode" else "assistant"
|
| 315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
# Get response and visualization with mode
|
| 317 |
bot_message, viz_image = chat(message, chat_history, mode)
|
| 318 |
|
|
@@ -332,9 +339,13 @@ with gr.Blocks() as demo:
|
|
| 332 |
chat_history.append({"role": "user", "content": user_text})
|
| 333 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 334 |
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
msg.submit(respond, [msg, chatbot, mode_toggle], [msg, chatbot, viz_output])
|
| 338 |
|
| 339 |
gr.Examples(
|
| 340 |
examples=[
|
|
|
|
| 309 |
with gr.Column(scale=1):
|
| 310 |
viz_output = gr.Image(label="Visualization", height=600)
|
| 311 |
|
| 312 |
+
# State to persist the uploaded image across interactions
|
| 313 |
+
current_image_state = gr.State(None)
|
| 314 |
+
|
| 315 |
+
def respond(message, chat_history, mode_selection, current_image):
|
| 316 |
# Convert mode selection to internal mode string
|
| 317 |
mode = "socratic" if mode_selection == "Tutor Mode" else "assistant"
|
| 318 |
|
| 319 |
+
# Track uploaded image - update state when new image is uploaded
|
| 320 |
+
if isinstance(message, dict) and message.get("files"):
|
| 321 |
+
current_image = message["files"][0]
|
| 322 |
+
|
| 323 |
# Get response and visualization with mode
|
| 324 |
bot_message, viz_image = chat(message, chat_history, mode)
|
| 325 |
|
|
|
|
| 339 |
chat_history.append({"role": "user", "content": user_text})
|
| 340 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 341 |
|
| 342 |
+
# If no visualization was generated, show the current uploaded image as reference
|
| 343 |
+
if viz_image is None and current_image is not None:
|
| 344 |
+
viz_image = current_image
|
| 345 |
+
|
| 346 |
+
return "", chat_history, viz_image, current_image
|
| 347 |
|
| 348 |
+
msg.submit(respond, [msg, chatbot, mode_toggle, current_image_state], [msg, chatbot, viz_output, current_image_state])
|
| 349 |
|
| 350 |
gr.Examples(
|
| 351 |
examples=[
|