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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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python3
"""
Whisper Model WER Evaluation - Fine-tunes vs Commercial APIs
Compares local fine-tuned models against commercial STT providers via EdenAI
"""

import os
import json
import time
from datetime import datetime
from pathlib import Path
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import requests
import jiwer
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Configuration
AUDIO_FILE = "eval/test-audio.wav"
TRUTH_FILE = "eval/truth.txt"
RESULTS_DIR = "results"

# Commercial providers to test via EdenAI
COMMERCIAL_PROVIDERS = ["deepgram", "openai", "assembly", "gladia"]

# Model configurations
MODELS = {
    # Local fine-tuned models
    "whisper-base-ft": {
        "type": "local",
        "path": "/home/daniel/ai/models/stt/finetunes/daniel-whisper-base-finetune",
        "description": "Fine-tuned Whisper Base"
    },
    "whisper-small-ft": {
        "type": "local",
        "path": "/home/daniel/ai/models/stt/finetunes/whisper-small-en-futo",
        "description": "Fine-tuned Whisper Small"
    },
    "whisper-tiny-ft": {
        "type": "local",
        "path": "/home/daniel/ai/models/stt/finetunes/whisper-tiny-en-futo",
        "description": "Fine-tuned Whisper Tiny"
    },
    "whisper-large-turbo-ft": {
        "type": "local",
        "path": "/home/daniel/ai/models/stt/finetunes/whisper-large-turbo-finetune",
        "description": "Fine-tuned Whisper Large Turbo"
    }
}

def load_ground_truth(truth_file):
    """Load ground truth transcription"""
    with open(truth_file, 'r') as f:
        return f.read().strip()

def transcribe_local_model(model_path, audio_file):
    """Transcribe audio using a local model"""
    try:
        device = "cuda:0" if torch.cuda.is_available() else "cpu"
        torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

        print(f"  Loading model from {model_path}...")
        model = AutoModelForSpeechSeq2Seq.from_pretrained(
            model_path,
            torch_dtype=torch_dtype,
            low_cpu_mem_usage=True,
            use_safetensors=True
        )
        model.to(device)

        processor = AutoProcessor.from_pretrained(model_path)

        pipe = pipeline(
            "automatic-speech-recognition",
            model=model,
            tokenizer=processor.tokenizer,
            feature_extractor=processor.feature_extractor,
            max_new_tokens=128,
            chunk_length_s=30,
            batch_size=16,
            return_timestamps=False,
            torch_dtype=torch_dtype,
            device=device,
        )

        print(f"  Transcribing...")
        result = pipe(audio_file)
        transcription = result["text"]

        # Clean up
        del model
        del processor
        del pipe
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

        return transcription.strip()

    except Exception as e:
        print(f"  ERROR: {str(e)}")
        return None

def transcribe_edenai(audio_file, providers, api_key):
    """Transcribe audio using EdenAI with multiple providers"""
    results = {}

    for provider in providers:
        print(f"\n  Testing {provider}...")
        try:
            # Submit async job
            url = "https://api.edenai.run/v2/audio/speech_to_text_async"
            headers = {"Authorization": f"Bearer {api_key}"}

            data = {
                "providers": provider,
                "language": "en"
            }

            with open(audio_file, 'rb') as f:
                files = {'file': f}
                response = requests.post(url, data=data, files=files, headers=headers)

            if response.status_code != 200:
                print(f"    ❌ Failed to submit job: {response.status_code}")
                print(f"    Response: {response.text}")
                results[provider] = None
                continue

            job_data = response.json()
            public_id = job_data.get("public_id")

            if not public_id:
                print(f"    ❌ No job ID returned")
                results[provider] = None
                continue

            print(f"    Job ID: {public_id}")
            print(f"    Polling for results...")

            # Poll for results
            result_url = f"https://api.edenai.run/v2/audio/speech_to_text_async/{public_id}"
            max_attempts = 60
            attempt = 0

            while attempt < max_attempts:
                time.sleep(2)
                result_response = requests.get(result_url, headers=headers)

                if result_response.status_code != 200:
                    print(f"    ❌ Failed to get results: {result_response.status_code}")
                    break

                result_data = result_response.json()
                status = result_data.get("status")

                if status == "finished":
                    # Extract transcription
                    provider_result = result_data.get("results", {}).get(provider, {})
                    transcription = provider_result.get("text", "")

                    if transcription:
                        print(f"    βœ“ Transcription received")
                        results[provider] = transcription.strip()
                    else:
                        print(f"    ⚠️  No transcription in response")
                        results[provider] = None
                    break
                elif status == "failed":
                    error = result_data.get("results", {}).get(provider, {}).get("error")
                    print(f"    ❌ Job failed: {error}")
                    results[provider] = None
                    break

                attempt += 1
                if attempt % 10 == 0:
                    print(f"    Still waiting... ({attempt}/{max_attempts})")

            if attempt >= max_attempts:
                print(f"    ⏱️  Timeout waiting for results")
                results[provider] = None

        except Exception as e:
            print(f"    ❌ Error: {str(e)}")
            results[provider] = None

    return results

