Port the original #27 detected-language pipeline onto the verified PR #39 prerequisite while preserving the current-main conversation instrument and host continuity changes. Keep voice selection server-side with no user selector or client voice field. Reuse 207c16ab only for its stricter exact-code trust boundary, omitting malformed or absent language so Piper defaults to Amy.
380 lines
16 KiB
Python
380 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
"""Apply fail-closed Atlas voice integration patches to pinned Hermes WebUI."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(os.environ.get("HERMES_WEBUI_PATCH_ROOT", "/opt/hermes-webui"))
|
|
# The WebUI imports the pinned agent's STT tooling from the same image, so the
|
|
# local-command transcription envelope is patched alongside the WebUI itself.
|
|
AGENT_ROOT = Path(os.environ.get("HERMES_AGENT_PATCH_ROOT", "/opt/hermes"))
|
|
|
|
|
|
def replace_exact(path: Path, before: str, after: str, count: int = 1) -> None:
|
|
"""Replace an exact upstream fragment and fail when the pin has drifted."""
|
|
source = path.read_text(encoding="utf-8")
|
|
if source.count(before) != count:
|
|
raise SystemExit(f"Atlas voice patch context changed in {path}: {before[:80]!r}")
|
|
path.write_text(source.replace(before, after, count), encoding="utf-8")
|
|
|
|
|
|
def replace_between_exact(
|
|
path: Path, start: str, end: str, after: str = "", count: int = 1
|
|
) -> None:
|
|
"""Replace one exact, bounded upstream region and fail when the pin drifts."""
|
|
source = path.read_text(encoding="utf-8")
|
|
if source.count(start) != count or source.count(end) != count:
|
|
raise SystemExit(
|
|
f"Atlas voice patch context changed in {path}: {start[:80]!r}"
|
|
)
|
|
start_index = source.index(start)
|
|
end_index = source.index(end, start_index) + len(end)
|
|
path.write_text(
|
|
source[:start_index] + after + source[end_index:], encoding="utf-8"
|
|
)
|
|
|
|
|
|
def assert_absent(path: Path, *needles: str) -> None:
|
|
"""Fail the image build if a removed voice-choice surface remains."""
|
|
source = path.read_text(encoding="utf-8")
|
|
remaining = [needle for needle in needles if needle in source]
|
|
if remaining:
|
|
raise SystemExit(f"Atlas voice choice remains in {path}: {remaining!r}")
|
|
|
|
|
|
def remove_lines_containing(path: Path, *needles: str) -> None:
|
|
"""Remove all pinned translation entries for a retired settings control."""
|
|
source = path.read_text(encoding="utf-8")
|
|
for needle in needles:
|
|
if needle not in source:
|
|
raise SystemExit(f"Atlas voice patch context changed in {path}: {needle!r}")
|
|
lines = source.splitlines(keepends=True)
|
|
path.write_text(
|
|
"".join(line for line in lines if not any(n in line for n in needles)),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
index = ROOT / "static/index.html"
|
|
replace_exact(
|
|
index,
|
|
'<link rel="stylesheet" href="static/style.css?v=__WEBUI_VERSION__">',
|
|
'<link rel="stylesheet" href="static/style.css?v=__WEBUI_VERSION__">\n'
|
|
'<link id="voiceInstrumentStyles" rel="stylesheet" '
|
|
'href="static/atlas-voice.css?v=__WEBUI_VERSION__">',
|
|
)
|
|
replace_exact(
|
|
index,
|
|
'<option value="browser">Browser speech synthesis</option><option value="edge">Edge TTS (server)</option>',
|
|
'<option value="atlas">Atlas Jetson (private)</option><option value="browser">Browser speech synthesis</option><option value="edge">Edge TTS (server)</option>',
|
|
)
|
|
replace_exact(
|
|
index,
|
|
'''<div class="settings-field"><label for="settingsTtsVoice" data-i18n="settings_label_tts_voice">Voice</label>
|
|
<select id="settingsTtsVoice" style="width:100%;padding:8px;background:var(--code-bg);color:var(--text);border:1px solid var(--border2);border-radius:6px">
|
|
<option value="">Default system voice</option>
|
|
</select>
|
|
<div style="font-size:11px;color:var(--muted);margin-top:4px" data-i18n="settings_desc_tts_voice">Preferred voice. Populated from your browser's available voices.</div>
|
|
</div>''',
|
|
"",
|
|
)
|
|
replace_exact(
|
|
index,
|
|
'<script src="static/boot.js?v=__WEBUI_VERSION__" defer></script>',
|
|
'<script src="static/boot.js?v=__WEBUI_VERSION__" defer></script>\n<script src="static/atlas-voice.js?v=__WEBUI_VERSION__" defer></script>',
|
|
)
|
|
replace_exact(
|
|
index,
|
|
''' <div class="voice-mode-bar" id="voiceModeBar" style="display:none">
|
|
<span class="voice-mode-indicator" id="voiceModeIndicator"></span>
|
|
<span class="voice-mode-label" id="voiceModeLabel"></span>
|
|
</div>''',
|
|
''' <div class="voice-mode-bar" id="voiceModeBar" style="display:none" role="status" aria-live="polite" aria-atomic="true">
|
|
<span class="voice-mode-indicator idle" id="voiceModeIndicator" aria-hidden="true">
|
|
<span class="voice-instrument-halo"></span>
|
|
<span class="voice-instrument-ripple"></span>
|
|
<span class="voice-instrument-orbit"></span>
|
|
<span class="voice-instrument-core">
|
|
<span class="voice-instrument-symbol">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" focusable="false">
|
|
<g class="voice-symbol voice-symbol-listening"><rect x="9" y="3" width="6" height="11" rx="3"/><path d="M6.5 11.5a5.5 5.5 0 0 0 11 0M12 17v3M9 20h6"/></g>
|
|
<g class="voice-symbol voice-symbol-transcribing"><path d="M5 7h14M5 12h10M5 17h7"/><path d="M18 15v5m-2.5-2.5L18 20l2.5-2.5"/></g>
|
|
<g class="voice-symbol voice-symbol-thinking"><path d="M12 3l1.15 4.1L17 8.5l-3.85 1.4L12 14l-1.15-4.1L7 8.5l3.85-1.4L12 3Z"/><path d="M18.5 13.5l.65 2.35 2.35.65-2.35.65-.65 2.35-.65-2.35-2.35-.65 2.35-.65.65-2.35Z"/><path d="M5.5 14l.45 1.55L7.5 16l-1.55.45L5.5 18l-.45-1.55L3.5 16l1.55-.45L5.5 14Z"/></g>
|
|
<g class="voice-symbol voice-symbol-speaking"><path d="M5 10v4h3l4 3V7L8 10H5Z"/><path d="M15.5 9.25a4 4 0 0 1 0 5.5M18 7a7 7 0 0 1 0 10"/></g>
|
|
<g class="voice-symbol voice-symbol-error"><path d="M12 4 21 20H3L12 4Z"/><path d="M12 9v5M12 17.2v.1"/></g>
|
|
</svg>
|
|
</span>
|
|
</span>
|
|
</span>
|
|
<span class="voice-mode-label" id="voiceModeLabel"></span>
|
|
</div>''',
|
|
)
|
|
|
|
ui = ROOT / "static/ui.js"
|
|
replace_exact(
|
|
ui,
|
|
''' const savedVoice=localStorage.getItem('hermes-tts-voice');
|
|
const voices=speechSynthesis.getVoices();
|
|
if(savedVoice&&voices.length){
|
|
const match=voices.find(v=>v.name===savedVoice);
|
|
if(match) utter.voice=match;
|
|
}
|
|
''',
|
|
"",
|
|
)
|
|
replace_exact(ui, "function _playEdgeTtsChunked(text, btn){", "function _playEdgeTtsChunked(text, btn, engineOverride){")
|
|
replace_exact(
|
|
ui,
|
|
" const voice=localStorage.getItem('hermes-tts-voice')||'zh-CN-XiaoxiaoNeural';\n",
|
|
"",
|
|
)
|
|
replace_exact(
|
|
ui,
|
|
"body:JSON.stringify({text:chunk, voice:voice, rate:rate, pitch:pitch})",
|
|
"body:JSON.stringify({text:chunk, rate:rate, pitch:pitch, engine:engineOverride||'edge'})",
|
|
)
|
|
replace_exact(
|
|
ui,
|
|
" voice: localStorage.getItem('hermes-tts-voice')||'',\n",
|
|
"",
|
|
count=2,
|
|
)
|
|
replace_exact(
|
|
ui,
|
|
"if(engine==='edge'){\n _playEdgeTtsChunked(clean, btn);",
|
|
"if(engine==='edge'||engine==='atlas'){\n _playEdgeTtsChunked(clean, btn, engine);",
|
|
)
|
|
replace_exact(
|
|
ui,
|
|
"if(engine==='edge'){\n _playEdgeTtsChunked(clean, null);",
|
|
"if(engine==='edge'||engine==='atlas'){\n _playEdgeTtsChunked(clean, null, engine);",
|
|
)
|
|
|
|
panels = ROOT / "static/panels.js"
|
|
replace_exact(panels, " tts_voice:'hermes-tts-voice',\n", "")
|
|
replace_exact(
|
|
panels,
|
|
''' const ttsVoiceSel=$('settingsTtsVoice');
|
|
if(ttsVoiceSel) _setOwnedSpeechPayload(payload,'tts_voice',ttsVoiceSel.value||'');
|
|
''',
|
|
"",
|
|
)
|
|
replace_exact(
|
|
panels,
|
|
''' localStorage.setItem('hermes-tts-engine',this.value);
|
|
window._populateTtsVoices();
|
|
_schedulePreferencesAutosave();''',
|
|
''' localStorage.setItem('hermes-tts-engine',this.value);
|
|
_schedulePreferencesAutosave();''',
|
|
)
|
|
replace_between_exact(
|
|
panels,
|
|
" // Populate voice selector based on engine\n",
|
|
" // TTS rate/pitch sliders\n",
|
|
" // TTS speaker selection is intentionally server policy only.\n",
|
|
)
|
|
replace_exact(
|
|
panels,
|
|
"let _settingsSpeechChangedKeys=new Set();\n",
|
|
"let _settingsSpeechChangedKeys=new Set();\n"
|
|
"try{localStorage.removeItem('hermes-tts-voice');}catch(_){}\n",
|
|
)
|
|
|
|
boot = ROOT / "static/boot.js"
|
|
replace_exact(
|
|
boot,
|
|
''' voice: localStorage.getItem("hermes-tts-voice")||'',
|
|
''',
|
|
"",
|
|
)
|
|
replace_exact(
|
|
boot,
|
|
''' const voice=localStorage.getItem("hermes-tts-voice")||"zh-CN-XiaoxiaoNeural";
|
|
''',
|
|
"",
|
|
)
|
|
replace_exact(
|
|
boot,
|
|
" body: JSON.stringify({text: clean, voice, rate, pitch})",
|
|
" body: JSON.stringify({text: clean, rate, pitch})",
|
|
)
|
|
replace_exact(
|
|
boot,
|
|
''' const savedVoice=localStorage.getItem('hermes-tts-voice');
|
|
const voices=speechSynthesis.getVoices();
|
|
if(savedVoice&&voices.length){
|
|
const match=voices.find(v=>v.name===savedVoice);
|
|
if(match) utter.voice=match;
|
|
}
|
|
''',
|
|
"",
|
|
)
|
|
replace_exact(boot, " tts_voice:'',\n", "")
|
|
replace_exact(boot, " ['tts_voice','hermes-tts-voice'],\n", "")
|
|
|
|
config = ROOT / "api/config.py"
|
|
replace_exact(config, ' "tts_voice": "",\n', "")
|
|
replace_exact(config, ' "tts_voice",\n', "")
|
|
replace_exact(
|
|
config,
|
|
''' if k == "tts_voice":
|
|
if not isinstance(v, str) or len(v) > 200 or "\\x00" in v:
|
|
continue
|
|
''',
|
|
"",
|
|
)
|
|
|
|
assert_absent(index, "settingsTtsVoice", "settings_label_tts_voice")
|
|
assert_absent(ui, "hermes-tts-voice", "voice:voice")
|
|
assert_absent(panels, "settingsTtsVoice", "tts_voice")
|
|
assert_absent(boot, "hermes-tts-voice", "tts_voice", "text: clean, voice")
|
|
assert_absent(config, '"tts_voice"')
|
|
|
|
i18n = ROOT / "static/i18n.js"
|
|
remove_lines_containing(
|
|
i18n,
|
|
"settings_label_tts_voice:",
|
|
"settings_desc_tts_voice:",
|
|
)
|
|
assert_absent(i18n, "settings_label_tts_voice", "settings_desc_tts_voice")
|
|
|
|
# The private Whisper service reports the language it decoded with. Carry that
|
|
# through the agent's local-command STT envelope so the WebUI can hand a voice
|
|
# hint to Piper instead of guessing the reply's language from its text.
|
|
transcription = AGENT_ROOT / "tools/transcription_tools.py"
|
|
replace_exact(
|
|
transcription,
|
|
''' transcript_text = txt_files[0].read_text(encoding="utf-8").strip()
|
|
logger.info(
|
|
"Transcribed %s via local STT command (%s, %d chars)",
|
|
Path(file_path).name,
|
|
normalized_model,
|
|
len(transcript_text),
|
|
)
|
|
return {"success": True, "transcript": transcript_text, "provider": "local_command"}
|
|
''',
|
|
''' transcript_text = txt_files[0].read_text(encoding="utf-8").strip()
|
|
logger.info(
|
|
"Transcribed %s via local STT command (%s, %d chars)",
|
|
Path(file_path).name,
|
|
normalized_model,
|
|
len(transcript_text),
|
|
)
|
|
detected_language = ""
|
|
language_files = sorted(Path(output_dir).glob("*.language"))
|
|
if language_files:
|
|
try:
|
|
candidate = language_files[0].read_text(encoding="utf-8").strip().lower()
|
|
except (OSError, ValueError):
|
|
candidate = ""
|
|
if 2 <= len(candidate) <= 3 and candidate.isascii() and candidate.isalpha():
|
|
detected_language = candidate
|
|
return {
|
|
"success": True,
|
|
"transcript": transcript_text,
|
|
"provider": "local_command",
|
|
"language": detected_language,
|
|
}
|
|
''',
|
|
)
|
|
|
|
upload = ROOT / "api/upload.py"
|
|
replace_exact(
|
|
upload,
|
|
""" transcript = str(result.get('transcript') or '').strip()
|
|
return j(handler, {'ok': True, 'transcript': transcript})
|
|
""",
|
|
""" transcript = str(result.get('transcript') or '').strip()
|
|
detected = str(result.get('language') or '').strip().lower()
|
|
if not (2 <= len(detected) <= 3 and detected.isascii() and detected.isalpha()):
|
|
detected = ''
|
|
return j(handler, {'ok': True, 'transcript': transcript, 'language': detected})
|
|
""",
|
|
)
|
|
|
|
routes = ROOT / "api/routes.py"
|
|
replace_exact(
|
|
routes,
|
|
"def _tts_open(req, *, timeout=30, opener_factory=None):",
|
|
'''ATLAS_TTS_LANGUAGES = ("en", "ru", "es")
|
|
|
|
|
|
def _atlas_tts_language(body):
|
|
"""Return a plain, allow-listed en/ru/es code, or "" to send no language.
|
|
|
|
This is a trust boundary, not a parser. Only the exact normalized codes the
|
|
private Piper deployment bakes a voice for are forwarded; a missing field,
|
|
a wrong type, a region tag, padding, control characters, a traversal or
|
|
injection string, an oversized value, an object, an array, a number or a
|
|
client-supplied "voice" all resolve to "" and the language field is then
|
|
omitted entirely, so the Jetson service applies its own English default.
|
|
Coercing a malformed value into a supported code would let a browser
|
|
describe hostile input as a language we support; omission cannot.
|
|
"""
|
|
if not isinstance(body, dict):
|
|
return ""
|
|
value = body.get("language")
|
|
if not isinstance(value, str):
|
|
return ""
|
|
return value if value in ATLAS_TTS_LANGUAGES else ""
|
|
|
|
|
|
def _tts_open(req, *, timeout=30, opener_factory=None):''',
|
|
)
|
|
marker = " # ── ElevenLabs TTS ──────────────────────────────────────────────────\n"
|
|
atlas = ''' # ── Atlas private Jetson TTS ─────────────────────────────────────────
|
|
if engine == "atlas":
|
|
atlas_url = os.getenv("HERMES_WEBUI_ATLAS_TTS_URL", "").strip()
|
|
expected_url = "http://hermes-tts.hermes.svc.cluster.local:9001/v1/audio/speech"
|
|
if atlas_url != expected_url:
|
|
from api.helpers import bad as _bad
|
|
return _bad(handler, "Atlas private TTS is not configured", 503)
|
|
speed = 1.0
|
|
if rate_str:
|
|
try:
|
|
speed = max(0.5, min(2.0, 1.0 + (float(rate_str.rstrip("%")) / 100.0)))
|
|
except ValueError:
|
|
speed = 1.0
|
|
request_payload = {
|
|
"model": "piper",
|
|
"input": text,
|
|
"speed": speed,
|
|
}
|
|
# Attach a language ONLY when the browser sent a plain allow-listed
|
|
# code. Omitting it is the fail-safe: the Jetson service then speaks
|
|
# its own English default, which is also what every partially rolled
|
|
# out combination of these components degrades to.
|
|
_atlas_language = _atlas_tts_language(data)
|
|
if _atlas_language:
|
|
request_payload["language"] = _atlas_language
|
|
request_body = json.dumps(request_payload).encode("utf-8")
|
|
request = Request(atlas_url, data=request_body, headers={
|
|
"Content-Type": "application/json",
|
|
"Accept": "audio/wav",
|
|
})
|
|
try:
|
|
with _tts_open(
|
|
request,
|
|
timeout=45,
|
|
opener_factory=lambda: build_opener(ProxyHandler({}), _NoRedirectTtsHandler()),
|
|
) as response:
|
|
audio_data = _buffer_tts_audio_response(response)
|
|
except Exception:
|
|
logger.exception("Atlas private TTS generation failed")
|
|
from api.helpers import bad as _bad
|
|
return _bad(handler, "Atlas private TTS generation failed", 502)
|
|
handler.send_response(200)
|
|
handler.send_header("Content-Type", "audio/wav")
|
|
handler.send_header("Cache-Control", "no-store")
|
|
handler.send_header("Content-Length", str(len(audio_data)))
|
|
handler.end_headers()
|
|
try:
|
|
handler.wfile.write(audio_data)
|
|
except (BrokenPipeError, ConnectionResetError):
|
|
pass
|
|
return True
|
|
|
|
'''
|
|
replace_exact(routes, marker, atlas + marker)
|