Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: STT lang detector #289

Merged
merged 4 commits into from
Nov 24, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions ovos_plugin_manager/templates/stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from abc import ABCMeta, abstractmethod
from queue import Queue
from threading import Thread, Event
from typing import List, Tuple, Optional
from typing import List, Tuple, Optional, Set, Union

from ovos_config import Configuration
from ovos_utils import classproperty
from ovos_utils.log import deprecated
from ovos_utils.log import deprecated, LOG
from ovos_utils.process_utils import RuntimeRequirements
from ovos_utils.lang import standardize_lang_tag
from ovos_plugin_manager.utils.config import get_plugin_config
from ovos_plugin_manager.templates.transformers import AudioLanguageDetector


class STT(metaclass=ABCMeta):
Expand All @@ -31,6 +32,16 @@ def __init__(self, config=None):

self.can_stream = False
self._recognizer = None
self._detector = None

def bind(self, detector: AudioLanguageDetector):
self._detector = detector
LOG.debug(f"{self.__class__.__name__} - Assigned lang detector: {detector}")
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved

def detect_language(self, audio, valid_langs: Optional[Union[Set[str], List[str]]] = None) -> Tuple[str, float]:
if self._detector is None:
raise NotImplementedError(f"{self.__class__.__name__} does not support audio language detection")
return self._detector.detect(audio, valid_langs=valid_langs or self.available_languages)

@classproperty
def runtime_requirements(self):
Expand Down Expand Up @@ -124,10 +135,12 @@ def execute(self, audio, language: Optional[str] = None) -> str:
def transcribe(self, audio, lang: Optional[str] = None) -> List[Tuple[str, float]]:
"""transcribe audio data to a list of
possible transcriptions and respective confidences"""
if lang is not None and lang == "auto":
lang, prob = self.detect_language(audio, self.available_languages)
return [(self.execute(audio, lang), 1.0)]

@property
def available_languages(self) -> set:
def available_languages(self) -> Set[str]:
"""Return languages supported by this STT implementation in this state
This property should be overridden by the derived class to advertise
what languages that engine supports.
Expand Down
Loading