bakbak-python 1.0.0rc2__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.
- bakbak/__init__.py +77 -0
- bakbak/_base_client.py +642 -0
- bakbak/_client.py +219 -0
- bakbak/_constants.py +43 -0
- bakbak/_exceptions.py +410 -0
- bakbak/_models.py +93 -0
- bakbak/_response.py +51 -0
- bakbak/_retry.py +52 -0
- bakbak/_sse.py +68 -0
- bakbak/_streaming.py +432 -0
- bakbak/_types.py +79 -0
- bakbak/_utils.py +62 -0
- bakbak/_version.py +3 -0
- bakbak/_ws/__init__.py +1 -0
- bakbak/_ws/_async.py +657 -0
- bakbak/_ws/_common.py +176 -0
- bakbak/_ws/_core.py +819 -0
- bakbak/_ws/_sync.py +649 -0
- bakbak/_ws/events.py +67 -0
- bakbak/audio.py +75 -0
- bakbak/py.typed +0 -0
- bakbak/resources/__init__.py +6 -0
- bakbak/resources/tts.py +339 -0
- bakbak/resources/voices.py +139 -0
- bakbak/text.py +463 -0
- bakbak/types.py +100 -0
- bakbak/websocket.py +23 -0
- bakbak_python-1.0.0rc2.dist-info/METADATA +416 -0
- bakbak_python-1.0.0rc2.dist-info/RECORD +31 -0
- bakbak_python-1.0.0rc2.dist-info/WHEEL +4 -0
- bakbak_python-1.0.0rc2.dist-info/licenses/LICENSE +202 -0
bakbak/_ws/events.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Events a WebSocket context yields (BEHAVIOR.md §6.2)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Generic, TypeAlias, TypeVar
|
|
7
|
+
|
|
8
|
+
from typing_extensions import override
|
|
9
|
+
|
|
10
|
+
from .._exceptions import BakbakError
|
|
11
|
+
|
|
12
|
+
__all__ = ["Cancelled", "Chunk", "Done", "Failed", "RequestResult", "WebSocketEvent"]
|
|
13
|
+
|
|
14
|
+
H = TypeVar("H")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(slots=True)
|
|
18
|
+
class Chunk(Generic[H]):
|
|
19
|
+
"""Audio of one request. It always holds whole samples."""
|
|
20
|
+
|
|
21
|
+
audio: bytes
|
|
22
|
+
request: H
|
|
23
|
+
|
|
24
|
+
@override
|
|
25
|
+
def __repr__(self) -> str:
|
|
26
|
+
return f"Chunk(audio=<{len(self.audio)} bytes>, request={self.request!r})"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(slots=True)
|
|
30
|
+
class Done(Generic[H]):
|
|
31
|
+
"""The request finished."""
|
|
32
|
+
|
|
33
|
+
request: H
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(slots=True)
|
|
37
|
+
class Failed(Generic[H]):
|
|
38
|
+
"""The request failed; ``error`` says why. Later requests still run."""
|
|
39
|
+
|
|
40
|
+
request: H
|
|
41
|
+
error: BakbakError
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(slots=True)
|
|
45
|
+
class Cancelled(Generic[H]):
|
|
46
|
+
"""The request was cancelled by ``ctx.cancel()``."""
|
|
47
|
+
|
|
48
|
+
request: H
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
WebSocketEvent: TypeAlias = Chunk[H] | Done[H] | Failed[H] | Cancelled[H]
|
|
52
|
+
"""What ``ctx.events()`` yields."""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(slots=True)
|
|
56
|
+
class RequestResult:
|
|
57
|
+
"""The outcome of a request that did not fail.
|
|
58
|
+
|
|
59
|
+
Attributes:
|
|
60
|
+
request_id: the server's id for the request, if it sent one.
|
|
61
|
+
audio_bytes: the audio delivered.
|
|
62
|
+
cancelled: whether ``ctx.cancel()`` ended it.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
request_id: str | None
|
|
66
|
+
audio_bytes: int
|
|
67
|
+
cancelled: bool = False
|
bakbak/audio.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Audio helpers: WAV wrapping and sample sizes (BEHAVIOR.md §5.2).
|
|
2
|
+
|
|
3
|
+
There are no playback helpers here on purpose; hand the bytes to the audio
|
|
4
|
+
library you already use.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import struct
|
|
10
|
+
|
|
11
|
+
from ._utils import as_mapping
|
|
12
|
+
|
|
13
|
+
__all__ = ["sample_size", "to_wav"]
|
|
14
|
+
|
|
15
|
+
_SAMPLE_SIZES: dict[str, int] = {"pcm_f32le": 4, "pcm_s16le": 2, "pcm_mulaw": 1, "pcm_alaw": 1}
|
|
16
|
+
# WAVE format tags: PCM, IEEE float, A-law, mu-law.
|
|
17
|
+
_FORMAT_TAGS: dict[str, int] = {"pcm_s16le": 1, "pcm_f32le": 3, "pcm_alaw": 6, "pcm_mulaw": 7}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def sample_size(encoding: str | None) -> int:
|
|
21
|
+
"""Bytes per sample of a mono ``encoding``; 1 for anything unknown."""
|
|
22
|
+
return _SAMPLE_SIZES.get(encoding or "", 1)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def stream_sample_size(output_format: object) -> int:
|
|
26
|
+
"""Bytes per sample a stream requested with ``output_format`` will carry.
|
|
27
|
+
|
|
28
|
+
Without an ``output_format`` the streams send 32-bit float PCM.
|
|
29
|
+
"""
|
|
30
|
+
fields = as_mapping(output_format)
|
|
31
|
+
if fields is None:
|
|
32
|
+
return _SAMPLE_SIZES["pcm_f32le"]
|
|
33
|
+
encoding = fields.get("encoding")
|
|
34
|
+
return sample_size(encoding) if isinstance(encoding, str) else 1
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def to_wav(data: bytes, encoding: str, sample_rate: int) -> bytes:
|
|
38
|
+
"""Wrap raw mono audio in a WAV header.
|
|
39
|
+
|
|
40
|
+
``pcm_s16le`` gets the canonical 44-byte PCM header. ``pcm_f32le``
|
|
41
|
+
(format 3), ``pcm_alaw`` (6) and ``pcm_mulaw`` (7) get an 18-byte ``fmt ``
|
|
42
|
+
chunk (``cbSize`` 0) plus a ``fact`` chunk: 58 bytes in all, as the WAV
|
|
43
|
+
specification requires for non-PCM formats.
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
ValueError: for an encoding WAV cannot carry this way (such as mp3), or
|
|
47
|
+
a sample rate that is not a positive integer.
|
|
48
|
+
"""
|
|
49
|
+
tag = _FORMAT_TAGS.get(encoding)
|
|
50
|
+
if tag is None:
|
|
51
|
+
known = ", ".join(sorted(_FORMAT_TAGS))
|
|
52
|
+
raise ValueError(f"Cannot wrap {encoding!r} audio in WAV; expected one of: {known}")
|
|
53
|
+
if isinstance(sample_rate, bool) or not isinstance(sample_rate, int) or sample_rate <= 0: # pyright: ignore[reportUnnecessaryIsInstance]
|
|
54
|
+
raise ValueError("sample_rate must be a positive integer")
|
|
55
|
+
width = _SAMPLE_SIZES[encoding]
|
|
56
|
+
size = len(data)
|
|
57
|
+
# RIFF chunks are word aligned: an odd-sized data chunk gets a pad byte that
|
|
58
|
+
# the RIFF size counts and the data size does not.
|
|
59
|
+
pad = b"\x00" if size % 2 else b""
|
|
60
|
+
if tag == 1:
|
|
61
|
+
header = struct.pack(
|
|
62
|
+
"<4sI4s4sIHHIIHH4sI",
|
|
63
|
+
b"RIFF", 36 + size + len(pad), b"WAVE",
|
|
64
|
+
b"fmt ", 16, tag, 1, sample_rate, sample_rate * width, width, width * 8,
|
|
65
|
+
b"data", size,
|
|
66
|
+
) # fmt: skip
|
|
67
|
+
else:
|
|
68
|
+
header = struct.pack(
|
|
69
|
+
"<4sI4s4sIHHIIHHH4sII4sI",
|
|
70
|
+
b"RIFF", 50 + size + len(pad), b"WAVE",
|
|
71
|
+
b"fmt ", 18, tag, 1, sample_rate, sample_rate * width, width, width * 8, 0,
|
|
72
|
+
b"fact", 4, size // width,
|
|
73
|
+
b"data", size,
|
|
74
|
+
) # fmt: skip
|
|
75
|
+
return b"".join((header, data, pad))
|
bakbak/py.typed
ADDED
|
File without changes
|
bakbak/resources/tts.py
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"""``client.tts``: whole files, SSE streams and the WebSocket."""
|
|
2
|
+
|
|
3
|
+
# The resources are the client's own internals.
|
|
4
|
+
# pyright: reportPrivateUsage=false
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable, Mapping
|
|
9
|
+
from functools import cached_property
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from typing_extensions import Unpack
|
|
13
|
+
|
|
14
|
+
from .._constants import STREAM_PATH, TTS_PATH
|
|
15
|
+
from .._exceptions import APIStatusError
|
|
16
|
+
from .._models import Audio
|
|
17
|
+
from .._response import RawResponse
|
|
18
|
+
from .._streaming import AsyncAudioStream, AudioStream
|
|
19
|
+
from .._types import NOT_GIVEN, NotGiven, TimeoutLike
|
|
20
|
+
from .._ws._async import AsyncTTSWebSocket
|
|
21
|
+
from .._ws._sync import TTSWebSocket
|
|
22
|
+
from ..audio import stream_sample_size
|
|
23
|
+
from ..types import RequestDefaults, SynthesisParams
|
|
24
|
+
|
|
25
|
+
if TYPE_CHECKING:
|
|
26
|
+
from .._client import AsyncBakbak, Bakbak
|
|
27
|
+
|
|
28
|
+
__all__ = ["TTS", "AsyncTTS"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TTS:
|
|
32
|
+
"""Text to speech."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, client: Bakbak) -> None:
|
|
35
|
+
self._client = client
|
|
36
|
+
|
|
37
|
+
def generate(
|
|
38
|
+
self,
|
|
39
|
+
*,
|
|
40
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
41
|
+
extra_body: Mapping[str, object] | None = None,
|
|
42
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
43
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
44
|
+
**params: Unpack[SynthesisParams],
|
|
45
|
+
) -> Audio:
|
|
46
|
+
"""Synthesize a whole file (``POST /v1/text-to-speech``).
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
text: 1 to 5,000 characters.
|
|
50
|
+
voice_id: a voice from ``client.voices.list()``.
|
|
51
|
+
language: e.g. ``"hi"``; ``m1`` voices use ``"en"`` for English.
|
|
52
|
+
model: ``"standard"`` (the default) or ``"m1"``; it must match the voice.
|
|
53
|
+
speed: 0.5 to 1.5.
|
|
54
|
+
output_format: e.g. ``{"container": "wav", "encoding": "pcm_s16le",
|
|
55
|
+
"sample_rate": 24000}``. Without it the result is 24 kHz WAV.
|
|
56
|
+
extra_headers: headers to add to this request.
|
|
57
|
+
extra_body: fields to add to the body, for anything the SDK does not
|
|
58
|
+
model yet.
|
|
59
|
+
timeout: overrides the client's timeout for this call.
|
|
60
|
+
max_retries: overrides the client's ``max_retries`` for this call.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
The audio, with its request id and format.
|
|
64
|
+
"""
|
|
65
|
+
return self.with_raw_response.generate(
|
|
66
|
+
extra_headers=extra_headers,
|
|
67
|
+
extra_body=extra_body,
|
|
68
|
+
timeout=timeout,
|
|
69
|
+
max_retries=max_retries,
|
|
70
|
+
**params,
|
|
71
|
+
).parse()
|
|
72
|
+
|
|
73
|
+
def stream(
|
|
74
|
+
self,
|
|
75
|
+
*,
|
|
76
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
77
|
+
extra_body: Mapping[str, object] | None = None,
|
|
78
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
79
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
80
|
+
**params: Unpack[SynthesisParams],
|
|
81
|
+
) -> AudioStream:
|
|
82
|
+
"""Stream one request's audio as it is generated (Server-Sent Events).
|
|
83
|
+
|
|
84
|
+
Takes the same arguments as :meth:`generate`. The ``output_format``
|
|
85
|
+
container must be ``raw``; without one, the stream is 24 kHz 32-bit
|
|
86
|
+
float PCM.
|
|
87
|
+
|
|
88
|
+
The request is sent when the stream is entered or first iterated::
|
|
89
|
+
|
|
90
|
+
with client.tts.stream(text="नमस्ते", voice_id=..., language="hi") as stream:
|
|
91
|
+
for chunk in stream:
|
|
92
|
+
play(chunk.audio)
|
|
93
|
+
"""
|
|
94
|
+
client = self._client
|
|
95
|
+
options = client._options(timeout, max_retries, extra_headers)
|
|
96
|
+
body = client._body(params, extra_body)
|
|
97
|
+
output_format = params.get("output_format")
|
|
98
|
+
return AudioStream(
|
|
99
|
+
lambda: client._request(
|
|
100
|
+
"POST",
|
|
101
|
+
STREAM_PATH,
|
|
102
|
+
body=body,
|
|
103
|
+
accept="text/event-stream",
|
|
104
|
+
options=options,
|
|
105
|
+
handle=client._start_stream,
|
|
106
|
+
)[0],
|
|
107
|
+
output_format=output_format,
|
|
108
|
+
sample_width=stream_sample_size(output_format),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
def websocket(
|
|
112
|
+
self,
|
|
113
|
+
*,
|
|
114
|
+
on_error: Callable[[APIStatusError], None] | None = None,
|
|
115
|
+
**defaults: Unpack[RequestDefaults],
|
|
116
|
+
) -> TTSWebSocket:
|
|
117
|
+
"""A WebSocket that carries many streaming requests on one connection.
|
|
118
|
+
|
|
119
|
+
Nothing is opened until ``connect()``, ``with``, or the first send.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
on_error: called (from the reader thread) with errors the server
|
|
123
|
+
reports about no request in particular. They are logged at
|
|
124
|
+
debug level when it is not set.
|
|
125
|
+
**defaults: request fields every context fills in, e.g. ``voice_id``,
|
|
126
|
+
``language``, ``model`` and ``output_format``.
|
|
127
|
+
"""
|
|
128
|
+
return TTSWebSocket(self._client, defaults, on_error)
|
|
129
|
+
|
|
130
|
+
@cached_property
|
|
131
|
+
def with_raw_response(self) -> TTSWithRawResponse:
|
|
132
|
+
"""The same methods, returning a ``RawResponse`` with the HTTP response."""
|
|
133
|
+
return TTSWithRawResponse(self._client)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class TTSWithRawResponse:
|
|
137
|
+
def __init__(self, client: Bakbak) -> None:
|
|
138
|
+
self._client = client
|
|
139
|
+
|
|
140
|
+
def generate(
|
|
141
|
+
self,
|
|
142
|
+
*,
|
|
143
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
144
|
+
extra_body: Mapping[str, object] | None = None,
|
|
145
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
146
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
147
|
+
**params: Unpack[SynthesisParams],
|
|
148
|
+
) -> RawResponse[Audio]:
|
|
149
|
+
client = self._client
|
|
150
|
+
audio, response, retries = client._request(
|
|
151
|
+
"POST",
|
|
152
|
+
TTS_PATH,
|
|
153
|
+
body=client._body(params, extra_body),
|
|
154
|
+
accept="*/*",
|
|
155
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
156
|
+
handle=client._read_audio,
|
|
157
|
+
)
|
|
158
|
+
return RawResponse(response, audio, retries)
|
|
159
|
+
|
|
160
|
+
def stream(
|
|
161
|
+
self,
|
|
162
|
+
*,
|
|
163
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
164
|
+
extra_body: Mapping[str, object] | None = None,
|
|
165
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
166
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
167
|
+
**params: Unpack[SynthesisParams],
|
|
168
|
+
) -> RawResponse[AudioStream]:
|
|
169
|
+
"""Send the request now and return once its first audio has arrived."""
|
|
170
|
+
client = self._client
|
|
171
|
+
opened, response, retries = client._request(
|
|
172
|
+
"POST",
|
|
173
|
+
STREAM_PATH,
|
|
174
|
+
body=client._body(params, extra_body),
|
|
175
|
+
accept="text/event-stream",
|
|
176
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
177
|
+
handle=client._start_stream,
|
|
178
|
+
)
|
|
179
|
+
output_format = params.get("output_format")
|
|
180
|
+
stream = AudioStream(
|
|
181
|
+
lambda: opened,
|
|
182
|
+
output_format=output_format,
|
|
183
|
+
sample_width=stream_sample_size(output_format),
|
|
184
|
+
)
|
|
185
|
+
stream.__enter__()
|
|
186
|
+
return RawResponse(response, stream, retries)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class AsyncTTS:
|
|
190
|
+
"""Text to speech."""
|
|
191
|
+
|
|
192
|
+
def __init__(self, client: AsyncBakbak) -> None:
|
|
193
|
+
self._client = client
|
|
194
|
+
|
|
195
|
+
async def generate(
|
|
196
|
+
self,
|
|
197
|
+
*,
|
|
198
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
199
|
+
extra_body: Mapping[str, object] | None = None,
|
|
200
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
201
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
202
|
+
**params: Unpack[SynthesisParams],
|
|
203
|
+
) -> Audio:
|
|
204
|
+
"""Synthesize a whole file (``POST /v1/text-to-speech``).
|
|
205
|
+
|
|
206
|
+
See :meth:`bakbak.resources.tts.TTS.generate` for the arguments.
|
|
207
|
+
"""
|
|
208
|
+
raw = await self.with_raw_response.generate(
|
|
209
|
+
extra_headers=extra_headers,
|
|
210
|
+
extra_body=extra_body,
|
|
211
|
+
timeout=timeout,
|
|
212
|
+
max_retries=max_retries,
|
|
213
|
+
**params,
|
|
214
|
+
)
|
|
215
|
+
return raw.parse()
|
|
216
|
+
|
|
217
|
+
def stream(
|
|
218
|
+
self,
|
|
219
|
+
*,
|
|
220
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
221
|
+
extra_body: Mapping[str, object] | None = None,
|
|
222
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
223
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
224
|
+
**params: Unpack[SynthesisParams],
|
|
225
|
+
) -> AsyncAudioStream:
|
|
226
|
+
"""Stream one request's audio as it is generated (Server-Sent Events).
|
|
227
|
+
|
|
228
|
+
The request is sent when the stream is entered or first iterated::
|
|
229
|
+
|
|
230
|
+
async with client.tts.stream(text="नमस्ते", voice_id=..., language="hi") as stream:
|
|
231
|
+
async for chunk in stream:
|
|
232
|
+
play(chunk.audio)
|
|
233
|
+
"""
|
|
234
|
+
client = self._client
|
|
235
|
+
options = client._options(timeout, max_retries, extra_headers)
|
|
236
|
+
body = client._body(params, extra_body)
|
|
237
|
+
output_format = params.get("output_format")
|
|
238
|
+
|
|
239
|
+
async def start() -> _AsyncOpen:
|
|
240
|
+
opened, _, _ = await client._request(
|
|
241
|
+
"POST",
|
|
242
|
+
STREAM_PATH,
|
|
243
|
+
body=body,
|
|
244
|
+
accept="text/event-stream",
|
|
245
|
+
options=options,
|
|
246
|
+
handle=client._start_stream,
|
|
247
|
+
)
|
|
248
|
+
return opened
|
|
249
|
+
|
|
250
|
+
return AsyncAudioStream(
|
|
251
|
+
start, output_format=output_format, sample_width=stream_sample_size(output_format)
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
def websocket(
|
|
255
|
+
self,
|
|
256
|
+
*,
|
|
257
|
+
on_error: Callable[[APIStatusError], None] | None = None,
|
|
258
|
+
**defaults: Unpack[RequestDefaults],
|
|
259
|
+
) -> AsyncTTSWebSocket:
|
|
260
|
+
"""A WebSocket that carries many streaming requests on one connection.
|
|
261
|
+
|
|
262
|
+
Nothing is opened until ``await ws.connect()`` (or ``await`` on the
|
|
263
|
+
result, or ``async with``), or the first send.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
on_error: called with errors the server reports about no request
|
|
267
|
+
in particular. They are logged at debug level when it is not set.
|
|
268
|
+
**defaults: request fields every context fills in, e.g. ``voice_id``,
|
|
269
|
+
``language``, ``model`` and ``output_format``.
|
|
270
|
+
"""
|
|
271
|
+
return AsyncTTSWebSocket(self._client, defaults, on_error)
|
|
272
|
+
|
|
273
|
+
@cached_property
|
|
274
|
+
def with_raw_response(self) -> AsyncTTSWithRawResponse:
|
|
275
|
+
"""The same methods, returning a ``RawResponse`` with the HTTP response."""
|
|
276
|
+
return AsyncTTSWithRawResponse(self._client)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class AsyncTTSWithRawResponse:
|
|
280
|
+
def __init__(self, client: AsyncBakbak) -> None:
|
|
281
|
+
self._client = client
|
|
282
|
+
|
|
283
|
+
async def generate(
|
|
284
|
+
self,
|
|
285
|
+
*,
|
|
286
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
287
|
+
extra_body: Mapping[str, object] | None = None,
|
|
288
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
289
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
290
|
+
**params: Unpack[SynthesisParams],
|
|
291
|
+
) -> RawResponse[Audio]:
|
|
292
|
+
client = self._client
|
|
293
|
+
audio, response, retries = await client._request(
|
|
294
|
+
"POST",
|
|
295
|
+
TTS_PATH,
|
|
296
|
+
body=client._body(params, extra_body),
|
|
297
|
+
accept="*/*",
|
|
298
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
299
|
+
handle=client._read_audio,
|
|
300
|
+
)
|
|
301
|
+
return RawResponse(response, audio, retries)
|
|
302
|
+
|
|
303
|
+
async def stream(
|
|
304
|
+
self,
|
|
305
|
+
*,
|
|
306
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
307
|
+
extra_body: Mapping[str, object] | None = None,
|
|
308
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
309
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
310
|
+
**params: Unpack[SynthesisParams],
|
|
311
|
+
) -> RawResponse[AsyncAudioStream]:
|
|
312
|
+
"""Send the request now and return once its first audio has arrived."""
|
|
313
|
+
client = self._client
|
|
314
|
+
opened, response, retries = await client._request(
|
|
315
|
+
"POST",
|
|
316
|
+
STREAM_PATH,
|
|
317
|
+
body=client._body(params, extra_body),
|
|
318
|
+
accept="text/event-stream",
|
|
319
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
320
|
+
handle=client._start_stream,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
async def start() -> _AsyncOpen:
|
|
324
|
+
return opened
|
|
325
|
+
|
|
326
|
+
output_format = params.get("output_format")
|
|
327
|
+
stream = AsyncAudioStream(
|
|
328
|
+
start, output_format=output_format, sample_width=stream_sample_size(output_format)
|
|
329
|
+
)
|
|
330
|
+
await stream.__aenter__()
|
|
331
|
+
return RawResponse(response, stream, retries)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
if TYPE_CHECKING:
|
|
335
|
+
from collections.abc import AsyncIterator
|
|
336
|
+
|
|
337
|
+
from .._streaming import OpenStream
|
|
338
|
+
|
|
339
|
+
_AsyncOpen = OpenStream[AsyncIterator[bytes]]
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""``client.voices``."""
|
|
2
|
+
|
|
3
|
+
# The resources are the client's own internals.
|
|
4
|
+
# pyright: reportPrivateUsage=false
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import builtins
|
|
9
|
+
from collections.abc import Mapping
|
|
10
|
+
from functools import cached_property
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from .._base_client import filter_voices
|
|
14
|
+
from .._constants import VOICES_PATH
|
|
15
|
+
from .._response import RawResponse
|
|
16
|
+
from .._types import NOT_GIVEN, NotGiven, TimeoutLike
|
|
17
|
+
from ..types import Language, Model, Voice
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from .._client import AsyncBakbak, Bakbak
|
|
21
|
+
|
|
22
|
+
__all__ = ["AsyncVoices", "Voices"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Voices:
|
|
26
|
+
"""The voice catalog."""
|
|
27
|
+
|
|
28
|
+
def __init__(self, client: Bakbak) -> None:
|
|
29
|
+
self._client = client
|
|
30
|
+
|
|
31
|
+
def list(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
model: Model | None = None,
|
|
35
|
+
language: Language | None = None,
|
|
36
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
37
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
38
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
39
|
+
) -> builtins.list[Voice]:
|
|
40
|
+
"""List the voices, optionally only those of one model and language.
|
|
41
|
+
|
|
42
|
+
The filters are applied by the SDK; the API returns every voice.
|
|
43
|
+
"""
|
|
44
|
+
return self.with_raw_response.list(
|
|
45
|
+
model=model,
|
|
46
|
+
language=language,
|
|
47
|
+
extra_headers=extra_headers,
|
|
48
|
+
timeout=timeout,
|
|
49
|
+
max_retries=max_retries,
|
|
50
|
+
).parse()
|
|
51
|
+
|
|
52
|
+
@cached_property
|
|
53
|
+
def with_raw_response(self) -> VoicesWithRawResponse:
|
|
54
|
+
"""The same methods, returning a ``RawResponse`` with the HTTP response."""
|
|
55
|
+
return VoicesWithRawResponse(self._client)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class VoicesWithRawResponse:
|
|
59
|
+
def __init__(self, client: Bakbak) -> None:
|
|
60
|
+
self._client = client
|
|
61
|
+
|
|
62
|
+
def list(
|
|
63
|
+
self,
|
|
64
|
+
*,
|
|
65
|
+
model: Model | None = None,
|
|
66
|
+
language: Language | None = None,
|
|
67
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
68
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
69
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
70
|
+
) -> RawResponse[builtins.list[Voice]]:
|
|
71
|
+
client = self._client
|
|
72
|
+
voices, response, retries = client._request(
|
|
73
|
+
"GET",
|
|
74
|
+
VOICES_PATH,
|
|
75
|
+
body=None,
|
|
76
|
+
accept="application/json",
|
|
77
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
78
|
+
handle=client._read_voices,
|
|
79
|
+
)
|
|
80
|
+
return RawResponse(response, filter_voices(voices, model, language), retries)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class AsyncVoices:
|
|
84
|
+
"""The voice catalog."""
|
|
85
|
+
|
|
86
|
+
def __init__(self, client: AsyncBakbak) -> None:
|
|
87
|
+
self._client = client
|
|
88
|
+
|
|
89
|
+
async def list(
|
|
90
|
+
self,
|
|
91
|
+
*,
|
|
92
|
+
model: Model | None = None,
|
|
93
|
+
language: Language | None = None,
|
|
94
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
95
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
96
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
97
|
+
) -> builtins.list[Voice]:
|
|
98
|
+
"""List the voices, optionally only those of one model and language.
|
|
99
|
+
|
|
100
|
+
The filters are applied by the SDK; the API returns every voice.
|
|
101
|
+
"""
|
|
102
|
+
raw = await self.with_raw_response.list(
|
|
103
|
+
model=model,
|
|
104
|
+
language=language,
|
|
105
|
+
extra_headers=extra_headers,
|
|
106
|
+
timeout=timeout,
|
|
107
|
+
max_retries=max_retries,
|
|
108
|
+
)
|
|
109
|
+
return raw.parse()
|
|
110
|
+
|
|
111
|
+
@cached_property
|
|
112
|
+
def with_raw_response(self) -> AsyncVoicesWithRawResponse:
|
|
113
|
+
"""The same methods, returning a ``RawResponse`` with the HTTP response."""
|
|
114
|
+
return AsyncVoicesWithRawResponse(self._client)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class AsyncVoicesWithRawResponse:
|
|
118
|
+
def __init__(self, client: AsyncBakbak) -> None:
|
|
119
|
+
self._client = client
|
|
120
|
+
|
|
121
|
+
async def list(
|
|
122
|
+
self,
|
|
123
|
+
*,
|
|
124
|
+
model: Model | None = None,
|
|
125
|
+
language: Language | None = None,
|
|
126
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
127
|
+
timeout: TimeoutLike | NotGiven = NOT_GIVEN,
|
|
128
|
+
max_retries: int | NotGiven = NOT_GIVEN,
|
|
129
|
+
) -> RawResponse[builtins.list[Voice]]:
|
|
130
|
+
client = self._client
|
|
131
|
+
voices, response, retries = await client._request(
|
|
132
|
+
"GET",
|
|
133
|
+
VOICES_PATH,
|
|
134
|
+
body=None,
|
|
135
|
+
accept="application/json",
|
|
136
|
+
options=client._options(timeout, max_retries, extra_headers),
|
|
137
|
+
handle=client._read_voices,
|
|
138
|
+
)
|
|
139
|
+
return RawResponse(response, filter_voices(voices, model, language), retries)
|