Merge pull request #5 from m5stack/dev

[feat] The tts backend supports model switching.
This commit is contained in:
Abandon-ht
2025-04-23 14:48:04 +08:00
committed by GitHub
2 changed files with 19 additions and 0 deletions
+8
View File
@@ -62,6 +62,7 @@ class ModelDispatcher:
self.backends = {} self.backends = {}
self.llm_models = set() self.llm_models = set()
self.asr_models = set() self.asr_models = set()
self.tts_models = set()
self.lock = asyncio.Lock() self.lock = asyncio.Lock()
async def get_backend(self, model_name): async def get_backend(self, model_name):
@@ -84,7 +85,14 @@ class ModelDispatcher:
elif model_config["type"] == "vision_model": elif model_config["type"] == "vision_model":
self.backends[model_name] = VisionModelBackend(model_config) self.backends[model_name] = VisionModelBackend(model_config)
elif model_config["type"] == "tts": elif model_config["type"] == "tts":
if model_name not in self.tts_models:
for old_model_name in list(self.tts_models):
old_instance = self.backends.pop(old_model_name, None)
if old_instance:
await old_instance.close()
self.asr_models.clear()
self.backends[model_name] = TtsClientBackend(model_config) self.backends[model_name] = TtsClientBackend(model_config)
self.tts_models.add(model_name)
elif model_config["type"] == "asr": elif model_config["type"] == "asr":
if model_name not in self.asr_models: if model_name not in self.asr_models:
for old_model_name in list(self.asr_models): for old_model_name in list(self.asr_models):
+11
View File
@@ -82,6 +82,17 @@ class TtsClientBackend(BaseModelBackend):
async with self._pool_lock: async with self._pool_lock:
self._client_pool.append(client) self._client_pool.append(client)
async def close(self):
for task in self._active_tasks:
task.cancel()
if self._active_tasks:
await asyncio.wait(self._active_tasks, timeout=2)
for client in self._client_pool:
client.exit()
self._client_pool.clear()
self._active_clients.clear()
self._inference_executor.shutdown(wait=False)
def _encode_stream_chunk(self, pcm_data: bytes, format: str) -> bytes: def _encode_stream_chunk(self, pcm_data: bytes, format: str) -> bytes:
if format == "pcm": if format == "pcm":
return pcm_data return pcm_data