#!/usr/bin/env python3 """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). """ import sys 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 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 if code is None: print("WARNING: tts_tool.py not found. Patching deferred to runtime.") print(f"Searched: {SEARCH_PATHS}") sys.exit(0) # 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)''' new_piper = ''' else: # Default: Piper TTS (local, CPU, no cloud, no Microsoft) 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: code = code.replace(old_edge, new_piper) print("Edge fallback replaced with Piper") elif 'Default: Piper TTS' in code: 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) with open(tts_path, 'w') as f: f.write(code) print(f"tts_tool.py patched successfully at: {tts_path}")