File size: 17,097 Bytes
f392205
 
 
9faedb3
 
f392205
 
 
 
 
 
bacf22b
 
 
 
 
 
 
 
 
 
 
 
f392205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9faedb3
c7f658d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9faedb3
bacf22b
 
f392205
 
bacf22b
 
 
 
9faedb3
 
f392205
bacf22b
 
 
9faedb3
 
f392205
 
 
 
c7f658d
 
9faedb3
f392205
 
 
 
 
9faedb3
f392205
9faedb3
 
 
 
 
f392205
 
 
 
 
 
 
 
 
 
 
9faedb3
 
 
 
 
 
 
 
f392205
9faedb3
c7f658d
 
 
bacf22b
 
 
 
 
 
c7f658d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9faedb3
 
c7f658d
9faedb3
f392205
 
c7f658d
 
 
9faedb3
f392205
9faedb3
c7f658d
9faedb3
f392205
c7f658d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9faedb3
 
 
c7f658d
9faedb3
 
f392205
 
c7f658d
 
 
f392205
c7f658d
 
 
 
 
 
 
 
 
9faedb3
 
f392205
 
 
9faedb3
 
 
bacf22b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import queue
import threading

import numpy as np

from custom_logger import logger_config as logger

# Models clients are allowed to request. Anything else is rejected before a load
# is ever attempted (an unknown name would otherwise trigger a download).
ALLOWED_MODELS = {"tiny", "base", "small", "medium", "large-v3"}

# Capabilities come from the shared registry so the CLI, backend and UI can
# never disagree about what an engine accepts.
from stt.registry import ENGINES, ALL_TASKS

# None/"auto" lets whisper detect the language, but an explicit code is more
# reliable on short streaming windows.
ALLOWED_LANGUAGES = set(ENGINES["fasterwhispher"]["languages"])

# "transcribe" keeps the source language; "translate" is whisper's built-in
# X -> English translation (so Hindi speech comes back as English text).
ALLOWED_TASKS = set(ALL_TASKS)

# Whisper weights are large, so identical (model, device) pairs are shared across
# connections instead of loaded once per connection (4 concurrent large-v3
# models would otherwise OOM). faster-whisper's WhisperModel is safe to use from
# multiple threads. Entries are ref-counted and freed when the last user leaves.
_MODEL_CACHE = {}
_MODEL_CACHE_LOCK = threading.Lock()


def _acquire_model(model_name, device):
    key = (model_name, device)
    with _MODEL_CACHE_LOCK:
        entry = _MODEL_CACHE.get(key)
        if entry is None:
            from faster_whisper import WhisperModel
            compute = "int8" if device == "cpu" else "float16"
            model = WhisperModel(model_name, device=device, compute_type=compute)
            entry = {"model": model, "refs": 0}
            _MODEL_CACHE[key] = entry
        entry["refs"] += 1
        return entry["model"]


def _release_model(model_name, device):
    key = (model_name, device)
    with _MODEL_CACHE_LOCK:
        entry = _MODEL_CACHE.get(key)
        if entry is None:
            return
        entry["refs"] -= 1
        if entry["refs"] <= 0:
            del _MODEL_CACHE[key]


