@@ -299,12 +299,15 @@ def handle_chat(
299299 ), gr .update (), gr .update (interactive = False )
300300
301301 # ------------------------------------------------------------------
302- # Resize uploaded image files (max 500×500 px, aspect ratio kept ).
303- # The resized paths are used for BOTH the preview and the backend .
302+ # Resize uploaded image files for preview only (max 500×500 px).
303+ # Original paths are kept for backend format detection .
304304 # Non-image files (DICOM, NIfTI, CSV, …) are passed through as-is.
305305 # ------------------------------------------------------------------
306+ original_paths : List [str ] = []
307+ resized_paths : List [str ] = []
308+ resize_temps : List [str ] = []
309+
306310 if files :
307- resized_files : List [str ] = []
308311 for f in files :
309312 if isinstance (f , str ):
310313 raw_path = f
@@ -315,41 +318,45 @@ def handle_chat(
315318 else :
316319 raw_path = str (f )
317320 if raw_path :
318- resized_files .append (resize_uploaded_image (raw_path ))
319- files = resized_files # replace with resized versions for all downstream use
321+ original_paths .append (raw_path )
322+ resized = resize_uploaded_image (raw_path )
323+ resized_paths .append (resized )
324+ if resized != raw_path :
325+ resize_temps .append (resized )
320326
321327 # If files were uploaded, build and show preview immediately
322- if files :
323- file_paths = []
324- for f in files :
325- if isinstance (f , str ):
326- file_paths .append (f )
327- elif hasattr (f , "name" ):
328- file_paths .append (f .name )
329-
330- if file_paths :
331- # Build preview
332- try :
333- preview_path , meta_text = _build_preview_for_vlm (file_paths )
334- if preview_path :
335- # Show preview message
336- preview_text = "📋 **Preview for analysis:**"
337- if meta_text :
338- preview_text += f"\n \n _{ meta_text } _"
339- history .append (
340- {"role" : "assistant" , "content" : preview_text }
341- )
342- history .append (
343- {
344- "role" : "assistant" ,
345- "content" : {"path" : preview_path },
346- }
347- )
348- yield history , state_dict , gr .update (), gr .update (), gr .update (), gr .update (), None , gr .update (
349- visible = False
350- ), gr .update (), gr .update ()
351- except Exception as e :
352- log .warning ("Preview generation failed: %r" , e )
328+ if original_paths :
329+ try :
330+ preview_path , meta_text = _build_preview_for_vlm (
331+ resized_paths , metadata_paths = original_paths
332+ )
333+ if preview_path :
334+ # Show preview message
335+ preview_text = "📋 **Preview for analysis:**"
336+ if meta_text :
337+ preview_text += f"\n \n _{ meta_text } _"
338+ history .append (
339+ {"role" : "assistant" , "content" : preview_text }
340+ )
341+ history .append (
342+ {
343+ "role" : "assistant" ,
344+ "content" : {"path" : preview_path },
345+ }
346+ )
347+ yield history , state_dict , gr .update (), gr .update (), gr .update (), gr .update (), None , gr .update (
348+ visible = False
349+ ), gr .update (), gr .update ()
350+ except Exception as e :
351+ log .warning ("Preview generation failed: %r" , e )
352+ finally :
353+ # Resized temp files were only needed for the preview.
354+ for tmp in resize_temps :
355+ try :
356+ os .unlink (tmp )
357+ except Exception :
358+ pass
359+ resize_temps .clear ()
353360
354361 # Show "thinking" indicator for agent processing
355362 thinking_msg = {"role" : "assistant" , "content" : "🤔 Finding tools..." }
@@ -362,7 +369,7 @@ def handle_chat(
362369 try :
363370 reply , new_state = respond (
364371 message = message or "" ,
365- files = files or [],
372+ files = original_paths or [],
366373 state_dict = state_dict ,
367374 doc_index = doc_index ,
368375 model = model ,
0 commit comments