voice2voice 0.1.1__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.
- voice2voice-0.1.1/.gitignore +28 -0
- voice2voice-0.1.1/LICENSE +21 -0
- voice2voice-0.1.1/PKG-INFO +332 -0
- voice2voice-0.1.1/README.md +302 -0
- voice2voice-0.1.1/examples/async_basic.py +32 -0
- voice2voice-0.1.1/examples/basic.py +31 -0
- voice2voice-0.1.1/pyproject.toml +65 -0
- voice2voice-0.1.1/tests/test_import.py +26 -0
- voice2voice-0.1.1/tests/test_injected_clients.py +54 -0
- voice2voice-0.1.1/voice2voice/__init__.py +23 -0
- voice2voice-0.1.1/voice2voice/async_client.py +230 -0
- voice2voice-0.1.1/voice2voice/client.py +191 -0
- voice2voice-0.1.1/voice2voice/exceptions.py +28 -0
- voice2voice-0.1.1/voice2voice/models.py +25 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
.mypy_cache/
|
|
7
|
+
.ruff_cache/
|
|
8
|
+
.coverage
|
|
9
|
+
htmlcov/
|
|
10
|
+
|
|
11
|
+
# Environments
|
|
12
|
+
.venv/
|
|
13
|
+
.venv*
|
|
14
|
+
venv/
|
|
15
|
+
.env
|
|
16
|
+
.env.*
|
|
17
|
+
|
|
18
|
+
# Build artifacts
|
|
19
|
+
build/
|
|
20
|
+
dist/
|
|
21
|
+
*.egg-info/
|
|
22
|
+
|
|
23
|
+
# IDE
|
|
24
|
+
.vscode/
|
|
25
|
+
.idea/
|
|
26
|
+
|
|
27
|
+
# FILES
|
|
28
|
+
*.zip
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Voice2Voice contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: voice2voice
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Synchronous and asynchronous Python SDK for the Voice2Voice Translator API
|
|
5
|
+
Project-URL: Homepage, https://github.com/ehsanulhaqislamyar-star/voice2voice-python
|
|
6
|
+
Project-URL: Repository, https://github.com/ehsanulhaqislamyar-star/voice2voice-python
|
|
7
|
+
Project-URL: Issues, https://github.com/ehsanulhaqislamyar-star/voice2voice-python/issues
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: audio,httpx,sdk,speech,speech-to-text,text-to-speech,translation,voice,voice-to-voice
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Requires-Dist: httpx<1.0,>=0.27
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Voice2Voice Python SDK
|
|
32
|
+
|
|
33
|
+
[](https://www.python.org/)
|
|
34
|
+
[](LICENSE)
|
|
35
|
+
|
|
36
|
+
A Python SDK for the **Voice2Voice Translator API**. It provides synchronous
|
|
37
|
+
and asynchronous clients for discovering supported languages, translating
|
|
38
|
+
audio, downloading generated audio, checking visitor quotas, and managing
|
|
39
|
+
administrator quota settings.
|
|
40
|
+
|
|
41
|
+
> **Release status:** `0.1.1` — early release. The SDK interface and server API
|
|
42
|
+
> may evolve.
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
- Synchronous client: `Voice2VoiceClient`
|
|
47
|
+
- Asynchronous client: `AsyncVoice2VoiceClient`
|
|
48
|
+
- Audio upload and translation
|
|
49
|
+
- Download translated audio
|
|
50
|
+
- Retrieve supported languages and visitor quota
|
|
51
|
+
- Optional administrator operations using a bearer token
|
|
52
|
+
- Typed result models and specific API exceptions
|
|
53
|
+
- Configurable request timeout
|
|
54
|
+
- Reusable HTTPX clients and context-manager support
|
|
55
|
+
- Persistent anonymous visitor ID when the server issues the
|
|
56
|
+
`vt_visitor_id` cookie
|
|
57
|
+
|
|
58
|
+
## Requirements
|
|
59
|
+
|
|
60
|
+
- Python 3.9 or newer
|
|
61
|
+
- Access to a running Voice2Voice API server
|
|
62
|
+
- `httpx` (installed automatically)
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
python -m pip install voice2voice
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Quick start
|
|
71
|
+
|
|
72
|
+
### Synchronous client
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError
|
|
76
|
+
|
|
77
|
+
with Voice2VoiceClient() as client:
|
|
78
|
+
languages = client.languages()
|
|
79
|
+
print([(language.code, language.name) for language in languages])
|
|
80
|
+
|
|
81
|
+
print("Quota:", client.quota())
|
|
82
|
+
|
|
83
|
+
result = client.translate(
|
|
84
|
+
file="sample.mp3",
|
|
85
|
+
source_language="en",
|
|
86
|
+
target_language="es",
|
|
87
|
+
output_format="mp3",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
print("Recognized:", result.recognized_text)
|
|
91
|
+
print("Cleaned:", result.cleaned_text)
|
|
92
|
+
print("Translated:", result.translated_text)
|
|
93
|
+
|
|
94
|
+
audio = client.download_audio(result)
|
|
95
|
+
with open(audio.filename, "wb") as output_file:
|
|
96
|
+
output_file.write(audio.content)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Handle API-specific errors when needed:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError
|
|
103
|
+
|
|
104
|
+
try:
|
|
105
|
+
with Voice2VoiceClient() as client:
|
|
106
|
+
result = client.translate(
|
|
107
|
+
file="sample.mp3",
|
|
108
|
+
source_language="en",
|
|
109
|
+
target_language="es",
|
|
110
|
+
)
|
|
111
|
+
except QuotaExceededError as exc:
|
|
112
|
+
print("Quota exceeded:", exc.message)
|
|
113
|
+
print("Remaining seconds:", exc.remaining_seconds)
|
|
114
|
+
except APIError as exc:
|
|
115
|
+
print(f"API request failed ({exc.status_code}): {exc.message}")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Asynchronous client
|
|
119
|
+
|
|
120
|
+
Use `async with` to close the asynchronous HTTPX client reliably:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
import asyncio
|
|
124
|
+
from voice2voice import AsyncVoice2VoiceClient
|
|
125
|
+
|
|
126
|
+
async def main():
|
|
127
|
+
async with AsyncVoice2VoiceClient() as client:
|
|
128
|
+
languages = await client.languages()
|
|
129
|
+
print([(language.code, language.name) for language in languages])
|
|
130
|
+
|
|
131
|
+
result = await client.translate(
|
|
132
|
+
file="sample.mp3",
|
|
133
|
+
source_language="en",
|
|
134
|
+
target_language="es",
|
|
135
|
+
output_format="mp3",
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
print("Translated:", result.translated_text)
|
|
139
|
+
|
|
140
|
+
audio = await client.download_audio(result)
|
|
141
|
+
with open(audio.filename, "wb") as output_file:
|
|
142
|
+
output_file.write(audio.content)
|
|
143
|
+
|
|
144
|
+
asyncio.run(main())
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Configuration
|
|
148
|
+
|
|
149
|
+
Both clients accept the following configuration options:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from voice2voice import Voice2VoiceClient
|
|
153
|
+
|
|
154
|
+
client = Voice2VoiceClient(
|
|
155
|
+
base_url="https://your-api.example.com",
|
|
156
|
+
timeout=120.0,
|
|
157
|
+
)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
| Option | Description |
|
|
161
|
+
|---|---|
|
|
162
|
+
| `base_url` | Base URL of the Voice2Voice API. Defaults to the hosted API configured by the SDK. |
|
|
163
|
+
| `visitor_id` | Optional previously obtained anonymous visitor ID. |
|
|
164
|
+
| `admin_token` | Bearer token used by administrator methods. |
|
|
165
|
+
| `timeout` | HTTPX request timeout in seconds. Set to `None` to disable the timeout. |
|
|
166
|
+
| `http_client` | Optional existing `httpx.Client`. The async client accepts an `httpx.AsyncClient`. |
|
|
167
|
+
|
|
168
|
+
When an external HTTPX client is injected, the SDK does **not** close it.
|
|
169
|
+
The caller remains responsible for managing its lifecycle. Requests use the
|
|
170
|
+
SDK's configured `base_url`, independently of the injected HTTPX client's
|
|
171
|
+
`base_url`.
|
|
172
|
+
|
|
173
|
+
Example with an externally managed HTTPX client:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
import httpx
|
|
177
|
+
from voice2voice import Voice2VoiceClient
|
|
178
|
+
|
|
179
|
+
transport = httpx.Client(timeout=45.0)
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
with Voice2VoiceClient(
|
|
183
|
+
base_url="https://your-api.example.com",
|
|
184
|
+
http_client=transport,
|
|
185
|
+
) as client:
|
|
186
|
+
print(client.languages())
|
|
187
|
+
finally:
|
|
188
|
+
transport.close()
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Anonymous visitor identity
|
|
192
|
+
|
|
193
|
+
The API server may issue a `vt_visitor_id` cookie to identify an anonymous
|
|
194
|
+
visitor and track that visitor's quota. The SDK exposes the current value:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
visitor_id = client.visitor_id
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
When the server issues this cookie, the SDK attempts to persist it at:
|
|
201
|
+
|
|
202
|
+
```text
|
|
203
|
+
~/.voice2voice/visitor.json
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The SDK continues working if the file cannot be read or written. You can also
|
|
207
|
+
pass `visitor_id=...` explicitly to restore an identity.
|
|
208
|
+
|
|
209
|
+
Treat visitor IDs as private. Do not publish them, commit them to source
|
|
210
|
+
control, or expose them in logs.
|
|
211
|
+
|
|
212
|
+
## Administrator operations
|
|
213
|
+
|
|
214
|
+
Administrator methods require a valid server-issued admin token:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from voice2voice import Voice2VoiceClient
|
|
218
|
+
|
|
219
|
+
with Voice2VoiceClient(admin_token="YOUR_ADMIN_TOKEN") as client:
|
|
220
|
+
current = client.admin_quota()
|
|
221
|
+
print(current)
|
|
222
|
+
|
|
223
|
+
updated = client.update_admin_quota(
|
|
224
|
+
quota_seconds=180,
|
|
225
|
+
period_type="daily",
|
|
226
|
+
)
|
|
227
|
+
print(updated)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Keep the token in an environment variable or secret manager rather than
|
|
231
|
+
hard-coding it.
|
|
232
|
+
|
|
233
|
+
The async client provides the equivalent methods:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
current = await client.admin_quota()
|
|
237
|
+
|
|
238
|
+
updated = await client.update_admin_quota(
|
|
239
|
+
quota_seconds=180,
|
|
240
|
+
period_type="daily",
|
|
241
|
+
)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The accepted `period_type` values and quota-update semantics are defined by
|
|
245
|
+
the API server. Check the server's current documentation before changing
|
|
246
|
+
administrator settings.
|
|
247
|
+
|
|
248
|
+
## API methods
|
|
249
|
+
|
|
250
|
+
| Method | Description |
|
|
251
|
+
|---|---|
|
|
252
|
+
| `languages()` | Return the languages advertised by the API. |
|
|
253
|
+
| `translate(file, source_language, target_language, output_format="mp3")` | Upload audio and return translation data. |
|
|
254
|
+
| `quota()` | Retrieve the current visitor's quota information. |
|
|
255
|
+
| `download_audio(result_or_url)` | Download generated audio and return an `AudioDownload`. |
|
|
256
|
+
| `admin_quota()` | Retrieve administrator quota settings. |
|
|
257
|
+
| `update_admin_quota(quota_seconds, period_type, custom_period_seconds=86400)` | Update administrator quota settings. |
|
|
258
|
+
|
|
259
|
+
For `AsyncVoice2VoiceClient`, use `await` with each method. It also provides
|
|
260
|
+
`aclose()` and asynchronous context-manager support.
|
|
261
|
+
|
|
262
|
+
Language codes, supported audio formats, upload limits, and quota behavior
|
|
263
|
+
are determined by the API server. Retrieve the current language list with
|
|
264
|
+
`client.languages()` and consult the server's current API documentation for
|
|
265
|
+
accepted formats and limits.
|
|
266
|
+
|
|
267
|
+
## Return models
|
|
268
|
+
|
|
269
|
+
- `Language`: `name`, `code`
|
|
270
|
+
- `TranslationResult`: `recognized_text`, `cleaned_text`, `translated_text`,
|
|
271
|
+
`audio_url`, `audio_filename`, and the original response dictionary in `raw`
|
|
272
|
+
- `AudioDownload`: downloaded `content` (`bytes`), `content_type`, and
|
|
273
|
+
`filename`
|
|
274
|
+
|
|
275
|
+
These models are immutable dataclasses.
|
|
276
|
+
|
|
277
|
+
## Error handling
|
|
278
|
+
|
|
279
|
+
All SDK-specific exceptions inherit from `Voice2VoiceError`.
|
|
280
|
+
|
|
281
|
+
| Exception | Meaning |
|
|
282
|
+
|---|---|
|
|
283
|
+
| `Voice2VoiceError` | Base SDK exception. |
|
|
284
|
+
| `APIError` | Unsuccessful HTTP response; includes `status_code`, `message`, and `detail`. |
|
|
285
|
+
| `AuthenticationError` | HTTP 401 or 403 response. |
|
|
286
|
+
| `QuotaExceededError` | HTTP 429 response with the API's `quota_exceeded` error code. |
|
|
287
|
+
|
|
288
|
+
`QuotaExceededError` also exposes quota fields when the server includes them:
|
|
289
|
+
`limit_seconds`, `used_seconds`, `remaining_seconds`, `requested_seconds`,
|
|
290
|
+
and `reset_at`.
|
|
291
|
+
|
|
292
|
+
Network and file-system errors are not converted into `APIError`. Handle
|
|
293
|
+
exceptions such as `httpx.TimeoutException`, `httpx.ConnectError`, and
|
|
294
|
+
`OSError` where appropriate.
|
|
295
|
+
|
|
296
|
+
## Development
|
|
297
|
+
|
|
298
|
+
Clone the repository:
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
git clone https://github.com/ehsanulhaqislamyar-star/voice2voice-python.git
|
|
302
|
+
cd voice2voice-python
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Install the development dependencies:
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
python -m pip install -e ".[dev]"
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Run the test suite:
|
|
312
|
+
|
|
313
|
+
```bash
|
|
314
|
+
python -m pytest
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Build the source distribution and wheel:
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
python -m build
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Validate the distributions:
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
python -m twine check dist/*
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## License
|
|
330
|
+
|
|
331
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for
|
|
332
|
+
the full license text.
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# Voice2Voice Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://www.python.org/)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
A Python SDK for the **Voice2Voice Translator API**. It provides synchronous
|
|
7
|
+
and asynchronous clients for discovering supported languages, translating
|
|
8
|
+
audio, downloading generated audio, checking visitor quotas, and managing
|
|
9
|
+
administrator quota settings.
|
|
10
|
+
|
|
11
|
+
> **Release status:** `0.1.1` — early release. The SDK interface and server API
|
|
12
|
+
> may evolve.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Synchronous client: `Voice2VoiceClient`
|
|
17
|
+
- Asynchronous client: `AsyncVoice2VoiceClient`
|
|
18
|
+
- Audio upload and translation
|
|
19
|
+
- Download translated audio
|
|
20
|
+
- Retrieve supported languages and visitor quota
|
|
21
|
+
- Optional administrator operations using a bearer token
|
|
22
|
+
- Typed result models and specific API exceptions
|
|
23
|
+
- Configurable request timeout
|
|
24
|
+
- Reusable HTTPX clients and context-manager support
|
|
25
|
+
- Persistent anonymous visitor ID when the server issues the
|
|
26
|
+
`vt_visitor_id` cookie
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- Python 3.9 or newer
|
|
31
|
+
- Access to a running Voice2Voice API server
|
|
32
|
+
- `httpx` (installed automatically)
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m pip install voice2voice
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick start
|
|
41
|
+
|
|
42
|
+
### Synchronous client
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError
|
|
46
|
+
|
|
47
|
+
with Voice2VoiceClient() as client:
|
|
48
|
+
languages = client.languages()
|
|
49
|
+
print([(language.code, language.name) for language in languages])
|
|
50
|
+
|
|
51
|
+
print("Quota:", client.quota())
|
|
52
|
+
|
|
53
|
+
result = client.translate(
|
|
54
|
+
file="sample.mp3",
|
|
55
|
+
source_language="en",
|
|
56
|
+
target_language="es",
|
|
57
|
+
output_format="mp3",
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
print("Recognized:", result.recognized_text)
|
|
61
|
+
print("Cleaned:", result.cleaned_text)
|
|
62
|
+
print("Translated:", result.translated_text)
|
|
63
|
+
|
|
64
|
+
audio = client.download_audio(result)
|
|
65
|
+
with open(audio.filename, "wb") as output_file:
|
|
66
|
+
output_file.write(audio.content)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Handle API-specific errors when needed:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from voice2voice import Voice2VoiceClient, APIError, QuotaExceededError
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
with Voice2VoiceClient() as client:
|
|
76
|
+
result = client.translate(
|
|
77
|
+
file="sample.mp3",
|
|
78
|
+
source_language="en",
|
|
79
|
+
target_language="es",
|
|
80
|
+
)
|
|
81
|
+
except QuotaExceededError as exc:
|
|
82
|
+
print("Quota exceeded:", exc.message)
|
|
83
|
+
print("Remaining seconds:", exc.remaining_seconds)
|
|
84
|
+
except APIError as exc:
|
|
85
|
+
print(f"API request failed ({exc.status_code}): {exc.message}")
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Asynchronous client
|
|
89
|
+
|
|
90
|
+
Use `async with` to close the asynchronous HTTPX client reliably:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
import asyncio
|
|
94
|
+
from voice2voice import AsyncVoice2VoiceClient
|
|
95
|
+
|
|
96
|
+
async def main():
|
|
97
|
+
async with AsyncVoice2VoiceClient() as client:
|
|
98
|
+
languages = await client.languages()
|
|
99
|
+
print([(language.code, language.name) for language in languages])
|
|
100
|
+
|
|
101
|
+
result = await client.translate(
|
|
102
|
+
file="sample.mp3",
|
|
103
|
+
source_language="en",
|
|
104
|
+
target_language="es",
|
|
105
|
+
output_format="mp3",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
print("Translated:", result.translated_text)
|
|
109
|
+
|
|
110
|
+
audio = await client.download_audio(result)
|
|
111
|
+
with open(audio.filename, "wb") as output_file:
|
|
112
|
+
output_file.write(audio.content)
|
|
113
|
+
|
|
114
|
+
asyncio.run(main())
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Configuration
|
|
118
|
+
|
|
119
|
+
Both clients accept the following configuration options:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from voice2voice import Voice2VoiceClient
|
|
123
|
+
|
|
124
|
+
client = Voice2VoiceClient(
|
|
125
|
+
base_url="https://your-api.example.com",
|
|
126
|
+
timeout=120.0,
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
| Option | Description |
|
|
131
|
+
|---|---|
|
|
132
|
+
| `base_url` | Base URL of the Voice2Voice API. Defaults to the hosted API configured by the SDK. |
|
|
133
|
+
| `visitor_id` | Optional previously obtained anonymous visitor ID. |
|
|
134
|
+
| `admin_token` | Bearer token used by administrator methods. |
|
|
135
|
+
| `timeout` | HTTPX request timeout in seconds. Set to `None` to disable the timeout. |
|
|
136
|
+
| `http_client` | Optional existing `httpx.Client`. The async client accepts an `httpx.AsyncClient`. |
|
|
137
|
+
|
|
138
|
+
When an external HTTPX client is injected, the SDK does **not** close it.
|
|
139
|
+
The caller remains responsible for managing its lifecycle. Requests use the
|
|
140
|
+
SDK's configured `base_url`, independently of the injected HTTPX client's
|
|
141
|
+
`base_url`.
|
|
142
|
+
|
|
143
|
+
Example with an externally managed HTTPX client:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
import httpx
|
|
147
|
+
from voice2voice import Voice2VoiceClient
|
|
148
|
+
|
|
149
|
+
transport = httpx.Client(timeout=45.0)
|
|
150
|
+
|
|
151
|
+
try:
|
|
152
|
+
with Voice2VoiceClient(
|
|
153
|
+
base_url="https://your-api.example.com",
|
|
154
|
+
http_client=transport,
|
|
155
|
+
) as client:
|
|
156
|
+
print(client.languages())
|
|
157
|
+
finally:
|
|
158
|
+
transport.close()
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Anonymous visitor identity
|
|
162
|
+
|
|
163
|
+
The API server may issue a `vt_visitor_id` cookie to identify an anonymous
|
|
164
|
+
visitor and track that visitor's quota. The SDK exposes the current value:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
visitor_id = client.visitor_id
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
When the server issues this cookie, the SDK attempts to persist it at:
|
|
171
|
+
|
|
172
|
+
```text
|
|
173
|
+
~/.voice2voice/visitor.json
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The SDK continues working if the file cannot be read or written. You can also
|
|
177
|
+
pass `visitor_id=...` explicitly to restore an identity.
|
|
178
|
+
|
|
179
|
+
Treat visitor IDs as private. Do not publish them, commit them to source
|
|
180
|
+
control, or expose them in logs.
|
|
181
|
+
|
|
182
|
+
## Administrator operations
|
|
183
|
+
|
|
184
|
+
Administrator methods require a valid server-issued admin token:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from voice2voice import Voice2VoiceClient
|
|
188
|
+
|
|
189
|
+
with Voice2VoiceClient(admin_token="YOUR_ADMIN_TOKEN") as client:
|
|
190
|
+
current = client.admin_quota()
|
|
191
|
+
print(current)
|
|
192
|
+
|
|
193
|
+
updated = client.update_admin_quota(
|
|
194
|
+
quota_seconds=180,
|
|
195
|
+
period_type="daily",
|
|
196
|
+
)
|
|
197
|
+
print(updated)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Keep the token in an environment variable or secret manager rather than
|
|
201
|
+
hard-coding it.
|
|
202
|
+
|
|
203
|
+
The async client provides the equivalent methods:
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
current = await client.admin_quota()
|
|
207
|
+
|
|
208
|
+
updated = await client.update_admin_quota(
|
|
209
|
+
quota_seconds=180,
|
|
210
|
+
period_type="daily",
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
The accepted `period_type` values and quota-update semantics are defined by
|
|
215
|
+
the API server. Check the server's current documentation before changing
|
|
216
|
+
administrator settings.
|
|
217
|
+
|
|
218
|
+
## API methods
|
|
219
|
+
|
|
220
|
+
| Method | Description |
|
|
221
|
+
|---|---|
|
|
222
|
+
| `languages()` | Return the languages advertised by the API. |
|
|
223
|
+
| `translate(file, source_language, target_language, output_format="mp3")` | Upload audio and return translation data. |
|
|
224
|
+
| `quota()` | Retrieve the current visitor's quota information. |
|
|
225
|
+
| `download_audio(result_or_url)` | Download generated audio and return an `AudioDownload`. |
|
|
226
|
+
| `admin_quota()` | Retrieve administrator quota settings. |
|
|
227
|
+
| `update_admin_quota(quota_seconds, period_type, custom_period_seconds=86400)` | Update administrator quota settings. |
|
|
228
|
+
|
|
229
|
+
For `AsyncVoice2VoiceClient`, use `await` with each method. It also provides
|
|
230
|
+
`aclose()` and asynchronous context-manager support.
|
|
231
|
+
|
|
232
|
+
Language codes, supported audio formats, upload limits, and quota behavior
|
|
233
|
+
are determined by the API server. Retrieve the current language list with
|
|
234
|
+
`client.languages()` and consult the server's current API documentation for
|
|
235
|
+
accepted formats and limits.
|
|
236
|
+
|
|
237
|
+
## Return models
|
|
238
|
+
|
|
239
|
+
- `Language`: `name`, `code`
|
|
240
|
+
- `TranslationResult`: `recognized_text`, `cleaned_text`, `translated_text`,
|
|
241
|
+
`audio_url`, `audio_filename`, and the original response dictionary in `raw`
|
|
242
|
+
- `AudioDownload`: downloaded `content` (`bytes`), `content_type`, and
|
|
243
|
+
`filename`
|
|
244
|
+
|
|
245
|
+
These models are immutable dataclasses.
|
|
246
|
+
|
|
247
|
+
## Error handling
|
|
248
|
+
|
|
249
|
+
All SDK-specific exceptions inherit from `Voice2VoiceError`.
|
|
250
|
+
|
|
251
|
+
| Exception | Meaning |
|
|
252
|
+
|---|---|
|
|
253
|
+
| `Voice2VoiceError` | Base SDK exception. |
|
|
254
|
+
| `APIError` | Unsuccessful HTTP response; includes `status_code`, `message`, and `detail`. |
|
|
255
|
+
| `AuthenticationError` | HTTP 401 or 403 response. |
|
|
256
|
+
| `QuotaExceededError` | HTTP 429 response with the API's `quota_exceeded` error code. |
|
|
257
|
+
|
|
258
|
+
`QuotaExceededError` also exposes quota fields when the server includes them:
|
|
259
|
+
`limit_seconds`, `used_seconds`, `remaining_seconds`, `requested_seconds`,
|
|
260
|
+
and `reset_at`.
|
|
261
|
+
|
|
262
|
+
Network and file-system errors are not converted into `APIError`. Handle
|
|
263
|
+
exceptions such as `httpx.TimeoutException`, `httpx.ConnectError`, and
|
|
264
|
+
`OSError` where appropriate.
|
|
265
|
+
|
|
266
|
+
## Development
|
|
267
|
+
|
|
268
|
+
Clone the repository:
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
git clone https://github.com/ehsanulhaqislamyar-star/voice2voice-python.git
|
|
272
|
+
cd voice2voice-python
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Install the development dependencies:
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
python -m pip install -e ".[dev]"
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Run the test suite:
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
python -m pytest
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Build the source distribution and wheel:
|
|
288
|
+
|
|
289
|
+
```bash
|
|
290
|
+
python -m build
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Validate the distributions:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
python -m twine check dist/*
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## License
|
|
300
|
+
|
|
301
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for
|
|
302
|
+
the full license text.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Minimal asynchronous Voice2Voice SDK example."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
from voice2voice import AsyncVoice2VoiceClient
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
async def main() -> None:
|
|
9
|
+
async with AsyncVoice2VoiceClient() as client:
|
|
10
|
+
languages = await client.languages()
|
|
11
|
+
print("Supported languages:")
|
|
12
|
+
for language in languages:
|
|
13
|
+
print(f"{language.code} - {language.name}")
|
|
14
|
+
|
|
15
|
+
result = await client.translate(
|
|
16
|
+
file="sample.mp3",
|
|
17
|
+
source_language="en",
|
|
18
|
+
target_language="es",
|
|
19
|
+
output_format="mp3",
|
|
20
|
+
)
|
|
21
|
+
print("Recognized:", result.recognized_text)
|
|
22
|
+
print("Translated:", result.translated_text)
|
|
23
|
+
|
|
24
|
+
audio = await client.download_audio(result)
|
|
25
|
+
output_path = audio.filename or "translated-audio.mp3"
|
|
26
|
+
with open(output_path, "wb") as output_file:
|
|
27
|
+
output_file.write(audio.content)
|
|
28
|
+
print("Saved audio to:", output_path)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if __name__ == "__main__":
|
|
32
|
+
asyncio.run(main())
|