class _HypothesisBuffer:
    """LocalAgreement-2 commit policy.

    Each window re-transcribes the unconfirmed audio. A word is only *committed*
    once two consecutive windows agree on it (longest common prefix); everything
    after the agreed prefix stays *tentative* and may be revised by the next
    window. This removes the duplicated/unstable output that naive overlapping
    re-transcription produces. (Macháček et al., whisper_streaming.)
    """

    def __init__(self):
        self.committed = []   # confirmed (start, end, word)
        self.buffer = []      # previous window's tentative tail
        self.new = []
        self.last_committed_time = 0.0

    def insert(self, words):
        # words: list of (start, end, text) in absolute seconds.
        self.new = [w for w in words if w[0] > self.last_committed_time - 0.1]
        if self.new and self.committed:
            # Drop a leading n-gram that repeats the tail we already committed
            # (whisper sometimes re-emits the previous words verbatim).
            if abs(self.new[0][0] - self.last_committed_time) < 1.0:
                cn, nn = len(self.committed), len(self.new)
                for i in range(1, min(cn, nn, 5) + 1):
                    tail = " ".join(self.committed[-j][2] for j in range(i, 0, -1))
                    head = " ".join(self.new[j][2] for j in range(i))
                    if tail == head:
                        del self.new[:i]
                        break

    def flush(self):
        """Commit the longest common prefix of this window and the last."""
        commit = []
        while self.new and self.buffer:
            if self.new[0][2] == self.buffer[0][2]:
                commit.append(self.new[0])
                self.last_committed_time = self.new[0][1]
                self.buffer.pop(0)
                self.new.pop(0)
            else:
                break
        self.buffer = self.new
        self.new = []
        self.committed.extend(commit)
        # Only the last few committed words are needed for n-gram dedup.
        if len(self.committed) > 100:
            self.committed = self.committed[-100:]
        return commit

    def complete(self):
        """Return remaining tentative words as final (no more audio coming)."""
        rest = self.buffer
        self.buffer = []
        return rest

    def tentative_text(self):
        return " ".join(w[2] for w in self.buffer)


class StreamingSTT:
    def __init__(self, model_name="base", device="cpu", sample_rate=16000,
                 language="en", task="transcribe"):
        if model_name not in ALLOWED_MODELS:
            raise ValueError(f"Unsupported model: {model_name}")
        if language is not None and language not in ALLOWED_LANGUAGES:
            raise ValueError(f"Unsupported language: {language}")
        if task not in ALLOWED_TASKS:
            raise ValueError(f"Unsupported task: {task}")
        self.sample_rate = sample_rate
        self.model_name = model_name
        self.device = device
        # None => let whisper detect the language per window.
        self.language = None if language == "auto" else language
        self.task = task
        self.buffer = np.array([], dtype=np.float32)
        self.processed_until = 0
        # Absolute sample index of buffer[0]. Grows as _trim_buffer() discards
        # leading samples, so timestamps stay anchored to real audio time
        # instead of drifting after a trim.
        self.buffer_start = 0
        self.min_chunk = 1.0  # seconds of new audio before a window is run
        self.hyp = _HypothesisBuffer()
        self.is_finalized = False
        # add_audio() runs on the event-loop thread while process()/flush() run
        # in an executor thread. Incoming audio is handed over through this
        # thread-safe queue so that only the executor thread ever mutates
        # self.buffer, avoiding a data race.
        self._incoming = queue.Queue()

        self.model = _acquire_model(model_name, device)

    def add_audio(self, audio_bytes: bytes):
        audio_float = (
            np.frombuffer(audio_bytes, dtype=np.int16).astype(np.float32) / 32768.0
        )
        self._incoming.put(audio_float)

    def _drain_incoming(self):
        chunks = []
        while True:
            try:
                chunks.append(self._incoming.get_nowait())
            except queue.Empty:
                break
        if chunks:
            self.buffer = np.append(self.buffer, np.concatenate(chunks))

    def _trim_buffer(self):
        max_buffered = self.sample_rate * 120
        if len(self.buffer) > max_buffered:
            trim_to = self.processed_until - self.sample_rate * 30
            if trim_to > 0:
                self.buffer = self.buffer[trim_to:]
                self.processed_until -= trim_to
                self.buffer_start += trim_to

    def _transcribe_words(self, audio, time_offset):
        """Transcribe audio, returning [(start, end, text), ...] in absolute time."""
        segments, _ = self.model.transcribe(
            audio,
            beam_size=1,
            vad_filter=True,
            language=self.language,
            task=self.task,
            word_timestamps=True,
        )
        words = []
        for seg in segments:
            for w in seg.words or []:
                text = w.word.strip()
                if text:
                    words.append((w.start + time_offset, w.end + time_offset, text))
        return words

    @staticmethod
    def _as_chunk(words):
        """Join committed words into a single transcript chunk, or None."""
        if not words:
            return None
        return {
            "start": round(words[0][0], 2),
            "end": round(words[-1][1], 2),
            "text": " ".join(w[2] for w in words),
        }

    def process(self):
        if self.is_finalized:
            return None

        self._drain_incoming()

        unprocessed = self.buffer[self.processed_until:]
        if len(unprocessed) < self.min_chunk * self.sample_rate:
            return None

        time_offset = (self.buffer_start + self.processed_until) / self.sample_rate
        try:
            words = self._transcribe_words(unprocessed, time_offset)
        except Exception as e:
            logger.error(f"[StreamingSTT] process error: {e}")
            return None

        self.hyp.insert(words)
        committed = self.hyp.flush()

        # Advance past the committed audio; tentative words stay unprocessed so
        # the next window can re-evaluate (and possibly correct) them.
        if committed:
            target = int(committed[-1][1] * self.sample_rate) - self.buffer_start
            self.processed_until = min(max(self.processed_until, target), len(self.buffer))
            self._trim_buffer()

        return {
            "commit": self._as_chunk(committed),
            "tentative": self.hyp.tentative_text(),
        }

    def flush(self):
        if self.is_finalized:
            return None
        self.is_finalized = True

        self._drain_incoming()

        unprocessed = self.buffer[self.processed_until:]
        final = []
        if len(unprocessed) >= 0.3 * self.sample_rate:
            time_offset = (self.buffer_start + self.processed_until) / self.sample_rate
            try:
                words = self._transcribe_words(unprocessed, time_offset)
                self.hyp.insert(words)
                final = self.hyp.flush()
            except Exception as e:
                logger.error(f"[StreamingSTT] flush error: {e}")
        # No more audio is coming, so commit whatever tentative words remain.
        final = final + self.hyp.complete()
        return {"commit": self._as_chunk(final)}

    def cleanup(self):
        if self.model is not None:
            self.model = None
            _release_model(self.model_name, self.device)
        self.buffer = np.array([], dtype=np.float32)
        import gc
        gc.collect()


