flowerfy / src /core /config.py
Toy
Apply code formatting and fix compatibility issues
b24c04f
"""
Configuration management for the application.
"""
import os
import torch
from .constants import DEFAULT_MODEL_ID
class AppConfig:
"""Application configuration singleton."""
def __init__(self):
self._setup_device()
self.model_id = DEFAULT_MODEL_ID
# Auto-detect Hugging Face Spaces environment
self.is_hf_spaces = os.getenv("SPACE_ID") is not None
self._setup_cache_paths()
def _setup_device(self):
"""Setup device configuration for PyTorch."""
if torch.cuda.is_available():
self.device = "cuda"
self.dtype = torch.float16
self.clf_device = 0
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
self.device = "mps"
self.dtype = torch.float16
self.clf_device = 0
else:
self.device = "cpu"
self.dtype = torch.float32
self.clf_device = -1
def _setup_cache_paths(self):
"""Setup cache paths based on environment."""
if self.is_hf_spaces:
# On HF Spaces, don't set HF_HOME - let it use default cache
if "HF_HOME" in os.environ:
del os.environ["HF_HOME"]
print("🏠 Using default Hugging Face cache on Spaces")
else:
# Local development - use external SSD if configured
local_hf_home = "/Volumes/extssd/huggingface"
if os.path.exists("/Volumes/extssd") and not os.getenv("HF_HOME"):
os.environ["HF_HOME"] = local_hf_home
print(f"🏠 Using external SSD cache: {local_hf_home}")
elif os.getenv("HF_HOME"):
print(f"🏠 Using configured HF_HOME: {os.getenv('HF_HOME')}")
else:
print("🏠 Using default Hugging Face cache")
@property
def is_cuda_available(self):
"""Check if CUDA is available."""
return torch.cuda.is_available()
@property
def is_mps_available(self):
"""Check if Apple MPS is available."""
return hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
# Global configuration instance
config = AppConfig()