AMontiB commited on
Commit
a721e26
·
1 Parent(s): ee73b8f

fix: add error handling for module imports to prevent startup crashes

Browse files
Files changed (1) hide show
  1. app.py +21 -1
app.py CHANGED
@@ -4,7 +4,19 @@ import sys
4
  import json
5
  import argparse
6
  from types import SimpleNamespace
7
- from support.detect import run_detect
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Download weights on first run (for HF Spaces)
10
  if os.environ.get("SPACE_ID"):
@@ -18,6 +30,14 @@ if os.environ.get("SPACE_ID"):
18
  DETECTORS = ['R50_TF', 'R50_nodown', 'CLIP-D', 'P2G', 'NPR']
19
 
20
  def predict(image_path, detector_name):
 
 
 
 
 
 
 
 
21
  if not image_path:
22
  return json.dumps({"error": "Please upload an image."}, indent=2)
23
 
 
4
  import json
5
  import argparse
6
  from types import SimpleNamespace
7
+
8
+ # Try to import detector - if this fails, we'll show an error in the UI
9
+ try:
10
+ from support.detect import run_detect
11
+ DETECTOR_AVAILABLE = True
12
+ IMPORT_ERROR = None
13
+ except Exception as e:
14
+ DETECTOR_AVAILABLE = False
15
+ IMPORT_ERROR = str(e)
16
+ print(f"Warning: Could not import detector: {e}")
17
+ # Create a dummy function
18
+ def run_detect(args):
19
+ raise ImportError(f"Detector not available: {IMPORT_ERROR}")
20
 
21
  # Download weights on first run (for HF Spaces)
22
  if os.environ.get("SPACE_ID"):
 
30
  DETECTORS = ['R50_TF', 'R50_nodown', 'CLIP-D', 'P2G', 'NPR']
31
 
32
  def predict(image_path, detector_name):
33
+ # Check if detector is available
34
+ if not DETECTOR_AVAILABLE:
35
+ return json.dumps({
36
+ "error": "Detector module not available",
37
+ "details": IMPORT_ERROR,
38
+ "message": "The detection system could not be initialized. Please check the logs."
39
+ }, indent=2)
40
+
41
  if not image_path:
42
  return json.dumps({"error": "Please upload an image."}, indent=2)
43