SpeechT5_hy / scripts /validate_optimization.py
Edmon02's picture
feat: Implement project organization plan and optimize TTS deployment
3f1840e
#!/usr/bin/env python3
"""
Quick Test and Validation Script
================================
Simple script to test the optimized TTS pipeline without full model loading.
Validates the architecture and basic functionality.
"""
import sys
import os
import time
import numpy as np
from typing import Dict, Any
# Add src to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
def test_text_processor():
"""Test text processing functionality."""
print("πŸ” Testing Text Processor...")
try:
from src.preprocessing import TextProcessor
processor = TextProcessor(max_chunk_length=100)
# Test basic processing
test_text = "Τ²Υ‘Φ€Φ‡ Υ±Υ₯Υ¦, Υ«ΥΆΥΉΥΊΥ₯՞ս Υ₯Φ„:"
processed = processor.process_text(test_text)
assert processed, "Text processing failed"
print(f" βœ… Basic processing: '{test_text}' β†’ '{processed}'")
# Test chunking
long_text = "Τ±Υ΅Υ½ Υ·Υ‘ΥΏ Υ₯Φ€Υ―Υ‘Φ€ ΥΏΥ₯Φ„Υ½ΥΏ Υ§. " * 10
chunks = processor.chunk_text(long_text)
assert len(chunks) > 1, "Chunking failed for long text"
print(f" βœ… Chunking: {len(long_text)} chars β†’ {len(chunks)} chunks")
# Test caching
stats_before = processor.get_cache_stats()
processor.process_text(test_text) # Should hit cache
stats_after = processor.get_cache_stats()
print(f" βœ… Caching: {stats_after}")
return True
except Exception as e:
print(f" ❌ Text processor test failed: {e}")
return False
def test_audio_processor():
"""Test audio processing functionality."""
print("πŸ” Testing Audio Processor...")
try:
from src.audio_processing import AudioProcessor
processor = AudioProcessor()
# Create test audio segments
segment1 = np.random.randint(-1000, 1000, 1000, dtype=np.int16)
segment2 = np.random.randint(-1000, 1000, 1000, dtype=np.int16)
# Test crossfading
result = processor.crossfade_audio_segments([segment1, segment2])
assert len(result) > len(segment1), "Crossfading failed"
print(f" βœ… Crossfading: {len(segment1)} + {len(segment2)} β†’ {len(result)} samples")
# Test processing
processed = processor.process_audio(segment1)
assert len(processed) == len(segment1), "Audio processing changed length unexpectedly"
print(f" βœ… Processing: {len(segment1)} samples processed")
# Test statistics
stats = processor.get_audio_stats(segment1)
assert "duration_seconds" in stats, "Audio stats missing duration"
print(f" βœ… Statistics: {stats['duration_seconds']:.3f}s duration")
return True
except Exception as e:
print(f" ❌ Audio processor test failed: {e}")
return False
def test_config_system():
"""Test configuration system."""
print("πŸ” Testing Configuration System...")
try:
from src.config import ConfigManager, get_config
# Test config creation
config = ConfigManager("development")
assert config.environment == "development", "Environment not set correctly"
print(f" βœ… Config creation: {config.environment} environment")
# Test configuration access
all_config = config.get_all_config()
assert "text_processing" in all_config, "Missing text_processing config"
assert "model" in all_config, "Missing model config"
print(f" βœ… Config structure: {len(all_config)} sections")
# Test global config
global_config = get_config()
assert global_config is not None, "Global config not accessible"
print(f" βœ… Global config: {global_config.environment}")
return True
except Exception as e:
print(f" ❌ Config system test failed: {e}")
return False
def test_pipeline_structure():
"""Test pipeline structure without model loading."""
print("πŸ” Testing Pipeline Structure...")
try:
# Test import structure
from src.preprocessing import TextProcessor
from src.audio_processing import AudioProcessor
from src.config import ConfigManager
# Test that pipeline can be imported
from src.pipeline import TTSPipeline
print(f" βœ… All modules import successfully")
# Test configuration integration
config = ConfigManager("development")
text_proc = TextProcessor(
max_chunk_length=config.text_processing.max_chunk_length,
overlap_words=config.text_processing.overlap_words
)
audio_proc = AudioProcessor(
crossfade_duration=config.audio_processing.crossfade_duration,
sample_rate=config.audio_processing.sample_rate
)
print(f" βœ… Components created with config")
return True
except Exception as e:
print(f" ❌ Pipeline structure test failed: {e}")
return False
def run_performance_mock():
"""Run mock performance test."""
print("πŸ” Running Performance Mock Test...")
try:
from src.preprocessing import TextProcessor
from src.audio_processing import AudioProcessor
# Test processing speed
processor = TextProcessor()
test_texts = [
"ΤΏΥ‘Φ€Υ³ ΥΏΥ₯Φ„Υ½ΥΏ",
"Υ„Υ«Υ»Υ«ΥΆ Υ₯Φ€Υ―Υ‘Φ€ΥΈΦ‚Υ©Υ΅Υ‘ΥΆ ΥΏΥ₯Φ„Υ½ΥΏ ΥΈΦ€Υ¨ ΥΊΥ‘Φ€ΥΈΦ‚ΥΆΥ‘Υ―ΥΈΦ‚Υ΄ Υ§ Υ΄Υ« Φ„Υ‘ΥΆΥ« Υ’Υ‘ΥΌ",
"Υ‡Υ‘ΥΏ Υ₯Φ€Υ―Υ‘Φ€ ΥΏΥ₯Φ„Υ½ΥΏ ΥΈΦ€Υ¨ Υ―Φ€Υ―ΥΆΥΎΥΈΦ‚Υ΄ Υ§ " * 20
]
times = []
for text in test_texts:
start = time.time()
processed = processor.process_text(text)
chunks = processor.chunk_text(processed)
end = time.time()
processing_time = end - start
times.append(processing_time)
print(f" πŸ“Š {len(text)} chars β†’ {len(chunks)} chunks in {processing_time:.4f}s")
avg_time = np.mean(times)
print(f" βœ… Average processing time: {avg_time:.4f}s")
# Mock audio processing
audio_proc = AudioProcessor()
test_audio = np.random.randint(-10000, 10000, 16000, dtype=np.int16)
start = time.time()
processed_audio = audio_proc.process_audio(test_audio)
end = time.time()
audio_time = end - start
print(f" πŸ“Š 1s audio processed in {audio_time:.4f}s")
return True
except Exception as e:
print(f" ❌ Performance mock test failed: {e}")
return False
def validate_file_structure():
"""Validate the project file structure."""
print("πŸ” Validating File Structure...")
required_files = [
"src/__init__.py",
"src/preprocessing.py",
"src/model.py",
"src/audio_processing.py",
"src/pipeline.py",
"src/config.py",
"app_optimized.py",
"requirements.txt",
"README.md",
"OPTIMIZATION_REPORT.md"
]
missing_files = []
for file_path in required_files:
if not os.path.exists(file_path):
missing_files.append(file_path)
if missing_files:
print(f" ❌ Missing files: {missing_files}")
return False
else:
print(f" βœ… All {len(required_files)} required files present")
return True
def main():
"""Run all validation tests."""
print("=" * 60)
print("πŸš€ TTS OPTIMIZATION VALIDATION")
print("=" * 60)
tests = [
("File Structure", validate_file_structure),
("Configuration System", test_config_system),
("Text Processor", test_text_processor),
("Audio Processor", test_audio_processor),
("Pipeline Structure", test_pipeline_structure),
("Performance Mock", run_performance_mock)
]
results = {}
for test_name, test_func in tests:
print(f"\nπŸ“‹ {test_name}")
print("-" * 40)
try:
success = test_func()
results[test_name] = success
if success:
print(f" πŸŽ‰ {test_name}: PASSED")
else:
print(f" πŸ’₯ {test_name}: FAILED")
except Exception as e:
print(f" πŸ’₯ {test_name}: ERROR - {e}")
results[test_name] = False
# Summary
print("\n" + "=" * 60)
print("πŸ“Š VALIDATION SUMMARY")
print("=" * 60)
passed = sum(results.values())
total = len(results)
for test_name, success in results.items():
status = "βœ… PASS" if success else "❌ FAIL"
print(f"{status} {test_name}")
print(f"\n🎯 Results: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
if passed == total:
print("πŸŽ‰ ALL TESTS PASSED - OPTIMIZATION SUCCESSFUL!")
print("\nπŸš€ Ready for deployment:")
print(" β€’ Run: python app_optimized.py")
print(" β€’ Or update app.py to use optimized version")
print(" β€’ Monitor performance with built-in analytics")
else:
print("⚠️ Some tests failed - review the output above")
print(" β€’ Check import paths and dependencies")
print(" β€’ Verify file structure")
print(" β€’ Run: pip install -r requirements.txt")
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)