File size: 1,568 Bytes
b89a72f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import difflib
import re


def _normalize(text: str) -> str:
    text = text.lower().strip()
    text = re.sub(r'[^\w\s]', '', text)
    text = re.sub(r'\s+', ' ', text)
    return text


def check_transcript(audio_path: str, expected_transcript: str,
                     model_name: str = "base") -> dict:
    """Verify audio matches expected transcript using Whisper."""
    try:
        import whisper
    except ImportError:
        return {
            "check": "transcript",
            "passed": False,
            "error": "whisper not installed (pip install openai-whisper)",
            "severity": "skipped",
        }

    try:
        model = whisper.load_model(model_name)
        result = model.transcribe(audio_path)
        actual = result["text"]
    except Exception as e:
        return {
            "check": "transcript",
            "passed": False,
            "error": "Whisper transcription failed: %s" % str(e),
            "severity": "high",
        }

    norm_expected = _normalize(expected_transcript)
    norm_actual = _normalize(actual)
    similarity = difflib.SequenceMatcher(
        None, norm_expected.split(), norm_actual.split()
    ).ratio()

    return {
        "check": "transcript",
        "passed": similarity >= 0.85,
        "expected_text": expected_transcript[:200],
        "actual_text": actual[:200],
        "word_similarity": round(similarity, 3),
        "threshold": 0.85,
        "severity": "high" if similarity < 0.5 else "medium" if similarity < 0.85 else "low",
    }