File size: 9,582 Bytes
f84ed9c
7701a0c
 
 
f84ed9c
be40b87
 
580c470
 
 
 
 
7158b5e
7701a0c
7158b5e
7701a0c
 
 
 
7158b5e
 
7701a0c
 
 
 
 
 
 
 
7158b5e
7701a0c
7158b5e
7701a0c
be40b87
 
 
 
 
 
 
 
 
7158b5e
7701a0c
 
 
 
be40b87
7701a0c
 
be40b87
7701a0c
f84ed9c
7701a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580c470
 
 
 
 
 
 
 
 
 
 
 
7701a0c
 
 
 
98ab978
7701a0c
 
98ab978
 
 
 
 
 
 
 
 
 
 
 
 
7701a0c
 
580c470
 
 
 
 
 
 
 
 
 
 
 
 
5399362
 
 
 
 
 
 
 
580c470
 
 
 
 
 
 
 
5399362
 
 
 
 
 
 
 
580c470
 
 
 
 
 
 
 
 
 
 
5399362
 
 
 
 
 
 
580c470
 
 
 
 
 
 
 
 
 
4cbfd1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580c470
 
 
 
 
 
 
 
 
 
 
 
 
7701a0c
 
 
7158b5e
 
 
7701a0c
 
 
 
 
 
 
 
 
d6ff044
 
 
 
 
 
 
 
 
 
 
 
7701a0c
 
 
 
 
7158b5e
7701a0c
e0ebe32
 
d6ff044
e0ebe32
 
 
7701a0c
e0ebe32
 
 
 
 
 
 
 
 
 
7701a0c
 
 
 
 
e0ebe32
7701a0c
 
 
e0ebe32
 
 
 
 
7701a0c
 
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
import logging
from typing import List, Dict

import torch
from transformers import pipeline
from transformers import logging as transformers_logging
import warnings
import os
from typing import Tuple

from app.core.chunking import split_audio_to_chunks
from app.core.audio_utils import get_audio_info

logger = logging.getLogger(__name__)

# ===============================
# Global model cache
# ===============================
_ASR_MODEL = None


def load_model(chunk_length_s: float = 30.0):
    """
    Load ASR model once and reuse.
    Safe to call multiple times.
    """
    global _ASR_MODEL
    if _ASR_MODEL is not None:
        return _ASR_MODEL

    logger.info("Loading ASR model PhoWhisper-base")

    device = 0 if torch.cuda.is_available() else -1
    dtype = torch.float16 if torch.cuda.is_available() else torch.float32

    # Reduce noisy transformer logs and warnings about experimental chunking
    try:
        transformers_logging.set_verbosity_error()
    except Exception:
        pass
    # filter the noisy chunk_length_s warnings (regex)
    warnings.filterwarnings("ignore", message=r".*chunk_length_s.*")

    _ASR_MODEL = pipeline(
        task="automatic-speech-recognition",
        model="vinai/PhoWhisper-base",
        device=device,
        dtype=dtype,
        chunk_length_s=chunk_length_s,
        return_timestamps=True,
        ignore_warning=True,
    )

    logger.info(
        "ASR model loaded (device=%s)", "cuda" if device >= 0 else "cpu"
    )
    return _ASR_MODEL


# ===============================
# Transcribe full text
# ===============================
def transcribe_file(
    model,
    wav_path: str,
    chunk_length_s: float = 30.0,
    stride_s: float = 5.0,
) -> str:
    """
    Return full transcript text.
    """
    if not wav_path:
        return ""

    # If audio is long, prefer chunked inference to avoid memory/time issues
    info = get_audio_info(wav_path) or {}
    duration = info.get("duration", 0)
    if duration and duration > chunk_length_s:
        try:
            text, _chunks = transcribe_long_audio(
                model, wav_path, chunk_length_s=chunk_length_s, overlap_s=stride_s
            )
            return text
        except Exception:
            logger.exception("transcribe_long_audio failed, falling back to pipeline")

    out = model(
        wav_path,
        chunk_length_s=chunk_length_s,
        stride_length_s=stride_s,
        # return_timestamps may be ignored for full-text outputs but safe to pass
    )

    # Primary: pipeline may return 'text'
    text = (out.get("text") or "").strip()
    if text:
        return text

    # Fallback: some pipeline versions return detailed segments/chunks
    segs = out.get("chunks") or out.get("segments") or []
    if segs:
        parts = [ (s.get("text") or "").strip() for s in segs ]
        joined = " ".join([p for p in parts if p])
        return joined.strip()

    return ""


