Spaces:
Sleeping
Sleeping
| # ๐ง HF Spaces Cache Permission Fix | |
| ## โ **Problem:** | |
| ``` | |
| ERROR:app:Failed to load model: [Errno 13] Permission denied: '/.cache' | |
| ``` | |
| HF Spaces containers can't write to the root `/.cache` directory, causing model downloads to fail. | |
| ## โ **Solution Applied:** | |
| ### 1. **Fixed Cache Directory in app.py** | |
| - โ Set custom cache directory: `/tmp/torch_cache` | |
| - โ Added proper permissions handling | |
| - โ Fixed OMP_NUM_THREADS environment variable issue | |
| ### 2. **Updated Dockerfile** | |
| - โ Set environment variables to use `/tmp` for caches | |
| - โ Pre-create cache directories | |
| - โ Fixed OMP_NUM_THREADS value | |
| ### 3. **Key Changes Made:** | |
| #### **app.py Changes:** | |
| ```python | |
| # Fixed cache directory for torch.hub | |
| state_dict = torch.hub.load_state_dict_from_url( | |
| model_url, | |
| map_location=device, | |
| model_dir=cache_dir, # Custom cache dir | |
| check_hash=False # Skip hash check for speed | |
| ) | |
| # Fixed environment variables | |
| os.environ["OMP_NUM_THREADS"] = "2" # Valid integer | |
| os.environ["TORCH_HOME"] = "/tmp/torch" | |
| os.environ["HF_HOME"] = "/tmp/huggingface" | |
| ``` | |
| #### **Dockerfile Changes:** | |
| ```dockerfile | |
| ENV OMP_NUM_THREADS=2 | |
| ENV TORCH_HOME=/tmp/torch | |
| ENV HF_HOME=/tmp/huggingface | |
| ENV TRANSFORMERS_CACHE=/tmp/transformers | |
| RUN mkdir -p /tmp/torch /tmp/huggingface /tmp/transformers | |
| ``` | |
| ## ๐ **Expected Results:** | |
| - โ No more "Permission denied: /.cache" errors | |
| - โ No more "Invalid value for environment variable OMP_NUM_THREADS" warnings | |
| - โ Model downloads work properly on HF Spaces | |
| - โ App starts correctly and clicking works | |
| ## ๐ **To Deploy:** | |
| 1. **Commit the changes**: `git add . && git commit -m "Fix HF Spaces cache permissions"` | |
| 2. **Push to HF Spaces**: `git push` | |
| 3. **Monitor logs**: Check that download succeeds without permission errors | |
| 4. **Test**: Click the game area - should work now! | |
| ## ๐ **Log Messages to Look For:** | |
| ### โ **Success:** | |
| ``` | |
| INFO:app:Loading state dict from https://huggingface.co/Etadingrui/diamond-1B/resolve/main/agent_epoch_00003.pt | |
| INFO:app:State dict loaded, applying to agent... | |
| INFO:app:Model has actor_critic weights: False | |
| INFO:app:Actor-critic model exists but has no trained weights - using dummy mode! | |
| INFO:app:WebPlayEnv set to human control mode (no trained weights) | |
| INFO:app:Models initialized successfully! | |
| ``` | |
| ### โ **If Still Failing:** | |
| ``` | |
| ERROR:app:Failed to load model: [Errno 13] Permission denied | |
| ``` | |
| ## ๐ฏ **What This Fixes:** | |
| 1. โ **Model downloading** - now uses writable `/tmp` directory | |
| 2. โ **Environment variables** - OMP_NUM_THREADS is valid | |
| 3. โ **Game clicking** - works after model loads (even without actor_critic) | |
| 4. โ **HF Spaces compatibility** - follows container best practices | |
| The app should now work perfectly on HF Spaces! ๐ | |