File size: 1,617 Bytes
b0dbe7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Model Loader for VITS TTS Models
Loads trained models from the models directory.
Models are expected to be in the models/ directory after training.
"""

import os
import logging
from pathlib import Path
from typing import Optional, List

logger = logging.getLogger(__name__)

# Model directory
MODELS_DIR = Path(os.environ.get("MODELS_DIR", "models"))


def _ensure_models_available():
    """
    Internal function to ensure model files are available.
    Called during engine initialization.
    """
    if MODELS_DIR.exists() and any(MODELS_DIR.iterdir()):
        return True
    
    # Models need to be loaded - this happens during Docker build
    logger.info("Initializing model directory...")
    MODELS_DIR.mkdir(exist_ok=True)
    
    try:
        from huggingface_hub import snapshot_download
        snapshot_download(
            repo_id="Harshil748/VoiceAPI-Models",
            local_dir=MODELS_DIR,
            local_dir_use_symlinks=False,
            ignore_patterns=["*.md", ".gitattributes"],
        )
        logger.info("Models initialized successfully")
        return True
    except Exception as e:
        logger.warning(f"Could not initialize models: {e}")
        return False


def get_model_path(voice_key: str) -> Optional[Path]:
    """Get path to a model directory"""
    model_dir = MODELS_DIR / voice_key
    if model_dir.exists():
        return model_dir
    return None


def list_available_models() -> List[str]:
    """List all available trained models"""
    if not MODELS_DIR.exists():
        return []
    return [d.name for d in MODELS_DIR.iterdir() if d.is_dir()]