diff --git a/stt_voice_messages/gtk/config_dialog.py b/stt_voice_messages/gtk/config_dialog.py index 6d5fe8d..d49335f 100644 --- a/stt_voice_messages/gtk/config_dialog.py +++ b/stt_voice_messages/gtk/config_dialog.py @@ -15,13 +15,23 @@ from __future__ import annotations -from dataclasses import asdict import logging import typing from pathlib import Path from typing import TYPE_CHECKING -import whisper +try: + import whisper +except ModuleNotFoundError: + if typing.TYPE_CHECKING: + import whisper + +try: + import faster_whisper as fwhisper +except ModuleNotFoundError: + if typing.TYPE_CHECKING: + import faster_whisper as fwhisper + from gi.repository import Gtk from gajim.common import app @@ -33,7 +43,7 @@ from gajim.gtk.sidebar_switcher import SideBarSwitcher from gajim.plugins.helpers import get_builder from gajim.plugins.plugins_i18n import _ -from ..models import openai_whisper +from ..models import faster_whisper, openai_whisper from ..models.model_settings import * if TYPE_CHECKING: @@ -56,18 +66,10 @@ SUPPORTED_MODELS: dict[str, Model] = { ['whisper'], openai_whisper.WhisperModel, OpenAIWhisperSettings), - 'model_ctranslate2': Model('CTranslate2', - ['ctranslate2'], - None, - None), - 'model_faster-whisper': Model('Fast-Whisper', - ['faster-whisper'], - None, - None), - 'model_distill': Model('Distill', - ['transformers', 'accelerate', 'datasets[audio]'], - None, - None) + 'model_faster-whisper': Model('Faster-Whisper', + ['faster_whisper'], + faster_whisper.FasterWhisperModel, + FasterWhisperSettings) } @@ -78,7 +80,7 @@ class Configuration: self._available_models: dict[str, Model] = {} self.check_available_moduls() - log.debug('config = %s', self._plugin.config['model_openaiwhisper']) + log.debug('config = %s', self._plugin.config) @property def plugin(self) -> STTVoiceMessagesPlugin: @@ -106,20 +108,20 @@ class Configuration: self._plugin.config.data[model].instance.set_config(self.plugin.config.data[model]) - def on_set_model(self, model: Any) -> None: - if isinstance(model, str): - model.strip() - log.debug('plugin config before:\n %s', self.plugin.config.data) - + def create_model(self, model: Any) -> None: if (self.plugin.config.data[model].instance is None and self._available_models[model].klass is not None): self.plugin.config.data[model].instance = \ self._available_models[model].klass() else: - return + log.debug('Could not create model %s', model) + + def on_set_model(self, model: Any, data: str = 'model') -> None: + if isinstance(model, str): + model.strip() self.plugin.config['model'] = model - log.debug('plugin config after:\n %s', self.plugin.config.data) + log.debug('Created model %s with config %s', model, self.plugin.config.data[model]) def check_available_moduls(self): def is_module_available(module: str) -> bool: @@ -146,6 +148,7 @@ class Configuration: log.debug('plugin config for model = %s', self.plugin.config[model]) self.plugin.config.data[model].instance = None self._available_models[model].config = self.plugin.config[model] + self.create_model(model) self.on_set_model(self._plugin.config['model']) @@ -192,12 +195,39 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow): prefs: list[tuple[str, type[PreferenceBox]]] = [ ('stt_behaviour', self.STTBehaviour), ('models', self.Models), - ('whisper_general', self.OpenAIWhisperGeneral), ] - self._add_prefs(prefs) + # TODO: Refactor this + if 'model_openaiwhisper' in config.available_models: + prefs.append(('openaiwhisper_general', self.OpenAIWhisperGeneral)) + else: + self._disable_pref('openai-whisper-viewport') # does not work yet + + if 'model_faster-whisper' in config.available_models: + prefs.append(('fasterwhisper_general', self.FasterWhisperGeneral)) + else: + self._disable_pref('faster-whisper') # does not work yet + + self._add_prefs(prefs) self.show_all() + def _add_prefs(self, prefs: list[tuple[str, type[PreferenceBox]]]): + for ui_name, klass in prefs: + pref_box = getattr(self._ui, ui_name) + pref = klass(self) # pyright: ignore + log.debug('ui_name = %s, klass = %s, pref_box = %s', ui_name, klass, pref_box) + pref_box.add(pref) + self._prefs[ui_name] = pref + + def _disable_pref(self, pref: str): + # TODO: Not scrolling to setting does not work! + pref_box = getattr(self._ui, pref) + log.debug('Disable Settings Page for %s', pref_box) + adj = Gtk.Adjustment(0, 0, 0) + pref_box.set_focus_hadjustment(adj) + pref_box.set_focus_vadjustment(adj) + + ############################################################################ # General Settings ############################################################################ @@ -266,9 +296,36 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow): def _set_config(self, value: Any, data: Any): self._config_dialog.config.on_config_model(self._model, value, data) - def _add_prefs(self, prefs: list[tuple[str, type[PreferenceBox]]]): - for ui_name, klass in prefs: - pref_box = getattr(self._ui, ui_name) - pref = klass(self) # pyright: ignore - pref_box.add(pref) - self._prefs[ui_name] = pref + ############################################################################ + # Faster Whisper Settings + ############################################################################ + class FasterWhisperGeneral(PreferenceBox): + def __init__(self, + config_dialog: STTVoiceMessagesConfigDialog) -> None: + self._model = 'model_faster-whisper' + self._config_dialog = config_dialog + + settings = [ + Setting(SettingKind.POPOVER, + _('Language Model Size'), + SettingType.VALUE, + value=config_dialog.config.available_models[ + self._model].config.model_size, + data='model_size', + callback=self._set_config, + props={'entries': fwhisper.available_models()}), + + Setting(SettingKind.SWITCH, + _('Translate'), + SettingType.VALUE, + value=config_dialog.config.available_models[ + self._model].config.translate_to_english, + data='translate_to_english', + callback=self._set_config) + ] + + PreferenceBox.__init__(self, settings) + + def _set_config(self, value: Any, data: Any): + self._config_dialog.config.on_config_model(self._model, value, + data) diff --git a/stt_voice_messages/gtk/config_dialog.ui b/stt_voice_messages/gtk/config_dialog.ui index 630d40d..026d1e2 100644 --- a/stt_voice_messages/gtk/config_dialog.ui +++ b/stt_voice_messages/gtk/config_dialog.ui @@ -127,14 +127,15 @@ - + + openai-whisper True True never in False - + True False @@ -145,7 +146,7 @@ 24 - + True False vertical @@ -220,19 +221,97 @@ - - - - - - - - - - - - - + + True + True + never + in + False + + + True + False + + + True + False + vertical + 24 + + + + True + False + vertical + 12 + + + True + False + General + 0 + + + + 0 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + True + 0 + + + + + + + + + + + + + + + + + + + + + faster-whisper + Faster Whisper + 2 +