Works but circular import
This commit is contained in:
@@ -17,7 +17,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import whisper
|
||||
from gi.repository import Gtk
|
||||
@@ -30,25 +30,12 @@ from gajim.gtk.sidebar_switcher import SideBarSwitcher
|
||||
from gajim.plugins.helpers import get_builder
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
from ..configs import *
|
||||
from ..configs import Configuration
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import stt_voice_messages
|
||||
from ..stt_voice_messages import STTVoiceMessagesPlugin
|
||||
|
||||
log = logging.getLogger('gajim.p.stt_voice_messages_config')
|
||||
|
||||
|
||||
@staticmethod
|
||||
def check_module(module: str) -> bool:
|
||||
try:
|
||||
__import__(module)
|
||||
return True
|
||||
except ModuleNotFoundError:
|
||||
log.debug('Could not find module %s', module)
|
||||
return False
|
||||
except ImportError as ex:
|
||||
log.debug(str(ex))
|
||||
return False
|
||||
log = logging.getLogger('gajim.p.stt_voice_messages_config_dialog')
|
||||
|
||||
|
||||
class PreferenceBox(SettingsBox):
|
||||
@@ -65,10 +52,8 @@ class PreferenceBox(SettingsBox):
|
||||
|
||||
|
||||
class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
||||
def __init__(self, plugin: stt_voice_messages.STTVoiceMessagesPlugin,
|
||||
parent: Gtk.Window) -> None:
|
||||
def __init__(self, plugin: STTVoiceMessagesPlugin, parent: Gtk.Window) -> None:
|
||||
Gtk.ApplicationWindow.__init__(self)
|
||||
self.plugin = plugin
|
||||
|
||||
self.set_application(app.app)
|
||||
self.set_position(Gtk.WindowPosition.CENTER)
|
||||
@@ -97,34 +82,35 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
||||
self.show_all()
|
||||
|
||||
class STTBehaviour(PreferenceBox):
|
||||
def __init__(self, config_dialog: STTVoiceMessagesConfigDialog) -> None:
|
||||
def __init__(self, config: Configuration) -> None:
|
||||
|
||||
settings = [
|
||||
Setting(SettingKind.SWITCH,
|
||||
_('Auto Transcribe'),
|
||||
SettingType.VALUE,
|
||||
value=config_dialog.plugin.config['auto_transcribe'],
|
||||
value=config.plugin.config['auto_transcribe'],
|
||||
data='auto_transcribe',
|
||||
callback=config_dialog._on_setting)
|
||||
callback=config.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)')),
|
||||
]
|
||||
def __init__(self, config: Configuration) -> None:
|
||||
models: list[tuple[str, str]] = []
|
||||
for key, value in config.available_models.items():
|
||||
assert value['name'] is str
|
||||
models.append(
|
||||
(key, value['name'])
|
||||
)
|
||||
|
||||
settings = [
|
||||
Setting(SettingKind.COMBO,
|
||||
_('Speech To Text Model'),
|
||||
SettingType.VALUE,
|
||||
value=config_dialog.plugin.config['model'],
|
||||
value=config.plugin.config['model'],
|
||||
data='model',
|
||||
callback=config_dialog._on_setting,
|
||||
callback=config.on_set_model,
|
||||
props={'combo_items': models},
|
||||
desc=_('Choose Model to use')),
|
||||
]
|
||||
@@ -132,23 +118,23 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
||||
PreferenceBox.__init__(self, settings)
|
||||
|
||||
class OpenAIWhisperGeneral(PreferenceBox):
|
||||
def __init__(self, config_dialog: STTVoiceMessagesConfigDialog) -> None:
|
||||
def __init__(self, config: Configuration) -> None:
|
||||
|
||||
settings = [
|
||||
Setting(SettingKind.POPOVER,
|
||||
_('Language Model Size'),
|
||||
SettingType.VALUE,
|
||||
value=config_dialog.plugin.config['whisperai_model_size'],
|
||||
value=config.plugin.config['whisperai_model_size'],
|
||||
data='whisperai_model_size',
|
||||
callback=config_dialog._on_setting,
|
||||
callback=config.on_setting,
|
||||
props={'entries': whisper.available_models()}),
|
||||
|
||||
Setting(SettingKind.SWITCH,
|
||||
_('Translate'),
|
||||
SettingType.VALUE,
|
||||
value=config_dialog.plugin.config['whisperai_translate'],
|
||||
value=config.plugin.config['whisperai_translate'],
|
||||
data='whisperai_translate',
|
||||
callback=config_dialog._on_setting)
|
||||
callback=config.on_setting)
|
||||
]
|
||||
|
||||
PreferenceBox.__init__(self, settings)
|
||||
@@ -160,9 +146,4 @@ class STTVoiceMessagesConfigDialog(Gtk.ApplicationWindow):
|
||||
pref_box.add(pref)
|
||||
self._prefs[ui_name] = pref
|
||||
|
||||
def _on_setting(self, value: Any, data: Any) -> None:
|
||||
if isinstance(value, str):
|
||||
value.strip()
|
||||
log.debug('plugin config before:\n %s', self.plugin.config.data)
|
||||
self.plugin.config[data] = value
|
||||
log.debug('plugin config after:\n %s', self.plugin.config.data)
|
||||
|
||||
|
||||
@@ -14,10 +14,14 @@
|
||||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from gi.repository import Gtk
|
||||
import logging
|
||||
|
||||
from .. import helper
|
||||
|
||||
from gajim.plugins.gajimplugin import GajimPluginConfig
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
log = logging.getLogger('gajim.p.stt_voice_messages_sttbox')
|
||||
|
||||
class STTBox(Gtk.Box):
|
||||
def __init__(self,
|
||||
@@ -48,15 +52,25 @@ class STTBox(Gtk.Box):
|
||||
|
||||
self.show_all()
|
||||
|
||||
def _on_transcribe_clicked(self, _button: Gtk.Button):
|
||||
#transcription_task = helper.BackgroundTask(
|
||||
# self._model.transcribe(),
|
||||
# self._show_result
|
||||
#)
|
||||
#transcription_task.start()
|
||||
pass
|
||||
#def update_config(self, config: GajimPluginConfig):
|
||||
# self._model = config.data['class']()
|
||||
|
||||
def _on_transcribe_clicked(self, _button: Gtk.Button) -> None:
|
||||
log.debug('config.data = %s', self._config.data)
|
||||
model_class = self._config.data['model_class']
|
||||
if model_class is None:
|
||||
return
|
||||
|
||||
self._model = model_class()
|
||||
|
||||
transcription_task = helper.BackgroundTask(
|
||||
self._model.transcribe(self._audio_file),
|
||||
self._show_result
|
||||
)
|
||||
transcription_task.start()
|
||||
|
||||
def _show_result(self):
|
||||
self._text = self._model.result
|
||||
if self._text.strip() != '':
|
||||
self._transcription_label.set_text(self._text.strip())
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user