Introduce dataclasses for configuring model settings
This commit is contained in:
@@ -32,12 +32,13 @@ 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 ..model_settings import *
|
||||||
from ..models import openai_whisper
|
from ..models import openai_whisper
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ..stt_voice_messages import STTVoiceMessagesPlugin
|
from ..stt_voice_messages import STTVoiceMessagesPlugin
|
||||||
|
|
||||||
log = logging.getLogger('gajim.p.stt_voice_messages_config_dialog')
|
log = logging.getLogger('gajim.p.sttvm_config_dialog')
|
||||||
|
|
||||||
|
|
||||||
SUPPORTED_MODELS: dict[str, dict[str, typing.Union[list[str], Any, str]]] = {
|
SUPPORTED_MODELS: dict[str, dict[str, typing.Union[list[str], Any, str]]] = {
|
||||||
@@ -67,6 +68,7 @@ SUPPORTED_MODELS: dict[str, dict[str, typing.Union[list[str], Any, str]]] = {
|
|||||||
class Configuration:
|
class Configuration:
|
||||||
def __init__(self, plugin: STTVoiceMessagesPlugin):
|
def __init__(self, plugin: STTVoiceMessagesPlugin):
|
||||||
self._plugin = plugin
|
self._plugin = plugin
|
||||||
|
self._openaiwhisper_settings = OpenAIWhisperSettings()
|
||||||
self._available_models: dict[
|
self._available_models: dict[
|
||||||
str, dict[str, typing.Union[list[str], Any, str]]] = {}
|
str, dict[str, typing.Union[list[str], Any, str]]] = {}
|
||||||
self.check_available_moduls()
|
self.check_available_moduls()
|
||||||
@@ -84,14 +86,20 @@ class Configuration:
|
|||||||
value.strip()
|
value.strip()
|
||||||
log.debug('plugin config before:\n %s', self.plugin.config.data)
|
log.debug('plugin config before:\n %s', self.plugin.config.data)
|
||||||
self.plugin.config[data] = value
|
self.plugin.config[data] = value
|
||||||
|
self._plugin.config['model_instance'].on_setting(data, value)
|
||||||
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
||||||
|
|
||||||
def on_set_model(self, value: Any, data: Any) -> None:
|
def on_set_model(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)
|
log.debug('plugin config before:\n %s', self.plugin.config.data)
|
||||||
|
|
||||||
|
self._available_models[value]['model_instance'] = self._available_models[value]['class']()
|
||||||
|
|
||||||
self.plugin.config['model_class'] = self._available_models[value][
|
self.plugin.config['model_class'] = self._available_models[value][
|
||||||
'class']
|
'class']
|
||||||
|
self.plugin.config['model_instance'] = self._available_models[value]['model_instance']
|
||||||
|
|
||||||
self.on_setting(value, data)
|
self.on_setting(value, data)
|
||||||
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
||||||
|
|
||||||
|
|||||||
@@ -49,26 +49,29 @@ class STTBox(Gtk.Box):
|
|||||||
self.add(self._transcribe_button)
|
self.add(self._transcribe_button)
|
||||||
self.add(self._transcription_label)
|
self.add(self._transcription_label)
|
||||||
|
|
||||||
|
self._result = helper.Results('')
|
||||||
|
|
||||||
self._transcribe_button.connect('clicked', self._on_transcribe_clicked)
|
self._transcribe_button.connect('clicked', self._on_transcribe_clicked)
|
||||||
|
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
|
||||||
def _on_transcribe_clicked(self, _button: Gtk.Button) -> None:
|
def _on_transcribe_clicked(self, _button: Gtk.Button) -> None:
|
||||||
log.debug('config.data = %s', self._config.data)
|
log.debug('config.data = %s', self._config.data)
|
||||||
model_class = self._config.data['model_class']
|
model = self._config.data['model_instance']
|
||||||
if model_class is None:
|
if model is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._model = model_class()
|
self._model = model
|
||||||
|
|
||||||
transcription_task = helper.BackgroundTask(
|
transcription_task = helper.BackgroundTask(
|
||||||
self._model.transcribe(self._audio_file),
|
self._model.transcribe(self._result, self._audio_file),
|
||||||
self._show_result
|
self._show_result
|
||||||
)
|
)
|
||||||
transcription_task.start()
|
transcription_task.start()
|
||||||
|
|
||||||
def _show_result(self):
|
def _show_result(self):
|
||||||
self._text = self._model.result
|
assert self._model is not None
|
||||||
|
self._text = self._result.text
|
||||||
if self._text.strip() != '':
|
if self._text.strip() != '':
|
||||||
self._transcription_label.set_text(self._text.strip())
|
self._transcription_label.set_text(self._text.strip())
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -13,8 +13,17 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from gi.repository import Gio, GObject
|
from gi.repository import Gio, GObject
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Results:
|
||||||
|
text: str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
https://discourse.gnome.org/t/gtk-threading-problem-with-glib-idle-add/13597/5
|
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
|
https://github.com/gdm-settings/gdm-settings/blob/f245d3000200fa6be2a35c7f6ac45b131dadb5d6/src/utils.py#L116..L162
|
||||||
|
|||||||
23
stt_voice_messages/model_settings.py
Normal file
23
stt_voice_messages/model_settings.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# 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 dataclasses import dataclass, field
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class OpenAIWhisperSettings:
|
||||||
|
whisperai_model_size: str = field(default='tiny', init=True)
|
||||||
|
|
||||||
@@ -18,11 +18,13 @@ from pathlib import Path
|
|||||||
|
|
||||||
from gajim.gtk.const import Setting
|
from gajim.gtk.const import Setting
|
||||||
|
|
||||||
|
from ..helper import Results
|
||||||
|
|
||||||
|
|
||||||
class Model(ABC):
|
class Model(ABC):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def transcribe(self, audio_file: Path) -> str:
|
def transcribe(self, result: Results, audio_file: Path) -> str:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|||||||
@@ -13,12 +13,16 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import logging
|
||||||
import typing
|
import typing
|
||||||
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from ..helper import Results
|
||||||
|
from ..model_settings import OpenAIWhisperSettings
|
||||||
from .model import Model
|
from .model import Model
|
||||||
|
|
||||||
from gajim.gtk.const import Setting
|
log = logging.getLogger('gajim.p.sttvm_whisper')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import whisper
|
import whisper
|
||||||
@@ -27,27 +31,26 @@ except ModuleNotFoundError:
|
|||||||
if typing.TYPE_CHECKING:
|
if typing.TYPE_CHECKING:
|
||||||
import whisper
|
import whisper
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Configuration:
|
||||||
|
model_size: str
|
||||||
|
|
||||||
class WhisperModel(Model):
|
class WhisperModel(Model):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# TODO
|
# TODO
|
||||||
self._model_sizes = ['tiny', 'small', 'base', 'medium', 'large']
|
|
||||||
self._multilanguage = True
|
|
||||||
self._result: str = ''
|
self._result: str = ''
|
||||||
|
self._config = OpenAIWhisperSettings()
|
||||||
self._config = {
|
|
||||||
'model_size': 'tiny'
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def result(self) -> str:
|
def result(self) -> str:
|
||||||
return self._result
|
return self._result
|
||||||
|
|
||||||
def transcribe(self, audio_file: Path) -> str:
|
def transcribe(self, result: Results, audio_file: Path) -> str:
|
||||||
model = whisper.load_model(self._config['model_size'])
|
model = whisper.load_model(self._config['whisperai_model_size'])
|
||||||
result = model.transcribe(audio_file)
|
log.debug('model size is used = %s', self._config['whisperai_model_size'])
|
||||||
self._result = result['text']
|
result.text = model.transcribe(audio_file)['text']
|
||||||
|
|
||||||
def on_setting(self, setting: Setting):
|
def on_setting(self, key, value):
|
||||||
pass
|
log.debug('key = %s, value = %s', key, value)
|
||||||
|
self._config[key] = value
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user