Harshith Reddy commited on
Commit
9e2df9f
·
1 Parent(s): a357ddd

Improve GPU detection and fallback logic

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -617,16 +617,31 @@ def create_interface():
617
  - **Example**: Use from your frontend with `fetch()` or `axios()`
618
  """)
619
 
620
- if HAS_SPACES and torch.cuda.is_available():
621
- try:
622
- predict_fn = spaces.GPU(predict_volume)
623
- print("Using GPU acceleration")
624
- except Exception as e:
625
- print(f"GPU decorator failed: {e}. Using CPU fallback.")
 
 
 
 
 
 
 
 
 
 
 
626
  predict_fn = predict_volume
627
  else:
 
 
 
 
 
628
  predict_fn = predict_volume
629
- print("Running on CPU (GPU not available or quota exhausted)")
630
 
631
  predict_btn.click(
632
  fn=predict_fn,
 
617
  - **Example**: Use from your frontend with `fetch()` or `axios()`
618
  """)
619
 
620
+ use_gpu = False
621
+ if HAS_SPACES:
622
+ if torch.cuda.is_available():
623
+ try:
624
+ test_tensor = torch.zeros(1).cuda()
625
+ del test_tensor
626
+ torch.cuda.empty_cache()
627
+ use_gpu = True
628
+ predict_fn = spaces.GPU(predict_volume)
629
+ print("✓ Using GPU acceleration (ZeroGPU/T4)")
630
+ except Exception as e:
631
+ print(f"⚠ GPU available but failed to initialize: {e}")
632
+ print("Falling back to CPU mode")
633
+ use_gpu = False
634
+ predict_fn = predict_volume
635
+ else:
636
+ print("ℹ No GPU available. Running on CPU (slower but free)")
637
  predict_fn = predict_volume
638
  else:
639
+ if torch.cuda.is_available():
640
+ print("✓ CUDA available, using GPU")
641
+ use_gpu = True
642
+ else:
643
+ print("ℹ Running on CPU")
644
  predict_fn = predict_volume
 
645
 
646
  predict_btn.click(
647
  fn=predict_fn,