File size: 13,300 Bytes
65879b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Parallel Audio Transcription Script

This script processes multiple audio files simultaneously using Whisper,
with various optimization options for maximum speed.

Usage:
    python3 parallel_transcribe.py [options]

Options:
    --workers N          Number of parallel workers (default: CPU count)
    --model tiny|base    Whisper model to use (default: tiny)
    --batch-size N       Batch size for processing (default: 10)
    --gpu                Use GPU if available
    --resume             Resume from previous run
    --output-dir DIR     Output directory (default: transcripts)
    --input-dir DIR      Input directory (default: audio_files)
    --format txt|json    Output format (default: txt)
"""

import os
import json
import whisper
import torch
import argparse
import multiprocessing
from pathlib import Path
from datetime import datetime
from concurrent.futures import ProcessPoolExecutor, as_completed
from tqdm import tqdm
import time
import sys
import subprocess

# Configuration
PROGRESS_FILE = "parallel_transcription_progress.json"

def get_system_info():
    """Get system information for optimization"""
    import psutil
    import torch
    
    info = {
        "cpu_count": multiprocessing.cpu_count(),
        "memory_gb": round(psutil.virtual_memory().total / (1024**3), 1),
        "gpu_available": torch.cuda.is_available(),
        "gpu_count": torch.cuda.device_count() if torch.cuda.is_available() else 0
    }
    
    if info["gpu_available"]:
        info["gpu_memory_gb"] = round(torch.cuda.get_device_properties(0).total_memory / (1024**3), 1)
    
    return info

def suggest_optimal_workers(model_size="tiny"):
    """Suggest optimal number of workers based on system resources"""
    info = get_system_info()
    
    # Memory requirements per worker (approximate)
    memory_per_worker = {
        "tiny": 1.0,    # GB
        "base": 1.5,    # GB
        "small": 2.0,   # GB
        "medium": 4.0,  # GB
        "large": 8.0    # GB
    }
    
    # CPU-based limit
    cpu_limit = info["cpu_count"]
    
    # Memory-based limit
    memory_limit = int(info["memory_gb"] * 0.8 / memory_per_worker.get(model_size, 1.0))
    
    # Conservative limit to avoid system overload
    suggested = min(cpu_limit, memory_limit, 8)  # Cap at 8 workers
    
    return max(1, suggested)

def validate_audio_file(audio_file):
    """Validate that the file is a proper audio file"""
    try:
        # Use ffprobe to check if file has audio streams
        cmd = [
            'ffprobe', '-v', 'quiet', '-select_streams', 'a:0',
            '-show_entries', 'stream=codec_type', '-of', 'csv=p=0',
            str(audio_file)
        ]
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
        
        # Check if we found an audio stream
        return 'audio' in result.stdout
        
    except (subprocess.TimeoutExpired, subprocess.CalledProcessError, Exception):
        return False

def transcribe_file_worker(args):
    """Worker function for parallel transcription"""
    audio_file, model_name, output_dir, output_format, use_gpu = args
    
    try:
        # Validate audio file first
        if not validate_audio_file(audio_file):
            return {
                "success": False,
                "file": audio_file,
                "output": "",
                "message": "Invalid audio file (may be video or corrupted)",
                "skipped": False,
                "duration": 0
            }

        # Load model in each worker process
        device = "cuda" if use_gpu and torch.cuda.is_available() else "cpu"
        model = whisper.load_model(model_name, device=device)
        
        # Prepare output file
        file_stem = Path(audio_file).stem
        if output_format == "json":
            output_file = Path(output_dir) / f"{file_stem}.json"
        else:
            output_file = Path(output_dir) / f"{file_stem}.txt"
        
        # Skip if already exists
        if output_file.exists():
            return {
                "success": True,
                "file": audio_file,
                "output": str(output_file),
                "message": "Already exists",
                "skipped": True,
                "duration": 0
            }
        
        # Transcribe
        start_time = time.time()
        result = model.transcribe(audio_file)
        duration = time.time() - start_time
        
        # Save results
        output_file.parent.mkdir(parents=True, exist_ok=True)
        
        if output_format == "json":
            # Save full JSON result
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(result, f, indent=2, ensure_ascii=False)
        else:
            # Save timestamped text format
            with open(output_file, 'w', encoding='utf-8') as f:
                for segment in result["segments"]:
                    start_time = segment["start"]
                    text = segment["text"].strip()
                    if text:
                        f.write(f"{start_time:.2f}s: {text}\n")
        
        return {
            "success": True,
            "file": audio_file,
            "output": str(output_file),
            "message": "Transcribed successfully",
            "skipped": False,
            "duration": duration
        }
        
    except Exception as e:
        return {
            "success": False,
            "file": audio_file,
            "output": "",
            "message": str(e),
            "skipped": False,
            "duration": 0
        }

def load_progress():
    """Load previous progress if exists"""
    if os.path.exists(PROGRESS_FILE):
        with open(PROGRESS_FILE, 'r') as f:
            return json.load(f)
    return {"completed": [], "failed": [], "skipped": []}

def save_progress(progress):
    """Save progress to file"""
    with open(PROGRESS_FILE, 'w') as f:
        json.dump(progress, f, indent=2)

def parallel_transcribe(input_dir, output_dir, model_name="tiny", num_workers=None, 
                       use_gpu=False, resume=False, output_format="txt"):
    """Main parallel transcription function"""
    
    # System information
    system_info = get_system_info()
    print(f"πŸ–₯️  System Info:")
    print(f"   CPU cores: {system_info['cpu_count']}")
    print(f"   Memory: {system_info['memory_gb']} GB")
    print(f"   GPU: {'' if system_info['gpu_available'] else '❌'}")
    
    # Determine optimal workers
    if num_workers is None:
        num_workers = suggest_optimal_workers(model_name)
    
    suggested = suggest_optimal_workers(model_name)
    if num_workers > suggested:
        print(f"⚠️  Warning: {num_workers} workers might be too many for your system.")
        print(f"   Suggested: {suggested} workers")
        response = input("   Continue anyway? (y/N): ")
        if response.lower() != 'y':
            num_workers = suggested
    
    print(f" Using {num_workers} workers with '{model_name}' model")
    
    # Find audio files
    input_path = Path(input_dir)
    audio_extensions = ['.mp3', '.wav', '.m4a', '.flac', '.ogg']
    audio_files = []
    
    for ext in audio_extensions:
        audio_files.extend(input_path.glob(f"*{ext}"))
    
    if not audio_files:
        print(f"❌ No audio files found in {input_dir}")
        return False
    
    print(f"πŸ“ Found {len(audio_files)} audio files")
    
    # Load progress
    progress = load_progress() if resume else {"completed": [], "failed": [], "skipped": []}
    
    # Filter files if resuming
    if resume:
        completed_files = set(progress["completed"] + progress["failed"] + progress["skipped"])
        audio_files = [f for f in audio_files if str(f) not in completed_files]
        print(f"πŸ“‹ Resume mode: {len(audio_files)} files remaining")
    
    if not audio_files:
        print(" All files already processed!")
        return True
    
    # Prepare arguments for workers
    worker_args = [
        (str(audio_file), model_name, output_dir, output_format, use_gpu)
        for audio_file in audio_files
    ]
    
    # Process files in parallel
    successful = 0
    failed = 0
    skipped = 0
    total_duration = 0
    
    print(f"🎯 Processing {len(audio_files)} files...")
    
    with ProcessPoolExecutor(max_workers=num_workers) as executor:
        # Submit all tasks
        future_to_file = {executor.submit(transcribe_file_worker, args): args[0] 
                          for args in worker_args}
        
        # Process results as they complete
        for future in tqdm(as_completed(future_to_file), total=len(audio_files), desc="Transcribing"):
            audio_file = future_to_file[future]
            try:
                result = future.result()
                
                if result["success"]:
                    if result["skipped"]:
                        skipped += 1
                        progress["skipped"].append(audio_file)
                    else:
                        successful += 1
                        progress["completed"].append(audio_file)
                        total_duration += result["duration"]
                    
                    # Print progress (optional - can be noisy)
                    if not result["skipped"]:
                        print(f" {Path(audio_file).name}: {result['duration']:.1f}s")
                else:
                    failed += 1
                    progress["failed"].append(audio_file)
                    print(f"❌ {Path(audio_file).name}: {result['message']}")
                
                # Save progress periodically
                if (successful + failed + skipped) % 10 == 0:
                    save_progress(progress)
                    
            except Exception as e:
                failed += 1
                progress["failed"].append(audio_file)
                print(f"❌ {Path(audio_file).name}: {str(e)}")
    
    # Final progress save
    save_progress(progress)
    
    # Summary
    print(f"\nπŸ“Š Processing Summary:")
    print(f"   Successful: {successful}")
    print(f"  ⏭️  Skipped: {skipped}")
    print(f"  ❌ Failed: {failed}")
    print(f"  πŸ“ Total: {len(audio_files)}")
    
    if successful > 0:
        avg_time = total_duration / successful
        print(f"  ⏱️  Average time per file: {avg_time:.1f}s")
        print(f"   Total processing time: {total_duration:.1f}s")
    
    return True

def main():
    # Get defaults from pipeline configuration
    try:
        from pipeline_config import WHISPER_MODEL_PARALLEL, PARALLEL_WORKERS
        default_model = WHISPER_MODEL_PARALLEL
        default_workers = PARALLEL_WORKERS
        print(f"πŸ“‹ Using pipeline config defaults: {default_model} model, {default_workers} workers")
    except ImportError:
        default_model = "tiny"
        default_workers = None
        print("πŸ“‹ Pipeline config not found, using built-in defaults")
    
    parser = argparse.ArgumentParser(description='Parallel audio transcription with Whisper')
    parser.add_argument('--workers', type=int, default=default_workers, 
                        help=f'Number of parallel workers (default: {default_workers or "auto"})')
    parser.add_argument('--model', choices=['tiny', 'base', 'small', 'medium', 'large'], 
                        default=default_model, help=f'Whisper model to use (default: {default_model})')
    parser.add_argument('--gpu', action='store_true', help='Use GPU if available')
    parser.add_argument('--resume', action='store_true', help='Resume from previous run')
    parser.add_argument('--output-dir', default='transcripts', help='Output directory')
    parser.add_argument('--input-dir', default='audio_files', help='Input directory')
    parser.add_argument('--format', choices=['txt', 'json'], default='txt', help='Output format')
    parser.add_argument('--info', action='store_true', help='Show system info and exit')
    
    args = parser.parse_args()
    
    if args.info:
        system_info = get_system_info()
        print("πŸ–₯️  System Information:")
        print(f"   CPU cores: {system_info['cpu_count']}")
        print(f"   Memory: {system_info['memory_gb']} GB")
        print(f"   GPU available: {'' if system_info['gpu_available'] else '❌'}")
        if system_info['gpu_available']:
            print(f"   GPU memory: {system_info['gpu_memory_gb']} GB")
        
        suggested = suggest_optimal_workers(args.model)
        print(f"\n🎯 Suggested workers for '{args.model}' model: {suggested}")
        return
    
    print(" Starting parallel audio transcription...")
    print("=" * 60)
    
    start_time = time.time()
    
    success = parallel_transcribe(
        input_dir=args.input_dir,
        output_dir=args.output_dir,
        model_name=args.model,
        num_workers=args.workers,
        use_gpu=args.gpu,
        resume=args.resume,
        output_format=args.format
    )
    
    total_time = time.time() - start_time
    
    if success:
        print(f"\nπŸŽ‰ Transcription complete! Total time: {total_time:.1f}s")
        print(f"πŸ“ Results saved to: {args.output_dir}")
    else:
        print(f"\n❌ Transcription failed!")
        sys.exit(1)

if __name__ == "__main__":
    main()