class IndicStreamingSTT:
    """Streaming wrapper around the AI4Bharat cascade.

    Whisper emits English tokens directly, so its output can be committed
    word-by-word. This cascade cannot: Hindi is verb-final, so a partial clause
    translates to something that the rest of the clause would invalidate.
    Instead, source-script text is shown as *tentative* the moment it is
    recognised, and a clause is only translated and *committed* once it is
    closed - detected either by trailing silence or by hitting the max window.
    """

    min_chunk = 2.0      # seconds of audio before any tentative output
    max_window = 8.0     # force a commit rather than growing without bound
    silence_tail = 0.6   # seconds of quiet that count as a clause boundary
    silence_rms = 0.012  # amplitude below which a frame is considered silent

    def __init__(self, model_name=None, device="cpu", sample_rate=16000,
                 language="hi", task="translate"):
        if language in (None, "", "auto"):
            raise ValueError("The indic engine needs an explicit language (no auto-detect)")

        from stt.indic import LANG_TAGS
        if language not in LANG_TAGS:
            raise ValueError(f"Unsupported language for the indic engine: {language}")
        if task not in ALLOWED_TASKS:
            raise ValueError(f"Unsupported task: {task}")

        self.sample_rate = sample_rate
        self.device = device
        self.language = language
        self.task = task
        self.src_tag = LANG_TAGS[language]
        self.buffer = np.array([], dtype=np.float32)
        self.processed_until = 0
        self.buffer_start = 0
        self.is_finalized = False
        self._incoming = queue.Queue()

        # Reuse the batch engine so the model-loading and translation logic
        # lives in exactly one place.
        from stt.indic import IndicSTTProcessor
        self.engine = IndicSTTProcessor(device=device)
        self.engine.language = language
        self.engine.task = task
        if task == "translate":
            self.engine._load_translator()

    def add_audio(self, audio_bytes: bytes):
        audio_float = (
            np.frombuffer(audio_bytes, dtype=np.int16).astype(np.float32) / 32768.0
        )
        self._incoming.put(audio_float)

    def _drain_incoming(self):
        chunks = []
        while True:
            try:
                chunks.append(self._incoming.get_nowait())
            except queue.Empty:
                break
        if chunks:
            self.buffer = np.append(self.buffer, np.concatenate(chunks))

    def _trim_buffer(self):
        max_buffered = self.sample_rate * 120
        if len(self.buffer) > max_buffered:
            trim_to = self.processed_until - self.sample_rate * 5
            if trim_to > 0:
                self.buffer = self.buffer[trim_to:]
                self.processed_until -= trim_to
                self.buffer_start += trim_to

    def _ends_in_silence(self, audio):
        tail = audio[-int(self.silence_tail * self.sample_rate):]
        if len(tail) < self.silence_tail * self.sample_rate:
            return False
        return float(np.sqrt(np.mean(tail ** 2))) < self.silence_rms

    def _transcribe(self, audio):
        import torch
        wav = torch.from_numpy(audio).unsqueeze(0).to(self.device)
        with torch.inference_mode():
            text = self.engine.model(wav, self.language, "ctc")
        if isinstance(text, (list, tuple)):
            text = " ".join(str(t) for t in text)
        return (text or "").strip()

    def _to_english(self, text):
        if self.task != "translate" or not text:
            return text
        sentences = self.engine._split_sentences(text)
        return " ".join(self.engine._translate(sentences, self.src_tag))

    def _commit(self, text, span_samples):
        start = (self.buffer_start + self.processed_until) / self.sample_rate
        end = start + span_samples / self.sample_rate
        self.processed_until += span_samples
        self._trim_buffer()
        return {
            "start": round(start, 2),
            "end": round(end, 2),
            "text": self._to_english(text),
        }

    def process(self):
        if self.is_finalized:
            return None

        self._drain_incoming()

        unprocessed = self.buffer[self.processed_until:]
        if len(unprocessed) < self.min_chunk * self.sample_rate:
            return None

        try:
            text = self._transcribe(unprocessed)
        except Exception as e:
            logger.error(f"[IndicStreamingSTT] process error: {e}")
            return None

        at_boundary = (
            len(unprocessed) >= self.max_window * self.sample_rate
            or self._ends_in_silence(unprocessed)
        )

        if not text:
            # Nothing recognised; drop silent audio so the window doesn't grow.
            if at_boundary:
                self.processed_until += len(unprocessed)
                self._trim_buffer()
            return {"commit": None, "tentative": ""}

        if at_boundary:
            try:
                return {"commit": self._commit(text, len(unprocessed)), "tentative": ""}
            except Exception as e:
                logger.error(f"[IndicStreamingSTT] translate error: {e}")
                return None

        # Clause still open - show the source script so there is live feedback.
        return {"commit": None, "tentative": text}

    def flush(self):
        if self.is_finalized:
            return None
        self.is_finalized = True

        self._drain_incoming()

        unprocessed = self.buffer[self.processed_until:]
        if len(unprocessed) < 0.3 * self.sample_rate:
            return {"commit": None}

        try:
            text = self._transcribe(unprocessed)
            if not text:
                return {"commit": None}
            return {"commit": self._commit(text, len(unprocessed))}
        except Exception as e:
            logger.error(f"[IndicStreamingSTT] flush error: {e}")
            return {"commit": None}

    def cleanup(self):
        if self.engine is not None:
            self.engine.cleanup()
            self.engine = None
        self.buffer = np.array([], dtype=np.float32)
        import gc
        gc.collect()


def create_streaming_stt(engine, model_name=None, device="cpu", language="en",
                         task="transcribe"):
    """Build the streaming implementation for a registry engine name."""
    from stt.registry import get_engine

    spec = get_engine(engine)
    if not spec["streaming"]:
        raise ValueError(f"Engine '{engine}' does not support live streaming")

    if engine == "fasterwhispher":
        return StreamingSTT(
            model_name=model_name or spec["default_size"] or "base",
            device=device,
            language=language,
            task=task,
        )
    if engine == "indic":
        return IndicStreamingSTT(device=device, language=language, task=task)

    raise ValueError(f"Engine '{engine}' is marked streaming but has no implementation")