-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathelevenlabs_change_language.py
117 lines (95 loc) · 3.89 KB
/
elevenlabs_change_language.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
from pathlib import Path
from dotenv import load_dotenv
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.llm import function_tool
from livekit.agents.voice import Agent, AgentSession
from livekit.plugins import deepgram, openai, elevenlabs, silero
logger = logging.getLogger("language-switcher")
logger.setLevel(logging.INFO)
load_dotenv(dotenv_path=Path(__file__).parent.parent / '.env')
class LanguageSwitcherAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""
You are a helpful assistant communicating through voice.
You can switch to a different language if asked.
Don't use any unpronouncable characters.
""",
stt=deepgram.STT(
model="nova-2-general",
language="en"
),
llm=openai.LLM(model="gpt-4o"),
tts=elevenlabs.TTS(
encoding="pcm_44100",
model="eleven_turbo_v2_5",
language="en"
),
vad=silero.VAD.load()
)
self.current_language = "en"
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"
}
self.greetings = {
"en": "Hello! I'm now speaking in English. How can I help you today?",
"es": "¡Hola! Ahora estoy hablando en español. ¿Cómo puedo ayudarte hoy?",
"fr": "Bonjour! Je parle maintenant en français. Comment puis-je vous aider aujourd'hui?",
"de": "Hallo! Ich spreche jetzt Deutsch. Wie kann ich Ihnen heute helfen?",
"it": "Ciao! Ora sto parlando in italiano. Come posso aiutarti oggi?"
}
async def on_enter(self):
await self.session.say(f"Hi there! I can speak in multiple languages including Spanish, French, German, and Italian. Just ask me to switch to any of these languages. How can I help you today?")
async def _switch_language(self, language_code: str) -> None:
"""Helper method to switch the language"""
if language_code == self.current_language:
await self.session.say(f"I'm already speaking in {self.language_names[language_code]}.")
return
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)
self.current_language = language_code
await self.session.say(self.greetings[language_code])
@function_tool
async def switch_to_english(self):
"""Switch to speaking English"""
await self._switch_language("en")
@function_tool
async def switch_to_spanish(self):
"""Switch to speaking Spanish"""
await self._switch_language("es")
@function_tool
async def switch_to_french(self):
"""Switch to speaking French"""
await self._switch_language("fr")
@function_tool
async def switch_to_german(self):
"""Switch to speaking German"""
await self._switch_language("de")
@function_tool
async def switch_to_italian(self):
"""Switch to speaking Italian"""
await self._switch_language("it")
async def entrypoint(ctx: JobContext):
await ctx.connect()
session = AgentSession()
await session.start(
agent=LanguageSwitcherAgent(),
room=ctx.room
)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))