File size: 9,773 Bytes
b163aa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)