From bb9823f6d0896e55d14e188f9f03b46fd0bba818 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 9 Nov 2025 18:40:38 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=9D=20Move=20XTTS=20imports=20to=20mod?= =?UTF-8?q?ule=20level=20for=20worker=20processes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Worker processes need access to XTTS classes (ModelManager, XttsConfig, Xtts, split_sentence, detect) but were only imported conditionally in __main__ block. **Solution:** Import at module level with try/except for graceful degradation in minimal installations. Set XTTS_AVAILABLE flag. This ensures worker processes can handle tts-1-hd requests properly. --- speech.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/speech.py b/speech.py index dbcc4d5..6e6d358 100755 --- a/speech.py +++ b/speech.py @@ -20,6 +20,24 @@ from pydantic import BaseModel from typing import Optional import uvicorn +# Try to import XTTS dependencies (might not be available in minimal installations) +try: + import torch + from TTS.tts.configs.xtts_config import XttsConfig + from TTS.tts.models.xtts import Xtts + from TTS.utils.manage import ModelManager + from TTS.tts.layers.xtts.tokenizer import split_sentence + from langdetect import detect + XTTS_AVAILABLE = True +except ImportError: + XTTS_AVAILABLE = False + torch = None + XttsConfig = None + Xtts = None + ModelManager = None + split_sentence = None + detect = None + @contextlib.asynccontextmanager async def lifespan(app): # Startup: Initialize voice caches in each worker process @@ -818,15 +836,9 @@ if __name__ == "__main__": logger.remove() logger.add(sink=sys.stderr, level=args.log_level) - if args.xtts_device != "none": - import torch - from TTS.tts.configs.xtts_config import XttsConfig - from TTS.tts.models.xtts import Xtts - from TTS.utils.manage import ModelManager - from TTS.tts.layers.xtts.tokenizer import split_sentence - from langdetect import detect - - if args.preload: + if args.preload and not XTTS_AVAILABLE: + logger.error("Cannot preload XTTS model - XTTS dependencies not available") + elif args.preload: xtts = xtts_wrapper(args.preload, device=args.xtts_device, unload_timer=args.unload_timer) app.register_model('tts-1')