Spaces:
Paused
Paused
Fix: Robust HF cache directory with graceful /data fallback
Browse files- Handle PermissionError when /data is not accessible
- Auto-fallback: /data/.huggingface → /home/user/.cache
- Add write permission verification
- Support both paid tier and free tier
Resolves: PermissionError [Errno 13] Permission denied: '/data'
- code/cube3d/config.py +44 -12
code/cube3d/config.py
CHANGED
|
@@ -58,22 +58,54 @@ DATA_DIR = PROJECT_ROOT / "data"
|
|
| 58 |
CAR_1K_DIR = DATA_DIR / "car_1k"
|
| 59 |
CAR_DATA_DIR = DATA_DIR / "1313个筛选车结构和对照渲染图"
|
| 60 |
|
| 61 |
-
# HuggingFace model cache directory
|
| 62 |
-
#
|
| 63 |
if ENVIRONMENT == "huggingface":
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
# Ensure directory exists
|
| 69 |
-
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
| 70 |
else:
|
| 71 |
-
# Local development
|
| 72 |
HF_CACHE_DIR = os.path.expanduser("~/.cache/huggingface")
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
# ============================================================================
|
|
|
|
| 58 |
CAR_1K_DIR = DATA_DIR / "car_1k"
|
| 59 |
CAR_DATA_DIR = DATA_DIR / "1313个筛选车结构和对照渲染图"
|
| 60 |
|
| 61 |
+
# HuggingFace model cache directory with robust fallback handling
|
| 62 |
+
# Priority: /data/.huggingface (persistent storage) → /home/user/.cache/huggingface (backup)
|
| 63 |
if ENVIRONMENT == "huggingface":
|
| 64 |
+
# HuggingFace Spaces: Try persistent storage with graceful fallback
|
| 65 |
+
|
| 66 |
+
# Candidate directories in priority order
|
| 67 |
+
candidates = [
|
| 68 |
+
os.getenv("HF_HOME", "/data/.huggingface"), # Persistent storage (paid tier)
|
| 69 |
+
"/home/user/.cache/huggingface", # Ephemeral but always writable
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
HF_CACHE_DIR = None
|
| 73 |
+
for cache_dir in candidates:
|
| 74 |
+
try:
|
| 75 |
+
# Attempt to create directory to verify write permissions
|
| 76 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 77 |
+
|
| 78 |
+
# Test write access by creating a test file
|
| 79 |
+
test_file = os.path.join(cache_dir, ".write_test")
|
| 80 |
+
with open(test_file, 'w') as f:
|
| 81 |
+
f.write("test")
|
| 82 |
+
os.remove(test_file)
|
| 83 |
+
|
| 84 |
+
# Success! Use this directory
|
| 85 |
+
HF_CACHE_DIR = cache_dir
|
| 86 |
+
print(f"✅ [Config] HuggingFace cache directory: {HF_CACHE_DIR}")
|
| 87 |
+
break
|
| 88 |
+
|
| 89 |
+
except (PermissionError, OSError) as e:
|
| 90 |
+
print(f"⚠️ [Config] Cannot use {cache_dir}: {e}")
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
# Fallback if all candidates failed (should never happen)
|
| 94 |
+
if HF_CACHE_DIR is None:
|
| 95 |
+
HF_CACHE_DIR = tempfile.gettempdir()
|
| 96 |
+
print(f"⚠️ [Config] Using system temp directory as last resort: {HF_CACHE_DIR}")
|
| 97 |
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
+
# Local development: use standard user cache
|
| 100 |
HF_CACHE_DIR = os.path.expanduser("~/.cache/huggingface")
|
| 101 |
+
try:
|
| 102 |
+
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
| 103 |
+
print(f"[Config] Local HuggingFace cache directory: {HF_CACHE_DIR}")
|
| 104 |
+
except (PermissionError, OSError) as e:
|
| 105 |
+
# Fallback to temp directory
|
| 106 |
+
HF_CACHE_DIR = os.path.join(tempfile.gettempdir(), "huggingface")
|
| 107 |
+
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
| 108 |
+
print(f"⚠️ [Config] Using temp directory due to permission error: {HF_CACHE_DIR}")
|
| 109 |
|
| 110 |
|
| 111 |
# ============================================================================
|