Skip to content

Commit

Permalink
Add handler for Neon core profile updates
Browse files Browse the repository at this point in the history
Add `_languages` dict with handling of language API responses
Updates default config to include languages supported in default Neon Core installation
Closes #35
  • Loading branch information
NeonDaniel committed Nov 20, 2023
1 parent 68378bd commit f2b65d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
7 changes: 6 additions & 1 deletion docker_overlay/etc/neon/neon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ iris:
- en-us
- fr-fr
- es-es
- uk-ua
- de-de
- it-it
- uk-ua
- nl-nl
- pt-pt
- ca-es
41 changes: 40 additions & 1 deletion neon_iris/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __init__(self, mq_config: dict = None, config_dir: str = None):
self.client_name = "unknown"
self._config = mq_config or dict(Configuration()).get("MQ")
self._connection = self._init_mq_connection()

self._languages = dict()
self._language_init = Event()
config_dir = config_dir or join(xdg_config_home(), "neon", "neon_iris")

self._user_config = get_neon_user_config(config_dir)
Expand All @@ -68,6 +69,21 @@ def __init__(self, mq_config: dict = None, config_dir: str = None):
self.audio_cache_dir = join(xdg_cache_home(), "neon", "neon_iris")
makedirs(self.audio_cache_dir, exist_ok=True)

# Collect supported languages
message = self._build_message("ovos.languages.stt", {})
self._send_message(message)
message = self._build_message("ovos.languages.tts", {})
self._send_message(message)
if self._language_init.wait(30):
LOG.info(f"Got language support: {self._languages}")
else:
self._languages['stt'] = self._config.get('iris',
{}).get('languages')
self._languages['tts'] = self._config.get('iris',
{}).get('languages')
LOG.warning(f"Timed out updating languages. Using configuration: "
f"{self._languages}")

@property
def uid(self) -> str:
"""
Expand Down Expand Up @@ -143,6 +159,10 @@ def handle_neon_response(self, channel, method, _, body):
self._handle_clear_data(message)
elif message.msg_type == "klat.error":
self.handle_error_response(message)
elif message.msg_type == "ovos.languages.stt.response":
self._handle_supported_languages(message)
elif message.msg_type == "ovos.languages.tts.response":
self._handle_supported_languages(message)
elif message.msg_type.endswith(".response"):
self.handle_api_response(message)
else:
Expand Down Expand Up @@ -227,6 +247,19 @@ def _clear_audio_cache():
# (CACHES, PROFILE, ALL_TR, CONF_LIKES, CONF_DISLIKES, ALL_DATA,
# ALL_MEDIA, ALL_UNITS, ALL_LANGUAGE

def _handle_supported_languages(self, message: Message):
if message.msg_type == "ovos.languages.stt.response":
LOG.debug("STT langauge response")
self._languages["stt"] = message.data.get("langs")
elif message.msg_type == "ovos.languages.tts.response":
LOG.debug("TTS language response")
self._languages["tts"] = message.data.get("langs")
else:
raise RuntimeError(f"Unexpected message type: {message.msg_type}")
if all((x in self._languages for x in ("stt", "tts"))):
LOG.info(f"Language support determined: {self._languages}")
self._language_init.set()

def send_utterance(self, utterance: str, lang: str = "en-us",
username: Optional[str] = None,
user_profiles: Optional[list] = None,
Expand Down Expand Up @@ -303,6 +336,12 @@ def _send_audio(self, audio_file: str, lang: str,
new_only=True)}
self._send_serialized_message(serialized)

def _send_message(self, message: Message):
serialized = {"msg_type": message.msg_type,
"data": message.data,
"context": message.context}
self._send_serialized_message(serialized)

def _send_serialized_message(self, serialized: dict):
try:
self.connection.emit_mq_message(
Expand Down
18 changes: 15 additions & 3 deletions neon_iris/web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,17 @@ def run(self):
with gradio.Column():
lang = self.get_lang(client_session.value)
stt_lang = gradio.Radio(label="Input Language",
choices=self.supported_languages,
choices=self._languages.get("stt")
or self.supported_languages,
value=lang)
tts_lang = gradio.Radio(label="Response Language",
choices=self.supported_languages,
choices=self._languages.get("tts")
or self.supported_languages,
value=lang)
tts_lang_2 = gradio.Radio(label="Second Response Language",
choices=[None] +
self.supported_languages,
(self._languages.get("tts") or
self.supported_languages),
value=None)
with gradio.Column():
time_format = gradio.Radio(label="Time Format",
Expand Down Expand Up @@ -316,6 +319,15 @@ def handle_api_response(self, message: Message):
"""
LOG.debug(f"Got {message.msg_type}: {message.data}")

def _handle_profile_update(self, message: Message):
updated_profile = message.data["profile"]
session_id = updated_profile['user']['username']
if session_id in self._profiles:
LOG.info(f"Got profile update for {session_id}")
self._profiles[session_id] = updated_profile
else:
LOG.warning(f"Ignoring profile update for {session_id}")

def handle_error_response(self, message: Message):
"""
Handle an error response from the MQ service attached to Neon. This
Expand Down

0 comments on commit f2b65d2

Please sign in to comment.