Spaces:
Runtime error
Runtime error
#!/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) | |