from typing import Any import whisper from utils import TimelineEvent class Transcriber: """This class performs transcription of the audio file.""" @staticmethod def get_models_list() -> list[str]: """Returns list of allowed `model` values for constructor.""" return whisper.available_models() def __init__(self, model: str, **kwargs) -> None: """Create the transcriber instance. Args: - model - model to use (call `get_models_list` to get the list of available models) - **kwargs are passed to `whisper.load_model(...)` """ allowed_models = self.get_models_list() if model not in allowed_models: raise ValueError( f"Model `{model}` is not available. " f"Use one of {', '.join(f'`{s}`' for s in allowed_models)}" ) self._model = whisper.load_model(model, **kwargs) def transcribe(self, path: str, **kwargs) -> list[TimelineEvent]: """Transcribe audiofile. The operation will take a lot of time for large files. Args: - path - path to the file to transcribe. - **kwargs - passed to `transcribe()` Returns: - list of timeline events you should use """ raw_segments: list[dict] raw_segments = self._model.transcribe(path, **kwargs)["segments"] # type: ignore result: list[TimelineEvent] = [] seg_id: int = 0 for raw_segment in raw_segments: ev = TimelineEvent( id = f"asr_{seg_id}", timestamp=float(raw_segment["start"]), duration=float(raw_segment["end"]) - float(raw_segment["start"]), payload=raw_segment["text"], custom={ "whisper_temperature": float(raw_segment["temperature"]), "whisper_avg_logprob": float(raw_segment["avg_logprob"]), "whisper_no_speech_prob": float(raw_segment["no_speech_prob"]) }, links=[] ) result.append(ev) seg_id += 1 return result