"""Motor de transcrição (STT): faster-whisper na GPU (float16) ou na CPU (int8). Uso no CenaForge: legenda dos episódios. Além do texto, devolve segmentos e (opcional) palavras com tempo de início e fim — é o que o render precisa para gerar SRT/ASS. O áudio é decodificado em memória (PyAV), em qualquer formato comum. Nada de texto ou áudio em log. """ from __future__ import annotations import time from dataclasses import dataclass, field from pathlib import Path from typing import Protocol import numpy as np from .decode import decode_audio from .errors import AudioTooLong, InvalidAudio SAMPLE_RATE = 16000 # Idioma do contrato → código do Whisper. WHISPER_LANGUAGES = {"pt-BR": "pt", "en": "en", "es": "es"} # Arquivos de cada modelo (o turbo da mobiuslabs troca vocabulary.txt por .json e traz o preprocessor). MODEL_FILES = { "base": ("config.json", "model.bin", "tokenizer.json", "vocabulary.txt"), "small": ("config.json", "model.bin", "tokenizer.json", "vocabulary.txt"), "medium": ("config.json", "model.bin", "tokenizer.json", "vocabulary.txt"), "large-v3-turbo": ("config.json", "model.bin", "preprocessor_config.json", "tokenizer.json", "vocabulary.json"), } # text = só o texto (mais barato); segments = frases com tempo; words = frases + cada palavra com tempo. DETAILS = ("text", "segments", "words") @dataclass(frozen=True) class Word: start: float end: float text: str probability: float @dataclass(frozen=True) class Segment: start: float end: float text: str words: tuple[Word, ...] = () @dataclass(frozen=True) class Transcript: text: str no_speech_prob: float # média dos segmentos; 1.0 quando não houve segmento nenhum segment_count: int cpu_s: float # tempo de CPU do processo gasto na transcrição (para medir custo) segments: tuple[Segment, ...] = field(default=()) duration_s: float = 0.0 class SttEngine(Protocol): name: str def decode(self, data: bytes, max_seconds: float) -> np.ndarray: ... def transcribe(self, samples: np.ndarray, language: str, detail: str = "text") -> Transcript: ... def mean_no_speech(probs: list[float]) -> float: """Média do `no_speech_prob` dos segmentos, presa em 0..1. Sem segmento = sem fala (1.0).""" if not probs: return 1.0 return float(min(1.0, max(0.0, sum(probs) / len(probs)))) def join_text(parts: list[str], max_chars: int) -> str: text = " ".join(p.strip() for p in parts if p.strip()) return text[:max_chars] def decode_ogg_opus(data: bytes, max_seconds: float) -> np.ndarray: """Mantido do dropsz_voice: só OGG/Opus. Hoje o serviço usa `decode_audio` (qualquer formato).""" if not data.startswith(b"OggS"): raise InvalidAudio() return decode_audio(data, SAMPLE_RATE, max_seconds) class FasterWhisperEngine: """faster-whisper (CTranslate2) carregado da pasta de modelos local, nunca da internet.""" def __init__(self, model_dir: Path, model_name: str, device: str, compute_type: str, threads: int, beam_size: int, vad: bool, max_chars: int): from faster_whisper import WhisperModel # import tardio: os testes usam o motor falso self.name = f"faster-whisper-{model_name}-{device}-{compute_type}" self.device = device self._beam_size = beam_size self._vad = vad self._max_chars = max_chars self._model = WhisperModel( str(model_dir), device=device, compute_type=compute_type, cpu_threads=threads, num_workers=1, local_files_only=True, ) def decode(self, data: bytes, max_seconds: float) -> np.ndarray: return decode_audio(data, SAMPLE_RATE, max_seconds) def warmup(self) -> None: """Roda o decodificador de verdade (sem VAD: com VAD o silêncio nunca chega ao modelo). É aqui que o CTranslate2 carrega cuBLAS/cuDNN; se faltar DLL, a falha aparece na subida e não na primeira legenda.""" tone = (np.sin(np.arange(SAMPLE_RATE) * 2 * np.pi * 220 / SAMPLE_RATE) * 0.1).astype(np.float32) segments, _ = self._model.transcribe(tone, language="pt", beam_size=1, vad_filter=False) list(segments) def transcribe(self, samples: np.ndarray, language: str, detail: str = "text") -> Transcript: cpu_start = time.process_time() segments, _info = self._model.transcribe( samples, language=language, beam_size=self._beam_size, vad_filter=self._vad, condition_on_previous_text=False, # evita laço de alucinação em áudio curto/ruidoso without_timestamps=detail == "text", word_timestamps=detail == "words", ) texts: list[str] = [] probs: list[float] = [] items: list[Segment] = [] for segment in segments: # gerador: a transcrição acontece aqui texts.append(segment.text) probs.append(segment.no_speech_prob) if detail != "text": words = tuple( Word(round(w.start, 3), round(w.end, 3), w.word.strip(), round(w.probability, 3)) for w in (segment.words or ()) ) items.append(Segment(round(segment.start, 3), round(segment.end, 3), segment.text.strip(), words)) return Transcript( join_text(texts, self._max_chars), mean_no_speech(probs), len(probs), time.process_time() - cpu_start, tuple(items), samples.size / SAMPLE_RATE, ) class FakeSttEngine: """Motor falso para testes: "decodifica" bytes OggS e devolve um texto fixo.""" name = "fake-stt" def __init__(self, text: str = "valeu pela live", probs: tuple[float, ...] = (0.1, 0.3), delay_s: float = 0.0): self.text = text self.probs = probs self.delay_s = delay_s self.calls = 0 self.details: list[str] = [] def decode(self, data: bytes, max_seconds: float) -> np.ndarray: if not data.startswith(b"OggS"): raise InvalidAudio() # Convenção do falso: cada byte depois do cabeçalho vale 10 ms de áudio. samples = max(1, (len(data) - 4)) * SAMPLE_RATE // 100 if samples > max_seconds * SAMPLE_RATE: raise AudioTooLong() return np.zeros(samples, dtype=np.float32) def transcribe(self, samples: np.ndarray, language: str, detail: str = "text") -> Transcript: self.calls += 1 self.details.append(detail) if self.delay_s: time.sleep(self.delay_s) duration = samples.size / SAMPLE_RATE segments: tuple[Segment, ...] = () if detail != "text" and self.text: tokens = self.text.split() step = duration / max(1, len(tokens)) words = tuple(Word(round(i * step, 3), round((i + 1) * step, 3), t, 0.9) for i, t in enumerate(tokens)) segments = (Segment(0.0, round(duration, 3), self.text, words if detail == "words" else ()),) return Transcript(self.text, mean_no_speech(list(self.probs)), len(self.probs), 0.0, segments, duration)