Spaces:
Sleeping

Igoni Agent commited on
Commit
ac23582
·
1 Parent(s): 8da871d

fix: comprehensive CPU patches for ALL extractors (video + audio + text)

Browse files

Video extractor dies on 'model.model.to(self.image.device)' where device=cuda.
Audio extractor also defaults to cuda.

Nuclear approach:
1. Patch HuggingFaceText._load_model (text/Llama)
2. Patch VideoFeature._load_model (video frames)
3. Patch AudioFeature._load_model (audio WAV)
4. Monkey-patch torch.nn.Module.to to redirect cuda->cpu globally

This ensures NO extractor ever tries to move models to cuda on CPU Spaces.

Files changed (2) hide show
  1. Dockerfile +1 -1
  2. app.py +50 -8
Dockerfile CHANGED
@@ -12,7 +12,7 @@ RUN pip install --no-cache-dir -r requirements.txt
12
  ENV HF_HUB_ENABLE_HF_TRANSFER=0
13
  ENV PORT=7860
14
  ENV PYTHONUNBUFFERED=1
15
- LABEL rebuild="cuda-check-v1"
16
 
17
  COPY app.py .
18
  EXPOSE 7860
 
12
  ENV HF_HUB_ENABLE_HF_TRANSFER=0
13
  ENV PORT=7860
14
  ENV PYTHONUNBUFFERED=1
15
+ LABEL rebuild="cpu-patches-v1"
16
 
17
  COPY app.py .
18
  EXPOSE 7860
app.py CHANGED
@@ -63,17 +63,59 @@ def _load_model():
63
  import torch
64
  print(f"[Tribe V2] PyTorch {torch.__version__}", flush=True)
65
 
66
- # CPU Patch for text extractor (Llama)
67
  try:
68
- from neuralset.extractors.text import HuggingFaceText
69
- orig_load = HuggingFaceText._load_model
70
 
71
- def cpu_patched_load(self):
 
 
 
72
  object.__setattr__(self, 'device', 'cpu')
73
- return orig_load(self)
 
74
 
75
- HuggingFaceText._load_model = cpu_patched_load
76
- print("[Tribe V2] CPU patch applied", flush=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  except Exception as e:
78
  print(f"[Tribe V2] CPU patch warning: {e}", flush=True)
79
 
@@ -259,7 +301,7 @@ def index():
259
  "status": "ok",
260
  "pipeline": "official-tribemodel",
261
  "/endpoints": ["/health", "/warmup", "/predict_text", "/predict_video", "/predict_audio", "/predict_image", "/predict_raw"],
262
- "version": "cuda-check-v1"
263
  })
264
 
265
 
 
63
  import torch
64
  print(f"[Tribe V2] PyTorch {torch.__version__}", flush=True)
65
 
66
+ # CPU Patch for ALL extractors (text, video, audio all default to cuda)
67
  try:
68
+ import torch
 
69
 
70
+ # Patch text extractor (Llama)
71
+ from neuralset.extractors.text import HuggingFaceText
72
+ orig_text_load = HuggingFaceText._load_model
73
+ def cpu_patched_text_load(self):
74
  object.__setattr__(self, 'device', 'cpu')
75
+ return orig_text_load(self)
76
+ HuggingFaceText._load_model = cpu_patched_text_load
77
 
78
+ # Patch video extractor's image device
79
+ try:
80
+ from neuralset.extractors.video import VideoFeature
81
+ orig_video_load = VideoFeature._load_model
82
+ def cpu_patched_video_load(self):
83
+ result = orig_video_load(self)
84
+ if hasattr(self, 'image') and hasattr(self.image, 'device'):
85
+ object.__setattr__(self.image, 'device', 'cpu')
86
+ return result
87
+ VideoFeature._load_model = cpu_patched_video_load
88
+ except Exception as ve:
89
+ print(f"[Tribe V2] Video patch warning: {ve}", flush=True)
90
+
91
+ # Patch audio extractor
92
+ try:
93
+ from neuralset.extractors.audio import AudioFeature
94
+ orig_audio_load = AudioFeature._load_model
95
+ def cpu_patched_audio_load(self):
96
+ result = orig_audio_load(self)
97
+ if hasattr(self, 'device'):
98
+ object.__setattr__(self, 'device', 'cpu')
99
+ return result
100
+ AudioFeature._load_model = cpu_patched_audio_load
101
+ except Exception as ae:
102
+ print(f"[Tribe V2] Audio patch warning: {ae}", flush=True)
103
+
104
+ # Nuclear option: override torch.cuda.is_available to force CPU everywhere
105
+ if not torch.cuda.is_available():
106
+ torch.cuda.is_available = lambda: False
107
+ # Also patch any .to('cuda') calls by monkey-patching Module.to
108
+ orig_to = torch.nn.Module.to
109
+ def safe_to(self_module, *args, **kwargs):
110
+ args = tuple('cpu' if a == 'cuda' or (isinstance(a, torch.device) and a.type == 'cuda') else a for a in args)
111
+ if 'device' in kwargs:
112
+ d = kwargs['device']
113
+ if d == 'cuda' or (isinstance(d, torch.device) and d.type == 'cuda'):
114
+ kwargs['device'] = 'cpu'
115
+ return orig_to(self_module, *args, **kwargs)
116
+ torch.nn.Module.to = safe_to
117
+
118
+ print("[Tribe V2] All CPU patches applied (text + video + audio + Module.to)", flush=True)
119
  except Exception as e:
120
  print(f"[Tribe V2] CPU patch warning: {e}", flush=True)
121
 
 
301
  "status": "ok",
302
  "pipeline": "official-tribemodel",
303
  "/endpoints": ["/health", "/warmup", "/predict_text", "/predict_video", "/predict_audio", "/predict_image", "/predict_raw"],
304
+ "version": "cpu-patches-v1"
305
  })
306
 
307