import logging import os import torch from silero import silero_tts from .config import Settings logger = logging.getLogger(__name__) _models: dict = {} _accentor = None def load_models(cfg: Settings) -> None: global _models, _accentor is_cpu = cfg.device == "cpu" if is_cpu: threads = cfg.torch_threads if threads == 0: threads = os.cpu_count() or 4 logger.info(f"CPU-режим: авто-определение ({threads} потоков), JIT={cfg.jit_compile}") else: logger.info(f"CPU-режим: {threads} потоков, JIT={cfg.jit_compile}") torch.set_num_threads(threads) torch.set_num_interop_threads(max(1, threads // 2)) device = torch.device(cfg.device) logger.info(f"Загрузка модели Silero {cfg.model} на {device}...") model, _ = silero_tts(language='ru', speaker=cfg.model) model.to(device) _models = { 'model': model, 'device': device, 'sample_rate': cfg.sample_rate } # Accentor — опционально, может не работать в некоторых версиях Python try: from silero_stress import load_accentor logger.info("Загрузка silero-stress...") _accentor = load_accentor(lang='ru') logger.info("Accentor загружен успешно") except Exception as e: logger.warning(f"Accentor не загружен: {e}. TTS будет работать без интонаций.") _accentor = None logger.info(f"Модели успешно загружены device={cfg.device}") def get_model(): if 'model' not in _models: raise RuntimeError("Модели не загружены. Вызовите load_models() при старте.") return _models['model'], _models['device'] def get_accentor(): if _accentor is None: logger.debug("Accentor не загружен, возвращаем None") return None return _accentor