File size: 2,025 Bytes
3353157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import solara
import soundfile as sf
from kokoro_onnx import Kokoro
from IPython.display import display, Audio

text = solara.reactive("")

@solara.component
def Example(example: str, example_shortened=None):
    def on_click():
        text.value = example
    with solara.Row():
        solara.Button(example, on_click=on_click)


models = ["kokoro-v0_19.onnx", "kokoro-quant.onnx"]
model = solara.reactive("kokoro-v0_19.onnx")
voices = ["af", "af_bella", "af_nicole", "af_sarah", "af_sky", "am_adam", "am_michael", "bf_emma", "bf_isabella", "bm_george", "bm_lewis"]
voice = solara.reactive("af_sarah")

generate = solara.reactive(False)
@solara.component
def Page():
    title = "Kokoro Text-To-Speech"
    with solara.Head():
        solara.Title(f"{title}")
    with solara.Column(style={"width": "100%", "padding": "50px", "align": "center"}):
        solara.Markdown(f"#{title}")
        solara.Markdown("Try some examples:")
        with solara.Row():
            Example("Hello! How can I assist you today?")
            Example("The capital of France is Paris.")
        solara.Markdown("Or write your own:")
        solara.InputTextArea("Enter some text", value=text, rows=3, continuous_update=False)
        with solara.Row():
            solara.Select(label="Select voice:", value=voice, values=voices, style={"width": "10%"})
            solara.Select(label="Select model:", value=model, values=models, style={"width": "10%"})
            solara.Button("Generate", on_click=generate.set(True))
            solara.Button("Clear", on_click=lambda: text.set(""))
        kokoro = Kokoro(model.value, "voices.bin")
        if text.value != "" and generate.value:
            start_time = time.time()
            samples, sample_rate = kokoro.create(text.value, voice=voice.value, speed=1.0)
            end_time = time.time()
            display(Audio(data=samples, rate=sample_rate, autoplay=True))
            solara.Text(f"Generation time: {end_time - start_time:.1f} seconds", style="color: blue")