Beepeen78 commited on
Commit
1e0bb79
·
1 Parent(s): 9515c2f

Add GPU decorator for ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -1584,6 +1584,30 @@ with gr.Blocks(
1584
  """)
1585
 
1586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1587
  if __name__ == "__main__":
1588
  import os
1589
 
@@ -1595,30 +1619,10 @@ if __name__ == "__main__":
1595
  if is_spaces:
1596
  print("Detected Hugging Face Spaces environment")
1597
  # On Spaces, use default settings - Gradio handles everything automatically
1598
- # Add GPU decorator for ZeroGPU/GPU hardware support
1599
  if SPACES_AVAILABLE:
1600
  print("GPU decorator available - using @spaces.GPU")
1601
-
1602
- @spaces.GPU
1603
- def launch_with_gpu():
1604
- # Initialize GPU operations to satisfy ZeroGPU requirements
1605
- if CUPY_AVAILABLE:
1606
- try:
1607
- # Perform GPU operations to keep context alive
1608
- test_gpu = cp.array([1.0, 2.0, 3.0])
1609
- result = cp.sum(test_gpu)
1610
- # Transfer result back to CPU to ensure operation completes
1611
- _ = float(result)
1612
- print("✅ GPU initialized and ready for data processing")
1613
- except Exception as e:
1614
- print(f"⚠️ GPU initialization failed (will use CPU): {e}")
1615
-
1616
- # Launch Gradio app - GPU will be used in prediction functions
1617
- demo.launch()
1618
-
1619
- launch_with_gpu()
1620
- else:
1621
- demo.launch()
1622
  else:
1623
  print("Running locally - server will be available at http://127.0.0.1:7860")
1624
  print("Watch this console for debug output when you upload files!")
 
1584
  """)
1585
 
1586
 
1587
+ # Module-level function for GPU decorator (ZeroGPU needs this at import time)
1588
+ def launch_app():
1589
+ """Launch the Gradio app with GPU support if available."""
1590
+ # Initialize GPU operations to satisfy ZeroGPU requirements
1591
+ if CUPY_AVAILABLE:
1592
+ try:
1593
+ # Perform GPU operations to keep context alive
1594
+ test_gpu = cp.array([1.0, 2.0, 3.0])
1595
+ result = cp.sum(test_gpu)
1596
+ # Transfer result back to CPU to ensure operation completes
1597
+ _ = float(result)
1598
+ print("✅ GPU initialized and ready for data processing")
1599
+ except Exception as e:
1600
+ print(f"⚠️ GPU initialization failed (will use CPU): {e}")
1601
+
1602
+ # Launch Gradio app - GPU will be used in prediction functions
1603
+ demo.launch()
1604
+
1605
+
1606
+ # Apply GPU decorator at module level for ZeroGPU detection
1607
+ if SPACES_AVAILABLE:
1608
+ launch_app = spaces.GPU(launch_app)
1609
+
1610
+
1611
  if __name__ == "__main__":
1612
  import os
1613
 
 
1619
  if is_spaces:
1620
  print("Detected Hugging Face Spaces environment")
1621
  # On Spaces, use default settings - Gradio handles everything automatically
1622
+ # Use module-level launch_app function which has GPU decorator if available
1623
  if SPACES_AVAILABLE:
1624
  print("GPU decorator available - using @spaces.GPU")
1625
+ launch_app()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1626
  else:
1627
  print("Running locally - server will be available at http://127.0.0.1:7860")
1628
  print("Watch this console for debug output when you upload files!")