0xZohar commited on
Commit
c811f2f
·
verified ·
1 Parent(s): 7e77ae8

Fix: Align cache path with preload_from_hub behavior

Browse files

Root cause (Git history analysis):
- preload_from_hub ALWAYS downloads to ~/.cache/huggingface/hub/ (hardcoded by HF Spaces)
- config.py was using HF_HOME env var → ~/.cache/huggingface (missing /hub suffix)
- Result: transformers looked in ~/.cache/huggingface/models--* but files were in ~/.cache/huggingface/hub/models--*

Solution:
- config.py now uses HF_HUB_CACHE environment variable
- HF_HUB_CACHE is set in README.md to /home/user/.cache/huggingface/hub
- This matches exactly where preload_from_hub downloads models
- Simplified logic: removed complex fallback, direct path alignment

Official documentation:
"Files are saved in the default huggingface_hub disk cache ~/.cache/huggingface/hub.
If your application expects them elsewhere or you changed your HF_HOME variable,
this pre-loading does not follow that at this time."
Source: https://huggingface.co/docs/hub/spaces-config-reference

Changes:
- code/cube3d/config.py lines 61-83: Use HF_HUB_CACHE instead of HF_HOME
- Removed complex write-test fallback logic
- Aligned with HF Spaces official behavior

Expected result:
✅ [Config] HuggingFace cache directory: /home/user/.cache/huggingface/hub
✅ CLIP model loaded successfully from preloaded cache

Files changed (1) hide show
  1. code/cube3d/config.py +10 -35
code/cube3d/config.py CHANGED
@@ -58,42 +58,17 @@ 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 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
 
58
  CAR_1K_DIR = DATA_DIR / "car_1k"
59
  CAR_DATA_DIR = DATA_DIR / "1313个筛选车结构和对照渲染图"
60
 
61
+ # HuggingFace model cache directory
62
+ # CRITICAL: Must match where preload_from_hub downloads models
63
  if ENVIRONMENT == "huggingface":
64
+ # HuggingFace Spaces: Use HF_HUB_CACHE (matches preload_from_hub behavior)
65
+ # preload_from_hub ALWAYS downloads to ~/.cache/huggingface/hub regardless of HF_HOME
66
+ # See: https://huggingface.co/docs/hub/spaces-config-reference
67
+ HF_CACHE_DIR = os.getenv(
68
+ "HF_HUB_CACHE",
69
+ os.path.expanduser("~/.cache/huggingface/hub")
70
+ )
71
+ print(f"✅ [Config] HuggingFace cache directory: {HF_CACHE_DIR}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  else:
74
  # Local development: use standard user cache