From 1db2d6389c110cf7575694fbeef1445555f303fd Mon Sep 17 00:00:00 2001 From: Maksym Sobolyev Date: Wed, 17 Jul 2024 19:29:51 -0700 Subject: [PATCH] Make sip configuration optional. Add live_translator_precache mode, which is when enabled causes only models to be loaded. --- Apps/LiveTranslator/LTActor.py | 14 ++++++++++++++ Apps/LiveTranslator/LTProfile.py | 17 ++++++++++++----- Core/InfernConfig.py | 22 +++++++++++++++------- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Apps/LiveTranslator/LTActor.py b/Apps/LiveTranslator/LTActor.py index c9e1271..468c6e8 100644 --- a/Apps/LiveTranslator/LTActor.py +++ b/Apps/LiveTranslator/LTActor.py @@ -50,6 +50,20 @@ def start(self, lt_prof: 'LTProfile', sip_actr:InfernSIPActor): ray.get(futs) self.sessions = {} + def precache(self, lt_prof: 'LTProfile'): + nltk.download('punkt') + lt_actr = ray.get_runtime_context().current_actor + tts_actrs = dict((l, InfernTTSActor.remote()) for l in lt_prof.tts_langs) + stt_actr = InfernSTTActor.remote() + futs = [_a.start.remote(**_k) for _a, _k in ((stt_actr, {}),) + + tuple((a, {'lang':l, 'output_sr':8000, 'device':'cpu'}) for l, a in tts_actrs.items())] + translators = [ntw_filter if _sol == _tl else + IG.get_translator(_sol, _tl, filter=partial(ntw_filter, obj=NumbersToWords(_tl))).translate + for _tl, _sol in zip(lt_prof.tts_langs, self.stt_out_langs)] + ray.get(futs) + for a in list(tts_actrs.values()) + [stt_actr]: + ray.get(a.stop.remote()) + def new_sip_session_received(self, new_sess:RemoteSessionOffer): if self.vds is None: self.vds = VADSignals() diff --git a/Apps/LiveTranslator/LTProfile.py b/Apps/LiveTranslator/LTProfile.py index 3d7b668..590e889 100644 --- a/Apps/LiveTranslator/LTProfile.py +++ b/Apps/LiveTranslator/LTProfile.py @@ -14,17 +14,24 @@ class LTProfile(): outbound_conn: 'InfernSIPProfile' outbount_params: str actor: Optional[LTActor] = None + precache: bool - def __init__(self, name, conf): + def __init__(self, name, conf, precache): self.name = name self.tts_langs = tuple(conf['tts_langs']) self.stt_langs = tuple(conf['stt_langs']) - self._outbound = conf['outbound'] + if not precache: + self._outbound = conf['outbound'] + self.precache = precache def finalize(self, iconf:'InfernConfig'): - sip_cname, params = self._outbound.split(';', 1) - self.outbound_conn = iconf.connectors[sip_cname] - self.outbount_params = params + if not self.precache: + sip_cname, params = self._outbound.split(';', 1) + self.outbound_conn = iconf.connectors[sip_cname] + self.outbount_params = params + else: + actor = LTActor.remote() + res = ray.get(actor.precache.remote(self)) def getActor(self, iconf:'InfernConfig', sip_act:InfernSIPActor): if self.actor is None: diff --git a/Core/InfernConfig.py b/Core/InfernConfig.py index c5e14f5..8be4bec 100644 --- a/Core/InfernConfig.py +++ b/Core/InfernConfig.py @@ -64,7 +64,8 @@ def validate_port_range(field, value, error): } } } - } + }, + 'live_translator_precache': {'type': 'boolean'}, } } } @@ -104,12 +105,19 @@ def __init__(self, filename: str): port = int(bind[1]) if len(bind) == 2 else self.sip_conf.lport self.sip_conf.laddr = bind[0] self.sip_conf.lport = port - self.connectors = dict((f'sip/{name}', InfernSIPProfile(name, conf)) - for name, conf in d['sip']['profiles'].items()) - self.apps = dict((f'apps/live_translator/{name}', LTProfile(name, conf)) + try: + self.connectors = dict((f'sip/{name}', InfernSIPProfile(name, conf)) + for name, conf in d['sip']['profiles'].items()) + except KeyError: + self.connectors = {} + precache = 'live_translator_precache' in d['apps'] and d['apps']['live_translator_precache'] + self.apps = dict((f'apps/live_translator/{name}', LTProfile(name, conf, precache)) for name, conf in d['apps']['live_translator']['profiles'].items()) for app in self.apps.values(): app.finalize(self) - self.sip_actr = InfernSIPActor.options(max_concurrency=2).remote() - for conn in self.connectors.values(): - conn.finalize(self.sip_actr, self) + if 'sip' in d: + self.sip_actr = InfernSIPActor.options(max_concurrency=2).remote() + for conn in self.connectors.values(): + conn.finalize(self.sip_actr, self) + else: + self.sip_actr = None