# 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 . from __future__ import annotations import logging from pathlib import Path from gi.repository import Gtk, Adw from gajim.plugins.gajimplugin import GajimPluginConfig from gajim.plugins.plugins_i18n import _ from .. import helper log = logging.getLogger('gajim.p.stt_voice_messages_sttbox') class STTBox(Gtk.Box): def __init__(self, config: GajimPluginConfig, audio_file: Path, ) -> None: Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL, spacing=6) self._config = config self._model = None self._audio_file = audio_file self._text = '' self._transcribe_button = Gtk.Button.new_from_icon_name("lucide-captions-symbolic") self._transcribe_button.set_tooltip_text(_('Transcribe voice message')) self._spinner = Adw.Spinner(valign=Gtk.Align.START, visible=False) self._transcription_label = Gtk.Label( label=_('Nothing transcribed yet')) self._transcription_label.set_max_width_chars(40) self._transcription_label.set_wrap(True) self.append(self._spinner) self.append(self._transcription_label) self._transcribe_button.connect('clicked', self._on_transcribe_clicked) self._result = helper.Results('') @property def button(self) -> Gtk.Button: return self._transcribe_button def _on_transcribe_clicked(self, _button: Gtk.Button) -> None: log.debug('config._instance = %s', self._config._instance) self._model = self._config._instance if self._model is None: return if self._model.is_loaded: text = _('Transcribing…') elif self._model.will_download: text = _('Downloading ') + self._model.model_id else: text = _('Loading model…') self._transcription_label.set_text(text) self._spinner.set_visible(True) self._task = helper.BackgroundTask( self._model.load, self._on_load_done) self._task.start() def _on_load_done(self): self._transcription_label.set_text(_('Transcribing…')) self._task = helper.BackgroundTask( lambda: self._model.recognize( self._result, helper.load_audio(self._audio_file)), self._show_result, ) self._task.start() def _show_result(self): assert self._model is not None self._text = self._result.text if self._text.strip() != '': self._transcription_label.set_text(self._text.strip()) else: self._transcription_label.set_text(_('_Have not heard any word!_')) self._spinner.set_visible(False)