Skip to content

Commit

Permalink
feat/lang_detection_plugin (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Apr 20, 2024
1 parent f0ea727 commit 460027a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
15 changes: 13 additions & 2 deletions ovos_plugin_manager/audio_transformers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ovos_plugin_manager.utils import PluginTypes, PluginConfigTypes
from ovos_plugin_manager.templates.transformers import AudioTransformer
from ovos_utils.log import LOG

from ovos_plugin_manager.templates.transformers import AudioTransformer, AudioLanguageDetector
from ovos_plugin_manager.utils import PluginTypes, PluginConfigTypes


def find_plugins(*args, **kwargs):
# TODO: Deprecate in 0.1.0
Expand Down Expand Up @@ -57,3 +58,13 @@ def get_audio_transformer_module_configs(module_name: str):
"""
from ovos_plugin_manager.utils.config import load_plugin_configs
return load_plugin_configs(module_name, PluginConfigTypes.AUDIO_TRANSFORMER)


def find_audio_lang_detector_plugins() -> dict:
"""
Find all installed audio language detector plugins
@return: dict plugin names to entrypoints
"""
from ovos_plugin_manager.utils import find_plugins
return {k: p for k, p in find_plugins(PluginTypes.AUDIO_TRANSFORMER).items()
if issubclass(p, AudioLanguageDetector)}
26 changes: 24 additions & 2 deletions ovos_plugin_manager/templates/transformers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import List, Tuple
import abc
from typing import List, Tuple, Optional

from ovos_config.config import Configuration
from ovos_bus_client.util import get_mycroft_bus
from ovos_config.config import Configuration
from ovos_config.locale import get_default_lang
from ovos_utils.log import LOG

from ovos_plugin_manager.utils import ReadWriteStream

Expand Down Expand Up @@ -233,3 +236,22 @@ def transform(self, wav_file: str, context: dict = None) -> Tuple[str, dict]:
def default_shutdown(self):
""" perform any shutdown actions """
pass


class AudioLanguageDetector(AudioTransformer):

@property
def valid_langs(self) -> List[str]:
return list(
set([get_default_lang()] + Configuration().get("secondary_langs", []))
)

@abc.abstractmethod
def detect(self, audio_data: bytes, valid_langs: Optional[List] = None) -> Tuple[str, float]:
raise NotImplementedError

# plugin api
def transform(self, audio_data: bytes):
lang, prob = self.detect(audio_data)
LOG.info(f"Detected speech language '{lang}' with probability {prob}")
return audio_data, {"stt_lang": lang, "lang_probability": prob}

0 comments on commit 460027a

Please sign in to comment.