Speech calls keep one connection per server, opened when listening starts
transcribe and speak share an httpx client per speech server with a 300 s keep-alive, and run on the engine's loop. stt warms it with GET /v1/models when an utterance opens, so a reply's first sentence finds the connection open: a new one over a lossy link cost up to a second (openpanel V4).
This commit is contained in:
@@ -9,6 +9,7 @@ it gets.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
@@ -128,6 +129,13 @@ class _Speech(Node):
|
||||
def _load(self, reference: dict[str, Any]) -> bytes:
|
||||
return b"".join(self._artifacts.read(str(reference["digest"])))
|
||||
|
||||
def _call(self, work: Coroutine[Any, Any, Any]) -> Future[Any]:
|
||||
"""Run on the engine's loop, where the server's connection is kept."""
|
||||
if self._loop is None:
|
||||
work.close()
|
||||
raise RuntimeError(f"{self.name} only runs in a live flow")
|
||||
return asyncio.run_coroutine_threadsafe(work, self._loop)
|
||||
|
||||
def _say(self, **values: Any) -> None:
|
||||
"""Publish on the ports that are wired."""
|
||||
wired = {spec.port for spec in self.output_ports if spec.name}
|
||||
@@ -231,18 +239,21 @@ class SttNode(_Speech):
|
||||
|
||||
def _spawn(self, work: Coroutine[Any, Any, None]) -> None:
|
||||
"""Run on the engine's loop, so this run can return at once."""
|
||||
if self._loop is None:
|
||||
work.close()
|
||||
raise RuntimeError("stt only hears in a live flow")
|
||||
task = asyncio.run_coroutine_threadsafe(work, self._loop)
|
||||
task = self._call(work)
|
||||
self._tasks.add(task)
|
||||
task.add_done_callback(self._tasks.discard)
|
||||
|
||||
def _open(self, url: str) -> asyncio.Queue[bytes | None]:
|
||||
chunks: asyncio.Queue[bytes | None] = asyncio.Queue()
|
||||
self._spawn(self._transcribe(url, chunks))
|
||||
# The reply's first sentence then finds the connection open.
|
||||
self._spawn(self._warm(url))
|
||||
return chunks
|
||||
|
||||
async def _warm(self, url: str) -> None:
|
||||
with contextlib.suppress(Exception):
|
||||
await client.warm(url + "/v1")
|
||||
|
||||
def _feed(self, chunks: asyncio.Queue[bytes | None], chunk: bytes | None) -> None:
|
||||
assert self._loop is not None
|
||||
self._loop.call_soon_threadsafe(chunks.put_nowait, chunk)
|
||||
@@ -355,7 +366,7 @@ class TtsNode(_Speech):
|
||||
reference = spoken.get(key)
|
||||
if reference is not None and self._artifacts.has(reference["digest"]):
|
||||
return dict(reference)
|
||||
data = asyncio.run(client.speak(url + "/v1", sentence, self.cfg.voice))
|
||||
data = self._call(client.speak(url + "/v1", sentence, self.cfg.voice)).result()
|
||||
stored: dict[str, Any] = self._artifacts.put(
|
||||
[data], name="speech.mp3", media_type="audio/mpeg"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user