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) | |