diff --git a/speech.py b/speech.py index 4e1344a..78462ce 100755 --- a/speech.py +++ b/speech.py @@ -119,7 +119,7 @@ kokoro_lang = None # Default args for worker processes (will be overridden in __main__) class DefaultArgs: - xtts_device = 'cpu' + xtts_device = None # Will be set after torch import use_deepspeed = False unload_timer = None log_level = 'INFO' @@ -130,6 +130,14 @@ class DefaultArgs: args = DefaultArgs() +# Set default device after torch is available (for worker processes) +# Main process will override this in __main__ with argparse +if args.xtts_device is None: + try: + args.xtts_device = auto_torch_device() + except: + args.xtts_device = 'cpu' + # Voice-to-model lookup cache (loaded at startup) voice_to_model_cache = {} @@ -800,10 +808,10 @@ async def generate_speech(request: GenerateSpeechRequest): async with kokoro_load_semaphore: # Double-check after acquiring lock (another request may have loaded it) if kokoro_pipeline is None or kokoro_lang != lang_code: - logger.info(f"Loading/switching Kokoro pipeline to language '{lang_code}' on {args.xtts_device}") # Run blocking model initialization in thread pool to avoid blocking event loop - # Use same device as XTTS for GPU acceleration + # Use GPU if available, fallback to CPU only if explicitly disabled device = args.xtts_device if args.xtts_device != 'none' else 'cpu' + logger.info(f"Loading/switching Kokoro pipeline to language '{lang_code}' on device '{device}'") kokoro_pipeline = await asyncio.to_thread(kokoro_wrapper, lang_code=lang_code, device=device) kokoro_lang = lang_code