hamxaameer commited on
Commit
60663e2
·
1 Parent(s): 903b858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -35,33 +35,42 @@ def load_model_from_pickle(pickle_path="best_model.pkl"):
35
  if not os.path.exists(pickle_path):
36
  return f"❌ Model file not found: {pickle_path}\n\nPlease ensure best_model.pkl is uploaded to the HuggingFace Space."
37
 
38
- # ULTIMATE FIX: Monkey-patch torch.cuda.is_available during unpickling
39
- # This tricks PyTorch into thinking CUDA is available, then we immediately move to CPU
40
- import torch.cuda
41
- import torch.serialization
42
 
43
- original_is_available = torch.cuda.is_available
44
- original_device_count = torch.cuda.device_count
 
 
 
 
 
 
 
 
 
45
 
46
- def fake_is_available():
47
- return True
48
-
49
- def fake_device_count():
50
- return 1
51
 
52
  try:
53
- # Temporarily make PyTorch think CUDA is available
54
- torch.cuda.is_available = fake_is_available
55
- torch.cuda.device_count = fake_device_count
56
 
57
- # Load the pickle (PyTorch will accept CUDA device references now)
58
  with open(pickle_path, 'rb') as f:
59
- model_package = torch.load(f, map_location=lambda storage, loc: storage.cpu())
 
 
 
 
60
 
61
  finally:
62
- # Always restore original functions
63
- torch.cuda.is_available = original_is_available
64
- torch.cuda.device_count = original_device_count
65
 
66
  # Success! Model loaded with one of the strategies above
67
  # Handle a few common package shapes.
 
35
  if not os.path.exists(pickle_path):
36
  return f"❌ Model file not found: {pickle_path}\n\nPlease ensure best_model.pkl is uploaded to the HuggingFace Space."
37
 
38
+ # NUCLEAR OPTION: Mock the entire CUDA module to prevent driver checks
39
+ import sys
40
+ import types
 
41
 
42
+ # Create a fake CUDA module that claims everything is available but does nothing
43
+ fake_cuda = types.ModuleType('cuda')
44
+ fake_cuda.is_available = lambda: True
45
+ fake_cuda.device_count = lambda: 1
46
+ fake_cuda.current_device = lambda: 0
47
+ fake_cuda.get_device_name = lambda x: "CPU (mocked as CUDA)"
48
+ fake_cuda.set_device = lambda x: None
49
+ fake_cuda.device = lambda x: types.SimpleNamespace(__enter__=lambda: None, __exit__=lambda *args: None)
50
+ fake_cuda.init = lambda: None
51
+ fake_cuda.is_initialized = lambda: True
52
+ fake_cuda._initialization_lock = types.SimpleNamespace(__enter__=lambda: None, __exit__=lambda *args: None)
53
 
54
+ # Save original
55
+ original_cuda = torch.cuda
 
 
 
56
 
57
  try:
58
+ # Replace torch.cuda with our mock during loading
59
+ torch.cuda = fake_cuda
60
+ sys.modules['torch'].cuda = fake_cuda
61
 
62
+ # Load with aggressive CPU mapping
63
  with open(pickle_path, 'rb') as f:
64
+ model_package = torch.load(
65
+ f,
66
+ map_location='cpu',
67
+ weights_only=False
68
+ )
69
 
70
  finally:
71
+ # Always restore original CUDA module
72
+ torch.cuda = original_cuda
73
+ sys.modules['torch'].cuda = original_cuda
74
 
75
  # Success! Model loaded with one of the strategies above
76
  # Handle a few common package shapes.