how to switch tts model while the conversation is ...
# ask-ai
b
how to switch tts model while the conversation is going on based on the language detected by the stt
t
Thanks for reaching out, Harsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thank you for reaching out! Just to make sure I understand correctly, are you using LiveKit's APIs for both TTS (Text-to-Speech) and STT (Speech-to-Text)? Also, would you like some help with setting up the logic to detect language and switch models, or are you looking for assistance with specific LiveKit configurations? I'm here to help you through this!
b
help with setting up the logic to detect language and switch models using livekit for both tts and stt
t
Let's walk through how you can set up logic to detect language and switch TTS models dynamically using LiveKit for both TTS and STT. I’m here to help you every step of the way: 1. Create a Language Switcher Agent: This agent will manage both STT and TTS for you. Here's a simple example to get started:
Copy code
class LanguageSwitcherAgent(Agent):
    def __init__(self):
        super().__init__(
            stt=deepgram.STT(
                model="nova-2-general",
                language="en"
            ),
            tts=elevenlabs.TTS(
                model="eleven_turbo_v2_5",
                language="en"
            )
        )
1. Define Supported Languages: It’s helpful to map language codes to their names and configurations like this:
Copy code
self.language_names = {
    "en": "English",
    "es": "Spanish",
    "fr": "French",
    "de": "German",
    "it": "Italian"
}

self.deepgram_language_codes = {
    "en": "en",
    "es": "es",
    "fr": "fr-CA",
    "de": "de",
    "it": "it"
}
1. Implement Language Switching Logic: This part updates your TTS and STT options dynamically whenever the language changes:
Copy code
async def _switch_language(self, language_code: str):
    if self.tts is not None:
        self.tts.update_options(language=language_code)

    if self.stt is not None:
        deepgram_language = self.deepgram_language_codes.get(language_code, language_code)
        self.stt.update_options(language=deepgram_language)
1. Create Function Tools: These tools let you switch languages programmatically, making your app more flexible:
Copy code
@function_tool
async def switch_to_spanish(self):
    await self._switch_language("es")
Just so you know, LiveKit's MultilingualModel supports English plus 13 other languages. It uses the language detected by the STT plugin to manage turn detection in multilingual conversations, which is pretty neat! If you’d like any more help implementing this or have questions along the way, please don’t hesitate to reach out. I’m here to support you! Sources: pipeline-tts/tts_comparison.py | LiveKit turn detector plugin | LiveKit Docs