Spaces:
Sleeping
Sleeping
Fix: ImageLoader no longer crashes when directory missing; move loader to create_app()
Browse files
warp/data/image_loader.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""Image loader for scraped medical practice images."""
|
| 2 |
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
from PIL import Image
|
|
@@ -21,15 +22,24 @@ class ImageLoader:
|
|
| 21 |
|
| 22 |
Args:
|
| 23 |
base_path: Root directory containing scraped images.
|
| 24 |
-
Defaults to
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
|
|
|
| 31 |
if not self.base_path.exists():
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
self._practices: list[str] | None = None
|
| 35 |
|
|
|
|
| 1 |
"""Image loader for scraped medical practice images."""
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
from PIL import Image
|
|
|
|
| 22 |
|
| 23 |
Args:
|
| 24 |
base_path: Root directory containing scraped images.
|
| 25 |
+
Defaults to ``/app/data/scrapedimages`` or the path
|
| 26 |
+
specified in the ``A360_IMAGE_DIR`` environment
|
| 27 |
+
variable.
|
| 28 |
"""
|
| 29 |
+
# Resolve base path with optional env override, defaulting to the
|
| 30 |
+
# Hugging Face Space path when running in that environment.
|
| 31 |
+
default_path = "/app/data/scrapedimages"
|
| 32 |
+
self.base_path = Path(base_path or os.environ.get("A360_IMAGE_DIR", default_path))
|
| 33 |
|
| 34 |
+
# IMPORTANT: Do NOT crash if the directory does not exist on Spaces.
|
| 35 |
if not self.base_path.exists():
|
| 36 |
+
print(
|
| 37 |
+
f"[ImageLoader] WARNING: Missing directory {self.base_path}. "
|
| 38 |
+
"Continuing with empty image list."
|
| 39 |
+
)
|
| 40 |
+
self.images = []
|
| 41 |
+
self._practices = []
|
| 42 |
+
return
|
| 43 |
|
| 44 |
self._practices: list[str] | None = None
|
| 45 |
|
warp/gradio_app/model_comparison.py
CHANGED
|
@@ -21,7 +21,9 @@ from warp.models import list_model_names
|
|
| 21 |
|
| 22 |
# Initialize components
|
| 23 |
engine = BackgroundRemovalEngine()
|
| 24 |
-
loader
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def load_test_image(practice: str) -> Optional[Image.Image]:
|
|
@@ -230,6 +232,12 @@ def create_single_model_tab():
|
|
| 230 |
|
| 231 |
def create_app():
|
| 232 |
"""Create the full Gradio app."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
with gr.Blocks(title="WARP Model Comparison", theme=gr.themes.Soft()) as app:
|
| 234 |
gr.Markdown(
|
| 235 |
"""
|
|
|
|
| 21 |
|
| 22 |
# Initialize components
|
| 23 |
engine = BackgroundRemovalEngine()
|
| 24 |
+
# ``loader`` will be created inside ``create_app`` so importing this module
|
| 25 |
+
# never fails, even if the image directory is missing.
|
| 26 |
+
loader: ImageLoader | None = None
|
| 27 |
|
| 28 |
|
| 29 |
def load_test_image(practice: str) -> Optional[Image.Image]:
|
|
|
|
| 232 |
|
| 233 |
def create_app():
|
| 234 |
"""Create the full Gradio app."""
|
| 235 |
+
global loader
|
| 236 |
+
# Lazily create the ImageLoader so that importing this module does not
|
| 237 |
+
# require the scraped image directory to exist.
|
| 238 |
+
if loader is None:
|
| 239 |
+
loader = ImageLoader()
|
| 240 |
+
|
| 241 |
with gr.Blocks(title="WARP Model Comparison", theme=gr.themes.Soft()) as app:
|
| 242 |
gr.Markdown(
|
| 243 |
"""
|