Spaces:
Running
Running
import logging | |
# Configure logging | |
logger = logging.getLogger(__name__) | |
# Import the factory pattern implementation | |
from utils.tts_factory import TTSFactory | |
# Import base classes | |
from utils.tts_base import TTSEngineBase, DummyTTSEngine | |
# Import engine-specific modules | |
from utils.tts_engines import ( | |
get_available_engines, | |
create_engine, | |
KokoroTTSEngine, | |
KokoroSpaceTTSEngine, | |
DiaTTSEngine | |
) | |
# Import legacy functions for backward compatibility | |
from utils.tts_kokoro import generate_speech as kokoro_generate_speech | |
from utils.tts_kokoro_space import generate_speech as kokoro_space_generate_speech | |
from utils.tts_dia import generate_speech as dia_generate_speech | |
# Convenience function to get the best available TTS engine | |
def get_best_engine(lang_code: str = 'z') -> TTSEngineBase: | |
"""Get the best available TTS engine | |
Args: | |
lang_code (str): Language code for the engine | |
Returns: | |
TTSEngineBase: An instance of the best available TTS engine | |
""" | |
return TTSFactory.create_engine(None, lang_code) | |
# Function to get a TTS engine instance (for backward compatibility) | |
def get_tts_engine(engine_type: str = None, lang_code: str = 'z') -> TTSEngineBase: | |
"""Get a TTS engine instance | |
This function is maintained for backward compatibility with app.py. | |
New code should use the factory pattern implementation directly. | |
Args: | |
engine_type (str, optional): Type of engine to create ('kokoro', 'kokoro_space', 'dia', 'dummy') | |
If None, the best available engine will be used | |
lang_code (str): Language code for the engine | |
Returns: | |
TTSEngineBase: An instance of a TTS engine | |
""" | |
return TTSFactory.create_engine(engine_type, lang_code) | |
# Legacy function for backward compatibility | |
def generate_speech(text: str, language: str = "z", voice: str = "af_heart", speed: float = 1.0) -> str: | |
"""Generate speech using the best available TTS engine | |
This is a legacy function maintained for backward compatibility. | |
New code should use the factory pattern implementation directly. | |
Args: | |
text (str): Input text to synthesize | |
language (str): Language code | |
voice (str): Voice ID to use | |
speed (float): Speech speed multiplier | |
Returns: | |
str: Path to the generated audio file | |
""" | |
engine = get_best_engine(language) | |
return engine.generate_speech(text, voice, speed) |