voice-cloning-backend / backend /setup_models.py
AJ50's picture
Add on-demand XTTS model download via setup_models.py
1b05367
raw
history blame
2 kB
#!/usr/bin/env python
"""Download XTTS model on-demand (runs once on first deployment)."""
import os
import sys
from pathlib import Path
def setup_models():
"""Ensure all required models are available."""
print("[Setup] Checking model requirements...")
# Ensure backend/models directory exists
models_dir = Path(__file__).parent.parent / "models"
models_dir.mkdir(parents=True, exist_ok=True)
tts_model_dir = models_dir / "tts" / "tts_models--multilingual--multi-dataset--xtts_v2"
if tts_model_dir.exists() and (tts_model_dir / "model.pth").exists():
print(f"[Setup] βœ“ XTTS model already present: {tts_model_dir}")
return True
print("[Setup] Downloading XTTS-v2 model (1.8GB, first time only)...")
print("[Setup] This may take 5-10 minutes on first deployment...")
try:
from TTS.api import TTS
import io
os.environ['TTS_HOME'] = str(models_dir)
# Suppress interactive prompts
old_stdin = sys.stdin
sys.stdin = io.StringIO("y\n")
try:
tts = TTS(
model_name="tts_models/multilingual/multi-dataset/xtts_v2",
gpu=False
)
print("[Setup] βœ“ XTTS model downloaded successfully")
# Verify model exists
if (tts_model_dir / "model.pth").exists():
print(f"[Setup] βœ“ Model verified at: {tts_model_dir}")
return True
else:
print(f"[Setup] βœ— Model not found at expected location: {tts_model_dir}")
return False
finally:
sys.stdin = old_stdin
except Exception as e:
print(f"[Setup] βœ— Failed to download XTTS model: {e}")
print("[Setup] Hindi synthesis will not be available")
return False
if __name__ == "__main__":
success = setup_models()
sys.exit(0 if success else 1)