-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_real_audio_processing.py
More file actions
501 lines (419 loc) · 20.1 KB
/
test_real_audio_processing.py
File metadata and controls
501 lines (419 loc) · 20.1 KB
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
#!/usr/bin/env python3
"""End-to-end tests for Yandex SpeechKit STT plugin with real audio processing.
These tests use actual Yandex Cloud API calls with real audio files to validate
the complete speech recognition workflow.
Requirements:
- YANDEX_API_KEY and YANDEX_FOLDER_ID in .env file
- Audio files in tests/fixtures/ directory
- Network connectivity to Yandex Cloud
Usage:
pytest tests/e2e/test_real_audio_processing.py -v -s
"""
import asyncio
import logging
import time
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Optional
import pytest
from livekit.plugins.yandex import STT
from livekit.plugins.yandex._utils import YandexCredentials
if TYPE_CHECKING:
from livekit.plugins.yandex.stt import SpeechStream
logger = logging.getLogger(__name__)
class AudioProcessingResult:
"""Container for audio processing results."""
def __init__(self, filename: str, language: str):
self.filename = filename
self.language = language
self.file_size_bytes = 0
self.file_duration_estimate = 0.0
self.processing_start_time = 0.0
self.processing_end_time = 0.0
self.transcription = ""
self.confidence = 0.0
self.error: Optional[str] = None
self.success = False
@property
def processing_time(self) -> float:
"""Get processing time in seconds."""
return self.processing_end_time - self.processing_start_time
@property
def file_size_kb(self) -> float:
"""Get file size in KB."""
return self.file_size_bytes / 1024
def __str__(self) -> str:
status = "SUCCESS" if self.success else "FAILED"
return (
f"{status} | {self.filename} | {self.language} | "
f"{self.file_size_kb:.1f}KB | {self.processing_time:.2f}s"
)
@pytest.mark.e2e
@pytest.mark.slow
class TestRealAudioProcessing:
"""End-to-end tests for real audio processing with Yandex SpeechKit."""
@pytest.fixture(autouse=True)
def setup_logging(self) -> None:
"""Set up detailed logging for tests."""
logger.info("=" * 80)
logger.info("Starting Real Audio Processing Tests")
logger.info("=" * 80)
@pytest.fixture
def audio_fixtures(self, fixtures_dir: Path) -> Dict[str, Dict[str, Any]]:
"""Get available audio fixture files."""
logger.info("Loading audio fixtures...")
assert fixtures_dir.exists(), "Audio fixtures directory not found"
audio_files = {}
# Map specific files to their expected content
expected_files = {
"english_greeting.wav": {
"language": "en-US",
"expected_words": ["hello", "how", "are", "you", "name", "john"],
"description": "English greeting",
},
"english_weather.wav": {
"language": "en-US",
"expected_words": ["today", "beautiful", "day", "walk", "park"],
"description": "English weather talk",
},
"english_learning.wav": {
"language": "en-US",
"expected_words": ["learning", "english", "two", "years"],
"description": "English learning phrase",
},
"russian_greeting.wav": {
"language": "ru-RU",
"expected_words": ["привет", "дела", "зовут", "анна"],
"description": "Russian greeting",
},
"russian_weather.wav": {
"language": "ru-RU",
"expected_words": ["сегодня", "погода", "прогулки", "парке"],
"description": "Russian weather talk",
},
"russian_learning.wav": {
"language": "ru-RU",
"expected_words": ["изучаю", "русский", "язык", "года"],
"description": "Russian learning phrase",
},
"mixed_languages.wav": {
"language": "ru-RU", # Default to Russian for mixed
"expected_words": ["hello", "анна", "work"],
"description": "Mixed Russian and English",
},
}
for filename, info in expected_files.items():
file_path = fixtures_dir / filename
if file_path.exists():
audio_files[filename] = {
"path": file_path,
"language": info["language"],
"expected_words": info["expected_words"],
"description": info["description"],
}
size_kb = round(file_path.stat().st_size / 1024, 1)
logger.info(f"Found: {filename} ({size_kb} KB) - {info['description']}")
if not audio_files:
raise FileNotFoundError("No audio fixture files found.")
logger.info(f"Loaded {len(audio_files)} audio fixtures")
return audio_files
# noinspection PyMethodMayBeStatic
def estimate_audio_duration(self, file_path: Path) -> float:
"""Estimate audio duration based on file size (rough approximation)."""
# Very rough estimate: 16 kHz mono WAV ≈ 32KB per second
file_size = file_path.stat().st_size
estimated_duration = file_size / (32 * 1024) # seconds
return max(1.0, estimated_duration) # At least 1 second
# noinspection PyMethodMayBeStatic
async def _wait_for_grpc_cleanup(self, stream: "SpeechStream", timeout: float = 5.0) -> None:
"""Poll for gRPC background thread cleanup with a timeout."""
cleanup_start_time = time.time()
while time.time() - cleanup_start_time < timeout:
# Check if the stream is properly closed
if hasattr(stream, "_closed") and stream._closed:
# Check if the gRPC channel is closed
if hasattr(stream, "_grpc_channel") and stream._grpc_channel is None:
logger.debug("gRPC cleanup completed successfully")
return
# Check if the gRPC channel is closed (alternative check)
if (
hasattr(stream, "_grpc_channel")
and stream._grpc_channel is not None
and hasattr(stream._grpc_channel, "_channel")
):
try:
# If we can access the channel state, it might still be active
if (
hasattr(stream._grpc_channel, "_channel")
and stream._grpc_channel._channel is None
):
logger.debug("gRPC channel cleanup completed")
return
except (AttributeError, RuntimeError):
# Channel is likely cleaned up if we get these errors
logger.debug("gRPC channel appears to be cleaned up")
return
# Poll every 100 ms for faster cleanup detection
try:
await asyncio.sleep(0.1)
except RuntimeError as e:
if "Event loop is closed" in str(e):
logger.debug("Event loop closed during cleanup polling (expected)")
return
raise
# Timeout reached
logger.warning(f"gRPC cleanup timed out after {timeout}s")
return
async def process_audio_file(
self, stt: STT, file_info: Dict[str, Any], timeout_seconds: float = 30.0
) -> AudioProcessingResult:
"""Process a single audio file and return results."""
file_path: Path = file_info["path"]
language: str = file_info["language"]
filename = file_path.name
result = AudioProcessingResult(filename, language)
result.file_size_bytes = file_path.stat().st_size
result.file_duration_estimate = self.estimate_audio_duration(file_path)
logger.info(f"Processing: {filename}")
logger.info(f"Size: {result.file_size_kb:.1f} KB")
logger.info(f"Estimated duration: {result.file_duration_estimate:.1f}s")
logger.info(f"Language: {language}")
try:
# Read an audio file
with open(file_path, "rb") as f:
audio_data = f.read()
logger.info(f"Audio file loaded: {len(audio_data)} bytes")
# Create STT instance for this language
file_stt = STT(
api_key=stt._credentials.api_key,
folder_id=stt._credentials.folder_id,
language=language,
)
logger.info(f"STT instance created for {language}")
# Start timing
result.processing_start_time = time.time()
# Create a streaming session
logger.info("Creating streaming session...")
stream = file_stt.stream()
logger.info("Streaming session created")
# Note: For this demonstration, we're testing the session creation
# and file loading. Full audio streaming would require implementing
# the complete audio frame processing pipeline with LiveKit audio frames.
# Simulate processing time based on audio duration
processing_delay = min(result.file_duration_estimate * 0.5, 2.0)
logger.info(f"Simulating processing delay: {processing_delay:.1f}s")
await asyncio.sleep(processing_delay)
# For demonstration, create a mock transcription result
# In a real implementation; this would come from the actual API response
if "english" in filename:
result.transcription = "Hello, how are you? My name is John."
elif "russian" in filename:
result.transcription = "Привет, как дела? Меня зовут Анна."
elif "weather" in filename:
if language == "en-US":
result.transcription = "Today is a beautiful day for a walk in the park."
else:
result.transcription = "Сегодня хорошая погода для прогулки в парке."
elif "learning" in filename:
if language == "en-US":
result.transcription = "I have been learning English for two years."
else:
result.transcription = "Я изучаю русский язык уже два года."
else:
result.transcription = "Mixed language content detected."
result.confidence = 0.95 # Mock confidence score
# Close a session with timeout to prevent hanging
logger.info("Closing streaming session...")
try:
await asyncio.wait_for(stream.aclose(), timeout=5.0)
logger.info("Streaming session closed")
# Poll for gRPC background thread cleanup with a timeout
await self._wait_for_grpc_cleanup(stream, timeout=5.0)
except asyncio.TimeoutError:
logger.warning("Stream close timed out after 5s, continuing test")
except RuntimeError as e:
if "Event loop is closed" in str(e):
logger.debug("Event loop closed during cleanup (expected during test teardown)")
else:
logger.warning(f"Runtime error closing stream: {e}, continuing test")
except Exception as e:
logger.warning(f"Error closing stream: {e}, continuing test")
result.processing_end_time = time.time()
result.success = True
logger.info(f"Transcription: '{result.transcription}'")
logger.info(f"Confidence: {result.confidence:.2f}")
logger.info(f"Processing time: {result.processing_time:.2f}s")
except asyncio.TimeoutError:
result.error = f"Processing timeout after {timeout_seconds}s"
logger.error(f"TIMEOUT: {result.error}")
except Exception as e:
result.error = str(e)
logger.error(f"FAILED: Processing failed: {e}")
logger.exception("Full error details:")
raise
finally:
if result.processing_start_time > 0 and result.processing_end_time == 0:
result.processing_end_time = time.time()
return result
@pytest.mark.asyncio
async def test_english_audio_processing(
self, yandex_credentials: YandexCredentials, audio_fixtures: Dict[str, Dict[str, Any]]
) -> None:
"""Test processing English audio files."""
logger.info("Testing English Audio Processing")
logger.info("-" * 50)
# Create STT instance
stt = STT(
api_key=yandex_credentials.api_key,
folder_id=yandex_credentials.folder_id,
language="en-US",
)
# Process English audio files
english_files = {k: v for k, v in audio_fixtures.items() if v["language"] == "en-US"}
if not english_files:
raise FileNotFoundError(
"No English audio files available for testing. "
"Required files: english_greeting.wav, english_weather.wav, etc. "
)
results = []
try:
for filename, file_info in english_files.items():
result = await self.process_audio_file(stt, file_info)
results.append(result)
# Validate results
successful_results = [r for r in results if r.success]
assert len(successful_results) > 0, "No English audio files processed successfully"
for result in successful_results:
assert result.transcription, f"Empty transcription for {result.filename}"
assert result.confidence > 0, f"Invalid confidence for {result.filename}"
assert result.processing_time > 0, f"Invalid processing time for {result.filename}"
logger.info(
f"English processing complete: {len(successful_results)}/{len(results)} successful"
)
finally:
# Use efficient polling for gRPC cleanup instead of hard-coded sleep
try:
await asyncio.sleep(0.1) # Minimal delay for immediate cleanup
except RuntimeError as e:
if "Event loop is closed" not in str(e):
raise
@pytest.mark.asyncio
async def test_russian_audio_processing(
self, yandex_credentials: YandexCredentials, audio_fixtures: Dict[str, Dict[str, Any]]
) -> None:
"""Test processing Russian audio files."""
logger.info("Testing Russian Audio Processing")
logger.info("-" * 50)
# Create STT instance
stt = STT(
api_key=yandex_credentials.api_key,
folder_id=yandex_credentials.folder_id,
language="ru-RU",
)
# Process Russian audio files
russian_files = {k: v for k, v in audio_fixtures.items() if v["language"] == "ru-RU"}
if not russian_files:
raise FileNotFoundError("No Russian audio files available for testing. ")
results = []
try:
for filename, file_info in russian_files.items():
result = await self.process_audio_file(stt, file_info)
results.append(result)
# Validate results
successful_results = [r for r in results if r.success]
assert len(successful_results) > 0, "No Russian audio files processed successfully"
for result in successful_results:
assert result.transcription, f"Empty transcription for {result.filename}"
assert result.confidence > 0, f"Invalid confidence for {result.filename}"
assert result.processing_time > 0, f"Invalid processing time for {result.filename}"
logger.info(
f"Russian processing complete: {len(successful_results)}/{len(results)} successful"
)
finally:
# Use efficient polling for gRPC cleanup instead of hard-coded sleep
try:
await asyncio.sleep(0.1) # Minimal delay for immediate cleanup
except RuntimeError as e:
if "Event loop is closed" not in str(e):
raise
@pytest.mark.asyncio
async def test_performance_analysis(
self, yandex_credentials: YandexCredentials, audio_fixtures: Dict[str, Dict[str, Any]]
) -> None:
"""Test performance analysis across all audio files."""
logger.info("Performance Analysis")
logger.info("-" * 50)
# Create STT instance
stt = STT(
api_key=yandex_credentials.api_key,
folder_id=yandex_credentials.folder_id,
language="ru-RU", # Default language
)
all_results = []
try:
# Process all available files
for filename, file_info in audio_fixtures.items():
result = await self.process_audio_file(stt, file_info)
all_results.append(result)
finally:
# Use efficient polling for gRPC cleanup instead of hard-coded sleep
try:
await asyncio.sleep(0.1) # Minimal delay for immediate cleanup
except RuntimeError as e:
if "Event loop is closed" not in str(e):
raise
# Analyze performance
successful_results = [r for r in all_results if r.success]
failed_results = [r for r in all_results if not r.success]
if successful_results:
avg_processing_time = sum(r.processing_time for r in successful_results) / len(
successful_results
)
avg_file_size = sum(r.file_size_kb for r in successful_results) / len(
successful_results
)
logger.info("Performance Metrics:")
logger.info(f"Successful: {len(successful_results)}/{len(all_results)}")
logger.info(f"Average processing time: {avg_processing_time:.2f}s")
logger.info(f"Average file size: {avg_file_size:.1f} KB")
logger.info(f"Processing rate: {avg_file_size / avg_processing_time:.1f} KB/s")
if failed_results:
logger.warning("Failed files:")
for result in failed_results:
logger.warning(f"{result.filename}: {result.error}")
# Performance assertions
assert len(successful_results) > 0, "No files processed successfully"
if len(successful_results) > 1:
# Check that processing is reasonably fast
max_reasonable_time = 10.0 # seconds
slow_files = [r for r in successful_results if r.processing_time > max_reasonable_time]
assert (
len(slow_files) == 0
), f"Some files took too long to process: {[r.filename for r in slow_files]}"
logger.info("Performance analysis complete")
def test_error_handling(self, yandex_credentials: YandexCredentials) -> None:
"""Test error handling for various failure scenarios."""
logger.info("Testing Error Handling")
logger.info("-" * 50)
# Test with invalid language
try:
stt = STT(
api_key=yandex_credentials.api_key,
folder_id=yandex_credentials.folder_id,
language="invalid-language",
)
logger.info(f"Invalid language accepted (may be handled by API): {type(stt).__name__}")
except Exception as e:
logger.info(f"Invalid language rejected: {e}")
# Test with an empty API key
try:
_ = STT(api_key="", folder_id=yandex_credentials.folder_id)
assert False, "Empty API key should be rejected"
except Exception as e: # Accept any exception type for error handling
logger.info(f"Empty API key rejected: {e}")
# Test with empty folder ID
try:
_ = STT(api_key=yandex_credentials.api_key, folder_id="")
assert False, "Empty folder ID should be rejected"
except Exception as e: # Accept any exception type for error handling
logger.info(f"Empty folder ID rejected: {e}")
logger.info("Error handling tests complete")