2026-05-09 13:41:37 +00:00
|
|
|
#!/usr/bin/env python3
|
2026-05-09 13:59:09 +00:00
|
|
|
"""Patch Hermes TTS tool: remove Edge TTS, replace with Piper as default/fallback."""
|
2026-05-09 13:41:37 +00:00
|
|
|
import sys
|
|
|
|
|
|
2026-05-09 14:27:07 +00:00
|
|
|
tts_path = '/opt/hermes/tools/tts_tool.py'
|
2026-05-09 13:41:37 +00:00
|
|
|
|
|
|
|
|
with open(tts_path) as f:
|
|
|
|
|
code = f.read()
|
|
|
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
else:
|
|
|
|
|
if 'Default: Piper TTS' in code:
|
2026-05-09 13:59:09 +00:00
|
|
|
print("Piper fallback already present")
|
2026-05-09 13:41:37 +00:00
|
|
|
else:
|
2026-05-09 13:59:09 +00:00
|
|
|
print("ERROR: Could not find Edge fallback in tts_tool.py")
|
|
|
|
|
# Debug output
|
2026-05-09 13:41:37 +00:00
|
|
|
import re
|
2026-05-09 13:59:09 +00:00
|
|
|
for m in re.finditer(r' else:\n # Default:', code):
|
|
|
|
|
start = max(0, m.start() - 100)
|
|
|
|
|
end = min(len(code), m.end() + 200)
|
|
|
|
|
print(f"Found else/default at position {m.start()}:")
|
|
|
|
|
print(code[start:end])
|
2026-05-09 13:41:37 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
with open(tts_path, 'w') as f:
|
|
|
|
|
f.write(code)
|
|
|
|
|
print("tts_tool.py patched successfully")
|