Spaces:
Sleeping
Sleeping
File size: 14,445 Bytes
24dc421 | 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 | # Integration Guide - Complete Backend
## π― Overview
This guide shows how all modules integrate together to form a complete voice-to-voice translation system.
---
## π System Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Server β
β (main.py) β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββΌβββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Security β β WebSocket β β Rooms β
β β β Server β β Manager β
β β’ Auth β β β β β
β β’ Rate Limit β β β’ Connection β β β’ Multi-room β
ββββββββββββββββ β β’ Heartbeat β β β’ Users β
ββββββββ¬ββββββββ ββββββββ¬ββββββββ
β β
ββββββββββ¬βββββββββ
β
ββββββββββΌβββββββββ
β Message β
β Router β
ββββββββββ¬βββββββββ
β
βββββββββββββββββββΌββββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Audio β β Pipeline β β Workers β
β β β Manager β β β
β β’ Buffer β β β β β’ Translationβ
β β’ Utils β β STTβTransβTTSβ β β’ TTS Pool β
β β’ Validator β β β β β
ββββββββββββββββ ββββββββ¬ββββββββ ββββββββββββββββ
β
βββββββββββββββββββΌββββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β STT β β Translation β β TTS β
β β β β β β
β β’ Vosk β β β’ Argos β β β’ Coqui β
β β’ Factory β β β’ Translator β β β’ Factory β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
```
---
## π Module Integration Flow
### 1. Client Connection Flow
```python
# Client connects via WebSocket
WebSocket Connect
β
Security Check (auth.py, rate_limiter.py)
β
Connection Manager (connection_manager.py)
β
Heartbeat Registration (heartbeat.py)
β
Connection Active
```
**Code Integration:**
```python
# In websocket_server.py
from app.security import get_auth_manager, get_connection_limiter
async def handle_connection(websocket):
# Security check
limiter = get_connection_limiter()
limiter.check_connection_limit(client_ip)
limiter.register_connection(user_id, client_ip)
# Register connection
await connection_manager.connect(user_id, websocket)
# Start heartbeat
heartbeat_manager.register(user_id)
```
---
### 2. Room Join Flow
```python
Client sends JOIN_ROOM message
β
Message Router (message_router.py)
β
Room Manager (room_manager.py)
β
User added to Room (room.py)
β
Confirmation sent to all room users
```
**Code Integration:**
```python
# In message_router.py
from app.rooms import room_manager
async def _handle_join_room(self, message, user_id, websocket):
# Add user to room
user = await room_manager.add_user_to_room(
room_id=room_id,
user_id=user_id,
name=name,
language=language
)
# Send confirmation
await connection_manager.send_to_room(room_id, message)
```
---
### 3. Audio Processing Flow
```python
Client sends AUDIO_DATA (binary)
β
Audio Buffer (audio_buffer.py)
β
PCM Validator (pcm_validator.py)
β
Pipeline Manager (pipeline_manager.py)
β
βββββββββββββββββββββββββββββββββββββββ
β Translation Pipeline β
β β
β 1. STT (vosk_engine.py) β
β β Text recognized β
β 2. Translation (argos_engine.py) β
β β Text translated β
β 3. TTS (coqui_engine.py) β
β β Audio synthesized β
βββββββββββββββββββββββββββββββββββββββ
β
Workers (translation_worker.py, tts_worker.py)
β
Send translated audio to target users
```
**Code Integration:**
```python
# In message_router.py
from app.audio import AudioBuffer, PCMValidator
from app.pipeline import get_pipeline_manager
async def _handle_audio_data(self, audio_data, user_id):
# Buffer audio
buffer = AudioBuffer(chunk_size=4096)
buffer.add(audio_data)
# Validate
validator = PCMValidator(sample_rate=16000)
if not validator.is_valid(audio_data):
return
# Process through pipeline
pipeline = get_pipeline_manager()
result = await pipeline.process_complete_audio(
audio_data=buffer.get_all(),
user_id=user_id,
source_lang=user.language,
target_lang=target_user.language
)
# Send translated audio
await connection_manager.send_binary_to_user(
target_user.id,
result.audio_data
)
```
---
## π Security Integration
### Rate Limiting
```python
from app.security import get_rate_limiter, get_connection_limiter
# In WebSocket message handler
rate_limiter = get_rate_limiter()
connection_limiter = get_connection_limiter()
# Check connection limit (on connect)
connection_limiter.check_connection_limit(ip_address)
connection_limiter.register_connection(user_id, ip_address)
# Check message rate (on each message)
connection_limiter.check_message_rate(user_id)
# Check API rate limit (on API calls)
rate_limiter.check_rate_limit(user_id)
```
### Authentication
```python
from app.security import get_auth_manager
auth_manager = get_auth_manager()
# Create token
token = auth_manager.create_access_token(user_id)
# Verify token
payload = auth_manager.verify_token(token)
user_id = auth_manager.get_user_id_from_token(token)
```
---
## π· Worker Pool Integration
### Translation Workers
```python
from app.workers import get_translation_pool, TranslationTask
# Start pool (in lifespan)
pool = get_translation_pool()
await pool.start()
# Submit task
task = TranslationTask(
task_id="task_123",
text="Hello world",
source_lang="en",
target_lang="es",
user_id="user_456",
callback=translation_callback
)
await pool.submit_task(task)
# Callback receives result
async def translation_callback(result):
print(f"Translated: {result.translated_text}")
```
### TTS Workers
```python
from app.workers import get_tts_pool, TTSTask
# Start pool
tts_pool = get_tts_pool()
await tts_pool.start()
# Submit task
task = TTSTask(
task_id="tts_123",
text="Hola mundo",
language="es",
user_id="user_456",
callback=tts_callback
)
await tts_pool.submit_task(task)
# Callback receives audio
async def tts_callback(result):
audio_bytes = result.audio_data
await send_audio_to_client(audio_bytes)
```
---
## π Complete Usage Example
```python
from fastapi import FastAPI, WebSocket
from app.config import get_settings
from app.server import WebSocketServer
from app.rooms import room_manager
from app.pipeline import get_pipeline_manager
from app.security import get_auth_manager, get_connection_limiter
from app.workers import get_translation_pool, get_tts_pool
app = FastAPI()
settings = get_settings()
@app.on_event("startup")
async def startup():
# Start worker pools
translation_pool = get_translation_pool()
await translation_pool.start()
tts_pool = get_tts_pool()
await tts_pool.start()
@app.on_event("shutdown")
async def shutdown():
# Stop worker pools
translation_pool = get_translation_pool()
await translation_pool.stop()
tts_pool = get_tts_pool()
await tts_pool.stop()
# Cleanup rooms
await room_manager.cleanup()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
# Security checks
connection_limiter = get_connection_limiter()
client_ip = websocket.client.host
try:
connection_limiter.check_connection_limit(client_ip)
except RateLimitError:
await websocket.close(code=1008, reason="Too many connections")
return
# Handle connection
ws_server = WebSocketServer()
await ws_server.handle_connection(websocket)
```
---
## π Configuration
All modules use centralized settings from `app/config/settings.py`:
```python
from app.config import get_settings
settings = get_settings()
# Audio settings
sample_rate = settings.audio_sample_rate # 16000
chunk_size = settings.audio_chunk_size # 4096
# Worker settings
translation_workers = settings.translation_workers # 4
tts_workers = settings.tts_workers # 2
# Security settings
max_connections = settings.max_connections_per_ip # 10
max_messages = settings.max_messages_per_second # 10
# Rate limiting
requests_per_min = settings.max_requests_per_minute # 100
```
---
## π§ͺ Testing Integration
```python
import pytest
from app.pipeline import get_pipeline_manager
from app.workers import get_translation_pool
@pytest.mark.asyncio
async def test_complete_pipeline():
# Initialize
pipeline = get_pipeline_manager()
pool = get_translation_pool()
await pool.start()
# Process audio
result = await pipeline.process_complete_audio(
audio_data=test_audio,
user_id="test_user",
source_lang="en",
target_lang="es"
)
# Verify
assert result.recognized_text
assert result.translated_text
assert result.audio_data
assert result.processing_time_ms < 5000
# Cleanup
await pool.stop()
```
---
## π Lifecycle Management
### Application Startup
1. Load configuration (`settings.py`)
2. Initialize logging (`logging.py`)
3. Start worker pools (`translation_worker.py`, `tts_worker.py`)
4. Initialize pipeline manager (`pipeline_manager.py`)
5. Load AI models (Vosk, Argos, Coqui)
6. Start FastAPI server (`main.py`)
### Request Handling
1. Accept WebSocket connection
2. Authenticate (if enabled)
3. Check rate limits
4. Register connection
5. Start heartbeat
6. Route messages
7. Process audio through pipeline
8. Send results to target users
### Application Shutdown
1. Stop accepting new connections
2. Close existing connections gracefully
3. Stop worker pools
4. Cleanup rooms
5. Release model resources
6. Flush logs
---
## π Monitoring Points
```python
# Connection stats
connection_manager.get_active_connections()
# Room stats
room_manager.get_room_count()
room_manager.get_all_rooms()
# Worker stats
translation_pool.get_stats()
tts_pool.get_stats()
# Rate limiter stats
rate_limiter.get_remaining_requests(user_id)
connection_limiter.get_busy_workers()
```
---
## π― Key Integration Points
1. **WebSocket β Security**: Authentication and rate limiting
2. **WebSocket β Rooms**: User management and message routing
3. **Rooms β Pipeline**: Audio processing and translation
4. **Pipeline β Workers**: Parallel processing
5. **Pipeline β Audio**: Buffering and validation
6. **All β Config**: Centralized settings
7. **All β Logging**: Structured logging
---
## β
Integration Checklist
- [x] WebSocket server handles connections
- [x] Security middleware validates and rate limits
- [x] Room manager orchestrates multi-room support
- [x] Audio module buffers and validates
- [x] Pipeline manager coordinates STTβTranslationβTTS
- [x] Worker pools enable parallel processing
- [x] All modules use centralized configuration
- [x] All modules use structured logging
- [x] Graceful startup and shutdown
- [x] Error handling throughout
---
## π Ready for Production!
All modules are integrated and work together seamlessly. The system is ready for:
- Load testing
- Performance optimization
- Production deployment
**Integration Status: β
COMPLETE**
|