Views
No views yet
1import soundfile as sf
2
3from txtai.pipeline import TextToSpeech
4
5# Build pipeline
6tts = TextToSpeech("NeuML/kokoro-int8-onnx")
7
8# Generate speech
9speech, rate = tts("Say something here")
10
11# Write to file
12sf.write("out.wav", speech, rate)ttstokenizer is a permissively licensed library with no external dependencies (such as espeak).1import json
2import numpy as np
3import onnxruntime
4import soundfile as sf
5
6from ttstokenizer import IPATokenizer
7
8# This example assumes the files have been downloaded locally
9with open("kokoro-int8-onnx/voices.json", "r", encoding="utf-8") as f:
10 voices = json.load(f)
11
12# Create model
13model = onnxruntime.InferenceSession(
14 "kokoro-int8-onnx/model.onnx",
15 providers=["CPUExecutionProvider"]
16)
17
18# Create tokenizer
19tokenizer = IPATokenizer()
20
21# Tokenize inputs
22inputs = tokenizer("Say something here")
23
24# Get speaker array
25speaker = np.array(voices["af"], dtype=np.float32)
26
27# Generate speech
28outputs = model.run(None, {
29 "tokens": [[0, *inputs, 0]],
30 "style": speaker[len(inputs)],
31 "speed": np.ones(1, dtype=np.float32) * 1.0
32})
33
34# Write to file
35sf.write("out.wav", outputs[0], 24000)speaker id from the reference table below.| SPEAKER | GENDER | NATIONALITY | EXAMPLE |
|---|---|---|---|
| af | F | American | |
| af_bella | F | American | |
| af_nicole | F | American | |
| af_sarah | F | American | |
| af_sky | F | American | |
| am_adam | M | American | |
| af_michael | M | American | |
| bf_emma | F | British | |
| bf_isabella | F | British | |
| bm_george | M | British | |
| bm_lewis | M | British |
speech, rate = tts("Say something here", speaker="af_sky")