Spaces:
Sleeping
Sleeping
Commit
·
60663e2
1
Parent(s):
903b858
Update app.py
Browse files
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 |
-
#
|
| 39 |
-
|
| 40 |
-
import
|
| 41 |
-
import torch.serialization
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def fake_device_count():
|
| 50 |
-
return 1
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
#
|
| 54 |
-
torch.cuda
|
| 55 |
-
torch.cuda
|
| 56 |
|
| 57 |
-
# Load
|
| 58 |
with open(pickle_path, 'rb') as f:
|
| 59 |
-
model_package = torch.load(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
finally:
|
| 62 |
-
# Always restore original
|
| 63 |
-
torch.cuda
|
| 64 |
-
torch.cuda
|
| 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.
|