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/text.py
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
"""Sentence segmentation for streaming text into speech (BEHAVIOR.md §7).
|
|
2
|
+
|
|
3
|
+
The models voice whole sentences best, and an LLM sends its reply a few
|
|
4
|
+
characters at a time. :class:`Segmenter` turns that stream into sentence-sized
|
|
5
|
+
requests as early as it safely can; :func:`split_sentences` does the same for a
|
|
6
|
+
whole text. Both follow ``conformance/segmenter/reference.py`` exactly, so the
|
|
7
|
+
Python, JavaScript and LiveKit clients split every text the same way.
|
|
8
|
+
|
|
9
|
+
The guiding rule is "when in doubt, don't split": a missed boundary only makes
|
|
10
|
+
one request a little longer, while a false split puts a falling intonation in
|
|
11
|
+
the middle of a sentence. ``3.14``, ``getraya.app``, ``Dr. Rao`` and
|
|
12
|
+
``है।राहुल`` never split.
|
|
13
|
+
|
|
14
|
+
Lengths are counted in Unicode code points.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import re
|
|
20
|
+
import unicodedata
|
|
21
|
+
from collections.abc import Iterator
|
|
22
|
+
from typing import NamedTuple
|
|
23
|
+
|
|
24
|
+
__all__ = ["MAX_CHARS_LIMIT", "Segmenter", "split_sentences"]
|
|
25
|
+
|
|
26
|
+
MAX_CHARS_LIMIT = 5000
|
|
27
|
+
"""The API's text limit; ``max_chars`` can never exceed it."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _cps(*codepoints: int) -> frozenset[str]:
|
|
31
|
+
return frozenset(chr(c) for c in codepoints)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# Every character class is listed by code point, as in the reference.
|
|
35
|
+
_WHITESPACE = _cps(
|
|
36
|
+
0x20, 0x09, 0x0A, 0x0D, 0x0C, 0x0B, 0x85, 0xA0, 0x1680,
|
|
37
|
+
0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008,
|
|
38
|
+
0x2009, 0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000,
|
|
39
|
+
) # fmt: skip
|
|
40
|
+
# . ! ? … । ॥ 。 ! ? # noqa: RUF003
|
|
41
|
+
_TERMINATORS = _cps(0x2E, 0x21, 0x3F, 0x2026, 0x0964, 0x0965, 0x3002, 0xFF01, 0xFF1F)
|
|
42
|
+
# " ' ) ] } ’ ” » › 》 」 』 # noqa: RUF003
|
|
43
|
+
_CLOSERS = _cps(0x22, 0x27, 0x29, 0x5D, 0x7D, 0x2019, 0x201D, 0xBB, 0x203A, 0x300B, 0x300D, 0x300F)
|
|
44
|
+
# , ; : — – ، (Arabic comma, used in Urdu) # noqa: RUF003
|
|
45
|
+
_SOFT_DELIMITERS = _cps(0x2C, 0x3B, 0x3A, 0x2014, 0x2013, 0x060C)
|
|
46
|
+
# ZWNJ, ZWJ
|
|
47
|
+
_JOINERS = _cps(0x200C, 0x200D)
|
|
48
|
+
# The viramas of Devanagari, Bengali, Gurmukhi, Gujarati, Odia, Tamil, Telugu,
|
|
49
|
+
# Kannada, Malayalam and Sinhala.
|
|
50
|
+
_VIRAMAS = _cps(0x094D, 0x09CD, 0x0A4D, 0x0ACD, 0x0B4D, 0x0BCD, 0x0C4D, 0x0CCD, 0x0D4D, 0x0DCA)
|
|
51
|
+
|
|
52
|
+
_TITLES = frozenset(
|
|
53
|
+
{
|
|
54
|
+
"mr", "mrs", "ms", "dr", "prof", "sr", "jr", "st", "vs", "rs", "smt",
|
|
55
|
+
"shri", "sh", "lt", "col", "gen", "capt", "sgt", "hon", "rev", "mt",
|
|
56
|
+
# Devanagari: Dr. (डॉ), Prof. (प्रो), Kumari (कु), late (स्व), Mr. (मि), Rs. (रु)
|
|
57
|
+
"".join(map(chr, (0x0921, 0x0949))),
|
|
58
|
+
"".join(map(chr, (0x092A, 0x094D, 0x0930, 0x094B))),
|
|
59
|
+
"".join(map(chr, (0x0915, 0x0941))),
|
|
60
|
+
"".join(map(chr, (0x0938, 0x094D, 0x0935))),
|
|
61
|
+
"".join(map(chr, (0x092E, 0x093F))),
|
|
62
|
+
"".join(map(chr, (0x0930, 0x0941))),
|
|
63
|
+
}
|
|
64
|
+
) # fmt: skip
|
|
65
|
+
_AMBIGUOUS = frozenset(
|
|
66
|
+
{
|
|
67
|
+
"etc", "inc", "ltd", "co", "corp", "approx", "dept", "govt", "est",
|
|
68
|
+
"fig", "no", "jan", "feb", "mar", "apr", "jun", "jul", "aug", "sep",
|
|
69
|
+
"sept", "oct", "nov", "dec",
|
|
70
|
+
}
|
|
71
|
+
) # fmt: skip
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
_SPECIAL = re.compile("[\n" + "".join(re.escape(c) for c in sorted(_TERMINATORS)) + "]")
|
|
75
|
+
_SPECIAL_CLAUSE = re.compile(
|
|
76
|
+
"[\n" + "".join(re.escape(c) for c in sorted(_TERMINATORS | _SOFT_DELIMITERS)) + "]"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class _NextSpecial:
|
|
81
|
+
"""Finds the next character that can start a candidate (a newline, a
|
|
82
|
+
terminator, or with ``clause`` a soft delimiter), remembering its last
|
|
83
|
+
answer: the wrap path asks again from a later start many times, and without
|
|
84
|
+
the memo a long text with no punctuation would be scanned once per piece."""
|
|
85
|
+
|
|
86
|
+
__slots__ = ("memo", "text")
|
|
87
|
+
|
|
88
|
+
def __init__(self, text: str) -> None:
|
|
89
|
+
self.text = text
|
|
90
|
+
self.memo: dict[bool, tuple[int, int]] = {}
|
|
91
|
+
|
|
92
|
+
def __call__(self, i: int, clause: bool) -> int:
|
|
93
|
+
lo, at = self.memo.get(clause, (-1, -1))
|
|
94
|
+
if lo <= i <= at:
|
|
95
|
+
return at
|
|
96
|
+
match = (_SPECIAL_CLAUSE if clause else _SPECIAL).search(self.text, i)
|
|
97
|
+
at = match.start() if match else len(self.text)
|
|
98
|
+
self.memo[clause] = (i, at)
|
|
99
|
+
return at
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _speakable(s: str) -> bool:
|
|
103
|
+
"""True if ``s`` has a letter or a number (Unicode categories L* and N*)."""
|
|
104
|
+
return any(unicodedata.category(ch)[0] in "LN" for ch in s)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _is_mark(ch: str) -> bool:
|
|
108
|
+
return unicodedata.category(ch)[0] == "M"
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _is_word_char(ch: str) -> bool:
|
|
112
|
+
if ("a" <= ch <= "z") or ("A" <= ch <= "Z") or ("0" <= ch <= "9"):
|
|
113
|
+
return True
|
|
114
|
+
cp = ord(ch)
|
|
115
|
+
return 0x0900 <= cp <= 0x0DFF and cp not in (0x0964, 0x0965)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _word_before(text: str, dot: int) -> tuple[str, int]:
|
|
119
|
+
"""The word ending right before index ``dot``, and where it starts."""
|
|
120
|
+
i = dot
|
|
121
|
+
while i > 0 and _is_word_char(text[i - 1]):
|
|
122
|
+
i -= 1
|
|
123
|
+
return text[i:dot], i
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _at_line_start(text: str, start: int) -> bool:
|
|
127
|
+
i = start
|
|
128
|
+
while i > 0 and text[i - 1] in (" ", "\t"):
|
|
129
|
+
i -= 1
|
|
130
|
+
return i == 0 or text[i - 1] == "\n"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _is_ascii_digits(word: str) -> bool:
|
|
134
|
+
return 1 <= len(word) <= 3 and all("0" <= c <= "9" for c in word)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _single_latin_letter(word: str) -> bool:
|
|
138
|
+
return len(word) == 1 and (("a" <= word <= "z") or ("A" <= word <= "Z"))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _strip(s: str) -> str:
|
|
142
|
+
a, b = 0, len(s)
|
|
143
|
+
while a < b and s[a] in _WHITESPACE:
|
|
144
|
+
a += 1
|
|
145
|
+
while b > a and s[b - 1] in _WHITESPACE:
|
|
146
|
+
b -= 1
|
|
147
|
+
return s[a:b]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _skip_ws(text: str, i: int) -> int:
|
|
151
|
+
n = len(text)
|
|
152
|
+
while i < n and text[i] in _WHITESPACE:
|
|
153
|
+
i += 1
|
|
154
|
+
return i
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class _Candidate(NamedTuple):
|
|
158
|
+
end: int # where the sentence ends (exclusive)
|
|
159
|
+
decided: bool
|
|
160
|
+
boundary: bool # meaningful only when decided
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _candidates(
|
|
164
|
+
text: str, start: int, final: bool, clause: tuple[int, int] | None, nxt: _NextSpecial
|
|
165
|
+
) -> Iterator[_Candidate]:
|
|
166
|
+
"""Boundary candidates in ``text[start:]``, in order, produced lazily:
|
|
167
|
+
callers stop at the first one that ends a group, which keeps segmentation
|
|
168
|
+
linear in the length of the text.
|
|
169
|
+
|
|
170
|
+
``clause``, when given, is ``(group_start, first_clause_min_chars)``: soft
|
|
171
|
+
delimiters followed by whitespace also count, if the stripped text from the
|
|
172
|
+
group start through the delimiter is long enough.
|
|
173
|
+
"""
|
|
174
|
+
n = len(text)
|
|
175
|
+
i = start
|
|
176
|
+
while i < n:
|
|
177
|
+
i = nxt(i, clause is not None) # skip ordinary characters
|
|
178
|
+
if i >= n:
|
|
179
|
+
break
|
|
180
|
+
ch = text[i]
|
|
181
|
+
if ch == "\n":
|
|
182
|
+
j = i
|
|
183
|
+
while j < n and text[j] in _WHITESPACE:
|
|
184
|
+
j += 1
|
|
185
|
+
yield _Candidate(i, final or j < n, True)
|
|
186
|
+
i = j
|
|
187
|
+
continue
|
|
188
|
+
is_clause = (
|
|
189
|
+
clause is not None
|
|
190
|
+
and ch in _SOFT_DELIMITERS
|
|
191
|
+
and len(_strip(text[clause[0] : i + 1])) >= clause[1]
|
|
192
|
+
)
|
|
193
|
+
if ch not in _TERMINATORS and not is_clause:
|
|
194
|
+
i += 1
|
|
195
|
+
continue
|
|
196
|
+
run_start = i
|
|
197
|
+
if is_clause:
|
|
198
|
+
j = i + 1
|
|
199
|
+
else:
|
|
200
|
+
j = i
|
|
201
|
+
while j < n and text[j] in _TERMINATORS:
|
|
202
|
+
j += 1
|
|
203
|
+
run_end = j
|
|
204
|
+
while j < n and text[j] in _CLOSERS:
|
|
205
|
+
j += 1
|
|
206
|
+
end = j # the sentence ends after the closers
|
|
207
|
+
if j >= n:
|
|
208
|
+
# Nothing after the run yet: undecided unless final (and the end of
|
|
209
|
+
# the text is a boundary anyway).
|
|
210
|
+
if not final:
|
|
211
|
+
yield _Candidate(end, False, False)
|
|
212
|
+
break
|
|
213
|
+
if text[j] not in _WHITESPACE:
|
|
214
|
+
i = j # "3.14", "a.com", "है।राहुल": not a candidate
|
|
215
|
+
continue
|
|
216
|
+
k = j
|
|
217
|
+
while k < n and text[k] in _WHITESPACE:
|
|
218
|
+
k += 1
|
|
219
|
+
if k >= n and not final:
|
|
220
|
+
yield _Candidate(end, False, False)
|
|
221
|
+
break
|
|
222
|
+
next_char = text[k] if k < n else ""
|
|
223
|
+
boundary = True
|
|
224
|
+
if not is_clause and run_end - run_start == 1 and text[run_start] == ".":
|
|
225
|
+
word, word_start = _word_before(text, run_start)
|
|
226
|
+
lowered = word.lower()
|
|
227
|
+
if (
|
|
228
|
+
_single_latin_letter(word)
|
|
229
|
+
or lowered in _TITLES
|
|
230
|
+
or (_is_ascii_digits(word) and _at_line_start(text, word_start))
|
|
231
|
+
or (lowered in _AMBIGUOUS and not ("A" <= next_char <= "Z"))
|
|
232
|
+
):
|
|
233
|
+
boundary = False
|
|
234
|
+
yield _Candidate(end, True, boundary)
|
|
235
|
+
i = k
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _wrap_cut(text: str, start: int, max_chars: int) -> int:
|
|
239
|
+
"""Where to cut the window ``text[start:start + max_chars]``; > ``start``."""
|
|
240
|
+
window_end = start + max_chars
|
|
241
|
+
half = start + max_chars // 2
|
|
242
|
+
for j in range(window_end, half, -1):
|
|
243
|
+
if text[j - 1] in _SOFT_DELIMITERS:
|
|
244
|
+
return j
|
|
245
|
+
for j in range(window_end, start, -1):
|
|
246
|
+
# Cut before the whitespace, and never produce an empty piece.
|
|
247
|
+
if text[j - 1] in _WHITESPACE and _strip(text[start : j - 1]):
|
|
248
|
+
return j - 1
|
|
249
|
+
# Hard cut. Never before a combining mark or a joiner, or right after a
|
|
250
|
+
# virama or a joiner, which would split an Indic conjunct or a cluster.
|
|
251
|
+
cut = window_end
|
|
252
|
+
while cut > start + 1 and (
|
|
253
|
+
_is_mark(text[cut])
|
|
254
|
+
or text[cut] in _JOINERS
|
|
255
|
+
or text[cut - 1] in _VIRAMAS
|
|
256
|
+
or text[cut - 1] in _JOINERS
|
|
257
|
+
):
|
|
258
|
+
cut -= 1
|
|
259
|
+
return cut if cut > start + 1 else window_end
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
# The longest word any rule compares (titles, ambiguous abbreviations,
|
|
263
|
+
# list-marker digits) has 6 code points, so 8 code points of a word are enough
|
|
264
|
+
# to know it is none of them.
|
|
265
|
+
_LOOKBEHIND_WORD = 8
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _lookbehind(text: str, gs: int) -> str:
|
|
269
|
+
"""The text a streaming segmenter keeps before the group start ``gs``.
|
|
270
|
+
|
|
271
|
+
The boundary rules look behind a full stop: at the word before it, and at
|
|
272
|
+
whether that word starts a line. They need only the last few characters of
|
|
273
|
+
that word and, when the word is short, what precedes it: a run of spaces
|
|
274
|
+
or tabs (one stands for any number) and the character before the run, or
|
|
275
|
+
nothing at the start of the text. Keeping exactly that bounds the buffer
|
|
276
|
+
however long the text is, and gives every decision the answer it would
|
|
277
|
+
have on the whole text. The kept text is never emitted, so it need not be
|
|
278
|
+
a slice of the input.
|
|
279
|
+
"""
|
|
280
|
+
w = gs
|
|
281
|
+
while w > 0 and _is_word_char(text[w - 1]):
|
|
282
|
+
w -= 1
|
|
283
|
+
if gs - w >= _LOOKBEHIND_WORD:
|
|
284
|
+
return text[gs - _LOOKBEHIND_WORD : gs]
|
|
285
|
+
i = w
|
|
286
|
+
while i > 0 and text[i - 1] in (" ", "\t"):
|
|
287
|
+
i -= 1
|
|
288
|
+
gap = " " if i < w else ""
|
|
289
|
+
before = text[i - 1] if i > 0 else ""
|
|
290
|
+
return before + gap + text[w:gs]
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _check_option(name: str, value: int, low: int, high: int | None = None) -> int:
|
|
294
|
+
if isinstance(value, bool) or not isinstance(value, int): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
295
|
+
raise TypeError(f"{name} must be an int")
|
|
296
|
+
if value < low or (high is not None and value > high):
|
|
297
|
+
bound = f"between {low} and {high}" if high is not None else f"at least {low}"
|
|
298
|
+
raise ValueError(f"{name} must be {bound}, got {value}")
|
|
299
|
+
return value
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class Segmenter:
|
|
303
|
+
"""Splits streamed text into sentences as soon as each one is final.
|
|
304
|
+
|
|
305
|
+
``push()`` returns the sentences the new text completed; ``flush()``
|
|
306
|
+
returns the rest at the end of the input and resets the segmenter. Any way
|
|
307
|
+
of cutting a text into pushed pieces gives the same sentences as
|
|
308
|
+
:func:`split_sentences` on the whole text.
|
|
309
|
+
|
|
310
|
+
Args:
|
|
311
|
+
min_chars: sentences shorter than this are merged with the next one.
|
|
312
|
+
first_min_chars: the same minimum for the first sentence. The default
|
|
313
|
+
of 1 sends a short first sentence ("Sure.") at once.
|
|
314
|
+
max_chars: longer runs are wrapped at a soft delimiter or a space. At
|
|
315
|
+
most 5,000, the API's limit.
|
|
316
|
+
eager_first_clause: until the first sentence is out, also split after
|
|
317
|
+
a comma, semicolon, colon or dash, which starts audio sooner when
|
|
318
|
+
the first sentence is long.
|
|
319
|
+
first_clause_min_chars: the shortest first clause ``eager_first_clause``
|
|
320
|
+
will send on its own.
|
|
321
|
+
"""
|
|
322
|
+
|
|
323
|
+
__slots__ = (
|
|
324
|
+
"_buf",
|
|
325
|
+
"_first",
|
|
326
|
+
"_start",
|
|
327
|
+
"_wrapping",
|
|
328
|
+
"eager_first_clause",
|
|
329
|
+
"first_clause_min_chars",
|
|
330
|
+
"first_min_chars",
|
|
331
|
+
"max_chars",
|
|
332
|
+
"min_chars",
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
def __init__(
|
|
336
|
+
self,
|
|
337
|
+
*,
|
|
338
|
+
min_chars: int = 20,
|
|
339
|
+
first_min_chars: int = 1,
|
|
340
|
+
max_chars: int = 400,
|
|
341
|
+
eager_first_clause: bool = False,
|
|
342
|
+
first_clause_min_chars: int = 24,
|
|
343
|
+
) -> None:
|
|
344
|
+
self.min_chars = _check_option("min_chars", min_chars, 0)
|
|
345
|
+
self.first_min_chars = _check_option("first_min_chars", first_min_chars, 0)
|
|
346
|
+
self.max_chars = _check_option("max_chars", max_chars, 1, MAX_CHARS_LIMIT)
|
|
347
|
+
self.eager_first_clause = bool(eager_first_clause)
|
|
348
|
+
self.first_clause_min_chars = _check_option(
|
|
349
|
+
"first_clause_min_chars", first_clause_min_chars, 0
|
|
350
|
+
)
|
|
351
|
+
self._buf = ""
|
|
352
|
+
self._start = 0 # where the pending text starts in _buf (see _lookbehind)
|
|
353
|
+
self._first = True
|
|
354
|
+
self._wrapping = False # inside a wrapped group: no merging until it ends
|
|
355
|
+
|
|
356
|
+
def push(self, text: str) -> list[str]:
|
|
357
|
+
"""Add text; return the sentences that are now final."""
|
|
358
|
+
if not isinstance(text, str): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
359
|
+
raise TypeError(f"push() takes str, not {type(text).__name__}")
|
|
360
|
+
self._buf += text
|
|
361
|
+
return self._drain(final=False)
|
|
362
|
+
|
|
363
|
+
def flush(self) -> list[str]:
|
|
364
|
+
"""End the input: return the remaining sentences and reset."""
|
|
365
|
+
out = self._drain(final=True)
|
|
366
|
+
self._buf = ""
|
|
367
|
+
self._start = 0
|
|
368
|
+
self._first = True
|
|
369
|
+
self._wrapping = False
|
|
370
|
+
return out
|
|
371
|
+
|
|
372
|
+
def _min(self) -> int:
|
|
373
|
+
return self.first_min_chars if self._first else self.min_chars
|
|
374
|
+
|
|
375
|
+
def _emit(self, out: list[str], piece: str) -> None:
|
|
376
|
+
# A piece with nothing to say (only punctuation or symbols) is dropped:
|
|
377
|
+
# the API refuses text that has no sentence in it.
|
|
378
|
+
if piece and _speakable(piece):
|
|
379
|
+
out.append(piece)
|
|
380
|
+
self._first = False
|
|
381
|
+
|
|
382
|
+
def _drain(self, final: bool) -> list[str]:
|
|
383
|
+
max_chars = self.max_chars
|
|
384
|
+
out: list[str] = []
|
|
385
|
+
text = self._buf
|
|
386
|
+
nxt = _NextSpecial(text)
|
|
387
|
+
gs = _skip_ws(text, self._start) # group start
|
|
388
|
+
while True:
|
|
389
|
+
clause = (
|
|
390
|
+
(gs, self.first_clause_min_chars)
|
|
391
|
+
if (self.eager_first_clause and self._first)
|
|
392
|
+
else None
|
|
393
|
+
)
|
|
394
|
+
emitted = False
|
|
395
|
+
first_open: int | None = None # the first candidate that is a boundary or undecided
|
|
396
|
+
for c in _candidates(text, gs, final, clause, nxt):
|
|
397
|
+
if first_open is None and (not c.decided or c.boundary):
|
|
398
|
+
first_open = c.end
|
|
399
|
+
if not c.decided:
|
|
400
|
+
break
|
|
401
|
+
if not c.boundary:
|
|
402
|
+
continue
|
|
403
|
+
group = _strip(text[gs : c.end])
|
|
404
|
+
if len(group) > max_chars:
|
|
405
|
+
break # handled by wrapping below
|
|
406
|
+
if not _speakable(group):
|
|
407
|
+
continue # punctuation only: nothing to say yet, extend
|
|
408
|
+
if self._wrapping or len(group) >= self._min():
|
|
409
|
+
self._emit(out, group)
|
|
410
|
+
self._wrapping = False
|
|
411
|
+
gs = _skip_ws(text, c.end)
|
|
412
|
+
emitted = True
|
|
413
|
+
break
|
|
414
|
+
# too short: extend the group to the next boundary
|
|
415
|
+
if emitted:
|
|
416
|
+
continue
|
|
417
|
+
# No boundary closes a group of acceptable size. Wrap if the group
|
|
418
|
+
# is certainly longer than max_chars.
|
|
419
|
+
limit = gs + max_chars
|
|
420
|
+
# The stripped rest is longer than max_chars exactly when something
|
|
421
|
+
# other than whitespace lies at or beyond `limit` (gs is not whitespace).
|
|
422
|
+
long_rest = _skip_ws(text, limit) < len(text)
|
|
423
|
+
certainly_long = long_rest and (final or first_open is None or first_open > limit)
|
|
424
|
+
if certainly_long:
|
|
425
|
+
cut = _wrap_cut(text, gs, max_chars)
|
|
426
|
+
self._emit(out, _strip(text[gs:cut]))
|
|
427
|
+
self._wrapping = True
|
|
428
|
+
gs = _skip_ws(text, cut)
|
|
429
|
+
continue
|
|
430
|
+
if final:
|
|
431
|
+
self._emit(out, _strip(text[gs:]))
|
|
432
|
+
gs = len(text)
|
|
433
|
+
break
|
|
434
|
+
context = _lookbehind(text, gs)
|
|
435
|
+
self._buf = context + text[gs:]
|
|
436
|
+
self._start = len(context) # where the pending text starts in _buf
|
|
437
|
+
return out
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def split_sentences(
|
|
441
|
+
text: str,
|
|
442
|
+
*,
|
|
443
|
+
min_chars: int = 20,
|
|
444
|
+
first_min_chars: int = 1,
|
|
445
|
+
max_chars: int = 400,
|
|
446
|
+
eager_first_clause: bool = False,
|
|
447
|
+
first_clause_min_chars: int = 24,
|
|
448
|
+
) -> list[str]:
|
|
449
|
+
"""Split a whole text into sentence-sized requests.
|
|
450
|
+
|
|
451
|
+
The options are those of :class:`Segmenter`.
|
|
452
|
+
|
|
453
|
+
>>> split_sentences("आपका पैकेज आज शाम तक पहुँच जाएगा। राहुल आपको कॉल करेगा।")
|
|
454
|
+
['आपका पैकेज आज शाम तक पहुँच जाएगा।', 'राहुल आपको कॉल करेगा।']
|
|
455
|
+
"""
|
|
456
|
+
segmenter = Segmenter(
|
|
457
|
+
min_chars=min_chars,
|
|
458
|
+
first_min_chars=first_min_chars,
|
|
459
|
+
max_chars=max_chars,
|
|
460
|
+
eager_first_clause=eager_first_clause,
|
|
461
|
+
first_clause_min_chars=first_clause_min_chars,
|
|
462
|
+
)
|
|
463
|
+
return segmenter.push(text) + segmenter.flush()
|
bakbak/types.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""Request parameter types and response models.
|
|
2
|
+
|
|
3
|
+
Request fields keep their wire names (BEHAVIOR.md §2). Enumerations are open:
|
|
4
|
+
the known values are listed for editors and type checkers, and any other string
|
|
5
|
+
is accepted, so a language added on the server needs no SDK release.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Literal, TypeAlias
|
|
12
|
+
|
|
13
|
+
from typing_extensions import Required, TypedDict
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"Container",
|
|
17
|
+
"Encoding",
|
|
18
|
+
"Language",
|
|
19
|
+
"Model",
|
|
20
|
+
"OutputFormat",
|
|
21
|
+
"RequestDefaults",
|
|
22
|
+
"SampleRate",
|
|
23
|
+
"SendParams",
|
|
24
|
+
"SynthesisParams",
|
|
25
|
+
"Voice",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
Language: TypeAlias = (
|
|
29
|
+
Literal[
|
|
30
|
+
"hi", "mr", "te", "kn", "bn", "as", "gu", "ne", "ml", "ta", "pa", "en", "en-in", "en-us"
|
|
31
|
+
]
|
|
32
|
+
| str
|
|
33
|
+
)
|
|
34
|
+
"""A language code. ``m1`` voices use ``en`` for English."""
|
|
35
|
+
|
|
36
|
+
Model: TypeAlias = Literal["standard", "m1"] | str
|
|
37
|
+
"""A model. It must match the voice's model; the server defaults to ``standard``."""
|
|
38
|
+
|
|
39
|
+
Container: TypeAlias = Literal["raw", "wav", "mp3"] | str
|
|
40
|
+
"""``raw`` or ``wav`` (whole file only) or ``mp3`` (whole file only)."""
|
|
41
|
+
|
|
42
|
+
Encoding: TypeAlias = Literal["pcm_f32le", "pcm_s16le", "pcm_mulaw", "pcm_alaw"] | str
|
|
43
|
+
"""Sample encoding of ``raw`` and ``wav`` audio. Always mono."""
|
|
44
|
+
|
|
45
|
+
SampleRate: TypeAlias = Literal[8000, 16000, 22050, 24000] | int
|
|
46
|
+
"""Sample rate in Hz. The server default is 24000."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class OutputFormat(TypedDict, total=False):
|
|
50
|
+
"""The audio format to return.
|
|
51
|
+
|
|
52
|
+
Without ``output_format`` the whole-file endpoint returns 24 kHz WAV and
|
|
53
|
+
the streams send 24 kHz 32-bit float PCM.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
container: Required[Container]
|
|
57
|
+
encoding: Encoding
|
|
58
|
+
"""Required for ``raw`` and ``wav``; leave it out for ``mp3``."""
|
|
59
|
+
sample_rate: SampleRate
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SynthesisParams(TypedDict, total=False):
|
|
63
|
+
"""The body of ``tts.generate`` and ``tts.stream``."""
|
|
64
|
+
|
|
65
|
+
text: Required[str]
|
|
66
|
+
"""1 to 5,000 characters. Hindi must be in Devanagari."""
|
|
67
|
+
voice_id: Required[str]
|
|
68
|
+
"""A voice id from ``voices.list()``. A voice belongs to one model."""
|
|
69
|
+
language: Required[Language]
|
|
70
|
+
model: Model
|
|
71
|
+
speed: float
|
|
72
|
+
"""0.5 to 1.5; default 1.0."""
|
|
73
|
+
output_format: OutputFormat
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class RequestDefaults(TypedDict, total=False):
|
|
77
|
+
"""Fields a WebSocket or one of its contexts fills in for every request."""
|
|
78
|
+
|
|
79
|
+
voice_id: str
|
|
80
|
+
language: Language
|
|
81
|
+
model: Model
|
|
82
|
+
speed: float
|
|
83
|
+
output_format: OutputFormat
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class SendParams(RequestDefaults, total=False):
|
|
87
|
+
"""The body of one WebSocket request. Fields left out come from the
|
|
88
|
+
defaults of the context and of the WebSocket."""
|
|
89
|
+
|
|
90
|
+
text: Required[str]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(slots=True)
|
|
94
|
+
class Voice:
|
|
95
|
+
"""A voice from ``GET /v1/voices``."""
|
|
96
|
+
|
|
97
|
+
id: str
|
|
98
|
+
name: str
|
|
99
|
+
language: str
|
|
100
|
+
model: str
|
bakbak/websocket.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""The WebSocket client: many streaming requests on one connection.
|
|
2
|
+
|
|
3
|
+
Get one from ``client.tts.websocket()``; see BEHAVIOR.md §6 for the protocol.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ._ws._async import AsyncContext, AsyncRequestHandle, AsyncTTSWebSocket
|
|
7
|
+
from ._ws._sync import Context, RequestHandle, TTSWebSocket
|
|
8
|
+
from ._ws.events import Cancelled, Chunk, Done, Failed, RequestResult, WebSocketEvent
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"AsyncContext",
|
|
12
|
+
"AsyncRequestHandle",
|
|
13
|
+
"AsyncTTSWebSocket",
|
|
14
|
+
"Cancelled",
|
|
15
|
+
"Chunk",
|
|
16
|
+
"Context",
|
|
17
|
+
"Done",
|
|
18
|
+
"Failed",
|
|
19
|
+
"RequestHandle",
|
|
20
|
+
"RequestResult",
|
|
21
|
+
"TTSWebSocket",
|
|
22
|
+
"WebSocketEvent",
|
|
23
|
+
]
|