2026-05-09 13:41:37 +00:00
|
|
|
#!/usr/bin/env python3
|
2026-05-09 17:36:26 +00:00
|
|
|
"""Patch Hermes TTS tool: remove Edge TTS, replace with Piper as default/fallback.
|
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
"""
|
2026-05-09 13:41:37 +00:00
|
|
|
import sys
|
2026-05-09 17:36:26 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# Search order: argument > site-packages > /opt/hermes/tools > /opt/hermes checkout
|
|
|
|
|
SEARCH_PATHS = []
|
|
|
|
|
|
|
|
|
|
# Accept path as first argument
|
|
|
|
|
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/tools/tts_tool.py",
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
tts_path = None
|
|
|
|
|
code = None
|
|
|
|
|
|
|
|
|
|
for p in SEARCH_PATHS:
|
|
|
|
|
if os.path.exists(p):
|
|
|
|
|
tts_path = p
|
|
|
|
|
with open(tts_path) as f:
|
|
|
|
|
code = f.read()
|
|
|
|
|
print(f"Found tts_tool.py at: {tts_path}")
|
|
|
|
|
break
|
2026-05-09 13:41:37 +00:00
|
|
|
|
2026-05-09 17:36:26 +00:00
|
|
|
if code is None:
|
|
|
|
|
# Try one more time: find it in the venv site-packages
|
|
|
|
|
import subprocess
|
|
|
|
|
try:
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
[sys.executable, "-c", "import tools.tts_tool; print(tools.tts_tool.__file__)"],
|
|
|
|
|
capture_output=True, text=True, timeout=5
|
|
|
|
|
)
|
|
|
|
|
if result.returncode == 0:
|
|
|
|
|
p = result.stdout.strip()
|
|
|
|
|
if os.path.exists(p):
|
|
|
|
|
tts_path = p
|
|
|
|
|
with open(tts_path) as f:
|
|
|
|
|
code = f.read()
|
|
|
|
|
print(f"Found tts_tool.py via import at: {tts_path}")
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
2026-05-09 13:41:37 +00:00
|
|
|
|
2026-05-09 17:36:26 +00:00
|
|
|
if code is None:
|
|
|
|
|
print("WARNING: tts_tool.py not found. Patching deferred to runtime.")
|
|
|
|
|
print(f"Searched: {SEARCH_PATHS}")
|
|
|
|
|
sys.exit(0)
|
2026-05-09 13:41:37 +00:00
|
|
|
|
|
|
|
|
# Replace the Edge fallback with Piper fallback
|
|
|
|
|
old_edge = ''' else:
|
|
|
|
|
# Default: Edge TTS (free), with NeuTTS as local fallback
|
|
|
|
|
edge_available = True
|
|
|
|
|
try:
|
|
|
|
|
_import_edge_tts()
|
|
|
|
|
except ImportError:
|
|
|
|
|
edge_available = False
|
|
|
|
|
|
|
|
|
|
if edge_available:
|
|
|
|
|
logger.info("Generating speech with Edge TTS...")
|
|
|
|
|
try:
|
|
|
|
|
import concurrent.futures
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
|
|
|
|
|
pool.submit(
|
|
|
|
|
lambda: asyncio.run(_generate_edge_tts(text, file_str, tts_config))
|
|
|
|
|
).result(timeout=60)
|
|
|
|
|
except RuntimeError:
|
|
|
|
|
asyncio.run(_generate_edge_tts(text, file_str, tts_config))
|
|
|
|
|
elif _check_neutts_available():
|
|
|
|
|
logger.info("Edge TTS not available, falling back to NeuTTS (local)...")
|
|
|
|
|
provider = "neutts"
|
|
|
|
|
_generate_neutts(text, file_str, tts_config)
|
|
|
|
|
else:
|
|
|
|
|
return json.dumps({
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": "No TTS provider available. Install edge-tts (pip install edge-tts) "
|
|
|
|
|
"or set up NeuTTS for local synthesis."
|
|
|
|
|
}, ensure_ascii=False)'''
|
|
|
|
|
|
2026-05-09 13:59:09 +00:00
|
|
|
new_piper = ''' else:
|
|
|
|
|
# Default: Piper TTS (local, CPU, no cloud, no Microsoft)
|
2026-05-09 13:41:37 +00:00
|
|
|
piper_available = False
|
|
|
|
|
try:
|
|
|
|
|
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):
|
|
|
|
|
piper_available = True
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if piper_available:
|
|
|
|
|
logger.info("Generating speech with Piper TTS (local, CPU)...")
|
|
|
|
|
import subprocess
|
|
|
|
|
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"]
|
|
|
|
|
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
raw_audio, stderr = proc.communicate(input=text.encode(), timeout=60)
|
|
|
|
|
if proc.returncode != 0:
|
|
|
|
|
raise RuntimeError(f"Piper TTS failed: {stderr.decode()[:200]}")
|
|
|
|
|
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)
|
|
|
|
|
logger.info("Piper TTS audio saved: %s", file_str)
|
|
|
|
|
else:
|
|
|
|
|
return json.dumps({
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": "No TTS provider available. Install Piper TTS (pip install piper-tts) "
|
|
|
|
|
"and download a voice model."
|
|
|
|
|
}, ensure_ascii=False)'''
|
|
|
|
|
|
|
|
|
|
if old_edge in code:
|
2026-05-09 13:59:09 +00:00
|
|
|
code = code.replace(old_edge, new_piper)
|
2026-05-09 13:41:37 +00:00
|
|
|
print("Edge fallback replaced with Piper")
|
2026-05-09 17:36:26 +00:00
|
|
|
elif 'Default: Piper TTS' in code:
|
|
|
|
|
print("Piper fallback already present")
|
2026-05-09 13:41:37 +00:00
|
|
|
else:
|
2026-05-09 17:36:26 +00:00
|
|
|
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)
|
2026-05-09 13:41:37 +00:00
|
|
|
|
|
|
|
|
with open(tts_path, 'w') as f:
|
|
|
|
|
f.write(code)
|
2026-05-09 17:36:26 +00:00
|
|
|
print(f"tts_tool.py patched successfully at: {tts_path}")
|