Spaces:
Sleeping
Sleeping
Commit ·
ae8a3d6
1
Parent(s): 14c06ea
Fix: Use gr.update() instead of gr.Slider() to avoid JSON schema bug
Browse files- requirements.txt +2 -2
- seg_app/ui/gradio_app.py +13 -17
requirements.txt
CHANGED
|
@@ -4,8 +4,8 @@
|
|
| 4 |
# =============================================================================
|
| 5 |
# Core Web Framework
|
| 6 |
# =============================================================================
|
| 7 |
-
# Gradio
|
| 8 |
-
gradio>=5.0.0
|
| 9 |
|
| 10 |
# =============================================================================
|
| 11 |
# Medical Imaging I/O
|
|
|
|
| 4 |
# =============================================================================
|
| 5 |
# Core Web Framework
|
| 6 |
# =============================================================================
|
| 7 |
+
# Gradio - compatible with HF Spaces (5.x is pre-installed on HF)
|
| 8 |
+
gradio>=5.0.0
|
| 9 |
|
| 10 |
# =============================================================================
|
| 11 |
# Medical Imaging I/O
|
seg_app/ui/gradio_app.py
CHANGED
|
@@ -44,7 +44,7 @@ _current_prompts: Prompts = Prompts()
|
|
| 44 |
_prompt_mode: str = "positive" # "positive" or "negative"
|
| 45 |
|
| 46 |
|
| 47 |
-
def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str,
|
| 48 |
"""Load a NIfTI volume and render initial multi-planar views.
|
| 49 |
|
| 50 |
Args:
|
|
@@ -52,20 +52,16 @@ def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str,
|
|
| 52 |
|
| 53 |
Returns:
|
| 54 |
Tuple of (axial_image, sagittal_image, coronal_image, volume_info_str,
|
| 55 |
-
metrics_str,
|
| 56 |
"""
|
| 57 |
global _current_volume, _current_mask, _current_slice_indices, _current_prompts
|
| 58 |
|
| 59 |
-
# Default slider
|
| 60 |
-
|
| 61 |
-
gr.Slider(minimum=0, maximum=100, value=50, step=1),
|
| 62 |
-
gr.Slider(minimum=0, maximum=100, value=50, step=1),
|
| 63 |
-
gr.Slider(minimum=0, maximum=100, value=50, step=1),
|
| 64 |
-
)
|
| 65 |
|
| 66 |
if file is None:
|
| 67 |
gr.Warning("Please upload a NIfTI file (.nii or .nii.gz)")
|
| 68 |
-
return None, None, None, "", "",
|
| 69 |
|
| 70 |
# Handle Gradio file input (could be path string or file object)
|
| 71 |
filepath = file.name if hasattr(file, 'name') else file
|
|
@@ -74,7 +70,7 @@ def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str,
|
|
| 74 |
_current_volume = load_nifti(filepath)
|
| 75 |
except Exception as e:
|
| 76 |
gr.Warning(f"Failed to load NIfTI file: {e}")
|
| 77 |
-
return None, None, None, "", "",
|
| 78 |
|
| 79 |
# Clear previous segmentation and prompts
|
| 80 |
_current_mask = None
|
|
@@ -101,10 +97,10 @@ def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str,
|
|
| 101 |
spacing = tuple(round(s, 2) for s in _current_volume.spacing)
|
| 102 |
volume_info_str = f"Shape: {shape}\nSpacing: {spacing} mm"
|
| 103 |
|
| 104 |
-
# Create
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
return (
|
| 110 |
view.axial.image,
|
|
@@ -112,9 +108,9 @@ def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str,
|
|
| 112 |
view.coronal.image,
|
| 113 |
volume_info_str,
|
| 114 |
"", # Clear metrics
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
)
|
| 119 |
|
| 120 |
|
|
|
|
| 44 |
_prompt_mode: str = "positive" # "positive" or "negative"
|
| 45 |
|
| 46 |
|
| 47 |
+
def load_volume(file) -> Tuple[Image.Image, Image.Image, Image.Image, str, str, dict, dict, dict]:
|
| 48 |
"""Load a NIfTI volume and render initial multi-planar views.
|
| 49 |
|
| 50 |
Args:
|
|
|
|
| 52 |
|
| 53 |
Returns:
|
| 54 |
Tuple of (axial_image, sagittal_image, coronal_image, volume_info_str,
|
| 55 |
+
metrics_str, axial_slider_update, sagittal_slider_update, coronal_slider_update)
|
| 56 |
"""
|
| 57 |
global _current_volume, _current_mask, _current_slice_indices, _current_prompts
|
| 58 |
|
| 59 |
+
# Default slider updates for error cases (use gr.update() for compatibility)
|
| 60 |
+
default_slider_update = gr.update(minimum=0, maximum=100, value=50)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if file is None:
|
| 63 |
gr.Warning("Please upload a NIfTI file (.nii or .nii.gz)")
|
| 64 |
+
return None, None, None, "", "", default_slider_update, default_slider_update, default_slider_update
|
| 65 |
|
| 66 |
# Handle Gradio file input (could be path string or file object)
|
| 67 |
filepath = file.name if hasattr(file, 'name') else file
|
|
|
|
| 70 |
_current_volume = load_nifti(filepath)
|
| 71 |
except Exception as e:
|
| 72 |
gr.Warning(f"Failed to load NIfTI file: {e}")
|
| 73 |
+
return None, None, None, "", "", default_slider_update, default_slider_update, default_slider_update
|
| 74 |
|
| 75 |
# Clear previous segmentation and prompts
|
| 76 |
_current_mask = None
|
|
|
|
| 97 |
spacing = tuple(round(s, 2) for s in _current_volume.spacing)
|
| 98 |
volume_info_str = f"Shape: {shape}\nSpacing: {spacing} mm"
|
| 99 |
|
| 100 |
+
# Create slider updates using gr.update() - compatible with all Gradio versions
|
| 101 |
+
axial_slider_update = gr.update(minimum=ax_min, maximum=ax_max, value=view.axial.index)
|
| 102 |
+
sagittal_slider_update = gr.update(minimum=sag_min, maximum=sag_max, value=view.sagittal.index)
|
| 103 |
+
coronal_slider_update = gr.update(minimum=cor_min, maximum=cor_max, value=view.coronal.index)
|
| 104 |
|
| 105 |
return (
|
| 106 |
view.axial.image,
|
|
|
|
| 108 |
view.coronal.image,
|
| 109 |
volume_info_str,
|
| 110 |
"", # Clear metrics
|
| 111 |
+
axial_slider_update,
|
| 112 |
+
sagittal_slider_update,
|
| 113 |
+
coronal_slider_update,
|
| 114 |
)
|
| 115 |
|
| 116 |
|