Load the model once at the beginning.
(model_dir="pretrained_models/Spark-TTS-0.5B", device=0)
| 27 | |
| 28 | |
| 29 | def initialize_model(model_dir="pretrained_models/Spark-TTS-0.5B", device=0): |
| 30 | """Load the model once at the beginning.""" |
| 31 | logging.info(f"Loading model from: {model_dir}") |
| 32 | |
| 33 | # Determine appropriate device based on platform and availability |
| 34 | if platform.system() == "Darwin": |
| 35 | # macOS with MPS support (Apple Silicon) |
| 36 | device = torch.device(f"mps:{device}") |
| 37 | logging.info(f"Using MPS device: {device}") |
| 38 | elif torch.cuda.is_available(): |
| 39 | # System with CUDA support |
| 40 | device = torch.device(f"cuda:{device}") |
| 41 | logging.info(f"Using CUDA device: {device}") |
| 42 | else: |
| 43 | # Fall back to CPU |
| 44 | device = torch.device("cpu") |
| 45 | logging.info("GPU acceleration not available, using CPU") |
| 46 | |
| 47 | model = SparkTTS(model_dir, device) |
| 48 | return model |
| 49 | |
| 50 | |
| 51 | def run_tts( |