Toy
Claude
commited on
Commit
Β·
7efd7d2
1
Parent(s):
07f3e95
Fix Hugging Face cache path configuration for Spaces
Browse files- Add environment-aware cache path setup in config.py
- Auto-detect HF Spaces and use default cache (not /Volumes/extssd)
- Initialize config early in app.py to set paths before model imports
- Remove .env from repository (local development only)
- Add .env to .gitignore to prevent future commits
π€ Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
- .env +0 -8
- .gitignore +2 -1
- app.py +5 -0
- src/core/config.py +19 -0
.env
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
# Hugging Face Configuration
|
2 |
-
# Point Hugging Face cache to external SSD using official environment variables
|
3 |
-
# This will create /Volumes/extssd/huggingface/hub and /Volumes/extssd/huggingface/datasets
|
4 |
-
HF_HOME=/Volumes/extssd/huggingface
|
5 |
-
|
6 |
-
# Model configuration (keep existing values)
|
7 |
-
MODEL_ID=stabilityai/stable-diffusion-xl-base-1.0
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
training_data/
|
2 |
.DS_Store
|
3 |
__pycache__/
|
4 |
-
*.pyc
|
|
|
|
1 |
training_data/
|
2 |
.DS_Store
|
3 |
__pycache__/
|
4 |
+
*.pyc
|
5 |
+
.env
|
app.py
CHANGED
@@ -18,6 +18,11 @@ src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")
|
|
18 |
if src_path not in sys.path:
|
19 |
sys.path.insert(0, src_path)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
from ui.french_style.french_style_tab import FrenchStyleTab
|
22 |
from ui.generate.generate_tab import GenerateTab
|
23 |
from ui.identify.identify_tab import IdentifyTab
|
|
|
18 |
if src_path not in sys.path:
|
19 |
sys.path.insert(0, src_path)
|
20 |
|
21 |
+
# Initialize config early to setup cache paths before model imports
|
22 |
+
from core.config import config
|
23 |
+
print(f"π§ Environment: {'HF Spaces' if config.is_hf_spaces else 'Local'}")
|
24 |
+
print(f"π§ Device: {config.device}, dtype: {config.dtype}")
|
25 |
+
|
26 |
from ui.french_style.french_style_tab import FrenchStyleTab
|
27 |
from ui.generate.generate_tab import GenerateTab
|
28 |
from ui.identify.identify_tab import IdentifyTab
|
src/core/config.py
CHANGED
@@ -14,6 +14,7 @@ class AppConfig:
|
|
14 |
self.model_id = DEFAULT_MODEL_ID
|
15 |
# Auto-detect Hugging Face Spaces environment
|
16 |
self.is_hf_spaces = os.getenv("SPACE_ID") is not None
|
|
|
17 |
|
18 |
def _setup_device(self):
|
19 |
"""Setup device configuration for PyTorch."""
|
@@ -29,6 +30,24 @@ class AppConfig:
|
|
29 |
self.device = "cpu"
|
30 |
self.dtype = torch.float32
|
31 |
self.clf_device = -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
@property
|
34 |
def is_cuda_available(self):
|
|
|
14 |
self.model_id = DEFAULT_MODEL_ID
|
15 |
# Auto-detect Hugging Face Spaces environment
|
16 |
self.is_hf_spaces = os.getenv("SPACE_ID") is not None
|
17 |
+
self._setup_cache_paths()
|
18 |
|
19 |
def _setup_device(self):
|
20 |
"""Setup device configuration for PyTorch."""
|
|
|
30 |
self.device = "cpu"
|
31 |
self.dtype = torch.float32
|
32 |
self.clf_device = -1
|
33 |
+
|
34 |
+
def _setup_cache_paths(self):
|
35 |
+
"""Setup cache paths based on environment."""
|
36 |
+
if self.is_hf_spaces:
|
37 |
+
# On HF Spaces, don't set HF_HOME - let it use default cache
|
38 |
+
if "HF_HOME" in os.environ:
|
39 |
+
del os.environ["HF_HOME"]
|
40 |
+
print("π Using default Hugging Face cache on Spaces")
|
41 |
+
else:
|
42 |
+
# Local development - use external SSD if configured
|
43 |
+
local_hf_home = "/Volumes/extssd/huggingface"
|
44 |
+
if os.path.exists("/Volumes/extssd") and not os.getenv("HF_HOME"):
|
45 |
+
os.environ["HF_HOME"] = local_hf_home
|
46 |
+
print(f"π Using external SSD cache: {local_hf_home}")
|
47 |
+
elif os.getenv("HF_HOME"):
|
48 |
+
print(f"π Using configured HF_HOME: {os.getenv('HF_HOME')}")
|
49 |
+
else:
|
50 |
+
print("π Using default Hugging Face cache")
|
51 |
|
52 |
@property
|
53 |
def is_cuda_available(self):
|