File size: 11,068 Bytes
d00203b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# VoiceForge API Documentation
## Base URL
- Development: `http://localhost:8000`
- Production: `https://api.voiceforge.example.com`
## Authentication
Most endpoints require authentication via JWT Bearer token.
```http
Authorization: Bearer <token>
```
---
## Endpoints
### Authentication
#### POST /api/v1/auth/register
Register a new user.
**Request:**
```json
{
"email": "user@example.com",
"password": "strongpassword123",
"name": "Jane Doe"
}
```
**Response:**
```json
{
"id": 1,
"email": "user@example.com",
"name": "Jane Doe",
"created_at": "2024-01-01T10:00:00"
}
```
#### POST /api/v1/auth/login
Login to get a JWT token.
**Request (Form Data):**
- username: user@example.com
- password: strongpassword123
**Response:**
```json
{
"access_token": "ey...",
"token_type": "bearer"
}
```
#### GET /api/v1/auth/me
Get current user profile.
---
### Endpoints
### Health Check
#### GET /health
Check if the API is running.
**Response:**
```json
{
"status": "healthy",
"service": "voiceforge-api",
"version": "1.0.0"
}
```
#### GET /health/memory
Get current memory usage and loaded models.
**Response:**
```json
{
"memory_mb": 1523.4,
"loaded_models": ["distil-small.en", "small"],
"models_detail": {
"distil-small.en": {"loaded": true, "idle_seconds": 45.2}
}
}
```
#### POST /health/memory/cleanup
Unload idle models (inactive > 5 minutes) to free memory.
#### POST /health/memory/unload-all
Unload ALL models to free maximum memory (~1GB reduction).
---
### WebSocket Endpoints
#### WS /api/v1/ws/tts/{client_id}
Real-time TTS streaming via WebSocket (ultra-low latency).
**Protocol:**
- **Client sends:** JSON `{"text": "...", "voice": "...", "rate": "+0%", "pitch": "+0Hz"}`
- **Server sends:** Binary audio chunks followed by JSON `{"status": "complete", "ttfb_ms": 150}`
**Expected TTFB:** <500ms
---
### Speech-to-Text
#### GET /api/v1/stt/languages
Get list of supported languages.
**Response:**
```json
{
"languages": [
{
"code": "en-US",
"name": "English (US)",
"native_name": "English",
"flag": "🇺🇸",
"stt_supported": true,
"tts_supported": true
}
],
"total": 10
}
```
#### POST /api/v1/stt/upload
Transcribe an uploaded audio file.
**Request:**
- Content-Type: `multipart/form-data`
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| file | file | Yes | Audio file (WAV, MP3, M4A, FLAC, OGG) |
| language | string | No | Language code (default: en-US) |
| enable_punctuation | boolean | No | Add punctuation (default: true) |
| enable_word_timestamps | boolean | No | Include word timing (default: true) |
| enable_diarization | boolean | No | Speaker detection (default: false) |
| speaker_count | integer | No | Expected speakers (2-10) |
**Response:**
```json
{
"id": 1,
"text": "Hello, world. This is a test transcription.",
"segments": [
{
"text": "Hello, world.",
"start_time": 0.0,
"end_time": 1.5,
"speaker": null,
"confidence": 0.95
}
],
"words": [
{
"word": "Hello",
"start_time": 0.0,
"end_time": 0.5,
"confidence": 0.98
}
],
"language": "en-US",
"confidence": 0.95,
"duration": 3.5,
"word_count": 7,
"processing_time": 1.23
}
```
#### POST /api/v1/stt/upload/quality
High-quality transcription mode (optimized for accuracy).
**Parameters (form-data):**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| file | file | Yes | Audio file |
| language | string | No | Language code (default: en-US) |
| preprocess | boolean | No | Apply noise reduction (default: false) |
**Features:**
- beam_size=5 for more accurate decoding (~40% fewer errors)
- condition_on_previous_text=False to reduce hallucinations
- Optional audio preprocessing for noisy environments
**Response:** Same as `/upload`
---
#### POST /api/v1/stt/upload/batch
Batch transcription for high throughput (2-3x speedup).
**Parameters (form-data):**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| files | file[] | Yes | Multiple audio files |
| language | string | No | Language code (default: en-US) |
| batch_size | integer | No | Batch size (default: 8) |
**Response:**
```json
{
"count": 3,
"results": [
{"filename": "audio1.mp3", "text": "...", "processing_time": 2.1},
{"filename": "audio2.mp3", "text": "...", "processing_time": 1.8}
],
"mode": "batched",
"batch_size": 8
}
```
```
#### POST /api/v1/stt/upload/diarize
Perform Speaker Diarization ("Who said what") on an audio file.
**Requirements:**
- `HF_TOKEN` must be set in `.env` (Hugging Face Token for pyannote model access)
- Uses `faster-whisper` for transcription + `pyannote.audio` for speaker identification
**Parameters (form-data):**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| file | file | Yes | Audio file |
| num_speakers | integer | No | Exact number of speakers (optional) |
| min_speakers | integer | No | Min expected speakers (optional) |
| max_speakers | integer | No | Max expected speakers (optional) |
| language | string | No | Language code, e.g. 'en' (auto-detected if not provided) |
**Response:**
```json
{
"segments": [
{
"start": 0.0,
"end": 0.84,
"text": "Hello Test",
"speaker": "SPEAKER_00"
}
],
"speaker_stats": {
"SPEAKER_00": 0.84
},
"language": "en",
"status": "success"
}
```
### Transcripts & Analysis
#### GET /api/v1/transcripts
List all past transcriptions.
**Response:**
```json
[
{
"id": 1,
"text": "Hello world...",
"created_at": "2024-01-01T12:00:00",
"word_count": 150,
"language": "en-US"
}
]
```
#### POST /api/v1/transcripts/{id}/analyze
Run NLP analysis (Sentiment, Keywords, Summary) on a transcript.
**Response:**
```json
{
"status": "success",
"analysis": {
"sentiment": {"polarity": 0.5, "subjectivity": 0.1},
"keywords": ["artificial intelligence", "voice", "app"],
"summary": "This is a summary of the transcript."
}
}
```
#### GET /api/v1/transcripts/{id}/export
Download transcript in a specific format.
**Query Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| format | string | Yes | txt, srt, vtt, pdf |
**Response:**
- File download (text/plain, text/vtt, application/pdf)
---
### Text-to-Speech
#### GET /api/v1/tts/voices
Get all available voices.
**Query Parameters:**
| Parameter | Type | Description |
|-----------|------|-------------|
| language | string | Filter by language code |
**Response:**
```json
{
"voices": [
{
"name": "en-US-Wavenet-D",
"language_code": "en-US",
"language_name": "English (US)",
"ssml_gender": "MALE",
"natural_sample_rate": 24000,
"voice_type": "WaveNet",
"display_name": "D (Male, WaveNet)",
"flag": "🇺🇸"
}
],
"total": 50,
"language_filter": null
}
```
#### GET /api/v1/tts/voices/{language}
Get voices for a specific language.
**Parameters:**
| Parameter | Type | Description |
|-----------|------|-------------|
| language | path | Language code (e.g., en-US) |
#### POST /api/v1/tts/synthesize
Convert text to speech.
**Request:**
```json
{
"text": "Hello, this is a test.",
"language": "en-US",
"voice": "en-US-Wavenet-D",
"audio_encoding": "MP3",
"speaking_rate": 1.0,
"pitch": 0.0,
"volume_gain_db": 0.0,
"use_ssml": false
}
```
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| text | string | Yes | Text to synthesize (max 5000 chars) |
| language | string | No | Language code (default: en-US) |
| voice | string | No | Voice name |
| audio_encoding | string | No | MP3, LINEAR16, OGG_OPUS |
| speaking_rate | float | No | Speed 0.25-4.0 (default: 1.0) |
| pitch | float | No | Pitch -20 to 20 (default: 0.0) |
| volume_gain_db | float | No | Volume -96 to 16 dB |
| use_ssml | boolean | No | Treat as SSML markup |
**Response:**
```json
{
"audio_content": "<base64 encoded audio>",
"audio_size": 12345,
"duration_estimate": 2.5,
"voice_used": "en-US-Wavenet-D",
"language": "en-US",
"encoding": "MP3",
"sample_rate": 24000,
"processing_time": 0.45
}
```
#### POST /api/v1/tts/synthesize/audio
Synthesize and return audio file directly.
Same request as `/synthesize`, but returns the audio file as a download.
#### POST /api/v1/tts/stream
Stream synthesized audio for immediate playback.
**Request:** Same as `/synthesize`.
**Response:** Chunked audio stream (`audio/mpeg`). Ideal for long text to reduce latency (TTFB).
#### POST /api/v1/tts/ssml
Synthesize audio using SSML for prosody control (rate, pitch, emphasis).
**Request:**
- `text`: Text to speak
- `voice`: Voice name (default: "en-US-AriaNeural")
- `rate`: Speed (e.g., "fast", "-10%")
- `pitch`: Pitch (e.g., "high", "+5Hz")
- `emphasis`: "strong", "moderate", "reduced"
- `auto_breaks`: true/false
**Response:** Audio file (`audio/mpeg`).
---
## Error Responses
All errors follow this format:
```json
{
"error": "error_type",
"message": "Human readable message",
"detail": "Additional details (debug mode only)"
}
```
### Common Error Codes
| Code | Type | Description |
|------|------|-------------|
| 400 | validation_error | Invalid request parameters |
| 401 | unauthorized | Missing or invalid auth token |
| 403 | forbidden | Insufficient permissions |
| 404 | not_found | Resource not found |
| 413 | file_too_large | Upload exceeds size limit |
| 429 | rate_limited | Too many requests |
| 500 | internal_error | Server error |
---
## Rate Limits
| Tier | Limit |
|------|-------|
| Free | 60 requests/minute |
| Pro | 600 requests/minute |
| Enterprise | Custom |
---
## Supported Audio Formats
| Format | Extension | Notes |
|--------|-----------|-------|
| WAV | .wav | Best quality, no conversion |
| MP3 | .mp3 | Common, converted |
| M4A | .m4a | iOS format |
| FLAC | .flac | Lossless |
| OGG | .ogg | Open format |
| WebM | .webm | Browser recording |
---
## Code Examples
### Python
```python
import requests
# Transcribe audio
with open("audio.wav", "rb") as f:
response = requests.post(
"http://localhost:8000/api/v1/stt/upload",
files={"file": f},
data={"language": "en-US"}
)
print(response.json()["text"])
# Synthesize speech
response = requests.post(
"http://localhost:8000/api/v1/tts/synthesize",
json={"text": "Hello world", "language": "en-US"}
)
import base64
audio = base64.b64decode(response.json()["audio_content"])
with open("output.mp3", "wb") as f:
f.write(audio)
```
### cURL
```bash
# Transcribe
curl -X POST http://localhost:8000/api/v1/stt/upload \
-F "file=@audio.wav" \
-F "language=en-US"
# Synthesize
curl -X POST http://localhost:8000/api/v1/tts/synthesize \
-H "Content-Type: application/json" \
-d '{"text": "Hello", "language": "en-US"}'
```
|