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:
@@ -160,6 +160,26 @@ async def list_models(api_base: str, api_key: str, timeout: float = 15.0) -> lis
|
||||
return sorted(str(m["id"]) for m in data if isinstance(m, dict) and "id" in m)
|
||||
|
||||
|
||||
#: One client per speech server, so its connection stays open between calls
|
||||
#: (`parrot serve` keeps an idle one 600 s). A new connection over a lossy
|
||||
#: link can cost a second, which a spoken reply cannot spare.
|
||||
_speech: dict[str, httpx.AsyncClient] = {}
|
||||
|
||||
|
||||
def _speech_client(api_base: str) -> httpx.AsyncClient:
|
||||
"""The client for this speech server. Used from the engine's loop only."""
|
||||
http = _speech.get(api_base)
|
||||
if http is None:
|
||||
limits = httpx.Limits(keepalive_expiry=300)
|
||||
http = _speech[api_base] = httpx.AsyncClient(limits=limits)
|
||||
return http
|
||||
|
||||
|
||||
async def warm(api_base: str) -> None:
|
||||
"""Open the connection to a speech server ahead of the call that needs it."""
|
||||
await _speech_client(api_base).get(api_base.rstrip("/") + "/models", timeout=10)
|
||||
|
||||
|
||||
async def transcribe(
|
||||
api_base: str,
|
||||
audio: bytes,
|
||||
@@ -170,12 +190,12 @@ async def transcribe(
|
||||
timeout: float = 30.0,
|
||||
) -> str:
|
||||
"""``POST {api_base}/audio/transcriptions``: a whole clip, and what it says."""
|
||||
async with httpx.AsyncClient(timeout=timeout) as http:
|
||||
response = await http.post(
|
||||
api_base.rstrip("/") + "/audio/transcriptions",
|
||||
files={"file": (filename, audio, media_type)},
|
||||
data={"model": model, "response_format": "json"},
|
||||
)
|
||||
response = await _speech_client(api_base).post(
|
||||
api_base.rstrip("/") + "/audio/transcriptions",
|
||||
files={"file": (filename, audio, media_type)},
|
||||
data={"model": model, "response_format": "json"},
|
||||
timeout=timeout,
|
||||
)
|
||||
if response.is_error:
|
||||
raise ProviderError(_detail(response))
|
||||
return str(response.json().get("text") or "")
|
||||
@@ -194,16 +214,11 @@ async def speak(
|
||||
MP3 rather than WAV: a seventh of the bytes, which over a slow link to
|
||||
the server is most of the wait for a sentence.
|
||||
"""
|
||||
async with httpx.AsyncClient(timeout=timeout) as http:
|
||||
response = await http.post(
|
||||
api_base.rstrip("/") + "/audio/speech",
|
||||
json={
|
||||
"model": model,
|
||||
"input": text,
|
||||
"voice": voice,
|
||||
"response_format": "mp3",
|
||||
},
|
||||
)
|
||||
response = await _speech_client(api_base).post(
|
||||
api_base.rstrip("/") + "/audio/speech",
|
||||
json={"model": model, "input": text, "voice": voice, "response_format": "mp3"},
|
||||
timeout=timeout,
|
||||
)
|
||||
if response.is_error:
|
||||
raise ProviderError(_detail(response))
|
||||
return response.content
|
||||
|
||||
Reference in New Issue
Block a user