Use comboboxes and nested classes
This commit is contained in:
@@ -19,8 +19,8 @@ import logging
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from gi.repository import Gtk
|
|
||||||
import whisper
|
import whisper
|
||||||
|
from gi.repository import Gtk
|
||||||
|
|
||||||
from gajim.common import app
|
from gajim.common import app
|
||||||
from gajim.gtk.builder import get_builder
|
from gajim.gtk.builder import get_builder
|
||||||
@@ -30,7 +30,6 @@ from gajim.gtk.sidebar_switcher import SideBarSwitcher
|
|||||||
from gajim.plugins.helpers import get_builder
|
from gajim.plugins.helpers import get_builder
|
||||||
from gajim.plugins.plugins_i18n import _
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
from ..models import openai_whisper
|
|
||||||
from ..configs import *
|
from ..configs import *
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -39,10 +38,7 @@ if TYPE_CHECKING:
|
|||||||
log = logging.getLogger('gajim.p.stt_voice_messages_config')
|
log = logging.getLogger('gajim.p.stt_voice_messages_config')
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
@staticmethod
|
||||||
# Helper
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
def check_module(module: str) -> bool:
|
def check_module(module: str) -> bool:
|
||||||
try:
|
try:
|
||||||
__import__(module)
|
__import__(module)
|
||||||
@@ -55,9 +51,18 @@ def check_module(module: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
class PreferenceBox(SettingsBox):
|
||||||
# Plugin Settings
|
def __init__(self, settings: list[Setting]) -> None:
|
||||||
################################################################################
|
SettingsBox.__init__(self, None)
|
||||||
|
self.get_style_context().add_class('border')
|
||||||
|
self.set_selection_mode(Gtk.SelectionMode.NONE)
|
||||||
|
self.set_vexpand(False)
|
||||||
|
self.set_valign(Gtk.Align.END)
|
||||||
|
|
||||||
|
for setting in settings:
|
||||||
|
self.add_setting(setting)
|
||||||
|
self.update_states()
|
||||||
|
|
||||||
|
|
||||||
class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
||||||
def __init__(self, plugin: stt_voice_messages.STTVoiceMessagesPlugin,
|
def __init__(self, plugin: stt_voice_messages.STTVoiceMessagesPlugin,
|
||||||
@@ -83,128 +88,81 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
|||||||
self.add(self._ui.grid)
|
self.add(self._ui.grid)
|
||||||
|
|
||||||
prefs: list[tuple[str, type[PreferenceBox]]] = [
|
prefs: list[tuple[str, type[PreferenceBox]]] = [
|
||||||
('stt_behaviour', STTBehaviour),
|
('stt_behaviour', self.STTBehaviour),
|
||||||
('models', Models),
|
('models', self.Models),
|
||||||
('file_preview', FilePreview),
|
('whisper_general', self.OpenAIWhisperGeneral),
|
||||||
('whisper_general', OpenAIWhisperGeneral),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
self._add_prefs(prefs)
|
self._add_prefs(prefs)
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
|
||||||
|
class STTBehaviour(PreferenceBox):
|
||||||
|
def __init__(self, config_dialog: STTVoiceMessagesConfigDialog) -> None:
|
||||||
|
|
||||||
|
settings = [
|
||||||
|
Setting(SettingKind.SWITCH,
|
||||||
|
_('Auto Transcribe'),
|
||||||
|
SettingType.VALUE,
|
||||||
|
value=config_dialog.plugin.config['auto_transcribe'],
|
||||||
|
data='auto_transcribe',
|
||||||
|
callback=config_dialog._on_setting)
|
||||||
|
]
|
||||||
|
|
||||||
|
PreferenceBox.__init__(self, settings)
|
||||||
|
|
||||||
|
class Models(PreferenceBox):
|
||||||
|
def __init__(self, config_dialog: STTVoiceMessagesConfigDialog) -> None:
|
||||||
|
models: list[tuple[str, str]] = [
|
||||||
|
('model_openai', _('OpenAI Whisper')),
|
||||||
|
('model_ctranslate2', _('CTranslate2 (not impl)')),
|
||||||
|
('model_distill', _('Distill (not impl)')),
|
||||||
|
]
|
||||||
|
|
||||||
|
settings = [
|
||||||
|
Setting(SettingKind.COMBO,
|
||||||
|
_('Speech To Text Model'),
|
||||||
|
SettingType.VALUE,
|
||||||
|
value=config_dialog.plugin.config['model'],
|
||||||
|
data='model',
|
||||||
|
callback=config_dialog._on_setting,
|
||||||
|
props={'combo_items': models},
|
||||||
|
desc=_('Choose Model to use')),
|
||||||
|
]
|
||||||
|
|
||||||
|
PreferenceBox.__init__(self, settings)
|
||||||
|
|
||||||
|
class OpenAIWhisperGeneral(PreferenceBox):
|
||||||
|
def __init__(self, config_dialog: STTVoiceMessagesConfigDialog) -> None:
|
||||||
|
|
||||||
|
settings = [
|
||||||
|
Setting(SettingKind.POPOVER,
|
||||||
|
_('Language Model Size'),
|
||||||
|
SettingType.VALUE,
|
||||||
|
value=config_dialog.plugin.config['whisperai_model_size'],
|
||||||
|
data='whisperai_model_size',
|
||||||
|
callback=config_dialog._on_setting,
|
||||||
|
props={'entries': whisper.available_models()}),
|
||||||
|
|
||||||
|
Setting(SettingKind.SWITCH,
|
||||||
|
_('Translate'),
|
||||||
|
SettingType.VALUE,
|
||||||
|
value=config_dialog.plugin.config['whisperai_translate'],
|
||||||
|
data='whisperai_translate',
|
||||||
|
callback=config_dialog._on_setting)
|
||||||
|
]
|
||||||
|
|
||||||
|
PreferenceBox.__init__(self, settings)
|
||||||
|
|
||||||
def _add_prefs(self, prefs: list[tuple[str, type[PreferenceBox]]]):
|
def _add_prefs(self, prefs: list[tuple[str, type[PreferenceBox]]]):
|
||||||
for ui_name, klass in prefs:
|
for ui_name, klass in prefs:
|
||||||
pref_box = getattr(self._ui, ui_name)
|
pref_box = getattr(self._ui, ui_name)
|
||||||
pref = klass(self) # pyright: ignore
|
pref = klass(self) # pyright: ignore
|
||||||
print("pref = ", pref)
|
|
||||||
pref_box.add(pref)
|
pref_box.add(pref)
|
||||||
self._prefs[ui_name] = pref
|
self._prefs[ui_name] = pref
|
||||||
|
|
||||||
def _on_setting(self, value: Any, data: Any) -> None:
|
def _on_setting(self, value: Any, data: Any) -> None:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
value.strip()
|
value.strip()
|
||||||
|
log.debug('plugin config before:\n %s', self.plugin.config.data)
|
||||||
self.plugin.config[data] = value
|
self.plugin.config[data] = value
|
||||||
self.plugin.update()
|
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
||||||
|
|
||||||
|
|
||||||
class PreferenceBox(SettingsBox):
|
|
||||||
def __init__(self, settings: list[Setting]) -> None:
|
|
||||||
SettingsBox.__init__(self, None)
|
|
||||||
self.get_style_context().add_class('border')
|
|
||||||
self.set_selection_mode(Gtk.SelectionMode.NONE)
|
|
||||||
self.set_vexpand(False)
|
|
||||||
self.set_valign(Gtk.Align.END)
|
|
||||||
|
|
||||||
for setting in settings:
|
|
||||||
self.add_setting(setting)
|
|
||||||
self.update_states()
|
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# General Preferences
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
class STTBehaviour(PreferenceBox):
|
|
||||||
def __init__(self, *args: Any) -> None:
|
|
||||||
main_window_on_startup_items = {
|
|
||||||
'always': _('Always'),
|
|
||||||
'never': _('Never'),
|
|
||||||
'last_state': _('Restore last state'),
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = [
|
|
||||||
Setting(SettingKind.POPOVER,
|
|
||||||
_('Show on Startup'),
|
|
||||||
SettingType.CONFIG,
|
|
||||||
'show_main_window_on_startup',
|
|
||||||
props={'entries': main_window_on_startup_items},
|
|
||||||
desc=_('Show window when starting Gajim')),
|
|
||||||
]
|
|
||||||
|
|
||||||
PreferenceBox.__init__(self, settings)
|
|
||||||
|
|
||||||
|
|
||||||
class Models(PreferenceBox):
|
|
||||||
def __init__(self, *args: Any) -> None:
|
|
||||||
main_window_on_startup_items = {
|
|
||||||
'always': _('Always'),
|
|
||||||
'never': _('Never'),
|
|
||||||
'last_state': _('Restore last state'),
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = [
|
|
||||||
Setting(SettingKind.POPOVER,
|
|
||||||
_('Show on Startup'),
|
|
||||||
SettingType.CONFIG,
|
|
||||||
'show_main_window_on_startup',
|
|
||||||
props={'entries': main_window_on_startup_items},
|
|
||||||
desc=_('Show window when starting Gajim')),
|
|
||||||
]
|
|
||||||
|
|
||||||
PreferenceBox.__init__(self, settings)
|
|
||||||
|
|
||||||
|
|
||||||
class FilePreview(PreferenceBox):
|
|
||||||
def __init__(self, *args: Any) -> None:
|
|
||||||
main_window_on_startup_items = {
|
|
||||||
'always': _('Always'),
|
|
||||||
'never': _('Never'),
|
|
||||||
'last_state': _('Restore last state'),
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = [
|
|
||||||
Setting(SettingKind.POPOVER,
|
|
||||||
_('Show on Startup'),
|
|
||||||
SettingType.CONFIG,
|
|
||||||
'show_main_window_on_startup',
|
|
||||||
props={'entries': main_window_on_startup_items},
|
|
||||||
desc=_('Show window when starting Gajim')),
|
|
||||||
]
|
|
||||||
|
|
||||||
PreferenceBox.__init__(self, settings)
|
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# Whisper Settings UI
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
class OpenAIWhisperGeneral(PreferenceBox):
|
|
||||||
def __init__(self, *args: Any) -> None:
|
|
||||||
self.config = PluginConfig().openaiwhisper
|
|
||||||
|
|
||||||
settings = [
|
|
||||||
Setting(SettingKind.POPOVER,
|
|
||||||
_('Language Model Size'),
|
|
||||||
SettingType.VALUE,
|
|
||||||
value=str(self.config['model_size']),
|
|
||||||
data='model_size',
|
|
||||||
callback=self._on_setting,
|
|
||||||
props={'entries': whisper.available_models()}),
|
|
||||||
]
|
|
||||||
|
|
||||||
PreferenceBox.__init__(self, settings)
|
|
||||||
|
|
||||||
def _on_setting(self, value: Any, data: Any) -> None:
|
|
||||||
print("before: ", self.config)
|
|
||||||
self.config[data] = value
|
|
||||||
print("after: ", self.config)
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class WhisperModel(Model):
|
|||||||
def transcribe(self, audio_file: Path) -> str:
|
def transcribe(self, audio_file: Path) -> str:
|
||||||
model = whisper.load_model(self._config['model_size'])
|
model = whisper.load_model(self._config['model_size'])
|
||||||
result = model.transcribe(audio_file)
|
result = model.transcribe(audio_file)
|
||||||
return result["text"]
|
return result['text']
|
||||||
|
|
||||||
def on_setting(self, setting: Setting):
|
def on_setting(self, setting: Setting):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -39,6 +39,13 @@ class STTVoiceMessagesPlugin(GajimPlugin):
|
|||||||
'preview_audio': (self._on_preview_audio_created, None),
|
'preview_audio': (self._on_preview_audio_created, None),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.config_default_values = {
|
||||||
|
'auto_transcribe': (False, ''),
|
||||||
|
'model': ('model_openai', ''),
|
||||||
|
'whisperai_model_size': ('tiny', ''),
|
||||||
|
'whisperai_translate': (False, ''),
|
||||||
|
}
|
||||||
|
|
||||||
self._audio_file: str = ''
|
self._audio_file: str = ''
|
||||||
self._preview_audio_widget = None
|
self._preview_audio_widget = None
|
||||||
self._stt_box = None
|
self._stt_box = None
|
||||||
|
|||||||
Reference in New Issue
Block a user