File size: 21,508 Bytes
552ebb8 |
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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
#!/usr/bin/env python3
"""
π§ͺ VAD Demo - Pre-deployment Testing & Optimization Script
This script helps you test and optimize your VAD demo before deploying
to Hugging Face Spaces for your WASPAA 2025 presentation.
Usage:
python test_and_optimize.py --test-all
python test_and_optimize.py --optimize-models
python test_and_optimize.py --benchmark
"""
import sys
import time
import traceback
import argparse
import numpy as np
import torch
import psutil
import subprocess
from pathlib import Path
from typing import Dict, List, Tuple
import warnings
warnings.filterwarnings('ignore')
# ===== PERFORMANCE TESTING =====
class VADTester:
"""Comprehensive testing suite for VAD demo"""
def __init__(self):
self.test_results = {}
self.performance_metrics = {}
def test_dependencies(self) -> bool:
"""Test all required dependencies"""
print("π Testing Dependencies...")
dependencies = [
'gradio', 'numpy', 'torch', 'librosa',
'plotly', 'scipy', 'soundfile'
]
missing = []
for dep in dependencies:
try:
__import__(dep)
print(f" β
{dep}")
except ImportError:
print(f" β {dep}")
missing.append(dep)
if missing:
print(f"\nβ οΈ Missing dependencies: {missing}")
print("Run: pip install " + " ".join(missing))
return False
print("β
All dependencies available")
return True
def test_audio_generation(self) -> bool:
"""Test synthetic audio generation"""
print("\nπ΅ Testing Audio Generation...")
try:
# Generate test audio signals
sample_rate = 16000
duration = 4.0
t = np.linspace(0, duration, int(sample_rate * duration))
# Test signals
test_signals = {
'silence': np.zeros_like(t),
'noise': np.random.normal(0, 0.1, len(t)),
'tone': np.sin(2 * np.pi * 440 * t) * 0.5,
'speech_sim': np.sin(2 * np.pi * 200 * t) * np.exp(-t/2) * 0.3
}
for name, signal in test_signals.items():
if len(signal) == int(sample_rate * duration):
print(f" β
{name} signal generated")
else:
print(f" β {name} signal incorrect length")
return False
self.test_audio = test_signals
print("β
Audio generation working")
return True
except Exception as e:
print(f"β Audio generation failed: {e}")
return False
def test_model_loading(self) -> Dict[str, bool]:
"""Test individual model loading"""
print("\nπ€ Testing Model Loading...")
# Import models from main app
try:
sys.path.append('.')
from app import (OptimizedSileroVAD, OptimizedWebRTCVAD,
OptimizedEPANNs, OptimizedAST, OptimizedPANNs)
models = {
'Silero-VAD': OptimizedSileroVAD,
'WebRTC-VAD': OptimizedWebRTCVAD,
'E-PANNs': OptimizedEPANNs,
'AST': OptimizedAST,
'PANNs': OptimizedPANNs
}
results = {}
for name, model_class in models.items():
try:
start_time = time.time()
model = model_class()
load_time = time.time() - start_time
print(f" β
{name} loaded ({load_time:.2f}s)")
results[name] = True
except Exception as e:
print(f" β {name} failed: {str(e)[:50]}...")
results[name] = False
return results
except ImportError as e:
print(f"β Cannot import models from app.py: {e}")
return {}
def test_model_inference(self, model_results: Dict[str, bool]) -> Dict[str, float]:
"""Test model inference speed"""
print("\nβ‘ Testing Model Inference...")
if not hasattr(self, 'test_audio'):
print("β No test audio available")
return {}
try:
from app import (OptimizedSileroVAD, OptimizedWebRTCVAD,
OptimizedEPANNs, OptimizedAST, OptimizedPANNs)
models = {}
if model_results.get('Silero-VAD', False):
models['Silero-VAD'] = OptimizedSileroVAD()
if model_results.get('WebRTC-VAD', False):
models['WebRTC-VAD'] = OptimizedWebRTCVAD()
if model_results.get('E-PANNs', False):
models['E-PANNs'] = OptimizedEPANNs()
if model_results.get('AST', False):
models['AST'] = OptimizedAST()
if model_results.get('PANNs', False):
models['PANNs'] = OptimizedPANNs()
inference_times = {}
test_audio = self.test_audio['speech_sim']
for name, model in models.items():
try:
# Warm-up run
model.predict(test_audio[:1000])
# Benchmark runs
times = []
for _ in range(5):
start = time.time()
result = model.predict(test_audio)
times.append(time.time() - start)
avg_time = np.mean(times)
inference_times[name] = avg_time
# Check if real-time capable
is_realtime = avg_time < 4.0 # 4 second audio
status = "β
" if is_realtime else "β οΈ "
print(f" {status} {name}: {avg_time:.3f}s (RTF: {avg_time/4.0:.3f})")
except Exception as e:
print(f" β {name} inference failed: {str(e)[:50]}...")
inference_times[name] = float('inf')
return inference_times
except Exception as e:
print(f"β Inference testing failed: {e}")
return {}
def test_memory_usage(self) -> Dict[str, float]:
"""Test memory usage of models"""
print("\nπΎ Testing Memory Usage...")
try:
import gc
from app import VADDemo
# Baseline memory
gc.collect()
baseline_mb = psutil.virtual_memory().used / 1024 / 1024
# Load demo
demo = VADDemo()
gc.collect()
demo_mb = psutil.virtual_memory().used / 1024 / 1024
memory_usage = {
'baseline': baseline_mb,
'with_demo': demo_mb,
'demo_overhead': demo_mb - baseline_mb
}
print(f" π Baseline: {baseline_mb:.0f}MB")
print(f" π With Demo: {demo_mb:.0f}MB")
print(f" π Demo Overhead: {memory_usage['demo_overhead']:.0f}MB")
# Check if within HF Spaces limits (16GB)
if demo_mb < 2000: # 2GB threshold for safety
print(" β
Memory usage acceptable for HF Spaces")
else:
print(" β οΈ High memory usage - consider optimization")
return memory_usage
except Exception as e:
print(f"β Memory testing failed: {e}")
return {}
def test_gradio_interface(self) -> bool:
"""Test Gradio interface creation"""
print("\nπ¨ Testing Gradio Interface...")
try:
from app import create_interface
# Create interface (don't launch)
interface = create_interface()
if interface is not None:
print(" β
Interface created successfully")
# Check if queue is supported
try:
interface.queue(max_size=5)
print(" β
Queue support working")
except:
print(" β οΈ Queue support limited")
return True
else:
print(" β Interface creation failed")
return False
except Exception as e:
print(f"β Interface testing failed: {e}")
return False
def benchmark_full_pipeline(self) -> Dict[str, float]:
"""Benchmark complete processing pipeline"""
print("\nπ Benchmarking Full Pipeline...")
try:
from app import VADDemo
demo = VADDemo()
test_audio = self.test_audio['speech_sim']
# Simulate audio stream format
audio_input = (16000, test_audio) # (sample_rate, data)
# Benchmark complete pipeline
times = []
for i in range(3):
start = time.time()
try:
result = demo.process_audio_stream(
audio_input,
'Silero-VAD',
'E-PANNs',
0.5
)
end = time.time()
times.append(end - start)
print(f" π Run {i+1}: {end-start:.3f}s")
except Exception as e:
print(f" β Run {i+1} failed: {e}")
times.append(float('inf'))
avg_time = np.mean([t for t in times if t != float('inf')])
if avg_time < 1.0:
print(f" β
Pipeline average: {avg_time:.3f}s (excellent)")
elif avg_time < 2.0:
print(f" β
Pipeline average: {avg_time:.3f}s (good)")
else:
print(f" β οΈ Pipeline average: {avg_time:.3f}s (slow)")
return {'avg_pipeline_time': avg_time, 'all_times': times}
except Exception as e:
print(f"β Pipeline benchmarking failed: {e}")
return {}
# ===== OPTIMIZATION UTILITIES =====
class VADOptimizer:
"""Optimization utilities for VAD demo"""
def __init__(self):
pass
def optimize_torch_settings(self):
"""Optimize PyTorch for CPU inference"""
print("π§ Optimizing PyTorch Settings...")
try:
import torch
# Set CPU threads for optimal performance
cpu_count = psutil.cpu_count(logical=False)
torch.set_num_threads(min(cpu_count, 4)) # Don't exceed 4 threads
# Disable gradient computation globally
torch.set_grad_enabled(False)
# Use optimized CPU operations
if hasattr(torch.backends, 'mkldnn'):
torch.backends.mkldnn.enabled = True
print(" β
MKL-DNN enabled")
print(f" β
CPU threads set to: {torch.get_num_threads()}")
print(" β
Gradients disabled globally")
except Exception as e:
print(f"β PyTorch optimization failed: {e}")
def create_optimized_requirements(self):
"""Create optimized requirements.txt"""
print("π¦ Creating Optimized Requirements...")
optimized_requirements = """# Core dependencies - CPU optimized
gradio>=4.0.0,<5.0.0
numpy>=1.21.0,<1.25.0
torch>=2.0.0,<2.1.0
torchaudio>=2.0.0,<2.1.0
# Audio processing - optimized versions
librosa>=0.10.0,<0.11.0
soundfile>=0.12.1,<0.13.0
scipy>=1.9.0,<1.12.0
# Visualization - stable version
plotly>=5.15.0,<5.17.0
# Machine learning - pinned versions
transformers>=4.30.0,<4.35.0
datasets>=2.12.0,<2.15.0
# Optional dependencies with fallbacks
webrtcvad>=2.0.10; sys_platform != "darwin"
scikit-learn>=1.1.0,<1.4.0
# System utilities
psutil>=5.9.0
matplotlib>=3.5.0,<3.8.0
# Memory optimization
pympler>=0.9; python_version >= "3.8"
"""
try:
with open('requirements_optimized.txt', 'w') as f:
f.write(optimized_requirements)
print(" β
Optimized requirements.txt created")
# Also create packages.txt for system dependencies
system_packages = """ffmpeg
libsndfile1
libasound2-dev
portaudio19-dev
"""
with open('packages_optimized.txt', 'w') as f:
f.write(system_packages)
print(" β
System packages.txt created")
except Exception as e:
print(f"β Requirements optimization failed: {e}")
def create_deployment_config(self):
"""Create optimized deployment configuration"""
print("βοΈ Creating Deployment Config...")
# Create .gitattributes for Git LFS
gitattributes = """*.pkl filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
"""
try:
with open('.gitattributes', 'w') as f:
f.write(gitattributes)
print(" β
.gitattributes created")
# Create Dockerfile for local testing (optional)
dockerfile = """FROM python:3.10-slim
WORKDIR /app
# System dependencies
RUN apt-get update && apt-get install -y \\
ffmpeg \\
libsndfile1 \\
&& rm -rf /var/lib/apt/lists/*
# Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application
COPY . .
# Expose port
EXPOSE 7860
# Run application
CMD ["python", "app.py"]
"""
with open('Dockerfile', 'w') as f:
f.write(dockerfile)
print(" β
Dockerfile created for local testing")
except Exception as e:
print(f"β Deployment config failed: {e}")
# ===== MAIN TESTING INTERFACE =====
def run_comprehensive_test():
"""Run all tests and optimizations"""
print("π§ͺ VAD Demo - Comprehensive Testing Suite")
print("=" * 50)
tester = VADTester()
optimizer = VADOptimizer()
# Optimization first
print("\nπ§ OPTIMIZATION PHASE")
optimizer.optimize_torch_settings()
optimizer.create_optimized_requirements()
optimizer.create_deployment_config()
# Testing phase
print("\nπ§ͺ TESTING PHASE")
# Test 1: Dependencies
deps_ok = tester.test_dependencies()
if not deps_ok:
print("\nβ Critical: Fix dependencies before proceeding")
return False
# Test 2: Audio generation
audio_ok = tester.test_audio_generation()
if not audio_ok:
print("\nβ Critical: Audio processing not working")
return False
# Test 3: Model loading
model_results = tester.test_model_loading()
working_models = sum(model_results.values())
print(f"\nπ Models Working: {working_models}/5")
if working_models == 0:
print("β Critical: No models working")
return False
elif working_models < 3:
print("β οΈ Warning: Limited models available")
# Test 4: Model inference
inference_results = tester.test_model_inference(model_results)
realtime_models = sum(1 for t in inference_results.values() if t < 4.0)
print(f"\nπ Real-time Models: {realtime_models}/{len(inference_results)}")
# Test 5: Memory usage
memory_results = tester.test_memory_usage()
if memory_results:
overhead = memory_results.get('demo_overhead', 0)
if overhead > 1000: # 1GB
print("β οΈ Warning: High memory usage")
# Test 6: Interface creation
interface_ok = tester.test_gradio_interface()
if not interface_ok:
print("β Critical: Gradio interface not working")
return False
# Test 7: Full pipeline
pipeline_results = tester.benchmark_full_pipeline()
avg_time = pipeline_results.get('avg_pipeline_time', float('inf'))
# Final assessment
print("\n" + "=" * 50)
print("π FINAL ASSESSMENT")
print("=" * 50)
if deps_ok and audio_ok and interface_ok and working_models >= 2:
if avg_time < 1.0 and realtime_models >= 2:
print("π EXCELLENT - Ready for WASPAA demo!")
print("β
All systems optimal")
elif avg_time < 2.0 and realtime_models >= 1:
print("β
GOOD - Demo ready with minor optimizations")
print("π‘ Consider further model optimization")
else:
print("β οΈ ACCEPTABLE - Demo functional but slow")
print("π‘ Consider upgrading to GPU Spaces for presentation")
else:
print("β NOT READY - Critical issues need fixing")
return False
# Performance summary
print(f"\nπ Performance Summary:")
print(f" β’ Working Models: {working_models}/5")
print(f" β’ Real-time Models: {realtime_models}")
print(f" β’ Average Pipeline: {avg_time:.3f}s")
if memory_results:
print(f" β’ Memory Overhead: {memory_results.get('demo_overhead', 0):.0f}MB")
# Recommendations
print(f"\nπ‘ Recommendations:")
if working_models < 5:
print(" β’ Check model loading errors above")
if realtime_models < 3:
print(" β’ Consider model optimization or GPU upgrade")
if avg_time > 1.0:
print(" β’ Optimize audio processing pipeline")
print("\nπ Next Steps:")
print(" 1. Fix any critical issues above")
print(" 2. Use optimized files: requirements_optimized.txt")
print(" 3. Deploy to Hugging Face Spaces")
print(" 4. Test live demo URL before WASPAA")
return True
def run_quick_test():
"""Run quick essential tests only"""
print("β‘ VAD Demo - Quick Test")
print("=" * 30)
tester = VADTester()
# Essential tests only
deps_ok = tester.test_dependencies()
audio_ok = tester.test_audio_generation()
model_results = tester.test_model_loading()
working_models = sum(model_results.values())
if deps_ok and audio_ok and working_models >= 2:
print("\nβ
QUICK TEST PASSED")
print(f"Ready for deployment with {working_models} models")
return True
else:
print("\nβ QUICK TEST FAILED")
print("Run --test-all for detailed diagnosis")
return False
def main():
parser = argparse.ArgumentParser(description='VAD Demo Testing & Optimization')
parser.add_argument('--test-all', action='store_true',
help='Run comprehensive test suite')
parser.add_argument('--quick-test', action='store_true',
help='Run quick essential tests')
parser.add_argument('--optimize', action='store_true',
help='Create optimized configuration files')
parser.add_argument('--benchmark', action='store_true',
help='Run performance benchmarks only')
args = parser.parse_args()
if args.test_all:
success = run_comprehensive_test()
sys.exit(0 if success else 1)
elif args.quick_test:
success = run_quick_test()
sys.exit(0 if success else 1)
elif args.optimize:
optimizer = VADOptimizer()
optimizer.optimize_torch_settings()
optimizer.create_optimized_requirements()
optimizer.create_deployment_config()
print("β
Optimization complete")
elif args.benchmark:
tester = VADTester()
tester.test_audio_generation()
model_results = tester.test_model_loading()
inference_results = tester.test_model_inference(model_results)
pipeline_results = tester.benchmark_full_pipeline()
print("π Benchmark complete")
else:
print("Usage: python test_and_optimize.py [--test-all|--quick-test|--optimize|--benchmark]")
print("\nFor WASPAA demo preparation, run:")
print(" python test_and_optimize.py --test-all")
if __name__ == "__main__":
main() |