livekit-plugins-elevenlabs 1.0.18__py3-none-any.whl → 1.0.20__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,11 +12,18 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ """ElevenLabs plugin for LiveKit Agents
16
+
17
+ See https://docs.livekit.io/agents/integrations/tts/elevenlabs/ for more information.
18
+ """
19
+
15
20
  from .models import TTSEncoding, TTSModels
21
+ from .stt import STT
16
22
  from .tts import DEFAULT_VOICE_ID, TTS, Voice, VoiceSettings
17
23
  from .version import __version__
18
24
 
19
25
  __all__ = [
26
+ "STT",
20
27
  "TTS",
21
28
  "Voice",
22
29
  "VoiceSettings",
@@ -0,0 +1,127 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ import asyncio
18
+ import os
19
+ from dataclasses import dataclass
20
+
21
+ import aiohttp
22
+
23
+ from livekit import rtc
24
+ from livekit.agents import (
25
+ DEFAULT_API_CONNECT_OPTIONS,
26
+ APIConnectionError,
27
+ APIConnectOptions,
28
+ APIStatusError,
29
+ APITimeoutError,
30
+ stt,
31
+ )
32
+ from livekit.agents.stt import SpeechEventType, STTCapabilities
33
+ from livekit.agents.types import NOT_GIVEN, NotGivenOr
34
+ from livekit.agents.utils import AudioBuffer, http_context, is_given
35
+
36
+ API_BASE_URL_V1 = "https://api.elevenlabs.io/v1"
37
+ AUTHORIZATION_HEADER = "xi-api-key"
38
+
39
+
40
+ @dataclass
41
+ class _STTOptions:
42
+ api_key: str
43
+ base_url: str
44
+ language_code: str = "en"
45
+
46
+
47
+ class STT(stt.STT):
48
+ def __init__(
49
+ self,
50
+ api_key: NotGivenOr[str] = NOT_GIVEN,
51
+ base_url: NotGivenOr[str] = NOT_GIVEN,
52
+ http_session: aiohttp.ClientSession | None = None,
53
+ language_code: NotGivenOr[str] = NOT_GIVEN,
54
+ ) -> None:
55
+ """
56
+ Create a new instance of ElevenLabs TTS.
57
+
58
+ Args:
59
+ api_key (NotGivenOr[str]): ElevenLabs API key. Can be set via argument or `ELEVEN_API_KEY` environment variable.
60
+ base_url (NotGivenOr[str]): Custom base URL for the API. Optional.
61
+ http_session (aiohttp.ClientSession | None): Custom HTTP session for API requests. Optional.
62
+ language (NotGivenOr[str]): Language code for the STT model. Optional.
63
+ """ # noqa: E501
64
+ super().__init__(capabilities=STTCapabilities(streaming=False, interim_results=True))
65
+
66
+ elevenlabs_api_key = api_key if is_given(api_key) else os.environ.get("ELEVEN_API_KEY")
67
+ if not elevenlabs_api_key:
68
+ raise ValueError(
69
+ "ElevenLabs API key is required, either as argument or "
70
+ "set ELEVEN_API_KEY environmental variable"
71
+ )
72
+ self._opts = _STTOptions(
73
+ api_key=elevenlabs_api_key,
74
+ base_url=base_url if is_given(base_url) else API_BASE_URL_V1,
75
+ language_code=language_code,
76
+ )
77
+ self._session = http_session
78
+
79
+ def _ensure_session(self) -> aiohttp.ClientSession:
80
+ if not self._session:
81
+ self._session = http_context.http_session()
82
+
83
+ return self._session
84
+
85
+ async def _recognize_impl(
86
+ self,
87
+ buffer: AudioBuffer,
88
+ *,
89
+ language: NotGivenOr[str] = NOT_GIVEN,
90
+ conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
91
+ ) -> stt.SpeechEvent:
92
+ if is_given(language):
93
+ self._opts.language_code = language
94
+
95
+ wav_bytes = rtc.combine_audio_frames(buffer).to_wav_bytes()
96
+ form = aiohttp.FormData()
97
+ form.add_field("file", wav_bytes, filename="audio.wav", content_type="audio/x-wav")
98
+ form.add_field("model_id", "scribe_v1")
99
+ form.add_field("language_code", self._opts.language_code)
100
+
101
+ try:
102
+ async with self._ensure_session().post(
103
+ f"{API_BASE_URL_V1}/speech-to-text",
104
+ data=form,
105
+ headers={AUTHORIZATION_HEADER: self._opts.api_key},
106
+ ) as response:
107
+ response_json = await response.json()
108
+ extracted_text = response_json.get("text")
109
+ except asyncio.TimeoutError as e:
110
+ raise APITimeoutError() from e
111
+ except aiohttp.ClientResponseError as e:
112
+ raise APIStatusError(
113
+ message=e.message,
114
+ status_code=e.status,
115
+ request_id=None,
116
+ body=None,
117
+ ) from e
118
+ except Exception as e:
119
+ raise APIConnectionError() from e
120
+
121
+ return self._transcription_to_speech_event(text=extracted_text)
122
+
123
+ def _transcription_to_speech_event(self, text: str) -> stt.SpeechEvent:
124
+ return stt.SpeechEvent(
125
+ type=SpeechEventType.FINAL_TRANSCRIPT,
126
+ alternatives=[stt.SpeechData(text=text, language=self._opts.language_code)],
127
+ )
@@ -88,7 +88,7 @@ class _TTSOptions:
88
88
  sample_rate: int
89
89
  streaming_latency: NotGivenOr[int]
90
90
  word_tokenizer: tokenize.WordTokenizer
91
- chunk_length_schedule: list[int]
91
+ chunk_length_schedule: NotGivenOr[list[int]]
92
92
  enable_ssml_parsing: bool
93
93
  inactivity_timeout: int
94
94
 
@@ -99,7 +99,7 @@ class TTS(tts.TTS):
99
99
  *,
100
100
  voice_id: str = DEFAULT_VOICE_ID,
101
101
  voice_settings: NotGivenOr[VoiceSettings] = NOT_GIVEN,
102
- model: TTSModels | str = "eleven_flash_v2_5",
102
+ model: TTSModels | str = "eleven_turbo_v2_5",
103
103
  encoding: NotGivenOr[TTSEncoding] = NOT_GIVEN,
104
104
  api_key: NotGivenOr[str] = NOT_GIVEN,
105
105
  base_url: NotGivenOr[str] = NOT_GIVEN,
@@ -124,14 +124,11 @@ class TTS(tts.TTS):
124
124
  inactivity_timeout (int): Inactivity timeout in seconds for the websocket connection. Defaults to 300.
125
125
  word_tokenizer (NotGivenOr[tokenize.WordTokenizer]): Tokenizer for processing text. Defaults to basic WordTokenizer.
126
126
  enable_ssml_parsing (bool): Enable SSML parsing for input text. Defaults to False.
127
- chunk_length_schedule (NotGivenOr[list[int]]): Schedule for chunk lengths, ranging from 50 to 500. Defaults to [80, 120, 200, 260].
127
+ chunk_length_schedule (NotGivenOr[list[int]]): Schedule for chunk lengths, ranging from 50 to 500. Defaults are [120, 160, 250, 290].
128
128
  http_session (aiohttp.ClientSession | None): Custom HTTP session for API requests. Optional.
129
129
  language (NotGivenOr[str]): Language code for the TTS model, as of 10/24/24 only valid for "eleven_turbo_v2_5".
130
130
  """ # noqa: E501
131
131
 
132
- if not is_given(chunk_length_schedule):
133
- chunk_length_schedule = [80, 120, 200, 260]
134
-
135
132
  if not is_given(encoding):
136
133
  encoding = _DefaultEncoding
137
134
 
@@ -404,11 +401,13 @@ class SynthesizeStream(tts.SynthesizeStream):
404
401
  # 11labs protocol expects the first message to be an "init msg"
405
402
  init_pkt = {
406
403
  "text": " ",
407
- "voice_settings": _strip_nones(dataclasses.asdict(self._opts.voice_settings))
408
- if is_given(self._opts.voice_settings)
409
- else None,
410
- "generation_config": {"chunk_length_schedule": self._opts.chunk_length_schedule},
411
404
  }
405
+ if is_given(self._opts.chunk_length_schedule):
406
+ init_pkt["generation_config"] = {
407
+ "chunk_length_schedule": self._opts.chunk_length_schedule
408
+ }
409
+ if is_given(self._opts.voice_settings):
410
+ init_pkt["voice_settings"] = _strip_nones(dataclasses.asdict(self._opts.voice_settings))
412
411
  await ws_conn.send_str(json.dumps(init_pkt))
413
412
  eos_sent = False
414
413
 
@@ -418,20 +417,25 @@ class SynthesizeStream(tts.SynthesizeStream):
418
417
  xml_content = []
419
418
  async for data in word_stream:
420
419
  text = data.token
421
- # send the xml phoneme in one go
420
+ # send xml tags fully formed
421
+ xml_start_tokens = ["<phoneme", "<break"]
422
+ xml_end_tokens = ["</phoneme>", "/>"]
423
+
422
424
  if (
423
425
  self._opts.enable_ssml_parsing
424
- and data.token.startswith("<phoneme")
426
+ and any(data.token.startswith(start) for start in xml_start_tokens)
425
427
  or xml_content
426
428
  ):
427
429
  xml_content.append(text)
428
- if data.token.find("</phoneme>") > -1:
430
+
431
+ if any(data.token.find(end) > -1 for end in xml_end_tokens):
429
432
  text = self._opts.word_tokenizer.format_words(xml_content)
430
433
  xml_content = []
431
434
  else:
432
435
  continue
433
436
 
434
437
  data_pkt = {"text": f"{text} "} # must always end with a space
438
+
435
439
  self._mark_started()
436
440
  await ws_conn.send_str(json.dumps(data_pkt))
437
441
  if xml_content:
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- __version__ = "1.0.18"
15
+ __version__ = "1.0.20"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: livekit-plugins-elevenlabs
3
- Version: 1.0.18
3
+ Version: 1.0.20
4
4
  Summary: Agent Framework plugin for voice synthesis with ElevenLabs' API.
5
5
  Project-URL: Documentation, https://docs.livekit.io
6
6
  Project-URL: Website, https://livekit.io/
@@ -18,12 +18,14 @@ Classifier: Topic :: Multimedia :: Sound/Audio
18
18
  Classifier: Topic :: Multimedia :: Video
19
19
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
20
  Requires-Python: >=3.9.0
21
- Requires-Dist: livekit-agents[codecs]>=1.0.18
21
+ Requires-Dist: livekit-agents[codecs]>=1.0.20
22
22
  Description-Content-Type: text/markdown
23
23
 
24
- # LiveKit Plugins Elevenlabs
24
+ # ElevenLabs plugin for LiveKit Agents
25
25
 
26
- Agent Framework plugin for voice synthesis with [ElevenLabs](https://elevenlabs.io/) API.
26
+ Support for voice synthesis with [ElevenLabs](https://elevenlabs.io/).
27
+
28
+ See [https://docs.livekit.io/agents/integrations/tts/elevenlabs/](https://docs.livekit.io/agents/integrations/tts/elevenlabs/) for more information.
27
29
 
28
30
  ## Installation
29
31
 
@@ -0,0 +1,10 @@
1
+ livekit/plugins/elevenlabs/__init__.py,sha256=wDHAYf2M89U-wTdW8UjDPI_TQaO9XK03Y0mJpsXvobk,1419
2
+ livekit/plugins/elevenlabs/log.py,sha256=hIuXqDsEB5GBa7rQY3z4Uqi1oCqc_lRmCHZEmXz0LHw,73
3
+ livekit/plugins/elevenlabs/models.py,sha256=p_wHEz15bdsNEqwzN831ysm70PNWQ-xeN__BKvGPZxA,401
4
+ livekit/plugins/elevenlabs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ livekit/plugins/elevenlabs/stt.py,sha256=1B8c7t_52GIbnPSFLq44Fkm0gnnFUZs7xX9nIWbsAQM,4528
6
+ livekit/plugins/elevenlabs/tts.py,sha256=0Mfm6zhmTJKUQZTR_WfEF_p9TIzzrb3p_-VX2dqIJ10,20294
7
+ livekit/plugins/elevenlabs/version.py,sha256=t4KmPVTpEy1pOJ2GRCA-GNJfCQq_-zHNDBxGj4GKfVk,601
8
+ livekit_plugins_elevenlabs-1.0.20.dist-info/METADATA,sha256=kPmu-4R8ZWvXMnkCKdIrcQ6oAs8jpqdg2THHLVXBnz4,1455
9
+ livekit_plugins_elevenlabs-1.0.20.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
+ livekit_plugins_elevenlabs-1.0.20.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- livekit/plugins/elevenlabs/__init__.py,sha256=Va24UYTuuosmRuTcuzd_DIHYQOgV-wSYKJIXmOSB2Go,1255
2
- livekit/plugins/elevenlabs/log.py,sha256=hIuXqDsEB5GBa7rQY3z4Uqi1oCqc_lRmCHZEmXz0LHw,73
3
- livekit/plugins/elevenlabs/models.py,sha256=p_wHEz15bdsNEqwzN831ysm70PNWQ-xeN__BKvGPZxA,401
4
- livekit/plugins/elevenlabs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- livekit/plugins/elevenlabs/tts.py,sha256=KPFf8845VCNG9z5mmwFocjgTilKk-uhA3uvRAnlAd8c,20142
6
- livekit/plugins/elevenlabs/version.py,sha256=cnPu9FVKZV9tFmmz7lEvftrO3B_nWJVFghi3j6UcJLs,601
7
- livekit_plugins_elevenlabs-1.0.18.dist-info/METADATA,sha256=fwQeB6XvUbWIgpZ8jkyN1eqBrgMXnb7HjJ5Ken8KgrQ,1314
8
- livekit_plugins_elevenlabs-1.0.18.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- livekit_plugins_elevenlabs-1.0.18.dist-info/RECORD,,