Skip to content

Commit

Permalink
feat/ovos_dialog_tts_transformers
Browse files Browse the repository at this point in the history
listener has AudioTransformers
core has MetadataTransformers and UtteranceTransformers

this PR extends the concept to ovos-audio for text before TTS stage, and wav file after TTS but before playback

use case demonstration https://gist.github.com/JarbasAl/16788bdcff3fa5bfb3634d6f2116cc04

this ties into OpenVoiceOS/ovos-persona#4
  • Loading branch information
JarbasAl committed Oct 6, 2023
1 parent 159d2cd commit 1f81731
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ dist

# Created by unit tests
.pytest_cache/
/.gtm/
41 changes: 41 additions & 0 deletions ovos_plugin_manager/dialog_transformers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ovos_plugin_manager.templates.transformers import DialogTransformer, TTSTransformer
from ovos_plugin_manager.utils import PluginTypes
from ovos_plugin_manager.utils import load_plugin, find_plugins


def find_dialog_transformer_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.DIALOG_TRANSFORMER)


def load_dialog_transformer_plugin(module_name: str) -> type(DialogTransformer):
"""Wrapper function for loading dialog_transformer plugin.
Arguments:
(str) Mycroft dialog_transformer module name from config
Returns:
class: found dialog_transformer plugin class
"""
return load_plugin(module_name, PluginTypes.DIALOG_TRANSFORMER)


def find_tts_transformer_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.TTS_TRANSFORMER)


def load_tts_transformer_plugin(module_name: str) -> type(TTSTransformer):
"""Wrapper function for loading dialog_transformer plugin.
Arguments:
(str) Mycroft dialog_transformer module name from config
Returns:
class: found dialog_transformer plugin class
"""
return load_plugin(module_name, PluginTypes.TTS_TRANSFORMER)
68 changes: 66 additions & 2 deletions ovos_plugin_manager/templates/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class MetadataTransformer:

""" runs after utterance transformers and before intent service"""
def __init__(self, name, priority=50, config=None):
self.name = name
self.bus = None
Expand Down Expand Up @@ -41,7 +41,7 @@ def default_shutdown(self):


class UtteranceTransformer:

""" runs before metadata transformers and intent service"""
def __init__(self, name, priority=50, config=None):
self.name = name
self.bus = None
Expand Down Expand Up @@ -165,3 +165,67 @@ def transform(self, audio_data):
def default_shutdown(self):
""" perform any shutdown actions """
pass


class DialogTransformer:
""" runs before TTS stage"""
def __init__(self, name, priority=50, config=None):
self.name = name
self.bus = None
self.priority = priority
if not config:
config_core = dict(Configuration())
config = config_core.get("dialog_transformers", {}).get(self.name)
self.config = config or {}

def bind(self, bus=None):
""" attach messagebus """
self.bus = bus or get_mycroft_bus()

def initialize(self):
""" perform any initialization actions """
pass

def transform(self, dialog: str) -> str:
"""
Optionally transform passed dialog and/or return additional context
:param dialog: str utterance to mutate before TTS
:returns: str mutated dialog
"""
return dialog, {}

def default_shutdown(self):
""" perform any shutdown actions """
pass


class TTSTransformer:
""" runs after TTS stage but before playback"""
def __init__(self, name, priority=50, config=None):
self.name = name
self.bus = None
self.priority = priority
if not config:
config_core = dict(Configuration())
config = config_core.get("dialog_transformers", {}).get(self.name)
self.config = config or {}

def bind(self, bus=None):
""" attach messagebus """
self.bus = bus or get_mycroft_bus()

def initialize(self):
""" perform any initialization actions """
pass

def transform(self, wav_file: str, context: dict = None) -> str:
"""
Optionally transform passed wav_file and return path to transformed file
:param wav_file: path to wav file generated in TTS stage
:returns: path to transformed wav file for playback
"""
return wav_file

def default_shutdown(self):
""" perform any shutdown actions """
pass
4 changes: 4 additions & 0 deletions ovos_plugin_manager/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class PluginTypes(str, Enum):
UTTERANCE_TRANSFORMER = "neon.plugin.text"
METADATA_TRANSFORMER = "neon.plugin.metadata"
AUDIO_TRANSFORMER = "neon.plugin.audio"
DIALOG_TRANSFORMER = "opm.transformer.dialog"
TTS_TRANSFORMER = "opm.transformer.tts"
QUESTION_SOLVER = "neon.plugin.solver"
TLDR_SOLVER = "opm.solver.summarization"
ENTAILMENT_SOLVER = "opm.solver.entailment"
Expand Down Expand Up @@ -71,6 +73,8 @@ class PluginConfigTypes(str, Enum):
UTTERANCE_TRANSFORMER = "neon.plugin.text.config"
METADATA_TRANSFORMER = "neon.plugin.metadata.config"
AUDIO_TRANSFORMER = "neon.plugin.audio.config"
DIALOG_TRANSFORMER = "opm.transformer.dialog.config"
TTS_TRANSFORMER = "opm.transformer.tts.config"
QUESTION_SOLVER = "neon.plugin.solver.config"
TLDR_SOLVER = "opm.solver.summarization.config"
ENTAILMENT_SOLVER = "opm.solver.entailment.config"
Expand Down

0 comments on commit 1f81731

Please sign in to comment.