| """ |
| 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 |
| |
| 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: |
| |
| if "HF_HOME" in os.environ: |
| del os.environ["HF_HOME"] |
| print("π Using default Hugging Face cache on Spaces") |
| else: |
| |
| 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() |
|
|
|
|
| |
| config = AppConfig() |
|
|