Download data/src/elevenlabs/flows_custom.py from introvoyz045/elevenlabs-python: direct link, hf CLI and curl.
- Browser
- Download file 39.2 kB
-
https://huggingface.co/datasets/introvoyz045/elevenlabs-python/resolve/main/data/src/elevenlabs/flows_custom.py
- Command line
-
hf download hf://datasets/introvoyz045/elevenlabs-python/data/src/elevenlabs/flows_custom.py
-
curl -L -o flows_custom.py https://huggingface.co/datasets/introvoyz045/elevenlabs-python/resolve/main/data/src/elevenlabs/flows_custom.py
39.2 kB
| import asyncio | |
| import email.utils | |
| import math | |
| import time | |
| import typing | |
| from random import random | |
| from elevenlabs.core.http_response import AsyncHttpResponse, HttpResponse | |
| from elevenlabs.core.request_options import RequestOptions | |
| from elevenlabs.flows.client import AsyncFlowsClient as AutogeneratedAsyncFlowsClient | |
| from elevenlabs.flows.client import FlowsClient as AutogeneratedFlowsClient | |
| from elevenlabs.flows.image.client import AsyncImageClient as AutogeneratedAsyncImageClient | |
| from elevenlabs.flows.image.client import ImageClient as AutogeneratedImageClient | |
| from elevenlabs.flows.templates.client import AsyncTemplatesClient as AutogeneratedAsyncTemplatesClient | |
| from elevenlabs.flows.templates.client import TemplatesClient as AutogeneratedTemplatesClient | |
| from elevenlabs.flows.templates.runs.client import AsyncRunsClient as AutogeneratedAsyncRunsClient | |
| from elevenlabs.flows.templates.runs.client import RunsClient as AutogeneratedRunsClient | |
| from elevenlabs.flows.text_to_speech.client import AsyncTextToSpeechClient as AutogeneratedAsyncTextToSpeechClient | |
| from elevenlabs.flows.text_to_speech.client import TextToSpeechClient as AutogeneratedTextToSpeechClient | |
| from elevenlabs.flows.video.client import AsyncVideoClient as AutogeneratedAsyncVideoClient | |
| from elevenlabs.flows.video.client import VideoClient as AutogeneratedVideoClient | |
| from elevenlabs.types.image_generation_request import ImageGenerationRequest | |
| from elevenlabs.types.media_generation_response import ( | |
| MediaGenerationResponse_Completed, | |
| MediaGenerationResponse_Failed, | |
| ) | |
| from elevenlabs.types.template_run_input import TemplateRunInput | |
| from elevenlabs.types.template_run_response import TemplateRunResponse | |
| from elevenlabs.types.text_to_speech_generation_request import TextToSpeechGenerationRequest | |
| from elevenlabs.types.video_generation_request import VideoGenerationRequest | |
| from elevenlabs.types.webhook_target import WebhookTarget | |
| # this is used as the default value for optional parameters | |
| OMIT = typing.cast(typing.Any, ...) | |
| # Matches the server's shortest `Retry-After` (3s for image, audio and text), so the SDK polls no faster | |
| # than the server would pace it when the header is missing. | |
| DEFAULT_MIN_POLL_INTERVAL_SECONDS = 3.0 | |
| # Below this, a missing server hint would turn the wait into a tight loop of status checks. | |
| _SMALLEST_MIN_POLL_INTERVAL_SECONDS = 0.5 | |
| # Spreads out clients that started together so their status checks don't arrive in lockstep. | |
| _POLL_JITTER = 0.2 | |
| _TERMINAL_STATUSES = frozenset({"completed", "failed"}) | |
| T = typing.TypeVar("T") | |
| TerminalMediaGenerationResponse = typing.Union[MediaGenerationResponse_Completed, MediaGenerationResponse_Failed] | |
| """What `create_and_wait` returns for a generation: always `completed` or `failed`, never in progress.""" | |
| class FlowsWaitTimeoutError(Exception): | |
| """ | |
| Raised by `create_and_wait` when a generation or template run is still unfinished after `wait_timeout_in_seconds` seconds. | |
| The work keeps running on the server. Resume waiting by calling `get` with `id` | |
| (and `template_id` for a template run), or let a webhook deliver the result. | |
| """ | |
| def __init__( | |
| self, | |
| *, | |
| id: str, | |
| wait_timeout_in_seconds: float, | |
| last_response: typing.Any, | |
| template_id: typing.Optional[str] = None, | |
| ) -> None: | |
| self.id = id | |
| self.template_id = template_id | |
| self.wait_timeout_in_seconds = wait_timeout_in_seconds | |
| self.last_response = last_response | |
| status = getattr(last_response, "status", None) | |
| if template_id is not None: | |
| message = ( | |
| f"Template run {id} of template {template_id} is still {status} after {wait_timeout_in_seconds}s; " | |
| "call `runs.get` with `template_id` and `id` to keep waiting" | |
| ) | |
| else: | |
| message = f"Generation {id} is still {status} after {wait_timeout_in_seconds}s; call `get` with this id to keep waiting" | |
| super().__init__(message) | |
| def _check_wait_options(wait_timeout_in_seconds: typing.Optional[float], min_interval: float) -> None: | |
| # NaN passes every `<` check, and an infinite interval would never poll, so both are rejected explicitly. | |
| if not math.isfinite(min_interval) or min_interval < _SMALLEST_MIN_POLL_INTERVAL_SECONDS: | |
| raise ValueError( | |
| f"min_poll_interval_in_seconds must be a finite number of at least {_SMALLEST_MIN_POLL_INTERVAL_SECONDS}s, got {min_interval}" | |
| ) | |
| # Written as `not >= 0` so NaN fails too: a NaN deadline never compares as passed, so the wait would | |
| # silently ignore the timeout. 0 times out straight after the create; None or inf waits indefinitely. | |
| if wait_timeout_in_seconds is not None and not wait_timeout_in_seconds >= 0: | |
| raise ValueError(f"wait_timeout_in_seconds must be at least 0s, got {wait_timeout_in_seconds}") | |
| def _poll_request_options(request_options: typing.Optional[RequestOptions]) -> typing.Optional[RequestOptions]: | |
| """The caller's options for the status GETs: everything except body parameters, which only the create takes.""" | |
| if request_options is None or "additional_body_parameters" not in request_options: | |
| return request_options | |
| return typing.cast(RequestOptions, {k: v for k, v in request_options.items() if k != "additional_body_parameters"}) | |
| def _parse_retry_after(headers: typing.Mapping[str, str]) -> typing.Optional[float]: | |
| """ | |
| Seconds from a `Retry-After` header, given as whole seconds or an HTTP date (RFC 9110), or None if absent | |
| or unparseable. Kept here rather than imported from the generated `core.http_client`, whose private | |
| helpers can change on any regeneration. | |
| """ | |
| value = next((v for k, v in headers.items() if k.lower() == "retry-after"), None) | |
| if value is None: | |
| return None | |
| value = value.strip() | |
| if value.isdigit(): | |
| return float(value) | |
| try: | |
| retry_date = email.utils.parsedate_to_datetime(value) | |
| except (TypeError, ValueError): | |
| return None | |
| if retry_date is None: # Python < 3.10 returns None instead of raising | |
| return None | |
| return max(retry_date.timestamp() - time.time(), 0.0) | |
| def _poll_delay(headers: typing.Mapping[str, str], min_interval: float) -> float: | |
| """Seconds to wait before the next GET: the server's `Retry-After`, never below `min_interval`, plus 0-20% jitter.""" | |
| retry_after = _parse_retry_after(headers) | |
| delay = max(retry_after if retry_after is not None else min_interval, min_interval) | |
| return delay * (1 + random() * _POLL_JITTER) | |
| def _is_terminal(response: typing.Any) -> bool: | |
| return getattr(response, "status", None) in _TERMINAL_STATUSES | |
| def _wait( | |
| *, | |
| id: str, | |
| first_response: typing.Any, | |
| first_headers: typing.Mapping[str, str], | |
| get: typing.Callable[[], HttpResponse[T]], | |
| started_at: float, | |
| wait_timeout_in_seconds: typing.Optional[float], | |
| min_interval: float, | |
| template_id: typing.Optional[str] = None, | |
| ) -> T: | |
| response: typing.Any = first_response | |
| headers = first_headers | |
| while not _is_terminal(response): | |
| delay = _poll_delay(headers, min_interval) | |
| if wait_timeout_in_seconds is not None: | |
| # The last sleep ends on the deadline; the GET after it takes time, so the next pass raises. | |
| remaining = started_at + wait_timeout_in_seconds - time.monotonic() | |
| if remaining <= 0: | |
| raise FlowsWaitTimeoutError( | |
| id=id, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| last_response=response, | |
| template_id=template_id, | |
| ) | |
| delay = min(delay, remaining) | |
| time.sleep(delay) | |
| raw = get() | |
| response, headers = raw.data, raw.headers | |
| return typing.cast(T, response) | |
| async def _async_wait( | |
| *, | |
| id: str, | |
| first_response: typing.Any, | |
| first_headers: typing.Mapping[str, str], | |
| get: typing.Callable[[], typing.Awaitable[AsyncHttpResponse[T]]], | |
| started_at: float, | |
| wait_timeout_in_seconds: typing.Optional[float], | |
| min_interval: float, | |
| template_id: typing.Optional[str] = None, | |
| ) -> T: | |
| response: typing.Any = first_response | |
| headers = first_headers | |
| while not _is_terminal(response): | |
| delay = _poll_delay(headers, min_interval) | |
| if wait_timeout_in_seconds is not None: | |
| # The last sleep ends on the deadline; the GET after it takes time, so the next pass raises. | |
| remaining = started_at + wait_timeout_in_seconds - time.monotonic() | |
| if remaining <= 0: | |
| raise FlowsWaitTimeoutError( | |
| id=id, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| last_response=response, | |
| template_id=template_id, | |
| ) | |
| delay = min(delay, remaining) | |
| await asyncio.sleep(delay) | |
| raw = await get() | |
| response, headers = raw.data, raw.headers | |
| return typing.cast(T, response) | |
| class ImageClient(AutogeneratedImageClient): | |
| def create_and_wait( | |
| self, | |
| *, | |
| request: ImageGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start an image generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Parameters | |
| ---------- | |
| request : ImageGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| _wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class AsyncImageClient(AutogeneratedAsyncImageClient): | |
| async def create_and_wait( | |
| self, | |
| *, | |
| request: ImageGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start an image generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Parameters | |
| ---------- | |
| request : ImageGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = await self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| await _async_wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class VideoClient(AutogeneratedVideoClient): | |
| def create_and_wait( | |
| self, | |
| *, | |
| request: VideoGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start a video generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Parameters | |
| ---------- | |
| request : VideoGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| _wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class AsyncVideoClient(AutogeneratedAsyncVideoClient): | |
| async def create_and_wait( | |
| self, | |
| *, | |
| request: VideoGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start a video generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Parameters | |
| ---------- | |
| request : VideoGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = await self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| await _async_wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class TextToSpeechClient(AutogeneratedTextToSpeechClient): | |
| def create_and_wait( | |
| self, | |
| *, | |
| request: TextToSpeechGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start a text-to-speech generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Charged per character via text-to-speech billing. Use this over `/v1/text-to-speech` for the asynchronous generation lifecycle or for models not offered there; for direct, synchronous speech synthesis, prefer `/v1/text-to-speech`. | |
| Parameters | |
| ---------- | |
| request : TextToSpeechGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| _wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class AsyncTextToSpeechClient(AutogeneratedAsyncTextToSpeechClient): | |
| async def create_and_wait( | |
| self, | |
| *, | |
| request: TextToSpeechGenerationRequest, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TerminalMediaGenerationResponse: | |
| """ | |
| Start a text-to-speech generation and wait until it is `completed` or `failed`, then return the final generation. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, set `webhook` on the request passed to `create` | |
| and handle the webhook event instead of holding a process open. | |
| A `failed` generation is returned, not raised; check `status` on the result. | |
| Charged per character via text-to-speech billing. Use this over `/v1/text-to-speech` for the asynchronous generation lifecycle or for models not offered there; for direct, synchronous speech synthesis, prefer `/v1/text-to-speech`. | |
| Parameters | |
| ---------- | |
| request : TextToSpeechGenerationRequest | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the generation `id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TerminalMediaGenerationResponse | |
| The generation in its terminal state: `MediaGenerationResponse_Completed` or `MediaGenerationResponse_Failed`. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = await self.with_raw_response.create(request=request, request_options=request_options) | |
| generation_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return typing.cast( | |
| TerminalMediaGenerationResponse, | |
| await _async_wait( | |
| id=generation_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(generation_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ), | |
| ) | |
| class RunsClient(AutogeneratedRunsClient): | |
| def create_and_wait( | |
| self, | |
| template_id: str, | |
| *, | |
| inputs: typing.Dict[str, TemplateRunInput], | |
| version_id: typing.Optional[str] = OMIT, | |
| webhook: typing.Optional[WebhookTarget] = OMIT, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TemplateRunResponse: | |
| """ | |
| Start a run of a flows template and wait until its `status` is `completed` or `failed`, then return the final run. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, pass `webhook` to `create` and handle the | |
| `flows_template_run` webhook instead of holding a process open. | |
| A `failed` run is returned, not raised; check `status` on the result and on each output. | |
| Parameters | |
| ---------- | |
| template_id : str | |
| The ID of the template, as shown in the ElevenLabs app or by `GET /v1/flows/templates`. | |
| inputs : typing.Dict[str, TemplateRunInput] | |
| Input values keyed by input port id. Every input port of the version being run must be given; a missing or unknown id is rejected. Pass `{}` for a template with no inputs. | |
| version_id : typing.Optional[str] | |
| The template snapshot to run. Pass a specific version id to pin that snapshot, or `latest` (the default when omitted) to run the template's most recently published version. Only published versions can be pinned, except by the template's owner, who may also pin an unpublished saved snapshot to try it out before publishing. The live draft is never run through this API. | |
| webhook : typing.Optional[WebhookTarget] | |
| Include to send the run's result to the workspace's configured flows webhooks once the run's `status` reaches `completed` or `failed`. One event for the whole run: the `flows_template_run` event's `data` matches the terminal response of `GET /v1/flows/templates/{template_id}/runs/{run_id}`. | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the run `id` and `template_id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TemplateRunResponse | |
| The run in its terminal state. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = self.with_raw_response.create( | |
| template_id, inputs=inputs, version_id=version_id, webhook=webhook, request_options=request_options | |
| ) | |
| run_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return _wait( | |
| id=run_id, | |
| template_id=template_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(template_id, run_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ) | |
| class AsyncRunsClient(AutogeneratedAsyncRunsClient): | |
| async def create_and_wait( | |
| self, | |
| template_id: str, | |
| *, | |
| inputs: typing.Dict[str, TemplateRunInput], | |
| version_id: typing.Optional[str] = OMIT, | |
| webhook: typing.Optional[WebhookTarget] = OMIT, | |
| wait_timeout_in_seconds: typing.Optional[float] = None, | |
| min_poll_interval_in_seconds: float = DEFAULT_MIN_POLL_INTERVAL_SECONDS, | |
| request_options: typing.Optional[RequestOptions] = None, | |
| ) -> TemplateRunResponse: | |
| """ | |
| Start a run of a flows template and wait until its `status` is `completed` or `failed`, then return the final run. | |
| Between status checks this waits as long as the server suggests, never less than `min_poll_interval_in_seconds`. | |
| This is meant for scripts and quickstarts: in production, pass `webhook` to `create` and handle the | |
| `flows_template_run` webhook instead of holding a process open. | |
| A `failed` run is returned, not raised; check `status` on the result and on each output. | |
| Parameters | |
| ---------- | |
| template_id : str | |
| The ID of the template, as shown in the ElevenLabs app or by `GET /v1/flows/templates`. | |
| inputs : typing.Dict[str, TemplateRunInput] | |
| Input values keyed by input port id. Every input port of the version being run must be given; a missing or unknown id is rejected. Pass `{}` for a template with no inputs. | |
| version_id : typing.Optional[str] | |
| The template snapshot to run. Pass a specific version id to pin that snapshot, or `latest` (the default when omitted) to run the template's most recently published version. Only published versions can be pinned, except by the template's owner, who may also pin an unpublished saved snapshot to try it out before publishing. The live draft is never run through this API. | |
| webhook : typing.Optional[WebhookTarget] | |
| Include to send the run's result to the workspace's configured flows webhooks once the run's `status` reaches `completed` or `failed`. One event for the whole run: the `flows_template_run` event's `data` matches the terminal response of `GET /v1/flows/templates/{template_id}/runs/{run_id}`. | |
| wait_timeout_in_seconds : typing.Optional[float] | |
| Seconds to wait in total before raising `FlowsWaitTimeoutError`, which carries the run `id` and `template_id` | |
| so you can resume with `get`. Waits indefinitely when omitted or `float("inf")`; must be at least 0 | |
| when set. Not to be confused with the client's `timeout` or `timeout_in_seconds` in | |
| `request_options`, which limit each HTTP request. | |
| min_poll_interval_in_seconds : float | |
| The shortest wait between status checks, in seconds, and the wait used when the server suggests none. | |
| Must be at least 0.5. | |
| request_options : typing.Optional[RequestOptions] | |
| Request-specific configuration, applied to the create call and to every status check, except | |
| `additional_body_parameters`, which only the create call gets. `timeout_in_seconds` and | |
| `max_retries` apply to each request on its own; `wait_timeout_in_seconds` bounds the whole wait. | |
| Returns | |
| ------- | |
| TemplateRunResponse | |
| The run in its terminal state. | |
| """ | |
| _check_wait_options(wait_timeout_in_seconds, min_poll_interval_in_seconds) | |
| started_at = time.monotonic() | |
| created = await self.with_raw_response.create( | |
| template_id, inputs=inputs, version_id=version_id, webhook=webhook, request_options=request_options | |
| ) | |
| run_id = created.data.id | |
| poll_options = _poll_request_options(request_options) | |
| return await _async_wait( | |
| id=run_id, | |
| template_id=template_id, | |
| first_response=created.data, | |
| first_headers=created.headers, | |
| get=lambda: self.with_raw_response.get(template_id, run_id, request_options=poll_options), | |
| started_at=started_at, | |
| wait_timeout_in_seconds=wait_timeout_in_seconds, | |
| min_interval=min_poll_interval_in_seconds, | |
| ) | |
| class TemplatesClient(AutogeneratedTemplatesClient): | |
| def runs(self) -> RunsClient: | |
| if self._runs is None: | |
| self._runs = RunsClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(RunsClient, self._runs) | |
| class AsyncTemplatesClient(AutogeneratedAsyncTemplatesClient): | |
| def runs(self) -> AsyncRunsClient: | |
| if self._runs is None: | |
| self._runs = AsyncRunsClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(AsyncRunsClient, self._runs) | |
| class FlowsClient(AutogeneratedFlowsClient): | |
| """ | |
| Extends the autogenerated flows client with `create_and_wait` helpers on | |
| `image`, `video`, `text_to_speech` and `templates.runs`. | |
| """ | |
| def video(self) -> VideoClient: | |
| if self._video is None: | |
| self._video = VideoClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(VideoClient, self._video) | |
| def image(self) -> ImageClient: | |
| if self._image is None: | |
| self._image = ImageClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(ImageClient, self._image) | |
| def text_to_speech(self) -> TextToSpeechClient: | |
| if self._text_to_speech is None: | |
| self._text_to_speech = TextToSpeechClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(TextToSpeechClient, self._text_to_speech) | |
| def templates(self) -> TemplatesClient: | |
| if self._templates is None: | |
| self._templates = TemplatesClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(TemplatesClient, self._templates) | |
| class AsyncFlowsClient(AutogeneratedAsyncFlowsClient): | |
| """ | |
| Extends the autogenerated async flows client with `create_and_wait` helpers on | |
| `image`, `video`, `text_to_speech` and `templates.runs`. | |
| """ | |
| def video(self) -> AsyncVideoClient: | |
| if self._video is None: | |
| self._video = AsyncVideoClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(AsyncVideoClient, self._video) | |
| def image(self) -> AsyncImageClient: | |
| if self._image is None: | |
| self._image = AsyncImageClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(AsyncImageClient, self._image) | |
| def text_to_speech(self) -> AsyncTextToSpeechClient: | |
| if self._text_to_speech is None: | |
| self._text_to_speech = AsyncTextToSpeechClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(AsyncTextToSpeechClient, self._text_to_speech) | |
| def templates(self) -> AsyncTemplatesClient: | |
| if self._templates is None: | |
| self._templates = AsyncTemplatesClient(client_wrapper=self._client_wrapper) | |
| return typing.cast(AsyncTemplatesClient, self._templates) | |