Eyob-Sol commited on
Commit
16523ff
·
verified ·
1 Parent(s): 89b2865

Update models/tts_router.py

Browse files
Files changed (1) hide show
  1. models/tts_router.py +26 -38
models/tts_router.py CHANGED
@@ -110,60 +110,48 @@ def _tts_with_pyttsx3(text: str) -> Optional[str]:
110
  def _tts_with_piper(text: str) -> Optional[str]:
111
  model = os.getenv("PIPER_MODEL")
112
  piper_bin = os.getenv("PIPER_BIN", "piper")
113
-
114
- print(f"[TTS][PIPER] model={model} exists={os.path.exists(model) if model else None} bin={piper_bin}")
115
-
116
  if not (model and os.path.exists(model)):
117
- print("[TTS][PIPER] missing or invalid PIPER_MODEL")
118
  return None
119
 
120
- # Accept absolute custom binary or PATH binary
121
- if os.path.isabs(piper_bin):
122
- if not os.path.exists(piper_bin):
123
- print("[TTS][PIPER] absolute PIPER_BIN not found:", piper_bin)
124
- return None
125
- _ensure_exec(piper_bin)
126
- else:
127
- if not _have(piper_bin):
128
- print("[TTS][PIPER] binary not in PATH:", piper_bin)
129
- return None
130
 
131
- out_dir = ensure_runtime_audio_dir()
132
- out_path = os.path.join(out_dir, f"tts_{uuid.uuid4().hex}.wav")
133
- safe_text = re.sub(r"[\x00-\x1F]+", " ", text).strip() or "Hello."
 
 
 
 
134
 
135
- cmd = [piper_bin, "--model", model, "--output_file", out_path]
136
- # Optional: speed/pitch envs
137
- spd = os.getenv("PIPER_SPEED")
138
- pit = os.getenv("PIPER_PITCH")
139
- if spd: cmd += ["--length_scale", spd]
140
- if pit: cmd += ["--pitch", pit]
141
 
142
  try:
143
- print("[TTS][PIPER] running:", " ".join(cmd))
144
  p = subprocess.Popen(
145
- cmd,
146
  stdin=subprocess.PIPE,
147
- stdout=subprocess.PIPE,
148
  stderr=subprocess.PIPE,
 
149
  )
150
- stdout, stderr = p.communicate(input=safe_text.encode("utf-8"), timeout=60)
151
- rc = p.returncode
152
- ok = os.path.exists(out_path) and _is_valid_wav(out_path)
153
- print(f"[TTS][PIPER] rc={rc} wrote={out_path if os.path.exists(out_path) else 'None'} valid={ok}")
154
  if not ok:
155
- if stderr:
156
  try:
157
- print("[TTS][PIPER][stderr]", stderr.decode("utf-8", "ignore")[:500])
158
  except Exception:
159
  pass
160
- return out_path if ok else None
161
- except subprocess.TimeoutExpired:
162
- p.kill()
163
- print("[TTS][PIPER] timeout")
164
- return None
165
  except Exception as e:
166
- print("[TTS][PIPER] error:", e)
167
  return None
168
 
169
  def _tts_with_say(text: str) -> Optional[str]:
 
110
  def _tts_with_piper(text: str) -> Optional[str]:
111
  model = os.getenv("PIPER_MODEL")
112
  piper_bin = os.getenv("PIPER_BIN", "piper")
 
 
 
113
  if not (model and os.path.exists(model)):
 
114
  return None
115
 
116
+ # accept either absolute binary path or something in PATH
117
+ bin_is_abs = os.path.isabs(piper_bin) and os.path.exists(piper_bin)
118
+ bin_in_path = which(piper_bin) is not None
119
+ if not (bin_is_abs or bin_in_path):
120
+ print(f"[TTS][PIPER] binary not found: {piper_bin}")
121
+ return None
 
 
 
 
122
 
123
+ # Prefer system espeak-ng data if available
124
+ if not os.getenv("ESPEAK_DATA_PATH"):
125
+ for cand in ("/usr/share/espeak-ng-data", "/usr/share/espeak-data",
126
+ "models/piper/espeak-ng-data"):
127
+ if os.path.isdir(cand):
128
+ os.environ["ESPEAK_DATA_PATH"] = cand
129
+ break
130
 
131
+ out_path = os.path.join(ensure_runtime_audio_dir(), f"tts_{uuid.uuid4().hex}.wav")
132
+ safe_text = re.sub(r"[\x00-\x1F]+", " ", text).strip()
 
 
 
 
133
 
134
  try:
135
+ # show stderr during debugging
136
  p = subprocess.Popen(
137
+ [piper_bin, "--model", model, "--output_file", out_path],
138
  stdin=subprocess.PIPE,
139
+ stdout=subprocess.DEVNULL,
140
  stderr=subprocess.PIPE,
141
+ env=os.environ.copy(),
142
  )
143
+ _, err = p.communicate(input=safe_text.encode("utf-8"), timeout=60)
144
+ ok = (p.returncode == 0 and _is_valid_wav(out_path))
 
 
145
  if not ok:
146
+ if err:
147
  try:
148
+ print("[TTS][PIPER][stderr]", err.decode("utf-8", "ignore")[:500])
149
  except Exception:
150
  pass
151
+ return None
152
+ return out_path
 
 
 
153
  except Exception as e:
154
+ print("[TTS] Piper error:", e)
155
  return None
156
 
157
  def _tts_with_say(text: str) -> Optional[str]: