vivekreddyr commited on
Commit
616f984
·
verified ·
1 Parent(s): 89e6b1a

ZeroGPU: force cuda placement via env; use transformers 5.x dtype arg

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -38,7 +38,13 @@ LAYOUT_MODELS = ["PP-DocLayoutV2", "PP-DocLayout_plus-L", "PP-DocLayout-L"]
38
  RENDER_DPI = 150 # PDF rasterization DPI
39
  MAX_SIDE = 2200 # cap the long side of a page (px)
40
 
41
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
42
 
43
  # Prompt per PaddleOCR-VL task.
44
  PROMPTS = {
@@ -84,11 +90,18 @@ def _load_vlm():
84
  from transformers import AutoModelForCausalLM, AutoProcessor
85
 
86
  print("[vlm] loading", MODEL_PATH, flush=True)
87
- model = AutoModelForCausalLM.from_pretrained(
88
- MODEL_PATH,
89
- trust_remote_code=True,
90
- torch_dtype=torch.bfloat16,
91
- ).eval()
 
 
 
 
 
 
 
92
  if DEVICE == "cuda":
93
  model = model.to("cuda")
94
  processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
 
38
  RENDER_DPI = 150 # PDF rasterization DPI
39
  MAX_SIDE = 2200 # cap the long side of a page (px)
40
 
41
+ # On ZeroGPU the GPU is not attached at import time, so torch.cuda.is_available()
42
+ # is False here; the `spaces` package intercepts `.to("cuda")` at module scope and
43
+ # streams the weights in on the first @spaces.GPU call. So trust the ZeroGPU env
44
+ # var, not is_available(), when deciding to place weights on cuda.
45
+ ON_ZEROGPU = bool(os.environ.get("SPACES_ZERO_GPU"))
46
+ USE_CUDA = ON_ZEROGPU or torch.cuda.is_available()
47
+ DEVICE = "cuda" if USE_CUDA else "cpu"
48
 
49
  # Prompt per PaddleOCR-VL task.
50
  PROMPTS = {
 
90
  from transformers import AutoModelForCausalLM, AutoProcessor
91
 
92
  print("[vlm] loading", MODEL_PATH, flush=True)
93
+ try:
94
+ model = AutoModelForCausalLM.from_pretrained(
95
+ MODEL_PATH,
96
+ trust_remote_code=True,
97
+ dtype=torch.bfloat16, # transformers 5.x name (was torch_dtype)
98
+ ).eval()
99
+ except TypeError:
100
+ model = AutoModelForCausalLM.from_pretrained(
101
+ MODEL_PATH,
102
+ trust_remote_code=True,
103
+ torch_dtype=torch.bfloat16,
104
+ ).eval()
105
  if DEVICE == "cuda":
106
  model = model.to("cuda")
107
  processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)