🦝 Move XTTS imports to module level for worker processes

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.
This commit is contained in:
Russell Ballestrini 2025-11-09 18:40:38 -05:00
parent 3887e9b850
commit bb9823f6d0

View file

@ -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')