def calculate_metrics(reference, hypothesis):
    """Calculate WER and other metrics"""
    output = jiwer.process_words(reference, hypothesis)
    return {
        "wer": output.wer,
        "mer": output.mer,
        "wil": output.wil,
        "wip": output.wip,
        "hits": output.hits,
        "substitutions": output.substitutions,
        "deletions": output.deletions,
        "insertions": output.insertions
    }

def save_transcription(model_name, transcription):
    """Save transcription to file"""
    transcriptions_dir = Path(RESULTS_DIR) / "transcriptions"
    transcriptions_dir.mkdir(parents=True, exist_ok=True)

    output_file = transcriptions_dir / f"transcription_{model_name}.txt"
    with open(output_file, 'w') as f:
        f.write(transcription)

    return output_file

def format_results_table(results):
    """Format results as ASCII table"""
    header = "| Rank | Model | Type | WER | MER | WIL | WIP |"
    separator = "|------|-------|------|-----|-----|-----|-----|"

    lines = [header, separator]
    for i, result in enumerate(results, 1):
        if result["model_type"] == "local":
            model_type = "Fine-tune"
        else:
            model_type = "Commercial"
        line = f"| {i} | {result['model_name']} | {model_type} | {result['wer']:.2%} | {result['mer']:.2%} | {result['wil']:.2%} | {result['wip']:.2%} |"
        lines.append(line)

    return "\n".join(lines)

def generate_comparison_chart(results):
    """Generate ASCII bar chart of WER results"""
    lines = ["WER Comparison (lower is better)", "=" * 80, ""]

    max_wer = max(r['wer'] for r in results) if results else 1
    max_bar_length = 60

    for result in results:
        wer = result['wer']
        bar_length = int((wer / max_wer) * max_bar_length) if max_wer > 0 else 0
        bar = "β–ˆ" * bar_length
        model_type = "FT" if result["model_type"] == "local" else "CM"
        line = f"{result['model_name'][:30]:<30} [{model_type}] {bar} {wer:.2%}"
        lines.append(line)

    lines.append("")
    lines.append("Legend: [FT] = Fine-tuned (local), [CM] = Commercial API")
    return "\n".join(lines)

