Spaces:
Sleeping
Sleeping
File size: 2,788 Bytes
02c6351 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# ๐ง 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! ๐
|