verbex-speech-sdk 0.1.0__tar.gz
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.
- verbex_speech_sdk-0.1.0/.gitignore +6 -0
- verbex_speech_sdk-0.1.0/PKG-INFO +322 -0
- verbex_speech_sdk-0.1.0/README.md +305 -0
- verbex_speech_sdk-0.1.0/examples/live_from_wav.py +32 -0
- verbex_speech_sdk-0.1.0/examples/speak_to_wav.py +12 -0
- verbex_speech_sdk-0.1.0/examples/transcribe_file.py +11 -0
- verbex_speech_sdk-0.1.0/pyproject.toml +29 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/__init__.py +25 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/_transport/__init__.py +1 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/_transport/common.py +240 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/_transport/http_client.py +89 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/_transport/retry.py +36 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/_transport/sockets.py +62 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/client.py +99 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/errors.py +165 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/listen/__init__.py +124 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/listen/jobs.py +333 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/listen/models.py +63 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/listen/stream.py +315 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/listen/webhook.py +63 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/py.typed +0 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/speak/__init__.py +97 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/speak/stream.py +203 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/speak/synthesis.py +126 -0
- verbex_speech_sdk-0.1.0/src/verbex_speech/speak/voices.py +37 -0
- verbex_speech_sdk-0.1.0/tests/__init__.py +0 -0
- verbex_speech_sdk-0.1.0/tests/conftest.py +39 -0
- verbex_speech_sdk-0.1.0/tests/socket_server.py +81 -0
- verbex_speech_sdk-0.1.0/tests/test_async.py +56 -0
- verbex_speech_sdk-0.1.0/tests/test_config.py +37 -0
- verbex_speech_sdk-0.1.0/tests/test_errors.py +33 -0
- verbex_speech_sdk-0.1.0/tests/test_idempotency.py +153 -0
- verbex_speech_sdk-0.1.0/tests/test_listen.py +179 -0
- verbex_speech_sdk-0.1.0/tests/test_live.py +51 -0
- verbex_speech_sdk-0.1.0/tests/test_retry.py +87 -0
- verbex_speech_sdk-0.1.0/tests/test_speak.py +59 -0
- verbex_speech_sdk-0.1.0/tests/test_speak_idempotency.py +118 -0
- verbex_speech_sdk-0.1.0/tests/test_streams.py +97 -0
- verbex_speech_sdk-0.1.0/tests/test_types.py +45 -0
- verbex_speech_sdk-0.1.0/tests/test_webhooks.py +39 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: verbex-speech-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for the Verbex Speech API: speech-to-text and text-to-speech.
|
|
5
|
+
Author: Verbex
|
|
6
|
+
License: Proprietary
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
|
|
9
|
+
Classifier: Typing :: Typed
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Requires-Dist: httpx<1,>=0.27
|
|
12
|
+
Requires-Dist: websockets<18,>=13
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
15
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# verbex-speech-sdk (Python)
|
|
19
|
+
|
|
20
|
+
Python SDK for the Verbex Speech API: speech-to-text (files and live audio) and text-to-speech.
|
|
21
|
+
Python 3.9 or later. Synchronous (`VerbexSpeech`) and asynchronous (`AsyncVerbexSpeech`) clients
|
|
22
|
+
with the same calls.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pip install verbex-speech-sdk
|
|
28
|
+
# or
|
|
29
|
+
poetry add verbex-speech-sdk
|
|
30
|
+
# or
|
|
31
|
+
uv add verbex-speech-sdk
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The package is installed as `verbex-speech-sdk` and imported as `verbex_speech`:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from verbex_speech import VerbexSpeech
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
From a checkout of this repository instead: `pip install ./python` (or `pip install -e ./python` to
|
|
41
|
+
edit the SDK as you use it).
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
1. Create an API key in the customer console (it starts with `vbx_`).
|
|
46
|
+
2. Tell the SDK the key and the API address:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
export VERBEX_API_KEY=vbx_...
|
|
50
|
+
export VERBEX_BASE_URL=https://speech-api-stage.verbex.ai
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
3. Pick a model and a voice your workspace can use:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from verbex_speech import VerbexSpeech
|
|
57
|
+
|
|
58
|
+
with VerbexSpeech() as client:
|
|
59
|
+
for m in client.listen.models():
|
|
60
|
+
print("stt", m.model, m.language, [mode.mode for mode in m.modes])
|
|
61
|
+
for v in client.speak.voices():
|
|
62
|
+
print("tts", v.model, v.code, v.language)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
4. Transcribe a file and speak a reply, in one script:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from verbex_speech import APIError, VerbexSpeech
|
|
69
|
+
|
|
70
|
+
with VerbexSpeech() as client:
|
|
71
|
+
try:
|
|
72
|
+
job = client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
73
|
+
except APIError as error:
|
|
74
|
+
raise SystemExit(f"{error.code}: {error.message} (request {error.request_id})")
|
|
75
|
+
if not job.completed:
|
|
76
|
+
raise SystemExit(f"job {job.job_id} {job.status}: {job.error}")
|
|
77
|
+
print(job.text)
|
|
78
|
+
|
|
79
|
+
# Voices are listed by language; take one that speaks the text's language.
|
|
80
|
+
voice = next(v for v in client.speak.voices() if v.language == "ja")
|
|
81
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="ありがとうございました。",
|
|
82
|
+
format="wav")
|
|
83
|
+
with open("reply.wav", "wb") as f:
|
|
84
|
+
f.write(audio.data)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Ready-to-run scripts are in `examples/`: `transcribe_file.py call.wav`, `live_from_wav.py call.wav`
|
|
88
|
+
(16 kHz mono 16-bit WAV) and `speak_to_wav.py out.wav`. Each reads the key and address from the
|
|
89
|
+
environment. The sections below cover every call and option.
|
|
90
|
+
|
|
91
|
+
## Set up
|
|
92
|
+
|
|
93
|
+
Create an API key in the customer console. The SDK reads the key and the API address from the
|
|
94
|
+
environment, or from the constructor:
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
export VERBEX_API_KEY=vbx_...
|
|
98
|
+
export VERBEX_BASE_URL=https://speech-api-stage.verbex.ai
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from verbex_speech import VerbexSpeech
|
|
103
|
+
|
|
104
|
+
client = VerbexSpeech() # or VerbexSpeech(api_key=..., base_url=...)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Keep the key secret: it is never logged by the SDK and never sent anywhere but the API.
|
|
108
|
+
|
|
109
|
+
## Transcribe a file
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
job = client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
113
|
+
print(job.status, job.text)
|
|
114
|
+
for segment in job.segments:
|
|
115
|
+
print(segment.start_sec, segment.end_sec, segment.text)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
WAV or MP3, up to 100 MiB. A file of up to 1 MiB is sent in the request; a larger one goes straight
|
|
119
|
+
to storage through the upload flow, without passing through the API. Either way the call returns
|
|
120
|
+
the finished job. It does not raise for a failed job: check `job.completed` or `job.status`, and
|
|
121
|
+
`job.error`.
|
|
122
|
+
|
|
123
|
+
Options: `hotwords=[...]`, `metadata={...}`, `webhook_url=...`, `tags=[...]`, `filename=`,
|
|
124
|
+
`size=` (for a stream). A `bytes` object, a path or an open binary file all work. To audio already
|
|
125
|
+
in your storage, pass `url="https://..."` instead of a file.
|
|
126
|
+
|
|
127
|
+
Without waiting:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
receipt = client.listen.submit("call.wav", model="verbex_jp_stt_x1", language="ja",
|
|
131
|
+
webhook_url="https://example.com/stt/done")
|
|
132
|
+
job = client.listen.get(receipt.job_id) # or client.listen.wait(receipt.job_id)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
A finished job is kept for 10 minutes.
|
|
136
|
+
|
|
137
|
+
### Submitting safely more than once
|
|
138
|
+
|
|
139
|
+
Every submit carries an `Idempotency-Key` header, so a submit whose answer was lost (a timeout, a
|
|
140
|
+
dropped connection) is retried without making a second job: the API answers the retry with the
|
|
141
|
+
first request's receipt, and `receipt.replayed` is `True`. The SDK makes a new key for each
|
|
142
|
+
`submit` or `transcribe` call and reuses it for that call's retries.
|
|
143
|
+
|
|
144
|
+
If your process may submit the same work again after a restart, pass your own key, saved with the
|
|
145
|
+
work, so the API recognises it for up to 24 hours:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
receipt = client.listen.submit("call.wav", model="verbex_jp_stt_x1", language="ja",
|
|
149
|
+
idempotency_key=f"call-{call_id}")
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
A key is 1 to 255 visible ASCII characters. Reusing a key for a different request raises
|
|
153
|
+
`BadRequestError` with code `idempotency_key_reused` (422).
|
|
154
|
+
|
|
155
|
+
### Verify a webhook
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
from verbex_speech import parse_webhook, WebhookVerificationError
|
|
159
|
+
|
|
160
|
+
def handle(raw_body: bytes, headers: dict):
|
|
161
|
+
try:
|
|
162
|
+
job = parse_webhook(API_KEY, raw_body, headers) # the key that submitted the job
|
|
163
|
+
except WebhookVerificationError:
|
|
164
|
+
return 401
|
|
165
|
+
...
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Pass the body exactly as received. `verify_webhook(api_key, raw_body, timestamp, signature)` returns
|
|
169
|
+
a bool if you only want the check.
|
|
170
|
+
|
|
171
|
+
## Live transcription
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
with client.listen.stream(model="verbex_jp_stt_x1", language="ja") as session:
|
|
175
|
+
# send raw PCM: 16 kHz, mono, signed 16-bit little-endian, at the pace it is produced
|
|
176
|
+
session.send(pcm_chunk)
|
|
177
|
+
...
|
|
178
|
+
session.finish()
|
|
179
|
+
for event in session:
|
|
180
|
+
if event.type == "segment":
|
|
181
|
+
print(event.text)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Send from one thread and read in another, as `examples/live_from_wav.py` does. Events are
|
|
185
|
+
`segment`, `configured` (after `session.configure(hotwords)`), `done` and `error`. Leaving the
|
|
186
|
+
`with` block without `finish()` sends the end of audio for you and waits briefly for the last
|
|
187
|
+
segments. A session the server ends raises `StreamError`; when `error.retryable` is true, reconnect
|
|
188
|
+
and resume sending audio.
|
|
189
|
+
|
|
190
|
+
## Text-to-speech
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
voice = next(v for v in client.speak.voices() if v.language == "ja")
|
|
194
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="こんにちは", format="wav")
|
|
195
|
+
open("hello.wav", "wb").write(audio.data)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
The default format is raw PCM (16 kHz, mono, 16-bit). To start playback before the utterance is
|
|
199
|
+
complete:
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
with client.speak.synthesize_stream(model=voice.model, voice=voice.code, text="...") as stream:
|
|
203
|
+
for chunk in stream:
|
|
204
|
+
player.write(chunk)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Speaking safely more than once
|
|
208
|
+
|
|
209
|
+
Every `synthesize` carries an `Idempotency-Key` header, the same one for each of its automatic
|
|
210
|
+
retries. An utterance is billed once: if the audio breaks off and you send the same request again
|
|
211
|
+
with the same key, it is spoken again without charge (up to 3 times within 24 hours), and
|
|
212
|
+
`audio.replayed` (or `stream.replayed`) is `True`. To use this for your own retries, pass your key:
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
key = str(uuid.uuid4()) # import uuid; one per utterance, kept for its retries
|
|
216
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="...", idempotency_key=key)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Past the 3 free replays the API refuses the key: `BadRequestError` with code
|
|
220
|
+
`idempotency_replays_exhausted` (422). Reusing a key for different text raises `BadRequestError`
|
|
221
|
+
with code `idempotency_key_reused` (422).
|
|
222
|
+
|
|
223
|
+
Many utterances over one connection, one at a time:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
with client.speak.stream() as session:
|
|
227
|
+
for line in lines:
|
|
228
|
+
utterance = session.say(model=voice.model, voice=voice.code, text=line)
|
|
229
|
+
for chunk in utterance:
|
|
230
|
+
player.write(chunk)
|
|
231
|
+
print(utterance.done.audio_seconds)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
A failed utterance raises `SpeakError`; the session stays open for the next one.
|
|
235
|
+
|
|
236
|
+
## Async
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
from verbex_speech import AsyncVerbexSpeech
|
|
240
|
+
|
|
241
|
+
async with AsyncVerbexSpeech() as client:
|
|
242
|
+
job = await client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
243
|
+
async with client.listen.stream(model="verbex_jp_stt_x1", language="ja") as session:
|
|
244
|
+
await session.send(pcm_chunk)
|
|
245
|
+
await session.finish()
|
|
246
|
+
async for event in session:
|
|
247
|
+
...
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Errors
|
|
251
|
+
|
|
252
|
+
Every error is a `VerbexError`.
|
|
253
|
+
|
|
254
|
+
| Error | When |
|
|
255
|
+
|---|---|
|
|
256
|
+
| `ConfigurationError` | No or malformed API key or base URL |
|
|
257
|
+
| `ValidationError` (also a `ValueError`) | Refused before sending: over 100 MiB, no size for a stream, a bad format |
|
|
258
|
+
| `BadRequestError` | 400, 405, 406, 409, 413, 415, 422 |
|
|
259
|
+
| `AuthenticationError` | 401 |
|
|
260
|
+
| `PaymentRequiredError` | 402: no plan, not enough credit, an overdue invoice |
|
|
261
|
+
| `PermissionDeniedError` | 403: the key lacks the permission |
|
|
262
|
+
| `NotFoundError` | 404 |
|
|
263
|
+
| `RateLimitError` | 429 (`retry_after` in seconds for the rate limits) |
|
|
264
|
+
| `ServerError` | 5xx |
|
|
265
|
+
| `APIConnectionError`, `APITimeoutError` | No answer |
|
|
266
|
+
| `UploadError` | The storage upload failed |
|
|
267
|
+
| `StreamError` | A WebSocket session ended by an error (`code`, `close_code`, `retryable`) |
|
|
268
|
+
| `SpeakError` | One utterance failed (`code`, `request_id`, `fields`) |
|
|
269
|
+
| `WaitTimeoutError` | `wait(..., timeout=)` ran out; `error.job` is the last state |
|
|
270
|
+
|
|
271
|
+
API errors carry `status`, `code` (branch on this), `message`, `request_id` (quote it to support)
|
|
272
|
+
and `fields` for `validation_failed`.
|
|
273
|
+
|
|
274
|
+
## Retries
|
|
275
|
+
|
|
276
|
+
Up to `max_retries` (default 2) automatic retries with exponential backoff, honouring
|
|
277
|
+
`Retry-After` up to 60 seconds:
|
|
278
|
+
|
|
279
|
+
- reads, upload starts, speech and submits: on connection errors, timeouts, 429 rate limits, 502,
|
|
280
|
+
503, 504 (speech only before any audio has arrived). Submits and speech are safe to retry because
|
|
281
|
+
they carry their idempotency key (above); both are also retried on `409 request_in_progress`,
|
|
282
|
+
while the first request with their key still runs;
|
|
283
|
+
- the storage upload and WebSocket handshakes: never.
|
|
284
|
+
|
|
285
|
+
`429 concurrency_limit` is not retried: wait for some of your own running work to finish.
|
|
286
|
+
|
|
287
|
+
`502 synthesis_failed` is not retried: the engine refused this text (for example, text in a language
|
|
288
|
+
the voice does not speak) and would refuse it the same way again.
|
|
289
|
+
|
|
290
|
+
## Modules
|
|
291
|
+
|
|
292
|
+
Every public name can be imported from `verbex_speech` directly. The package is split by
|
|
293
|
+
responsibility, the same way as the Java SDK:
|
|
294
|
+
|
|
295
|
+
```
|
|
296
|
+
verbex_speech/
|
|
297
|
+
client.py VerbexSpeech, AsyncVerbexSpeech: configuration, HTTP client, .listen and .speak
|
|
298
|
+
errors.py the errors every product shares (VerbexError, APIError and its subclasses,
|
|
299
|
+
ConfigurationError, ValidationError, APIConnectionError, APITimeoutError, StreamError)
|
|
300
|
+
listen/ Listen, AsyncListen (client.listen)
|
|
301
|
+
models.py ListenModel, ModelMode, Feature; listing models
|
|
302
|
+
jobs.py Job, SubmitReceipt, UploadStarted, Segment, JobError, UploadError, WaitTimeoutError;
|
|
303
|
+
submit, the upload flow, get, wait, transcribe
|
|
304
|
+
stream.py ListenEvent, LiveSession, AsyncLiveSession; transcribe_stream
|
|
305
|
+
webhook.py verify_webhook, parse_webhook, signing_key, WebhookVerificationError
|
|
306
|
+
speak/ Speak, AsyncSpeak (client.speak), SpeakError
|
|
307
|
+
voices.py Voice; listing voices
|
|
308
|
+
synthesis.py SpeechAudio, SpeechStream, AsyncSpeechStream; synthesize, synthesize_stream
|
|
309
|
+
stream.py SpeakSession, AsyncSpeakSession, Utterance, AsyncUtterance, SpeakDone
|
|
310
|
+
_transport/ private: the HTTP request and retry loop, retry rules, WebSocket opening,
|
|
311
|
+
configuration and request-shape helpers
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
## Development
|
|
315
|
+
|
|
316
|
+
```
|
|
317
|
+
pip install -e '.[dev]'
|
|
318
|
+
pytest # the live tests run only with VERBEX_API_KEY and VERBEX_BASE_URL set
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
The tests load the shared fixtures in `../conformance/`, which the Node.js and Java SDKs test
|
|
322
|
+
against too. The behaviour all three share is specified in `../SPEC.md`.
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
# verbex-speech-sdk (Python)
|
|
2
|
+
|
|
3
|
+
Python SDK for the Verbex Speech API: speech-to-text (files and live audio) and text-to-speech.
|
|
4
|
+
Python 3.9 or later. Synchronous (`VerbexSpeech`) and asynchronous (`AsyncVerbexSpeech`) clients
|
|
5
|
+
with the same calls.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install verbex-speech-sdk
|
|
11
|
+
# or
|
|
12
|
+
poetry add verbex-speech-sdk
|
|
13
|
+
# or
|
|
14
|
+
uv add verbex-speech-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The package is installed as `verbex-speech-sdk` and imported as `verbex_speech`:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from verbex_speech import VerbexSpeech
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
From a checkout of this repository instead: `pip install ./python` (or `pip install -e ./python` to
|
|
24
|
+
edit the SDK as you use it).
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
1. Create an API key in the customer console (it starts with `vbx_`).
|
|
29
|
+
2. Tell the SDK the key and the API address:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
export VERBEX_API_KEY=vbx_...
|
|
33
|
+
export VERBEX_BASE_URL=https://speech-api-stage.verbex.ai
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
3. Pick a model and a voice your workspace can use:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from verbex_speech import VerbexSpeech
|
|
40
|
+
|
|
41
|
+
with VerbexSpeech() as client:
|
|
42
|
+
for m in client.listen.models():
|
|
43
|
+
print("stt", m.model, m.language, [mode.mode for mode in m.modes])
|
|
44
|
+
for v in client.speak.voices():
|
|
45
|
+
print("tts", v.model, v.code, v.language)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
4. Transcribe a file and speak a reply, in one script:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from verbex_speech import APIError, VerbexSpeech
|
|
52
|
+
|
|
53
|
+
with VerbexSpeech() as client:
|
|
54
|
+
try:
|
|
55
|
+
job = client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
56
|
+
except APIError as error:
|
|
57
|
+
raise SystemExit(f"{error.code}: {error.message} (request {error.request_id})")
|
|
58
|
+
if not job.completed:
|
|
59
|
+
raise SystemExit(f"job {job.job_id} {job.status}: {job.error}")
|
|
60
|
+
print(job.text)
|
|
61
|
+
|
|
62
|
+
# Voices are listed by language; take one that speaks the text's language.
|
|
63
|
+
voice = next(v for v in client.speak.voices() if v.language == "ja")
|
|
64
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="ありがとうございました。",
|
|
65
|
+
format="wav")
|
|
66
|
+
with open("reply.wav", "wb") as f:
|
|
67
|
+
f.write(audio.data)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Ready-to-run scripts are in `examples/`: `transcribe_file.py call.wav`, `live_from_wav.py call.wav`
|
|
71
|
+
(16 kHz mono 16-bit WAV) and `speak_to_wav.py out.wav`. Each reads the key and address from the
|
|
72
|
+
environment. The sections below cover every call and option.
|
|
73
|
+
|
|
74
|
+
## Set up
|
|
75
|
+
|
|
76
|
+
Create an API key in the customer console. The SDK reads the key and the API address from the
|
|
77
|
+
environment, or from the constructor:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
export VERBEX_API_KEY=vbx_...
|
|
81
|
+
export VERBEX_BASE_URL=https://speech-api-stage.verbex.ai
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from verbex_speech import VerbexSpeech
|
|
86
|
+
|
|
87
|
+
client = VerbexSpeech() # or VerbexSpeech(api_key=..., base_url=...)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Keep the key secret: it is never logged by the SDK and never sent anywhere but the API.
|
|
91
|
+
|
|
92
|
+
## Transcribe a file
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
job = client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
96
|
+
print(job.status, job.text)
|
|
97
|
+
for segment in job.segments:
|
|
98
|
+
print(segment.start_sec, segment.end_sec, segment.text)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
WAV or MP3, up to 100 MiB. A file of up to 1 MiB is sent in the request; a larger one goes straight
|
|
102
|
+
to storage through the upload flow, without passing through the API. Either way the call returns
|
|
103
|
+
the finished job. It does not raise for a failed job: check `job.completed` or `job.status`, and
|
|
104
|
+
`job.error`.
|
|
105
|
+
|
|
106
|
+
Options: `hotwords=[...]`, `metadata={...}`, `webhook_url=...`, `tags=[...]`, `filename=`,
|
|
107
|
+
`size=` (for a stream). A `bytes` object, a path or an open binary file all work. To audio already
|
|
108
|
+
in your storage, pass `url="https://..."` instead of a file.
|
|
109
|
+
|
|
110
|
+
Without waiting:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
receipt = client.listen.submit("call.wav", model="verbex_jp_stt_x1", language="ja",
|
|
114
|
+
webhook_url="https://example.com/stt/done")
|
|
115
|
+
job = client.listen.get(receipt.job_id) # or client.listen.wait(receipt.job_id)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
A finished job is kept for 10 minutes.
|
|
119
|
+
|
|
120
|
+
### Submitting safely more than once
|
|
121
|
+
|
|
122
|
+
Every submit carries an `Idempotency-Key` header, so a submit whose answer was lost (a timeout, a
|
|
123
|
+
dropped connection) is retried without making a second job: the API answers the retry with the
|
|
124
|
+
first request's receipt, and `receipt.replayed` is `True`. The SDK makes a new key for each
|
|
125
|
+
`submit` or `transcribe` call and reuses it for that call's retries.
|
|
126
|
+
|
|
127
|
+
If your process may submit the same work again after a restart, pass your own key, saved with the
|
|
128
|
+
work, so the API recognises it for up to 24 hours:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
receipt = client.listen.submit("call.wav", model="verbex_jp_stt_x1", language="ja",
|
|
132
|
+
idempotency_key=f"call-{call_id}")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
A key is 1 to 255 visible ASCII characters. Reusing a key for a different request raises
|
|
136
|
+
`BadRequestError` with code `idempotency_key_reused` (422).
|
|
137
|
+
|
|
138
|
+
### Verify a webhook
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from verbex_speech import parse_webhook, WebhookVerificationError
|
|
142
|
+
|
|
143
|
+
def handle(raw_body: bytes, headers: dict):
|
|
144
|
+
try:
|
|
145
|
+
job = parse_webhook(API_KEY, raw_body, headers) # the key that submitted the job
|
|
146
|
+
except WebhookVerificationError:
|
|
147
|
+
return 401
|
|
148
|
+
...
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Pass the body exactly as received. `verify_webhook(api_key, raw_body, timestamp, signature)` returns
|
|
152
|
+
a bool if you only want the check.
|
|
153
|
+
|
|
154
|
+
## Live transcription
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
with client.listen.stream(model="verbex_jp_stt_x1", language="ja") as session:
|
|
158
|
+
# send raw PCM: 16 kHz, mono, signed 16-bit little-endian, at the pace it is produced
|
|
159
|
+
session.send(pcm_chunk)
|
|
160
|
+
...
|
|
161
|
+
session.finish()
|
|
162
|
+
for event in session:
|
|
163
|
+
if event.type == "segment":
|
|
164
|
+
print(event.text)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Send from one thread and read in another, as `examples/live_from_wav.py` does. Events are
|
|
168
|
+
`segment`, `configured` (after `session.configure(hotwords)`), `done` and `error`. Leaving the
|
|
169
|
+
`with` block without `finish()` sends the end of audio for you and waits briefly for the last
|
|
170
|
+
segments. A session the server ends raises `StreamError`; when `error.retryable` is true, reconnect
|
|
171
|
+
and resume sending audio.
|
|
172
|
+
|
|
173
|
+
## Text-to-speech
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
voice = next(v for v in client.speak.voices() if v.language == "ja")
|
|
177
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="こんにちは", format="wav")
|
|
178
|
+
open("hello.wav", "wb").write(audio.data)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The default format is raw PCM (16 kHz, mono, 16-bit). To start playback before the utterance is
|
|
182
|
+
complete:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
with client.speak.synthesize_stream(model=voice.model, voice=voice.code, text="...") as stream:
|
|
186
|
+
for chunk in stream:
|
|
187
|
+
player.write(chunk)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Speaking safely more than once
|
|
191
|
+
|
|
192
|
+
Every `synthesize` carries an `Idempotency-Key` header, the same one for each of its automatic
|
|
193
|
+
retries. An utterance is billed once: if the audio breaks off and you send the same request again
|
|
194
|
+
with the same key, it is spoken again without charge (up to 3 times within 24 hours), and
|
|
195
|
+
`audio.replayed` (or `stream.replayed`) is `True`. To use this for your own retries, pass your key:
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
key = str(uuid.uuid4()) # import uuid; one per utterance, kept for its retries
|
|
199
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="...", idempotency_key=key)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Past the 3 free replays the API refuses the key: `BadRequestError` with code
|
|
203
|
+
`idempotency_replays_exhausted` (422). Reusing a key for different text raises `BadRequestError`
|
|
204
|
+
with code `idempotency_key_reused` (422).
|
|
205
|
+
|
|
206
|
+
Many utterances over one connection, one at a time:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
with client.speak.stream() as session:
|
|
210
|
+
for line in lines:
|
|
211
|
+
utterance = session.say(model=voice.model, voice=voice.code, text=line)
|
|
212
|
+
for chunk in utterance:
|
|
213
|
+
player.write(chunk)
|
|
214
|
+
print(utterance.done.audio_seconds)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
A failed utterance raises `SpeakError`; the session stays open for the next one.
|
|
218
|
+
|
|
219
|
+
## Async
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
from verbex_speech import AsyncVerbexSpeech
|
|
223
|
+
|
|
224
|
+
async with AsyncVerbexSpeech() as client:
|
|
225
|
+
job = await client.listen.transcribe("call.wav", model="verbex_jp_stt_x1", language="ja")
|
|
226
|
+
async with client.listen.stream(model="verbex_jp_stt_x1", language="ja") as session:
|
|
227
|
+
await session.send(pcm_chunk)
|
|
228
|
+
await session.finish()
|
|
229
|
+
async for event in session:
|
|
230
|
+
...
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Errors
|
|
234
|
+
|
|
235
|
+
Every error is a `VerbexError`.
|
|
236
|
+
|
|
237
|
+
| Error | When |
|
|
238
|
+
|---|---|
|
|
239
|
+
| `ConfigurationError` | No or malformed API key or base URL |
|
|
240
|
+
| `ValidationError` (also a `ValueError`) | Refused before sending: over 100 MiB, no size for a stream, a bad format |
|
|
241
|
+
| `BadRequestError` | 400, 405, 406, 409, 413, 415, 422 |
|
|
242
|
+
| `AuthenticationError` | 401 |
|
|
243
|
+
| `PaymentRequiredError` | 402: no plan, not enough credit, an overdue invoice |
|
|
244
|
+
| `PermissionDeniedError` | 403: the key lacks the permission |
|
|
245
|
+
| `NotFoundError` | 404 |
|
|
246
|
+
| `RateLimitError` | 429 (`retry_after` in seconds for the rate limits) |
|
|
247
|
+
| `ServerError` | 5xx |
|
|
248
|
+
| `APIConnectionError`, `APITimeoutError` | No answer |
|
|
249
|
+
| `UploadError` | The storage upload failed |
|
|
250
|
+
| `StreamError` | A WebSocket session ended by an error (`code`, `close_code`, `retryable`) |
|
|
251
|
+
| `SpeakError` | One utterance failed (`code`, `request_id`, `fields`) |
|
|
252
|
+
| `WaitTimeoutError` | `wait(..., timeout=)` ran out; `error.job` is the last state |
|
|
253
|
+
|
|
254
|
+
API errors carry `status`, `code` (branch on this), `message`, `request_id` (quote it to support)
|
|
255
|
+
and `fields` for `validation_failed`.
|
|
256
|
+
|
|
257
|
+
## Retries
|
|
258
|
+
|
|
259
|
+
Up to `max_retries` (default 2) automatic retries with exponential backoff, honouring
|
|
260
|
+
`Retry-After` up to 60 seconds:
|
|
261
|
+
|
|
262
|
+
- reads, upload starts, speech and submits: on connection errors, timeouts, 429 rate limits, 502,
|
|
263
|
+
503, 504 (speech only before any audio has arrived). Submits and speech are safe to retry because
|
|
264
|
+
they carry their idempotency key (above); both are also retried on `409 request_in_progress`,
|
|
265
|
+
while the first request with their key still runs;
|
|
266
|
+
- the storage upload and WebSocket handshakes: never.
|
|
267
|
+
|
|
268
|
+
`429 concurrency_limit` is not retried: wait for some of your own running work to finish.
|
|
269
|
+
|
|
270
|
+
`502 synthesis_failed` is not retried: the engine refused this text (for example, text in a language
|
|
271
|
+
the voice does not speak) and would refuse it the same way again.
|
|
272
|
+
|
|
273
|
+
## Modules
|
|
274
|
+
|
|
275
|
+
Every public name can be imported from `verbex_speech` directly. The package is split by
|
|
276
|
+
responsibility, the same way as the Java SDK:
|
|
277
|
+
|
|
278
|
+
```
|
|
279
|
+
verbex_speech/
|
|
280
|
+
client.py VerbexSpeech, AsyncVerbexSpeech: configuration, HTTP client, .listen and .speak
|
|
281
|
+
errors.py the errors every product shares (VerbexError, APIError and its subclasses,
|
|
282
|
+
ConfigurationError, ValidationError, APIConnectionError, APITimeoutError, StreamError)
|
|
283
|
+
listen/ Listen, AsyncListen (client.listen)
|
|
284
|
+
models.py ListenModel, ModelMode, Feature; listing models
|
|
285
|
+
jobs.py Job, SubmitReceipt, UploadStarted, Segment, JobError, UploadError, WaitTimeoutError;
|
|
286
|
+
submit, the upload flow, get, wait, transcribe
|
|
287
|
+
stream.py ListenEvent, LiveSession, AsyncLiveSession; transcribe_stream
|
|
288
|
+
webhook.py verify_webhook, parse_webhook, signing_key, WebhookVerificationError
|
|
289
|
+
speak/ Speak, AsyncSpeak (client.speak), SpeakError
|
|
290
|
+
voices.py Voice; listing voices
|
|
291
|
+
synthesis.py SpeechAudio, SpeechStream, AsyncSpeechStream; synthesize, synthesize_stream
|
|
292
|
+
stream.py SpeakSession, AsyncSpeakSession, Utterance, AsyncUtterance, SpeakDone
|
|
293
|
+
_transport/ private: the HTTP request and retry loop, retry rules, WebSocket opening,
|
|
294
|
+
configuration and request-shape helpers
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Development
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
pip install -e '.[dev]'
|
|
301
|
+
pytest # the live tests run only with VERBEX_API_KEY and VERBEX_BASE_URL set
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
The tests load the shared fixtures in `../conformance/`, which the Node.js and Java SDKs test
|
|
305
|
+
against too. The behaviour all three share is specified in `../SPEC.md`.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Stream a 16 kHz mono 16-bit WAV at real time and print segments as they settle.
|
|
2
|
+
|
|
3
|
+
python live_from_wav.py call.wav (convert first: ffmpeg -i in.mp3 -ar 16000 -ac 1 call.wav)
|
|
4
|
+
"""
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
8
|
+
import wave
|
|
9
|
+
|
|
10
|
+
from verbex_speech import VerbexSpeech
|
|
11
|
+
|
|
12
|
+
CHUNK_SECONDS = 0.1
|
|
13
|
+
|
|
14
|
+
with wave.open(sys.argv[1]) as w:
|
|
15
|
+
assert (w.getframerate(), w.getnchannels(), w.getsampwidth()) == (16000, 1, 2), "16 kHz mono 16-bit only"
|
|
16
|
+
pcm = w.readframes(w.getnframes())
|
|
17
|
+
|
|
18
|
+
with VerbexSpeech() as client:
|
|
19
|
+
with client.listen.stream(model="verbex_jp_stt_x1", language="ja") as session:
|
|
20
|
+
print("session", session.session_id)
|
|
21
|
+
|
|
22
|
+
def feed():
|
|
23
|
+
step = int(16000 * 2 * CHUNK_SECONDS)
|
|
24
|
+
for i in range(0, len(pcm), step):
|
|
25
|
+
session.send(pcm[i:i + step])
|
|
26
|
+
time.sleep(CHUNK_SECONDS) # at the pace audio is produced
|
|
27
|
+
session.finish()
|
|
28
|
+
|
|
29
|
+
threading.Thread(target=feed, daemon=True).start()
|
|
30
|
+
for event in session:
|
|
31
|
+
if event.type == "segment":
|
|
32
|
+
print(f"{event.start_sec:7.2f} {event.text}")
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Speak a sentence to a WAV file: python speak_to_wav.py out.wav"""
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from verbex_speech import VerbexSpeech
|
|
5
|
+
|
|
6
|
+
with VerbexSpeech() as client:
|
|
7
|
+
voice = next(v for v in client.speak.voices() if v.language == "ja")
|
|
8
|
+
audio = client.speak.synthesize(model=voice.model, voice=voice.code, text="お電話ありがとうございます。",
|
|
9
|
+
format="wav")
|
|
10
|
+
with open(sys.argv[1], "wb") as f:
|
|
11
|
+
f.write(audio.data)
|
|
12
|
+
print(f"{len(audio.data)} bytes, request {audio.request_id}")
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Transcribe a WAV or MP3 file of up to 100 MiB: python transcribe_file.py call.wav"""
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from verbex_speech import VerbexSpeech
|
|
5
|
+
|
|
6
|
+
with VerbexSpeech() as client: # VERBEX_API_KEY and VERBEX_BASE_URL from the environment
|
|
7
|
+
job = client.listen.transcribe(sys.argv[1], model="verbex_jp_stt_x1", language="ja")
|
|
8
|
+
if not job.completed:
|
|
9
|
+
sys.exit(f"{job.status}: {job.error.code if job.error else ''}")
|
|
10
|
+
for segment in job.segments:
|
|
11
|
+
print(f"{segment.start_sec:7.2f}-{segment.end_sec:7.2f} {segment.text}")
|