Spaces:
Sleeping
Sleeping
| # utils/startup_models.py | |
| from huggingface_hub import snapshot_download | |
| from pathlib import Path | |
| import os | |
| # Where to put runtime files (works in Spaces; persists if you enable Persistent Storage) | |
| RUNTIME_DIR = Path(os.environ.get("RUNTIME_DIR", "/home/user/app/runtime")) | |
| MODELS_DIR = RUNTIME_DIR / "models" | |
| MODELS_DIR.mkdir(parents=True, exist_ok=True) | |
| def ensure_model(repo_id: str, pattern: str | None = None, local_name: str | None = None) -> str: | |
| dest = MODELS_DIR / (local_name or repo_id.replace("/", "_")) | |
| if dest.exists(): | |
| return str(dest) | |
| snapshot_download( | |
| repo_id=repo_id, | |
| local_dir=str(dest), | |
| local_dir_use_symlinks=False, # real copies; safer with persistent storage | |
| allow_patterns=pattern # e.g. "*.gguf" or "*.onnx" | |
| ) | |
| return str(dest) |