def main():
    print("=" * 80)
    print("Whisper Model WER Evaluation - Fine-tunes vs Commercial APIs")
    print("=" * 80)
    print()

    # Check EdenAI API key
    print("Checking EdenAI API key...")
    api_key = os.environ.get("EDENAI_API_KEY")
    if not api_key:
        print("⚠️  Warning: EDENAI_API_KEY not set.")
        print("   Export EDENAI_API_KEY=your_key to enable commercial API comparison")
        print("   Continuing with local models only...")
    else:
        print("βœ“ EDENAI_API_KEY found")
    print()

    # Load ground truth
    print(f"Loading ground truth from {TRUTH_FILE}...")
    reference = load_ground_truth(TRUTH_FILE)
    print(f"Ground truth loaded: {len(reference.split())} words")
    print()

    # Create results directory
    Path(RESULTS_DIR).mkdir(exist_ok=True)

    # Evaluate models
    results = []
    failed_models = []

    print("Evaluating local fine-tuned models...")
    print("-" * 80)

    for model_name, config in MODELS.items():
        print(f"\n{model_name} ({config['description']})")

        start_time = time.time()

        if not Path(config["path"]).exists():
            print(f"  ⚠️  Model path not found: {config['path']}")
            failed_models.append({
                "model_name": model_name,
                "description": config["description"],
                "error": "Model path not found"
            })
            continue

        transcription = transcribe_local_model(config["path"], AUDIO_FILE)

        elapsed_time = time.time() - start_time

        if transcription is None:
            failed_models.append({
                "model_name": model_name,
                "description": config["description"],
                "error": "Transcription failed"
            })
            continue

        # Save transcription
        save_transcription(model_name, transcription)
        print(f"  Saved transcription")

        # Calculate metrics
        metrics = calculate_metrics(reference, transcription)

        results.append({
            "model_name": model_name,
            "description": config["description"],
            "model_type": "local",
            "transcription": transcription,
            "processing_time": elapsed_time,
            **metrics
        })

        print(f"  WER: {metrics['wer']:.2%}")
        print(f"  Processing time: {elapsed_time:.2f}s")

    # Test commercial providers via EdenAI
    if api_key:
        print("\n" + "=" * 80)
        print("Evaluating commercial STT providers via EdenAI...")
        print("-" * 80)

        commercial_results = transcribe_edenai(AUDIO_FILE, COMMERCIAL_PROVIDERS, api_key)

        for provider, transcription in commercial_results.items():
            if transcription:
                model_name = f"{provider}-api"

                # Save transcription
                save_transcription(model_name, transcription)

                # Calculate metrics
                metrics = calculate_metrics(reference, transcription)

                results.append({
                    "model_name": model_name,
                    "description": f"{provider.title()} STT API",
                    "model_type": "commercial",
                    "transcription": transcription,
                    "processing_time": 0,  # EdenAI doesn't provide this
                    **metrics
                })

                print(f"\nβœ“ {provider}: WER {metrics['wer']:.2%}")
            else:
                failed_models.append({
                    "model_name": f"{provider}-api",
                    "description": f"{provider.title()} STT API",
                    "error": "Transcription failed"
                })

    # Sort by WER (best first)
    results.sort(key=lambda x: x['wer'])

    # Generate report
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    report_file = Path(RESULTS_DIR) / f"commercial_comparison_report_{timestamp}.txt"
    json_file = Path(RESULTS_DIR) / f"commercial_comparison_results_{timestamp}.json"

    # Generate results table
    results_table = format_results_table(results)

    # Generate comparison chart
    comparison_chart = generate_comparison_chart(results)

    # Write report
    with open(report_file, 'w') as f:
        f.write("=" * 80 + "\n")
        f.write("WHISPER MODEL WER EVALUATION - FINE-TUNES VS COMMERCIAL APIS\n")
        f.write("=" * 80 + "\n\n")
        f.write(f"Evaluation Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
        f.write(f"Test Audio: {AUDIO_FILE}\n")
        f.write(f"Ground Truth: {TRUTH_FILE}\n")
        f.write(f"Reference Word Count: {len(reference.split())} words\n")
        f.write(f"Commercial Providers: {', '.join(COMMERCIAL_PROVIDERS)}\n\n")

        f.write("RESULTS RANKED BY WER (BEST TO WORST)\n")
        f.write("=" * 80 + "\n\n")
        f.write(results_table + "\n\n")

        f.write("WER COMPARISON CHART\n")
        f.write("=" * 80 + "\n\n")
        f.write(comparison_chart + "\n\n")

        f.write("DETAILED METRICS\n")
        f.write("=" * 80 + "\n\n")

        for result in results:
            f.write(f"{result['model_name']} - {result['description']}\n")
            f.write(f"  Type: {result['model_type'].title()}\n")
            f.write(f"  WER: {result['wer']:.2%}\n")
            f.write(f"  MER: {result['mer']:.2%}\n")
            f.write(f"  WIL: {result['wil']:.2%}\n")
            f.write(f"  WIP: {result['wip']:.2%}\n")
            f.write(f"  Hits: {result['hits']}\n")
            f.write(f"  Substitutions: {result['substitutions']}\n")
            f.write(f"  Deletions: {result['deletions']}\n")
            f.write(f"  Insertions: {result['insertions']}\n")
            if result['processing_time'] > 0:
                f.write(f"  Processing Time: {result['processing_time']:.2f}s\n")
            f.write("\n")

        if failed_models:
            f.write("FAILED MODELS\n")
            f.write("=" * 80 + "\n\n")
            for failed in failed_models:
                f.write(f"{failed['model_name']} - {failed['description']}\n")
                f.write(f"  Error: {failed['error']}\n\n")

        f.write("CONCLUSIONS\n")
        f.write("=" * 80 + "\n\n")

        if results:
            best = results[0]
            worst = results[-1]

            f.write(f"Best Performer: {best['model_name']} ({best['description']})\n")
            f.write(f"  WER: {best['wer']:.2%}\n")
            f.write(f"  Type: {best['model_type'].title()}\n\n")

            f.write(f"Worst Performer: {worst['model_name']} ({worst['description']})\n")
            f.write(f"  WER: {worst['wer']:.2%}\n")
            f.write(f"  Type: {worst['model_type'].title()}\n\n")

            # Compare best fine-tune vs commercial
            local_models = [r for r in results if r['model_type'] == 'local']
            commercial_models = [r for r in results if r['model_type'] == 'commercial']

            if local_models and commercial_models:
                best_local = local_models[0]
                best_commercial = commercial_models[0]

                f.write(f"Best Fine-tune: {best_local['model_name']} - WER {best_local['wer']:.2%}\n")
                f.write(f"Best Commercial: {best_commercial['model_name']} - WER {best_commercial['wer']:.2%}\n\n")

                if best_local['wer'] < best_commercial['wer']:
                    improvement = ((best_commercial['wer'] - best_local['wer']) / best_commercial['wer']) * 100
                    f.write(f"🎯 Fine-tuning Improvement: {improvement:.1f}% better WER than best commercial API\n")
                else:
                    difference = ((best_local['wer'] - best_commercial['wer']) / best_commercial['wer']) * 100
                    f.write(f"Commercial API Advantage: {difference:.1f}% better WER than best fine-tune\n")

    # Save JSON results
    with open(json_file, 'w') as f:
        json.dump({
            "timestamp": timestamp,
            "audio_file": AUDIO_FILE,
            "truth_file": TRUTH_FILE,
            "reference_word_count": len(reference.split()),
            "commercial_providers": COMMERCIAL_PROVIDERS,
            "results": results,
            "failed_models": failed_models
        }, f, indent=2)

    # Copy to latest/
    latest_dir = Path(RESULTS_DIR) / "latest"
    latest_dir.mkdir(exist_ok=True)

    import shutil
    shutil.copy(report_file, latest_dir / "commercial_comparison_report.txt")
    shutil.copy(json_file, latest_dir / "commercial_comparison_results.json")

    with open(latest_dir / "commercial_comparison_chart.txt", 'w') as f:
        f.write(comparison_chart)

    # Print summary
    print("\n" + "=" * 80)
    print("EVALUATION COMPLETE")
    print("=" * 80)
    print(f"\nResults saved to:")
    print(f"  Report: {report_file}")
    print(f"  JSON: {json_file}")
    print(f"  Latest: {latest_dir}/")
    print(f"\nEvaluated {len(results)} models successfully")
    if failed_models:
        print(f"Failed to evaluate {len(failed_models)} models")
    print()
    print(results_table)
    print()

if __name__ == "__main__":
    main()