-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathkokoro_cli.py
More file actions
172 lines (139 loc) · 5.31 KB
/
kokoro_cli.py
File metadata and controls
172 lines (139 loc) · 5.31 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
"""Minimal Kokoro v1.1-zh CLI wrapper for local_tts_server.
Usage:
python kokoro_cli.py <text_file> <out_file> <voice> <speed>
Reads text from <text_file>, synthesizes with kokoro, writes WAV to <out_file>.
"""
from __future__ import annotations
import argparse
import os
import sys
import wave
from pathlib import Path
import numpy as np
from local_tts_profiles import (
DEFAULT_KOKORO_REPO_ID,
DEFAULT_KOKORO_VOICE,
available_kokoro_voices,
find_kokoro_model_file,
resolve_kokoro_model_dir,
resolve_kokoro_voice_file,
)
SAMPLE_RATE = 24000
def _audio_from_result(result):
if hasattr(result, "audio"):
return result.audio
if isinstance(result, tuple) and result:
return result[-1]
return None
def _infer_lang_code(voice: str) -> str:
# Kokoro uses single-letter lang codes: z=zh, a=en-us, b=en-gb.
if voice.startswith(("a", "af", "am")):
return "a"
if voice.startswith(("b", "bf", "bm")):
return "b"
return "z"
def _speed_callable(base_speed: float):
"""Mitigate rushed long Chinese phoneme sequences in v1.1-zh."""
base = base_speed if base_speed > 0 else 1.0
def speed_by_len(len_ps: int) -> float:
speed = 1.0
if len_ps > 83 and len_ps < 183:
speed = 1.0 - (len_ps - 83) / 500.0
elif len_ps >= 183:
speed = 0.8
return max(0.5, speed * base)
return speed_by_len
def synthesize(text_path: str, out_path: str, voice: str, speed: float) -> int:
try:
import torch
from kokoro import KModel, KPipeline
except ImportError:
print(
'kokoro v1.1-zh deps missing. Run: uv pip install "kokoro>=0.8.2" "misaki[zh]>=0.8.2"',
file=sys.stderr,
)
return 1
text = Path(text_path).read_text(encoding="utf-8").strip()
if not text:
print("Empty text file", file=sys.stderr)
return 1
model_dir = resolve_kokoro_model_dir()
repo_id = os.getenv("LOCAL_TTS_KOKORO_REPO_ID", DEFAULT_KOKORO_REPO_ID).strip() or DEFAULT_KOKORO_REPO_ID
voice = (voice or "").strip() or os.getenv("LOCAL_TTS_KOKORO_DEFAULT_VOICE", DEFAULT_KOKORO_VOICE)
available_voices = set(available_kokoro_voices(model_dir))
if available_voices and voice not in available_voices:
fallback_voice = os.getenv("LOCAL_TTS_KOKORO_DEFAULT_VOICE", DEFAULT_KOKORO_VOICE).strip() or DEFAULT_KOKORO_VOICE
if fallback_voice not in available_voices:
fallback_voice = sorted(available_voices)[0]
print(
f"Kokoro voice '{voice}' not found in local model dir; falling back to '{fallback_voice}'.",
file=sys.stderr,
)
voice = fallback_voice
pipeline_voice = resolve_kokoro_voice_file(voice, model_dir)
lang = _infer_lang_code(voice)
device = os.getenv("LOCAL_TTS_KOKORO_DEVICE", "").strip()
if not device:
device = "cuda" if torch.cuda.is_available() else "cpu"
if model_dir:
config_path = model_dir / "config.json"
model_path = find_kokoro_model_file(model_dir)
if not config_path.is_file() or model_path is None:
print(
f"Invalid LOCAL_TTS_KOKORO_MODEL_DIR: {model_dir} "
"(expected config.json and a .pth model file)",
file=sys.stderr,
)
return 1
model = KModel(repo_id=repo_id, config=str(config_path), model=str(model_path)).to(device).eval()
else:
model = KModel(repo_id=repo_id).to(device).eval()
en_pipeline = None
en_callable = None
if lang == "z":
en_pipeline = KPipeline(lang_code="a", repo_id=repo_id, model=False)
def en_callable(text_part: str):
if text_part == "Kokoro":
return "kˈOkəɹO"
if text_part == "Sol":
return "sˈOl"
return next(en_pipeline(text_part)).phonemes
pipeline = KPipeline(
lang_code=lang,
repo_id=repo_id,
model=model,
en_callable=en_callable,
)
effective_speed = _speed_callable(speed) if lang == "z" else speed
generator = pipeline(text, voice=pipeline_voice, speed=effective_speed)
chunks: list[np.ndarray] = []
for result in generator:
audio = _audio_from_result(result)
if audio is not None:
chunks.append(np.asarray(audio, dtype=np.float32))
if not chunks:
print("No audio generated", file=sys.stderr)
return 1
pcm = np.concatenate(chunks)
pcm = np.clip(pcm, -1.0, 1.0)
pcm_int16 = (pcm * 32767.0).astype(np.int16)
with wave.open(out_path, "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(SAMPLE_RATE)
wf.writeframes(pcm_int16.tobytes())
print(
f"Wrote {out_path}: {len(pcm_int16)} samples @ {SAMPLE_RATE} Hz "
f"repo={repo_id} model_dir={model_dir or '<hf-cache>'} voice={voice} device={device}"
)
return 0
def main() -> int:
parser = argparse.ArgumentParser(description="Kokoro CLI wrapper for local_tts")
parser.add_argument("text_file")
parser.add_argument("out_file")
parser.add_argument("voice")
parser.add_argument("speed", type=float)
args = parser.parse_args()
return synthesize(args.text_file, args.out_file, args.voice, args.speed)
if __name__ == "__main__":
raise SystemExit(main())