ashutosh-koottu commited on
Commit
17fbed6
·
1 Parent(s): 736a246

Graceful fallback to CPU

Browse files
Files changed (1) hide show
  1. handler.py +29 -5
handler.py CHANGED
@@ -19,9 +19,32 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
19
 
20
  class EndpointHandler:
21
  def __init__(self, model_dir=None):
22
- # Initialize FaceAnalysis with GPU support
23
- self.app = FaceAnalysis(root="/tmp/.insightface", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
24
- self.app.prepare(ctx_id=0) # 0 = GPU, -1 = CPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  print("=" * 80)
27
  print("InsightFace Providers:")
@@ -70,7 +93,8 @@ class EndpointHandler:
70
  """Pre-warm GPU and compile ONNX models on startup to eliminate cold-start latency."""
71
  try:
72
  print("\n" + "="*80)
73
- print("PRE-WARMING GPU AND COMPILING MODELS")
 
74
  print("="*80)
75
  start = time.time()
76
 
@@ -81,7 +105,7 @@ class EndpointHandler:
81
  _ = self.app.get(dummy_img)
82
 
83
  elapsed = time.time() - start
84
- print(f"✅ Models pre-warmed in {elapsed:.2f}s")
85
  print("="*80 + "\n")
86
  except Exception as e:
87
  print(f"Warning: Model pre-warming failed (non-fatal): {e}")
 
19
 
20
  class EndpointHandler:
21
  def __init__(self, model_dir=None):
22
+ # Initialize FaceAnalysis with GPU/CPU fallback support
23
+ print("\n" + "="*80)
24
+ print("INITIALIZING FACEANALYSIS")
25
+ print("="*80)
26
+
27
+ try:
28
+ # Try GPU first
29
+ print("Attempting to initialize with GPU (CUDA)...")
30
+ self.app = FaceAnalysis(root="/tmp/.insightface", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
31
+ self.app.prepare(ctx_id=0) # 0 = GPU
32
+ print("✅ GPU initialization successful (ctx_id=0)")
33
+ self.gpu_available = True
34
+ except RuntimeError as e:
35
+ # GPU not available, fall back to CPU
36
+ print(f"⚠️ GPU initialization failed: {str(e)[:100]}...")
37
+ print("Falling back to CPU (CPUExecutionProvider)...")
38
+ try:
39
+ self.app = FaceAnalysis(root="/tmp/.insightface", providers=['CPUExecutionProvider'])
40
+ self.app.prepare(ctx_id=-1) # -1 = CPU
41
+ print("✅ CPU initialization successful (ctx_id=-1)")
42
+ self.gpu_available = False
43
+ except Exception as cpu_error:
44
+ print(f"❌ CPU initialization also failed: {cpu_error}")
45
+ raise
46
+
47
+ print("="*80 + "\n")
48
 
49
  print("=" * 80)
50
  print("InsightFace Providers:")
 
93
  """Pre-warm GPU and compile ONNX models on startup to eliminate cold-start latency."""
94
  try:
95
  print("\n" + "="*80)
96
+ mode = "GPU" if self.gpu_available else "CPU"
97
+ print(f"PRE-WARMING MODELS ({mode} MODE)")
98
  print("="*80)
99
  start = time.time()
100
 
 
105
  _ = self.app.get(dummy_img)
106
 
107
  elapsed = time.time() - start
108
+ print(f"✅ Models pre-warmed in {elapsed:.2f}s ({mode})")
109
  print("="*80 + "\n")
110
  except Exception as e:
111
  print(f"Warning: Model pre-warming failed (non-fatal): {e}")