devmm03 commited on
Commit
059bf81
·
1 Parent(s): fd73401

fix: import spaces before any CUDA/torch modules for ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -5,15 +5,31 @@ Serves both the REST API for the Static Frontend and an interactive Gradio diagn
5
 
6
  from __future__ import annotations
7
 
8
- import base64
9
- from io import BytesIO
10
  import os
11
- from pathlib import Path
12
- import sys
13
 
14
  # Disable Gradio 5 SSR (Server-Side Rendering) mode to prevent it from spawning an internal Node.js server on port 7861
15
  os.environ["GRADIO_SSR_MODE"] = "false"
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Ensure backend root and project root are on sys.path
18
  CURRENT_DIR = Path(__file__).resolve().parent
19
  if str(CURRENT_DIR) not in sys.path:
@@ -38,20 +54,6 @@ except ImportError:
38
  from config import DEVICE
39
 
40
 
41
- try:
42
- import spaces
43
- except ImportError:
44
- class _MockSpaces:
45
- @staticmethod
46
- def GPU(func=None, **kwargs):
47
- if func is not None:
48
- return func
49
- def decorator(f):
50
- return f
51
- return decorator
52
- spaces = _MockSpaces()
53
-
54
-
55
  def b64_to_pil(b64_str: str) -> Image.Image:
56
  """Decode a data URI base64 string to a PIL Image."""
57
  if "," in b64_str:
 
5
 
6
  from __future__ import annotations
7
 
 
 
8
  import os
 
 
9
 
10
  # Disable Gradio 5 SSR (Server-Side Rendering) mode to prevent it from spawning an internal Node.js server on port 7861
11
  os.environ["GRADIO_SSR_MODE"] = "false"
12
 
13
+ # IMPORTANT FOR HUGGING FACE ZEROGPU:
14
+ # `spaces` must be imported before any CUDA-related package (e.g. torch, torchvision, backend modules)
15
+ try:
16
+ import spaces
17
+ except ImportError:
18
+ class _MockSpaces:
19
+ @staticmethod
20
+ def GPU(func=None, **kwargs):
21
+ if func is not None:
22
+ return func
23
+ def decorator(f):
24
+ return f
25
+ return decorator
26
+ spaces = _MockSpaces()
27
+
28
+ import base64
29
+ from io import BytesIO
30
+ from pathlib import Path
31
+ import sys
32
+
33
  # Ensure backend root and project root are on sys.path
34
  CURRENT_DIR = Path(__file__).resolve().parent
35
  if str(CURRENT_DIR) not in sys.path:
 
54
  from config import DEVICE
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def b64_to_pil(b64_str: str) -> Image.Image:
58
  """Decode a data URI base64 string to a PIL Image."""
59
  if "," in b64_str: