Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·84 lines (70 loc) · 2.62 KB
/
Copy pathmain.py
File metadata and controls
executable file
·84 lines (70 loc) · 2.62 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
<
85F7
div data-line-number="82" class="react-line-number react-code-text" style="padding-right:16px">8283
84
#!/usr/bin/env -S uv run
import argparse
import signal
import sys
import warnings
import os
signal.signal(signal.SIGINT, lambda *_: sys.exit(130))
warnings.filterwarnings("ignore")
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
os.environ["HF_HUB_VERBOSITY"] = "error"
VOICES = {
"a": [
"af_heart", "af_alloy", "af_aoede", "af_bella", "af_jessica", "af_kore",
"af_nicole", "af_nova", "af_river", "af_sarah", "af_sky",
"am_adam", "am_echo", "am_eric", "am_fenrir", "am_liam",
"am_michael", "am_onyx", "am_puck", "am_santa",
],
"b": [
"bf_alice", "bf_emma", "bf_isabella", "bf_lily",
"bm_daniel", "bm_fable", "bm_george", "bm_lewis",
],
"j": ["jf_alpha", "jf_gongitsune", "jf_nezumi", "jf_tebukuro", "jm_kumo"],
"z": [
"zf_xiaobei", "zf_xiaoni", "zf_xiaoxiao", "zf_xiaoyi",
"zm_yunjian", "zm_yunxi", "zm_yunxia", "zm_yunyang",
],
"e": ["ef_dora", "em_alex", "em_santa"],
"f": ["ff_siwis"],
"h": ["hf_alpha", "hf_beta", "hm_omega", "hm_psi"],
"i": ["if_sara", "im_nicola"],
"p": ["pf_dora", "pm_alex", "pm_santa"],
}
LANG_NAMES = {
"a": "American English",
"b": "British English",
"j": "Japanese",
"z": "Mandarin Chinese",
"e": "Spanish",
"f": "French",
"h": "Hindi",
"i": "Italian",
"p": "Brazilian Portuguese",
}
def list_voices(lang=None):
langs = [lang] if lang else VOICES.keys()
for code in langs:
if code not in VOICES:
print(f"Unknown language code: {code}", file=sys.stderr)
sys.exit(1)
print(f"{LANG_NAMES[code]} ({code}):")
for voice in VOICES[code]:
print(f" {voice}")
parser = argparse.ArgumentParser(description="Text-to-speech via Kokoro")
parser.add_argument("-s", "--speed", type=float, default=1.0, help="Speech speed (default: 1.0)")
parser.add_argument("-l", "--lang", default="a", choices=VOICES.keys(), help="Language code (default: a)")
parser.add_argument("-v", "--voice", default=None, help="Voice name (default: first voice for language)")
parser.add_argument("--list-voices", action="store_true", help="List available voices and exit")
args = parser.parse_args()
if args.list_voices:
list_voices(args.lang if args.lang != "a" or "--lang" in sys.argv or "-l" in sys.argv else None)
sys.exit(0)
voice = args.voice or VOICES[args.lang][0]
import sounddevice as sd
from kokoro import KPipeline
pipeline = KPipeline(lang_code=args.lang, repo_id="hexgrad/Kokoro-82M")
text = sys.stdin.read()
for chunk in pipeline(text, voice=voice, speed=args.speed):
audio = chunk.audio
sd.play(audio, samplerate=24000)
sd.wait()