-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
318 lines (259 loc) · 8.87 KB
/
Copy pathserver.py
File metadata and controls
318 lines (259 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""Local Kokoro TTS HTTP server with chunked, streaming synthesis.
Long selections are split into sentence-bounded chunks. The first chunk
generates and starts playing while later chunks generate in the background,
so the time-to-first-word is roughly one short sentence regardless of total
selection length.
POST /speak {"text": "...", "voice": "af_heart", "speed": 1.0}
POST /stop
GET /health
GET /voices
GET /config
POST /config {"voice": "...", "speed": 1.0} (partial ok)
"""
from __future__ import annotations
import json
import logging
import os
import queue
import re
import subprocess
import tempfile
import threading
import uuid
from pathlib import Path
import soundfile as sf
from flask import Flask, jsonify, request
from kokoro_onnx import Kokoro
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("tts")
ROOT = Path.home() / ".local/tts"
MODEL_DIR = ROOT / "models"
MODEL_PATH = MODEL_DIR / "kokoro-v1.0.onnx"
VOICES_PATH = MODEL_DIR / "voices-v1.0.bin"
CONFIG_PATH = ROOT / "config.json"
DEFAULTS = {"voice": "af_heart", "speed": 1.0, "lang": "en-us"}
# Chunk size targets. Smaller first chunk minimizes time-to-first-word;
# larger later chunks keep total synth calls down.
FIRST_CHUNK_MAX = 140
CHUNK_TARGET_CHARS = 380
QUEUE_LOOKAHEAD = 3 # max chunks pre-generated and waiting to play
if not MODEL_PATH.exists() or not VOICES_PATH.exists():
raise SystemExit(
f"Missing model files. Expected:\n {MODEL_PATH}\n {VOICES_PATH}\n"
"Run download_models.sh first."
)
def load_config() -> dict:
cfg = dict(DEFAULTS)
if CONFIG_PATH.exists():
try:
cfg.update(json.loads(CONFIG_PATH.read_text()))
except Exception as e:
log.warning("config load failed: %s", e)
return cfg
def save_config(cfg: dict) -> None:
CONFIG_PATH.write_text(json.dumps(cfg, indent=2))
def split_text(
text: str,
first_max: int = FIRST_CHUNK_MAX,
max_chars: int = CHUNK_TARGET_CHARS,
) -> list[str]:
"""Split into chunks ending on sentence boundaries when possible.
First chunk is intentionally smaller so playback starts faster;
later chunks are larger to reduce total synthesis calls.
"""
text = (text or "").strip()
if not text:
return []
parts = re.split(r"(?<=[.!?…])\s+|\n{2,}", text)
chunks: list[str] = []
cur = ""
target = first_max
for p in parts:
p = p.strip()
if not p:
continue
if not cur:
cur = p
continue
candidate = cur + " " + p
if len(candidate) <= target:
cur = candidate
else:
chunks.append(cur)
cur = p
target = max_chars
if cur:
chunks.append(cur)
# hard-split any sentence-less walls of text
final: list[str] = []
for c in chunks:
while len(c) > max_chars * 2:
split_at = c.rfind(" ", 0, max_chars * 2)
if split_at <= 0:
split_at = max_chars * 2
final.append(c[:split_at].strip())
c = c[split_at:].lstrip()
if c.strip():
final.append(c.strip())
return final
log.info("Loading Kokoro model...")
kokoro = Kokoro(str(MODEL_PATH), str(VOICES_PATH))
log.info("Warming up...")
try:
kokoro.create("ready.", voice=DEFAULTS["voice"], speed=1.0, lang=DEFAULTS["lang"])
except Exception as e:
log.warning("warmup failed: %s", e)
log.info("Kokoro ready.")
config = load_config()
log.info("Config: %s", config)
app = Flask(__name__)
# ─── Streaming pipeline ──────────────────────────────────────────────────
_state_lock = threading.Lock()
_session_id = 0
_proc: subprocess.Popen | None = None
_play_queue: queue.Queue = queue.Queue(maxsize=QUEUE_LOOKAHEAD)
def _drain_queue() -> None:
while True:
try:
_, path = _play_queue.get_nowait()
except queue.Empty:
return
if path:
try:
os.unlink(path)
except OSError:
pass
def _cancel_current() -> bool:
"""Bump session, kill any playing afplay, drain queued chunks."""
global _proc, _session_id
stopped = False
with _state_lock:
_session_id += 1
if _proc is not None and _proc.poll() is None:
_proc.terminate()
try:
_proc.wait(timeout=1.0)
except subprocess.TimeoutExpired:
_proc.kill()
stopped = True
_proc = None
_drain_queue()
return stopped
def _start_session() -> int:
"""Cancel anything in flight and return a fresh session id."""
_cancel_current()
with _state_lock:
return _session_id
def _producer(session: int, chunks: list[str], voice: str, speed: float, lang: str) -> None:
for chunk in chunks:
with _state_lock:
if session != _session_id:
return
try:
samples, sr = kokoro.create(chunk, voice=voice, speed=speed, lang=lang)
except Exception:
log.exception("synthesis failed for chunk")
return
path = str(Path(tempfile.gettempdir()) / f"tts-{uuid.uuid4().hex}.wav")
sf.write(path, samples, sr)
with _state_lock:
stale = session != _session_id
if stale:
try:
os.unlink(path)
except OSError:
pass
return
# Bounded queue — blocks until consumer drains a slot.
# If session is cancelled while we're blocked, the drain will
# free the slot, and the next iteration's session check stops us.
while True:
try:
_play_queue.put((session, path), timeout=0.5)
break
except queue.Full:
with _state_lock:
if session != _session_id:
try:
os.unlink(path)
except OSError:
pass
return
def _consumer_loop() -> None:
global _proc
while True:
session, path = _play_queue.get()
with _state_lock:
current = _session_id
if session != current or not path or not os.path.exists(path):
if path:
try:
os.unlink(path)
except OSError:
pass
continue
proc = subprocess.Popen(["afplay", path])
with _state_lock:
_proc = proc
proc.wait()
try:
os.unlink(path)
except OSError:
pass
threading.Thread(target=_consumer_loop, daemon=True).start()
# ─── HTTP routes ─────────────────────────────────────────────────────────
_cfg_lock = threading.Lock()
@app.post("/speak")
def speak():
data = request.get_json(force=True, silent=True) or {}
text = (data.get("text") or "").strip()
if not text:
return jsonify({"error": "no text"}), 400
with _cfg_lock:
voice = data.get("voice") or config["voice"]
speed = float(data.get("speed") or config["speed"])
lang = data.get("lang") or config["lang"]
chunks = split_text(text)
if not chunks:
return jsonify({"error": "empty after splitting"}), 400
session = _start_session()
threading.Thread(
target=_producer,
args=(session, chunks, voice, speed, lang),
daemon=True,
).start()
log.info(
"speak voice=%s speed=%s len=%d chunks=%d session=%d",
voice, speed, len(text), len(chunks), session,
)
return jsonify({"ok": True, "chunks": len(chunks), "session": session})
@app.post("/stop")
def stop():
return jsonify({"ok": True, "stopped": _cancel_current()})
@app.get("/health")
def health():
return jsonify({"ok": True})
@app.get("/voices")
def voices():
return jsonify({"voices": sorted(kokoro.get_voices())})
@app.get("/config")
def get_config():
with _cfg_lock:
return jsonify(config)
@app.post("/config")
def set_config():
data = request.get_json(force=True, silent=True) or {}
allowed = {"voice", "speed", "lang"}
updates = {k: v for k, v in data.items() if k in allowed}
if "speed" in updates:
updates["speed"] = float(updates["speed"])
if "voice" in updates and updates["voice"] not in kokoro.get_voices():
return jsonify({"error": f"unknown voice: {updates['voice']}"}), 400
with _cfg_lock:
config.update(updates)
save_config(config)
snapshot = dict(config)
log.info("config updated: %s", updates)
return jsonify(snapshot)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8765, threaded=True)