Voice-Clone / model_loader.py
Shanmukhcr7's picture
Upload folder using huggingface_hub
faaced4 verified
Raw
History Blame Contribute Delete
1.56 kB
import os
import torch
import gc
from config import CACHE_DIR
class ModelSingleton:
_instance = None
_model = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(ModelSingleton, cls).__new__(cls)
return cls._instance
def load_model(self):
if self._model is not None:
return self._model
print("Initializing Chatterbox TTS Model (CPU Only)...")
# Optimize for CPU
torch.set_num_threads(max(1, os.cpu_count() - 1)) # Leave 1 thread for OS
try:
# We are using CPU strictly
device = "cpu"
model_path = os.path.join(CACHE_DIR, "chatterbox")
# If not downloaded, download it
if not os.path.exists(model_path):
print("Model not found locally. Downloading now...")
from download_model import download_model
download_model()
try:
from chatterbox import ChatterboxTTS
except ImportError:
raise ImportError("Could not import chatterbox. Please run install.py")
# Initialize ChatterboxTTS
self._model = ChatterboxTTS.from_local(model_path, device=device)
print("Model loaded successfully.")
return self._model
except Exception as e:
print(f"Failed to load model: {e}")
raise e
def get_model():
return ModelSingleton().load_model()