Make Faster Whisper and OpenAI Whisper work
This commit is contained in:
60
stt_voice_messages/models/faster_whisper.py
Normal file
60
stt_voice_messages/models/faster_whisper.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user