from typing import AsyncGenerator from app.core.providers.base import TTSProvider class TTSService: """ Service class for generating speech from text using a TTS provider. """ def __init__(self, tts_provider: TTSProvider): """ Initializes the TTSService with a concrete TTS provider. """ self.tts_provider = tts_provider async def create_speech_stream(self, text: str) -> AsyncGenerator[bytes, None]: """ Generates a stream of audio bytes from the given text using the configured TTS provider. Args: text: The text to be converted to speech. Returns: An async generator that yields chunks of audio bytes. """ return self.tts_provider.generate_speech(text) async def create_speech_non_stream(self, text: str) -> bytes: """ Generates a complete audio file from the given text without streaming. Args: text: The text to be converted to speech. Returns: The complete audio file as bytes. """ # Awaiting the coroutine is necessary to get the result. # The previous version was missing this 'await'. audio_data = await self.tts_provider.generate_speech(text) return audio_data