"""Read-only Whisper QA, including the complete audio of long practice cues. Uses Transformers' documented sequential long-form decoder, without truncation or supplied reference text. Models must already exist locally. """ import argparse import json import os from pathlib import Path import subprocess from ming_narration import file_hash LANGUAGES = ('en', 'es', 'cs', 'de', 'fr', 'pt', 'ru', 'ja', 'ko', 'zh') MODEL = 'openai/whisper-small' REVISION = '973afd24965f72e36ca33b3055d56a652f456b4d' class Transcriber: def __init__(self, model_path, threads=2): os.environ.update(HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1', OMP_NUM_THREADS=str(threads), MKL_NUM_THREADS=str(threads)) import torch from transformers import WhisperProcessor, WhisperForConditionalGeneration torch.set_num_threads(threads) self.torch = torch self.processor = WhisperProcessor.from_pretrained(model_path, local_files_only=True) self.model = WhisperForConditionalGeneration.from_pretrained( model_path, local_files_only=True).eval() def transcribe(self, path, language): if language not in LANGUAGES: raise ValueError('Unsupported narration QA language') import numpy as np raw = subprocess.run(['ffmpeg', '-nostdin', '-v', 'error', '-threads', '1', '-i', str(path), '-f', 'f32le', '-ar', '16000', '-ac', '1', '-'], check=True, capture_output=True).stdout audio = np.frombuffer(raw, dtype='