Skip to content

Commit

Permalink
(fix): async tts is no longer awaited (elevenlabs#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-support authored Apr 3, 2024
1 parent 7ed8ef4 commit 92911c6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ src/elevenlabs/client.py
src/elevenlabs/play.py
src/elevenlabs/realtime_tts.py

# Async TTS
src/elevenlabs/text_to_speech/client.py (temporary)

.github/workflows/ci.yml
.github/workflows/tests.yml

Expand Down
2 changes: 1 addition & 1 deletion src/elevenlabs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async def generate(
typing.Sequence[PronunciationDictionaryVersionLocator]
] = OMIT,
request_options: typing.Optional[RequestOptions] = None
) -> Union[bytes, AsyncIterator[bytes]]:
) -> AsyncIterator[bytes]:
"""
This is a manually mnaintained helper function that generates a
voice from provided text.
Expand Down
4 changes: 2 additions & 2 deletions src/elevenlabs/text_to_speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ async def convert(
_request["voice_settings"] = voice_settings
if pronunciation_dictionary_locators is not OMIT:
_request["pronunciation_dictionary_locators"] = pronunciation_dictionary_locators
async with await self._client_wrapper.httpx_client.stream(
async with self._client_wrapper.httpx_client.stream(
"POST",
urllib.parse.urljoin(
f"{self._client_wrapper.get_base_url()}/", f"v1/text-to-speech/{jsonable_encoder(voice_id)}"
Expand Down Expand Up @@ -442,7 +442,7 @@ async def convert_as_stream(
_request["voice_settings"] = voice_settings
if pronunciation_dictionary_locators is not OMIT:
_request["pronunciation_dictionary_locators"] = pronunciation_dictionary_locators
async with await self._client_wrapper.httpx_client.stream(
async with self._client_wrapper.httpx_client.stream(
"POST",
urllib.parse.urljoin(
f"{self._client_wrapper.get_base_url()}/", f"v1/text-to-speech/{jsonable_encoder(voice_id)}/stream"
Expand Down
31 changes: 31 additions & 0 deletions tests/test_async_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import asyncio
import pytest

from .utils import IN_GITHUB, async_client
from elevenlabs import play

def test_async_generation():
async def main():
results = await async_client.generate(
voice='Rachel',
model='eleven_multilingual_v2',
text='This is an example sentence',
)
out = b''
async for value in results:
out += value
if not IN_GITHUB:
play(out)

results = await async_client.generate(
voice='Rachel',
model='eleven_multilingual_v2',
text='This is an example sentence with streaming',
stream=True
)
out = b''
async for value in results:
out += value
if not IN_GITHUB:
play(out)
asyncio.run(main())
5 changes: 4 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import httpx

from typing import Sequence, Generator
from elevenlabs.client import ElevenLabs
from elevenlabs.client import ElevenLabs, \
AsyncElevenLabs

IN_GITHUB = "GITHUB_ACTIONS" in os.environ

client = ElevenLabs()

async_client = AsyncElevenLabs()


def as_local_files(urls: Sequence[str]) -> Generator[str, None, None]:
"""Util to download files from urls and return local file paths"""
Expand Down

0 comments on commit 92911c6

Please sign in to comment.