astrosbd commited on
Commit
8c702c0
·
verified ·
1 Parent(s): 9f24e01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -204,6 +204,35 @@ def simulate_damage_detection(rgb_image: np.ndarray, seed_from: np.ndarray = Non
204
  boxes.append({"bbox":[x1,y1,x2,y2], "score":conf, "label":"damage"})
205
  return boxes
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  def run_damage_detection(pil_image: Image.Image, score_thresh: float = 0.5):
208
  """
209
  Returns:
@@ -687,28 +716,22 @@ if __name__ == "__main__":
687
  print(f"📍 Device: {DEVICE}")
688
  print(f"📦 Classifier Path: {huggingface_model_path or DEFAULT_AI_DETECTION_MODEL_PATH}")
689
  print(f"🛠️ Damage Model Path: {DEFAULT_DAMAGE_MODEL_PATH} ({'exists' if os.path.exists(DEFAULT_DAMAGE_MODEL_PATH) else 'missing'})")
 
 
 
690
 
691
- # Preload RADIO model (Stage 2)
692
- models_loaded = preload_models()
693
 
694
  # Load classifier
695
  model_path = huggingface_model_path or DEFAULT_AI_DETECTION_MODEL_PATH
696
  classifier_loaded = load_ai_detection_classifier(model_path) is not None
697
 
698
  print("=" * 60)
699
- print(f"✅ RADIO Model: {'Loaded' if models_loaded else 'Not Available (Demo Mode)'}")
700
- print(f"✅ Classifier: {'Loaded' if classifier_loaded else 'Not Available (Demo Mode)'}")
701
- print(f"✅ Detectron2: {'Available' if DETECTRON2_AVAILABLE else 'Not Available'}")
702
  print("=" * 60)
703
 
704
- if not models_loaded or not classifier_loaded:
705
- print("⚠️ Running in DEMO MODE for Stage 2 (RADIO/classifier missing)")
706
- print(" To enable full functionality, ensure:")
707
- print(" 1) MODEL_REPO environment variable is set")
708
- print(" 2) V1.pkl classifier is available (or set PRIVATE_REPO + HF token)")
709
 
710
- if not DETECTRON2_AVAILABLE or not os.path.exists(DEFAULT_DAMAGE_MODEL_PATH):
711
- print("ℹ️ Stage 1 will use simulator unless Detectron2 and model_final.pth are present.")
712
 
713
  app = create_gradio_interface()
714
  app.launch(
 
204
  boxes.append({"bbox":[x1,y1,x2,y2], "score":conf, "label":"damage"})
205
  return boxes
206
 
207
+ def auto_install_dependencies():
208
+ """Attempt to install dependencies if needed"""
209
+ try:
210
+ import importlib.util
211
+
212
+ # Check for PyTorch
213
+ if importlib.util.find_spec("torch") is None:
214
+ print("Installing PyTorch...")
215
+ os.system("pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu")
216
+
217
+ # Check for Detectron2
218
+ if importlib.util.find_spec("detectron2") is None:
219
+ print("Installing Detectron2...")
220
+ os.system("pip install git+https://github.com/facebookresearch/detectron2.git")
221
+
222
+ # Check for Gradio
223
+ if importlib.util.find_spec("gradio") is None:
224
+ print("Installing Gradio...")
225
+ os.system("pip install gradio")
226
+
227
+ print("Dependencies installation complete!")
228
+ return True
229
+ except Exception as e:
230
+ print(f"Error installing dependencies: {e}")
231
+ return False
232
+
233
+
234
+
235
+
236
  def run_damage_detection(pil_image: Image.Image, score_thresh: float = 0.5):
237
  """
238
  Returns:
 
716
  print(f"📍 Device: {DEVICE}")
717
  print(f"📦 Classifier Path: {huggingface_model_path or DEFAULT_AI_DETECTION_MODEL_PATH}")
718
  print(f"🛠️ Damage Model Path: {DEFAULT_DAMAGE_MODEL_PATH} ({'exists' if os.path.exists(DEFAULT_DAMAGE_MODEL_PATH) else 'missing'})")
719
+
720
+ # Check if dependencies are installed
721
+ auto_install_dependencies()
722
 
723
+ # Preload C model at startup
724
+ preload_models()
725
 
726
  # Load classifier
727
  model_path = huggingface_model_path or DEFAULT_AI_DETECTION_MODEL_PATH
728
  classifier_loaded = load_ai_detection_classifier(model_path) is not None
729
 
730
  print("=" * 60)
 
 
 
731
  print("=" * 60)
732
 
 
 
 
 
 
733
 
734
+
 
735
 
736
  app = create_gradio_interface()
737
  app.launch(