Skip to content

Commit

Permalink
support language code in ElevenLabs TTS (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
cch41 authored Nov 1, 2024
1 parent 2ff2493 commit 5fb1e84
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class _TTSOptions:
api_key: str
voice: Voice
model: TTSModels | str
language: str | None
base_url: str
encoding: TTSEncoding
sample_rate: int
Expand Down Expand Up @@ -114,6 +115,7 @@ def __init__(
http_session: aiohttp.ClientSession | None = None,
# deprecated
model_id: TTSModels | str | None = None,
language: str | None = None,
) -> None:
"""
Create a new instance of ElevenLabs TTS.
Expand All @@ -129,6 +131,7 @@ def __init__(
enable_ssml_parsing (bool): Enable SSML parsing for input text. Defaults to False.
chunk_length_schedule (list[int]): Schedule for chunk lengths, ranging from 50 to 500. Defaults to [80, 120, 200, 260].
http_session (aiohttp.ClientSession | None): Custom HTTP session for API requests. Optional.
language (str | None): Language code for the TTS model, as of 10/24/24 only valid for "eleven_turbo_v2_5". Optional.
"""

super().__init__(
Expand Down Expand Up @@ -162,6 +165,7 @@ def __init__(
word_tokenizer=word_tokenizer,
chunk_length_schedule=chunk_length_schedule,
enable_ssml_parsing=enable_ssml_parsing,
language=language,
)
self._session = http_session

Expand Down Expand Up @@ -523,8 +527,12 @@ def _stream_url(opts: _TTSOptions) -> str:
output_format = opts.encoding
latency = opts.streaming_latency
enable_ssml = str(opts.enable_ssml_parsing).lower()
return (
language = opts.language
url = (
f"{base_url}/text-to-speech/{voice_id}/stream-input?"
f"model_id={model_id}&output_format={output_format}&optimize_streaming_latency={latency}&"
f"enable_ssml_parsing={enable_ssml}"
)
if language is not None:
url += f"&language_code={language}"
return url

0 comments on commit 5fb1e84

Please sign in to comment.