Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch generated voice id and add to response #472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/
__pycache__/
poetry.toml
.ruff_cache/
env/
38 changes: 27 additions & 11 deletions src/elevenlabs/voice_generation/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def generate(
accent_strength: float,
text: str,
request_options: typing.Optional[RequestOptions] = None,
) -> typing.Iterator[bytes]:
) -> typing.Tuple[str, typing.Iterator[bytes]]:
"""
Generate a random voice based on parameters. This method returns a generated_voice_id in the response header, and a sample of the voice in the body. If you like the generated voice call /v1/voice-generation/create-voice with the generated_voice_id to create the voice.
Generate a random voice based on parameters. This method returns a generated_voice_id
and an iterator of the voice bytes. If you like the generated voice, call
/v1/voice-generation/create-voice with the generated_voice_id to create the voice.

Parameters
----------
Expand All @@ -99,10 +101,10 @@ def generate(
request_options : typing.Optional[RequestOptions]
Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

Yields
------
typing.Iterator[bytes]
Successful Response
Returns
-------
typing.Tuple[str, typing.Iterator[bytes]]
A tuple containing the generated_voice_id and an iterator of the voice bytes.

Examples
--------
Expand All @@ -111,7 +113,7 @@ def generate(
client = ElevenLabs(
api_key="YOUR_API_KEY",
)
client.voice_generation.generate(
generated_voice_id, voice_iterator = client.voice_generation.generate(
gender="female",
accent="american",
age="middle_aged",
Expand All @@ -137,10 +139,24 @@ def generate(
) as _response:
try:
if 200 <= _response.status_code < 300:
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
yield _chunk
return
generated_voice_id = _response.headers.get('generated_voice_id')
if not generated_voice_id:
raise ApiError(
status_code=_response.status_code,
body="Missing generated_voice_id in response headers."
)

def voice_iterator():
_chunk_size = (
request_options.get("chunk_size", 1024)
if request_options is not None
else 1024
)
for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
yield _chunk

return generated_voice_id, voice_iterator()

_response.read()
if _response.status_code == 422:
raise UnprocessableEntityError(
Expand Down