# This file is part of Gajim. # # Gajim is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Gajim is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Gajim. If not, see . import logging import typing from dataclasses import dataclass from pathlib import Path from ..helper import Results from .model_settings import FasterWhisperSettings from .model_template import Model log = logging.getLogger('gajim.p.sttvm_faster_whisper') try: import faster_whisper except ModuleNotFoundError: if typing.TYPE_CHECKING: import faster_whisper @dataclass class Configuration: model_size: str class FasterWhisperModel(Model): def __init__(self): self._result: str = '' self._config = FasterWhisperSettings() @property def result(self) -> str: return self._result def transcribe(self, result: Results, audio_file: Path) -> None: model = faster_whisper.WhisperModel(self._config.model_size, compute_type="float32") log.debug('model size is used = %s', self._config.model_size) segments, _ = model.transcribe(audio_file) segments = list(segments) result.text = '' log.debug('segments = %s', segments) for segment in segments: result.text += segment.text print("[%.2fs -> %.2fs] %s" % ( segment.start, segment.end, segment.text)) def set_config(self, config: FasterWhisperSettings) -> None: self._config = config