edeler commited on
Commit
315b4d2
·
verified ·
1 Parent(s): a2ee4a0

Upload 8 files

Browse files
Files changed (2) hide show
  1. app.py +25 -6
  2. requirements.txt +1 -0
app.py CHANGED
@@ -38,6 +38,18 @@ except ImportError:
38
  print("Warning: RF-DETR not found. Please ensure it's properly installed.")
39
  RFDETRMedium = None
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # ============================================================================
42
  # Configuration for Hugging Face Spaces
43
  # ============================================================================
@@ -423,6 +435,7 @@ def create_detection_interface():
423
  "#66ff66", "#99ff00",
424
  ])
425
 
 
426
  def annotate_image(image: Image.Image, threshold: float, model_size: str = "4B") -> Tuple[Image.Image, str]:
427
  """Process an image and return annotated version with description."""
428
 
@@ -589,12 +602,18 @@ def main():
589
  # Ensure results directory exists
590
  os.makedirs(app_state.config.get('results_dir'), exist_ok=True)
591
 
592
- # Preload all models into VRAM
593
- try:
594
- app_state.preload_all_models()
595
- except Exception as e:
596
- print(f"⚠️ Warning: Failed to preload models: {e}")
597
- print("Models will be loaded on first use instead")
 
 
 
 
 
 
598
 
599
  # Create and launch the interface
600
  demo = create_detection_interface()
 
38
  print("Warning: RF-DETR not found. Please ensure it's properly installed.")
39
  RFDETRMedium = None
40
 
41
+ # Try to import spaces for Hugging Face Spaces GPU support
42
+ try:
43
+ import spaces
44
+ HF_SPACES_GPU = True
45
+ except ImportError:
46
+ # Create a dummy decorator if not running on Spaces
47
+ class spaces:
48
+ @staticmethod
49
+ def GPU(func):
50
+ return func
51
+ HF_SPACES_GPU = False
52
+
53
  # ============================================================================
54
  # Configuration for Hugging Face Spaces
55
  # ============================================================================
 
435
  "#66ff66", "#99ff00",
436
  ])
437
 
438
+ @spaces.GPU
439
  def annotate_image(image: Image.Image, threshold: float, model_size: str = "4B") -> Tuple[Image.Image, str]:
440
  """Process an image and return annotated version with description."""
441
 
 
602
  # Ensure results directory exists
603
  os.makedirs(app_state.config.get('results_dir'), exist_ok=True)
604
 
605
+ # Preload models if NOT running on HF Spaces with GPU
606
+ # On HF Spaces, models will be loaded on first inference call (triggered by @spaces.GPU)
607
+ if not HF_SPACES_GPU:
608
+ print("Running locally - preloading models into VRAM...")
609
+ try:
610
+ app_state.preload_all_models()
611
+ except Exception as e:
612
+ print(f"⚠️ Warning: Failed to preload models: {e}")
613
+ print("Models will be loaded on first use instead")
614
+ else:
615
+ print("Running on HF Spaces - models will load on first inference (via @spaces.GPU)")
616
+ print("This is the recommended approach for Spaces GPU management.")
617
 
618
  # Create and launch the interface
619
  demo = create_detection_interface()
requirements.txt CHANGED
@@ -2,6 +2,7 @@ torch
2
  transformers
3
  huggingface_hub
4
  gradio
 
5
  pillow
6
  opencv-python-headless
7
  rfdetr
 
2
  transformers
3
  huggingface_hub
4
  gradio
5
+ spaces
6
  pillow
7
  opencv-python-headless
8
  rfdetr