nicka360 commited on
Commit
ee9f854
·
1 Parent(s): 80e0dcf

Fix: ImageLoader no longer crashes when directory is missing; loader moved into create_app

Browse files
warp/data/image_loader.py CHANGED
@@ -1,5 +1,10 @@
1
- """Image loader for scraped medical practice images."""
2
 
 
 
 
 
 
3
  from pathlib import Path
4
 
5
  from PIL import Image
@@ -21,17 +26,27 @@ class ImageLoader:
21
 
22
  Args:
23
  base_path: Root directory containing scraped images.
24
- Defaults to project's scrapedimages folder.
 
 
25
  """
26
- if base_path is None:
27
- self.base_path = DEFAULT_SCRAPED_IMAGES_DIR
28
- else:
29
- self.base_path = Path(base_path)
30
-
31
- if not self.base_path.exists():
32
- raise ValueError(f"Image directory does not exist: {self.base_path}")
33
 
 
 
34
  self._practices: list[str] | None = None
 
 
 
 
 
 
 
 
 
35
 
36
  @property
37
  def practices(self) -> list[str]:
 
1
+ """Image loader for scraped medical practice images.
2
 
3
+ Gracefully handles missing image directory so that applications can start
4
+ without local scraped images present (e.g., in a Hugging Face Space).
5
+ """
6
+
7
+ import os
8
  from pathlib import Path
9
 
10
  from PIL import Image
 
26
 
27
  Args:
28
  base_path: Root directory containing scraped images.
29
+ Defaults to ``/app/data/scrapedimages`` or the
30
+ path specified in the ``A360_IMAGE_DIR`` environment
31
+ variable.
32
  """
33
+ # Resolve base path with optional env override, defaulting to the
34
+ # Hugging Face Space path when running in that environment.
35
+ default_path = "/app/data/scrapedimages"
36
+ self.base_path = Path(base_path or os.environ.get("A360_IMAGE_DIR", default_path))
 
 
 
37
 
38
+ # Always define practices and images so callers can interact with an
39
+ # "empty" loader when the directory is missing instead of crashing.
40
  self._practices: list[str] | None = None
41
+ self.images: list[Path] = []
42
+
43
+ if not self.base_path.exists():
44
+ print(
45
+ f"[ImageLoader] WARNING: Missing directory {self.base_path}. "
46
+ "Continuing with empty image list."
47
+ )
48
+ self._practices = []
49
+ return
50
 
51
  @property
52
  def practices(self) -> list[str]:
warp/gradio_app/model_comparison.py CHANGED
@@ -21,11 +21,21 @@ from warp.models import list_model_names
21
 
22
  # Initialize components
23
  engine = BackgroundRemovalEngine()
24
- loader = ImageLoader()
 
 
25
 
26
 
27
  def load_test_image(practice: str) -> Optional[Image.Image]:
28
- """Load a random test image from a practice."""
 
 
 
 
 
 
 
 
29
  try:
30
  images = loader.get_random_images(practice, n=1)
31
  if images:
@@ -164,8 +174,14 @@ def create_comparison_tab():
164
 
165
  # Quick load options
166
  with gr.Row():
 
 
 
 
 
 
167
  practice_dropdown = gr.Dropdown(
168
- choices=loader.practices if hasattr(loader, "practices") else [],
169
  label="Load Random Image From",
170
  value=None,
171
  )
@@ -230,6 +246,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`` is created lazily inside ``create_app`` so that importing this
25
+ # module never fails, even if no local scraped images are present.
26
+ loader: ImageLoader | None = None
27
 
28
 
29
  def load_test_image(practice: str) -> Optional[Image.Image]:
30
+ """Load a random test image from a practice.
31
+
32
+ When no image directory is configured or available, this simply returns
33
+ ``None`` instead of raising.
34
+ """
35
+ global loader
36
+ if loader is None:
37
+ return None
38
+
39
  try:
40
  images = loader.get_random_images(practice, n=1)
41
  if images:
 
174
 
175
  # Quick load options
176
  with gr.Row():
177
+ global loader
178
+ practices = (
179
+ loader.practices
180
+ if loader is not None and hasattr(loader, "practices")
181
+ else []
182
+ )
183
  practice_dropdown = gr.Dropdown(
184
+ choices=practices,
185
  label="Load Random Image From",
186
  value=None,
187
  )
 
246
 
247
  def create_app():
248
  """Create the full Gradio app."""
249
+ global loader
250
+ # Lazily create the ImageLoader so that simply importing this module
251
+ # does not require the scraped image directory to exist.
252
+ if loader is None:
253
+ loader = ImageLoader()
254
+
255
  with gr.Blocks(title="WARP Model Comparison", theme=gr.themes.Soft()) as app:
256
  gr.Markdown(
257
  """