def transcribe_long_audio(
    model,
    wav_path: str,
    chunk_length_s: float = 30.0,
    overlap_s: float = 5.0,
) -> Tuple[str, List[Dict]]:
    """
    Split `wav_path` into chunks and run inference on each chunk sequentially.
    Returns (full_text, chunks) where chunks have global start/end timestamps.
    """
    if not wav_path:
        return "", []

    # prefer VAD-based splitting if available
    try:
        from app.core.chunking import split_audio_with_vad
        chunk_paths = split_audio_with_vad(wav_path)
    except Exception:
        chunk_paths = split_audio_to_chunks(wav_path, chunk_length_s=chunk_length_s, overlap_s=overlap_s)

    logger.debug("transcribe_long_audio: split into %d chunk_paths", len(chunk_paths))
    combined_text_parts = []
    combined_chunks: List[Dict] = []

    step = chunk_length_s - overlap_s
    try:
        for i, cp in enumerate(chunk_paths):
            base_offset = i * step

            try:
                cinfo = get_audio_info(cp) or {}
                logger.debug(
                    "chunk[%d]=%s duration=%.3fs samplerate=%s", i, cp, cinfo.get("duration"), cinfo.get("samplerate")
                )
            except Exception:
                logger.debug("chunk[%d]=%s (info unavailable)", i, cp)

            try:
                out = model(
                    cp,
                    chunk_length_s=chunk_length_s,
                    stride_length_s=overlap_s,
                    return_timestamps=True,
                )
            except Exception:
                logger.exception("model inference failed for chunk %s", cp)
                continue

            # debug: log output shape/keys (only first few chunks to avoid huge logs)
            try:
                if i < 5:
                    logger.debug("model out keys for chunk[%d]: %s", i, list(out.keys()) if isinstance(out, dict) else type(out))
            except Exception:
                logger.debug("failed to log model out keys for chunk %d", i)

            part_text = (out.get("text") or "").strip()
            if not part_text:
                segs = out.get("chunks") or out.get("segments") or []
                parts = [ (s.get("text") or "").strip() for s in segs ]
                part_text = " ".join([p for p in parts if p]).strip()

            if part_text:
                combined_text_parts.append(part_text)

            raw_segs = out.get("chunks") or out.get("segments") or []
            if raw_segs:
                for s in raw_segs:
                    start = None
                    end = None
                    if isinstance(s.get("timestamp"), (list, tuple)) and len(s.get("timestamp")) >= 2:
                        ts = s.get("timestamp")
                        start, end = ts[0], ts[1]
                    elif s.get("start") is not None and s.get("end") is not None:
                        start, end = s.get("start"), s.get("end")

                    text = (s.get("text") or "").strip()
                    if not text or start is None or end is None:
                        continue

                    try:
                        combined_chunks.append(
                            {"start": float(start) + base_offset, "end": float(end) + base_offset, "text": text}
                        )
                    except Exception:
                        continue
            else:
                # If model returned text but no timestamped segments for this chunk,
                # create a fallback chunk spanning the chunk file duration.
                if part_text:
                    try:
                        cinfo = get_audio_info(cp) or {}
                        cdur = cinfo.get("duration") or chunk_length_s
                        combined_chunks.append({
                            "start": float(base_offset),
                            "end": float(base_offset) + float(cdur),
                            "text": part_text,
                        })
                    except Exception:
                        logger.exception("failed to create fallback chunk for %s", cp)

    finally:
        for p in chunk_paths:
            try:
                if p and os.path.exists(p):
                    os.remove(p)
            except Exception:
                logger.debug("Failed to remove chunk file %s", p)

    full_text = " ".join([p for p in combined_text_parts if p]).strip()
    return full_text, combined_chunks


# ===============================
# Transcribe chunks with timestamps
# ===============================
def transcribe_file_chunks(
    model,
    wav_path: str,
    chunk_length_s: float = 30.0,
    stride_s: float = 5.0,
) -> List[Dict]:
    """
    Return list of chunks:
    [{ start, end, text }]
    """
    if not wav_path:
        return []
    # For long audio prefer explicit chunked inference (split + per-chunk inference)
    info = get_audio_info(wav_path) or {}
    duration = info.get("duration", 0)
    if duration and duration > chunk_length_s:
        try:
            _, combined = transcribe_long_audio(
                model, wav_path, chunk_length_s=chunk_length_s, overlap_s=stride_s
            )
            return combined
        except Exception:
            logger.exception("transcribe_long_audio failed in transcribe_file_chunks, falling back to pipeline")

    out = model(
        wav_path,
        chunk_length_s=chunk_length_s,
        stride_length_s=stride_s,
        return_timestamps=True,
    )

    # Pipeline output can vary across transformers versions/models:
    # - some return `chunks` (with `timestamp` list),
    # - others return `segments` (with `start`/end),
    # so be permissive and handle both shapes.
    raw_segments = out.get("chunks") or out.get("segments") or []

    chunks = []
    for c in raw_segments:
        # try multiple timestamp shapes
        start = None
        end = None

        if isinstance(c.get("timestamp"), (list, tuple)) and len(c.get("timestamp")) >= 2:
            ts = c.get("timestamp")
            start, end = ts[0], ts[1]
        elif c.get("start") is not None and c.get("end") is not None:
            start, end = c.get("start"), c.get("end")

        text = (c.get("text") or "").strip()
        if not text:
            continue

        # If timestamps are missing, skip (we don't want chunks without timing)
        if start is None or end is None:
            continue

        try:
            chunks.append({"start": float(start), "end": float(end), "text": text})
        except Exception:
            # be robust against unexpected types
            continue

    return chunks