158 lines
5.0 KiB
Python
158 lines
5.0 KiB
Python
# 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/>.
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from functools import partial
|
|
from pathlib import Path
|
|
|
|
import whisper
|
|
from gi.repository import Gio, GObject, Gtk
|
|
from stt_voice_messages.config_dialog import STTVoiceMessagesConfigDialog
|
|
|
|
from gajim.plugins import GajimPlugin
|
|
from gajim.plugins.plugins_i18n import _
|
|
|
|
log = logging.getLogger('gajim.p.stt_voice_messages')
|
|
|
|
|
|
class STTVoiceMessagesPlugin(GajimPlugin):
|
|
def init(self) -> None:
|
|
self.description = _('Transcribes voice messages to text.')
|
|
self.config_dialog = partial(STTVoiceMessagesConfigDialog, self)
|
|
|
|
self.gui_extension_points = {
|
|
'preview_audio': (self._preview_audio_created, None),
|
|
}
|
|
|
|
self.config_default_values = {
|
|
'use_multilanguage_model': (True, ''),
|
|
'model_size': ('base', '')
|
|
}
|
|
|
|
self._audio_file = None
|
|
self._preview_audio_widget = None
|
|
self._stt_box = None
|
|
|
|
def _preview_audio_created(self,
|
|
preview_audio_widget: Gtk.Box,
|
|
audio_file: Path
|
|
) -> None:
|
|
self._preview_audio_widget = preview_audio_widget
|
|
self._audio_file = audio_file.as_posix()
|
|
self._create_stt_box()
|
|
|
|
def _create_stt_box(self) -> None:
|
|
assert self._preview_audio_widget is not None
|
|
self._stt_box = STTBox(self._preview_audio_widget,
|
|
self.config,
|
|
self._audio_file)
|
|
self._preview_audio_widget.pack_end(self._stt_box, False, False, 0)
|
|
|
|
class STTBox(Gtk.Box):
|
|
def __init__(self,
|
|
preview_audio_widget: Gtk.Box,
|
|
config: GajimPluginConfig,
|
|
audio_file: Path,
|
|
) -> None:
|
|
|
|
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL, spacing=12)
|
|
|
|
self._config = config
|
|
self._preview_audio = preview_audio_widget
|
|
self._audio_file = audio_file
|
|
self._text = ''
|
|
|
|
self._transcribe_button = Gtk.Button(label=_('Transcribe'))
|
|
|
|
self._transcription_label = Gtk.Label(label=_('Nothing transcribed yet'))
|
|
self._transcription_label.set_max_width_chars(40)
|
|
self._transcription_label.set_line_wrap(True)
|
|
|
|
self.add(self._transcribe_button)
|
|
self.add(self._transcription_label)
|
|
|
|
self._transcribe_button.connect('clicked', self._on_transcribe_clicked)
|
|
|
|
self.show_all()
|
|
|
|
def _on_transcribe_clicked(self, _button: Gtk.Button):
|
|
transcription_task = BackgroundTask(
|
|
self._trascribe_by_whisper,
|
|
self._show_result
|
|
)
|
|
transcription_task.start()
|
|
|
|
def _show_result(self):
|
|
if self._text.strip() != '':
|
|
self._transcription_label.set_text(self._text.strip())
|
|
else:
|
|
self._transcription_label.set_text(_('_Have not heard any word!_'))
|
|
|
|
def _trascribe_by_whisper(self) -> str:
|
|
model = whisper.load_model(self._config['model_size'])
|
|
result = model.transcribe(self._audio_file)
|
|
self._text = result["text"]
|
|
return text
|
|
|
|
|
|
'''
|
|
https://discourse.gnome.org/t/gtk-threading-problem-with-glib-idle-add/13597/5
|
|
https://github.com/gdm-settings/gdm-settings/blob/f245d3000200fa6be2a35c7f6ac45b131dadb5d6/src/utils.py#L116..L162
|
|
'''
|
|
class BackgroundTask (GObject.Object):
|
|
__gtype_name__ = 'BackgroundTask'
|
|
|
|
def __init__ (self, function, finish_callback, **kwargs):
|
|
super().__init__(**kwargs)
|
|
|
|
self.function = function
|
|
self.finish_callback = finish_callback
|
|
self._current = None
|
|
|
|
def start(self):
|
|
if self._current:
|
|
AlreadyRunningError('Task is already running')
|
|
|
|
finish_callback = lambda self, task, nothing: self.finish_callback()
|
|
|
|
task = Gio.Task.new(self, None, finish_callback, None)
|
|
task.run_in_thread(self._thread_cb)
|
|
|
|
self._current = task
|
|
|
|
@staticmethod
|
|
def _thread_cb (task, self, task_data, cancellable):
|
|
try:
|
|
retval = self.function()
|
|
task.return_value(retval)
|
|
except Exception as e:
|
|
task.return_value(e)
|
|
|
|
def finish (self):
|
|
task = self._current
|
|
self._current = None
|
|
|
|
if not Gio.Task.is_valid(task, self):
|
|
raise InvalidGioTaskError()
|
|
|
|
value = task.propagate_value().value
|
|
|
|
if isinstance(value, Exception):
|
|
raise value
|
|
|
|
return value
|