aido-onyx/patch_voice.py
livdebus cfb3b7d418 fix(voice): route Onyx 5.0.3 STT/TTS to LocalAI Whisper endpoint
Replace the broken sed-based entrypoint from 4.8.2 with a runtime Python
patch script for Onyx 5.0.3 voice module compatibility.

Background
----------
Onyx 5.0.3 refactored the voice subsystem to use raw aiohttp instead of
the OpenAI Python SDK. The old /v1 suffix in our LocalAI base URL now
causes a double-prefix (https://.../v1/v1/audio/...) and 404s.
Additionally, line numbers and constants shifted, making the previous
sed patches fail silently.

Changes
-------
* api_server: override CMD with a startup sequence that runs
  patch_voice.py before uvicorn.
* Add backend/scripts/patch_voice.py that patches:
  - factory.py: fallback api_base, stt_model and api_key when no
    voice provider is configured in the Onyx admin UI.
  - providers/openai.py: DEFAULT_OPENAI_API_BASE (without /v1),
    realtime model constants, and optional chunked-STT language
    injection (WHISPER_LANGUAGE=de).
* Mount patch script read-only into /app/scripts/.
* Env: WHISPER_API_BASE now points to the origin only
  (https://aido-whisper-ch.livtec.cloud), leaving /v1 path assembly
  to Onyx code.
* Env: WHISPER_MODEL=whisper-1, WHISPER_LANGUAGE=de.

Migration
---------
The previous entrypoint sed block is removed. No admin UI voice
provider configuration is required; the patch is fully transparent.

Refs: aido-whisper-ch deployment, Onyx 5.0.3 upgrade
2026-11-12 20:10:14 +01:00

90 lines
No EOL
3.1 KiB
Python

#!/usr/bin/env python3
"""
Patch Onyx 5.0.3 voice module to use a LocalAI Whisper endpoint.
Idempotent: safe to run on every container start.
"""
import os
# ── Configuration from environment ───────────────────────────────────
LOCALAI_BASE = os.environ.get("WHISPER_API_BASE", "https://aido-whisper-ch.livtec.cloud").rstrip("/")
LOCALAI_MODEL = os.environ.get("WHISPER_MODEL", "whisper-1")
LOCALAI_LANG = os.environ.get("WHISPER_LANGUAGE", "de")
FACTORY = "/app/onyx/voice/factory.py"
OPENAI = "/app/onyx/voice/providers/openai.py"
def patch(path, replacements):
with open(path, "r", encoding="utf-8") as f:
content = f.read()
changed = False
for old, new in replacements:
if old in content:
content = content.replace(old, new, 1)
changed = True
print(f"[PATCH] {path}: {old[:60]}...")
else:
print(f"[SKIP ] {path}: {old[:60]}...")
if changed:
with open(path, "w", encoding="utf-8") as f:
f.write(content)
return changed
# ── 1. factory.py: fallbacks when no voice provider is saved in UI ──
patch(FACTORY, [
(
"api_base = provider.api_base",
f"api_base = provider.api_base or '{LOCALAI_BASE}'",
),
(
"stt_model = provider.stt_model",
f"stt_model = provider.stt_model or '{LOCALAI_MODEL}'",
),
(
"api_key = _extract_sensitive_string(provider.api_key)",
"api_key = _extract_sensitive_string(provider.api_key) or 'dummy-localai-key'",
),
])
# ── 2. openai.py: hardcoded defaults ────────────────────────────────
patch(OPENAI, [
(
'DEFAULT_OPENAI_API_BASE = "https://api.openai.com"',
f'DEFAULT_OPENAI_API_BASE = "{LOCALAI_BASE}"',
),
(
'OPENAI_REALTIME_SESSION_MODEL = "gpt-realtime-1.5"',
f'OPENAI_REALTIME_SESSION_MODEL = "{LOCALAI_MODEL}"',
),
(
'OPENAI_REALTIME_STT_MODEL = "gpt-realtime-whisper"',
f'OPENAI_REALTIME_STT_MODEL = "{LOCALAI_MODEL}"',
),
])
# ── 3. Best-effort language injection (optional for LocalAI) ─────────
with open(OPENAI, "r", encoding="utf-8") as f:
lines = f.readlines()
if not any("WHISPER_LANGUAGE" in ln for ln in lines):
if not any(ln.startswith("import os") for ln in lines):
lines.insert(0, "import os\n")
injected = False
for i, line in enumerate(lines):
if "add_field('model'" in line or 'add_field("model"' in line:
indent = line[: len(line) - len(line.lstrip())]
lines.insert(
i + 1,
indent + f"form.add_field('language', os.environ.get('WHISPER_LANGUAGE', '{LOCALAI_LANG}'))\n",
)
injected = True
break
if injected:
with open(OPENAI, "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"[PATCH] {OPENAI}: injected language field")
else:
print(f"[WARN ] {OPENAI}: chunked STT language injection skipped (likely auto-detect)")
print("[DONE ] Voice patches applied.")