Spaces:
Build error
Build error
Download models/load_model.py from panchadip/sentiment2-test: direct link, hf CLI and curl.
- Browser
- Download file 708 Bytes
-
https://huggingface.co/spaces/panchadip/sentiment2-test/resolve/main/models/load_model.py
- Command line
-
hf download hf://spaces/panchadip/sentiment2-test/models/load_model.py
-
curl -L -o load_model.py https://huggingface.co/spaces/panchadip/sentiment2-test/resolve/main/models/load_model.py
708 Bytes
| import joblib | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| # Define a custom load function to force CPU loading | |
| def custom_torch_load(f, *args, **kwargs): | |
| if 'map_location' not in kwargs: | |
| kwargs['map_location'] = torch.device("cpu") | |
| return torch_load_backup(f, *args, **kwargs) | |
| # Monkey patch torch.load inside joblib.load | |
| torch_load_backup = torch.load # Backup the original function | |
| torch.load = custom_torch_load # Override with CPU-only loading | |
| # Load the model | |
| topic_model = joblib.load("bertopic_model_max_compressed.joblib") | |
| # Restore the original torch.load function | |
| torch.load = torch_load_backup | |
| # Verify loading success | |
| print("Model loaded successfully on CPU") | |