fix: update TTS patch for latest hermes-agent tts_tool.py
- Patch now matches the current tts_tool.py (newer version ships in nousresearch/hermes-agent:latest with different Edge fallback text) - Adds dedicated elif provider == 'piper' block before else: - Replaces else: fallback to use Piper instead of Edge - Patches ALL copies (venv site-packages + /opt/hermes/tools/) - Removes Edge TTS entirely as default/provider
This commit is contained in:
@@ -1,38 +1,33 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Patch Hermes TTS tool: remove Edge TTS, replace with Piper as default/fallback.
|
"""Patch Hermes TTS tool: add Piper TTS provider, remove Edge TTS as default.
|
||||||
|
|
||||||
|
Patches ALL copies of tts_tool.py found (venv site-packages + /opt/hermes/tools/).
|
||||||
|
|
||||||
Searches multiple paths for tts_tool.py so it works both at build time
|
Searches multiple paths for tts_tool.py so it works both at build time
|
||||||
(in the image venv) and at runtime (on the mounted data volume).
|
(in the image venv) and at runtime (on the mounted data volume).
|
||||||
|
|
||||||
|
Idempotent: if already patched, does nothing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Search order: argument > site-packages > /opt/hermes/tools > /opt/hermes checkout
|
# ---------------------------------------------------------------------------
|
||||||
SEARCH_PATHS = []
|
# Search for all copies of tts_tool.py
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
# Accept path as first argument
|
CANDIDATE_PATHS = [
|
||||||
if len(sys.argv) > 1:
|
|
||||||
SEARCH_PATHS.append(sys.argv[1])
|
|
||||||
|
|
||||||
# Add known locations
|
|
||||||
SEARCH_PATHS.extend([
|
|
||||||
"/opt/hermes/.venv/lib/python3.13/site-packages/tools/tts_tool.py",
|
"/opt/hermes/.venv/lib/python3.13/site-packages/tools/tts_tool.py",
|
||||||
"/opt/hermes/tools/tts_tool.py",
|
"/opt/hermes/tools/tts_tool.py",
|
||||||
])
|
]
|
||||||
|
|
||||||
tts_path = None
|
found_paths = []
|
||||||
code = None
|
|
||||||
|
|
||||||
for p in SEARCH_PATHS:
|
for p in CANDIDATE_PATHS:
|
||||||
if os.path.exists(p):
|
if os.path.exists(p):
|
||||||
tts_path = p
|
found_paths.append(p)
|
||||||
with open(tts_path) as f:
|
print(f"Found tts_tool.py at: {p}")
|
||||||
code = f.read()
|
|
||||||
print(f"Found tts_tool.py at: {tts_path}")
|
|
||||||
break
|
|
||||||
|
|
||||||
if code is None:
|
# Also try to find via Python import
|
||||||
# Try one more time: find it in the venv site-packages
|
|
||||||
import subprocess
|
import subprocess
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
@@ -41,21 +36,21 @@ if code is None:
|
|||||||
)
|
)
|
||||||
if result.returncode == 0:
|
if result.returncode == 0:
|
||||||
p = result.stdout.strip()
|
p = result.stdout.strip()
|
||||||
if os.path.exists(p):
|
if os.path.exists(p) and p not in found_paths:
|
||||||
tts_path = p
|
found_paths.append(p)
|
||||||
with open(tts_path) as f:
|
print(f"Found tts_tool.py via import at: {p}")
|
||||||
code = f.read()
|
|
||||||
print(f"Found tts_tool.py via import at: {tts_path}")
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if code is None:
|
if not found_paths:
|
||||||
print("WARNING: tts_tool.py not found. Patching deferred to runtime.")
|
print("WARNING: tts_tool.py not found anywhere. Patching deferred to runtime.")
|
||||||
print(f"Searched: {SEARCH_PATHS}")
|
print(f"Searched: {CANDIDATE_PATHS}")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Replace the Edge fallback with Piper fallback
|
# ---------------------------------------------------------------------------
|
||||||
old_edge = ''' else:
|
# Old else block: the Edge TTS default fallback to replace
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
old_else = ''' else:
|
||||||
# Default: Edge TTS (free), with NeuTTS as local fallback
|
# Default: Edge TTS (free), with NeuTTS as local fallback
|
||||||
edge_available = True
|
edge_available = True
|
||||||
try:
|
try:
|
||||||
@@ -84,53 +79,103 @@ old_edge = ''' else:
|
|||||||
"or set up NeuTTS for local synthesis."
|
"or set up NeuTTS for local synthesis."
|
||||||
}, ensure_ascii=False)'''
|
}, ensure_ascii=False)'''
|
||||||
|
|
||||||
new_piper = ''' else:
|
# ---------------------------------------------------------------------------
|
||||||
# Default: Piper TTS (local, CPU, no cloud, no Microsoft)
|
# New block: elif provider == "piper" + else: fallback with Piper only
|
||||||
piper_available = False
|
# ---------------------------------------------------------------------------
|
||||||
try:
|
new_block = ''' elif provider == "piper":
|
||||||
|
# Piper TTS (local, CPU, no cloud, no Microsoft)
|
||||||
piper_binary = "/opt/hermes/.venv/bin/piper"
|
piper_binary = "/opt/hermes/.venv/bin/piper"
|
||||||
piper_config = tts_config.get("piper", {})
|
piper_config = tts_config.get("piper", {})
|
||||||
voice = piper_config.get("voice", "en_US-lessac-medium")
|
voice = piper_config.get("voice", "en_US-lessac-medium")
|
||||||
model_dir = piper_config.get("model_dir", "/opt/hermes/.venv/share/piper/voices")
|
model_dir = piper_config.get("model_dir", "/opt/hermes/.venv/share/piper/voices")
|
||||||
model_path = os.path.join(model_dir, f"{voice}.onnx")
|
model_path = os.path.join(model_dir, f"{voice}.onnx")
|
||||||
if os.path.exists(model_path):
|
if not os.path.exists(model_path):
|
||||||
piper_available = True
|
return json.dumps({
|
||||||
except Exception:
|
"success": False,
|
||||||
pass
|
"error": "Piper TTS voice model not found. "
|
||||||
|
"Install Piper TTS and download a voice model."
|
||||||
if piper_available:
|
}, ensure_ascii=False)
|
||||||
logger.info("Generating speech with Piper TTS (local, CPU)...")
|
logger.info("Generating speech with Piper TTS (local, CPU)...")
|
||||||
import subprocess
|
import subprocess as _sp
|
||||||
piper_binary = "/opt/hermes/.venv/bin/piper"
|
|
||||||
piper_config = tts_config.get("piper", {})
|
|
||||||
voice = piper_config.get("voice", "en_US-lessac-medium")
|
|
||||||
model_dir = piper_config.get("model_dir", "/opt/hermes/.venv/share/piper/voices")
|
|
||||||
model_path = os.path.join(model_dir, f"{voice}.onnx")
|
|
||||||
cmd = [piper_binary, "--model", model_path, "--output-raw"]
|
cmd = [piper_binary, "--model", model_path, "--output-raw"]
|
||||||
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
try:
|
||||||
|
proc = _sp.Popen(cmd, stdin=_sp.PIPE, stdout=_sp.PIPE, stderr=_sp.PIPE)
|
||||||
raw_audio, stderr = proc.communicate(input=text.encode(), timeout=60)
|
raw_audio, stderr = proc.communicate(input=text.encode(), timeout=60)
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
raise RuntimeError(f"Piper TTS failed: {stderr.decode()[:200]}")
|
raise RuntimeError(f"Piper TTS failed: {stderr.decode()[:200]}")
|
||||||
ffmpeg_cmd = ["ffmpeg", "-f", "s16le", "-ar", "22050", "-ac", "1", "-i", "-", "-y", file_str]
|
ffmpeg_cmd = ["ffmpeg", "-f", "s16le", "-ar", "22050", "-ac", "1", "-i", "-", "-y", file_str]
|
||||||
subprocess.run(ffmpeg_cmd, input=raw_audio, capture_output=True, timeout=30)
|
_sp.run(ffmpeg_cmd, input=raw_audio, capture_output=True, timeout=30)
|
||||||
logger.info("Piper TTS audio saved: %s", file_str)
|
except Exception as e:
|
||||||
|
return json.dumps({
|
||||||
|
"success": False,
|
||||||
|
"error": f"Piper TTS failed: {e}"
|
||||||
|
}, ensure_ascii=False)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Default: Piper TTS (local, CPU, no cloud, no Microsoft)
|
||||||
|
piper_binary = "/opt/hermes/.venv/bin/piper"
|
||||||
|
piper_config = tts_config.get("piper", {})
|
||||||
|
voice = piper_config.get("voice", "en_US-lessac-medium")
|
||||||
|
model_dir = piper_config.get("model_dir", "/opt/hermes/.venv/share/piper/voices")
|
||||||
|
model_path = os.path.join(model_dir, f"{voice}.onnx")
|
||||||
|
if os.path.exists(model_path) and os.path.exists(piper_binary):
|
||||||
|
logger.info("Generating speech with Piper TTS (local, CPU)...")
|
||||||
|
import subprocess as _sp
|
||||||
|
cmd = [piper_binary, "--model", model_path, "--output-raw"]
|
||||||
|
try:
|
||||||
|
proc = _sp.Popen(cmd, stdin=_sp.PIPE, stdout=_sp.PIPE, stderr=_sp.PIPE)
|
||||||
|
raw_audio, stderr = proc.communicate(input=text.encode(), timeout=60)
|
||||||
|
if proc.returncode != 0:
|
||||||
|
raise RuntimeError(stderr.decode()[:200])
|
||||||
|
ffmpeg_cmd = ["ffmpeg", "-f", "s16le", "-ar", "22050", "-ac", "1", "-i", "-", "-y", file_str]
|
||||||
|
_sp.run(ffmpeg_cmd, input=raw_audio, capture_output=True, timeout=30)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
"success": False,
|
"success": False,
|
||||||
"error": "No TTS provider available. Install Piper TTS (pip install piper-tts) "
|
"error": "Piper TTS not available. Install piper-tts and download a voice model."
|
||||||
"and download a voice model."
|
|
||||||
}, ensure_ascii=False)'''
|
}, ensure_ascii=False)'''
|
||||||
|
|
||||||
if old_edge in code:
|
# ---------------------------------------------------------------------------
|
||||||
code = code.replace(old_edge, new_piper)
|
# Apply the patch to all copies found
|
||||||
print("Edge fallback replaced with Piper")
|
# ---------------------------------------------------------------------------
|
||||||
elif 'Default: Piper TTS' in code:
|
patched_any = False
|
||||||
print("Piper fallback already present")
|
|
||||||
else:
|
|
||||||
print("WARNING: Could not find Edge fallback in tts_tool.py")
|
|
||||||
print("The tts_tool.py may be a version not matching this patch.")
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
for tts_path in found_paths:
|
||||||
|
with open(tts_path) as f:
|
||||||
|
code = f.read()
|
||||||
|
|
||||||
|
if 'provider == "piper"' in code:
|
||||||
|
print(f"ALREADY PATCHED: {tts_path}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if old_else in code:
|
||||||
|
code = code.replace(old_else, new_block, 1)
|
||||||
with open(tts_path, 'w') as f:
|
with open(tts_path, 'w') as f:
|
||||||
f.write(code)
|
f.write(code)
|
||||||
print(f"tts_tool.py patched successfully at: {tts_path}")
|
print(f"PATCHED: {tts_path}")
|
||||||
|
patched_any = True
|
||||||
|
else:
|
||||||
|
print(f"SKIP {tts_path}: Edge fallback pattern not found")
|
||||||
|
import re
|
||||||
|
for m in re.finditer(r' else:\n # Default:', code):
|
||||||
|
start = max(0, m.start() - 100)
|
||||||
|
end = min(len(code), m.end() + 300)
|
||||||
|
print(f" Found 'else:/# Default:' at position {m.start()}:")
|
||||||
|
print(f" {code[start:end]}")
|
||||||
|
print(" ---")
|
||||||
|
# Don't exit with error — if one copy isn't patchable, try the others
|
||||||
|
|
||||||
|
if not patched_any:
|
||||||
|
all_patched = all(
|
||||||
|
'provider == "piper"' in open(p).read()
|
||||||
|
for p in found_paths
|
||||||
|
)
|
||||||
|
if all_patched:
|
||||||
|
print("All copies already patched.")
|
||||||
|
sys.exit(0)
|
||||||
|
print("WARNING: Could not patch any copy of tts_tool.py")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print("tts_tool.py patched successfully across all copies.")
|
||||||
|
|||||||
Reference in New Issue
Block a user