| |
| """ |
| download_models.py |
| |
| Downloads all required models to /tmp/models (more space available on HF Spaces) |
| and creates a symlink at ./models β /tmp/models so main.py can find them |
| without any changes. |
| |
| Run this ONCE before starting the server: |
| python download_models.py |
| """ |
|
|
| import os |
| import sys |
| import logging |
| from pathlib import Path |
| from huggingface_hub import snapshot_download |
|
|
| logging.basicConfig( |
| level=logging.INFO, |
| format="%(asctime)s [%(levelname)s] %(message)s", |
| ) |
| logger = logging.getLogger(__name__) |
|
|
| |
| TMP_MODELS_DIR = Path("/tmp/models") |
| SYMLINK_PATH = Path("./models") |
|
|
| |
| MODELS = { |
| "urdu-speecht5-finetuned": "ahmedjaved812/urdu-tts-phonemes-speecht5-finetuned", |
| "urdu-tts-processor": "ahmedjaved812/urdu-phonemes-speecht5-processor", |
| "speecht5-hifigan": "microsoft/speecht5_hifigan", |
| "spkrec-xvect-voxceleb": "speechbrain/spkrec-xvect-voxceleb", |
| } |
|
|
|
|
| def download_models(): |
| TMP_MODELS_DIR.mkdir(parents=True, exist_ok=True) |
| logger.info(f"Downloading models to {TMP_MODELS_DIR}") |
|
|
| for subdir, repo_id in MODELS.items(): |
| dest = TMP_MODELS_DIR / subdir |
|
|
| if dest.exists() and any(dest.iterdir()): |
| logger.info(f"β
Already exists, skipping: {subdir}") |
| continue |
|
|
| logger.info(f"β¬οΈ Downloading {repo_id} β {dest}") |
| try: |
| snapshot_download( |
| repo_id=repo_id, |
| local_dir=str(dest), |
| ignore_patterns=["*.msgpack", "flax_model*", "tf_model*", "*.h5"], |
| ) |
| logger.info(f"β
Done: {subdir}") |
| except Exception as e: |
| logger.error(f"β Failed to download {repo_id}: {e}") |
| sys.exit(1) |
|
|
|
|
| def create_symlink(): |
| |
| target = TMP_MODELS_DIR.resolve() |
| symlink = SYMLINK_PATH.resolve() |
|
|
| if symlink.is_symlink(): |
| if symlink.resolve() == target: |
| logger.info(f"β
Symlink already correct: {symlink} β {target}") |
| return |
| else: |
| logger.info(f"π Removing stale symlink: {symlink}") |
| symlink.unlink() |
|
|
| elif symlink.exists(): |
| |
| logger.error( |
| f"β '{symlink}' already exists as a real directory, not a symlink. " |
| f"Remove it manually and re-run this script." |
| ) |
| sys.exit(1) |
|
|
| os.symlink(target, symlink) |
| logger.info(f"β
Symlink created: {symlink} β {target}") |
|
|
|
|
| def copy_embeddings(): |
| """ |
| Copy the embeddings/ directory from the app folder into /tmp/models/embeddings |
| so it lives alongside the models and is accessible via the ./models symlink. |
| """ |
| src = Path("./embeddings").resolve() |
| dst = TMP_MODELS_DIR / "embeddings" |
|
|
| if not src.exists(): |
| logger.error(f"β embeddings/ directory not found at {src}. Add normal_embedding.pt to it.") |
| sys.exit(1) |
|
|
| if dst.exists(): |
| logger.info(f"β
Embeddings already copied, skipping: {dst}") |
| return |
|
|
| import shutil |
| shutil.copytree(str(src), str(dst)) |
| logger.info(f"β
Embeddings copied: {src} β {dst}") |
|
|
|
|
| if __name__ == "__main__": |
| download_models() |
| copy_embeddings() |
| create_symlink() |
| logger.info("π All models ready. You can now start the server.") |
|
|