File size: 7,325 Bytes
a72c43d
 
 
 
 
 
 
9f97307
 
 
 
 
 
a72c43d
 
 
9f97307
 
a72c43d
 
 
 
 
 
9f97307
 
a72c43d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f97307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a72c43d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces
from kokoro import KModel, KPipeline
import gradio as gr
import os
import random
import torch

print(os.system("""
cd front;
npm ci;
npm run build;
cd ..;
"""))

CHAR_LIMIT = 5000 # test

SPACE_ID = os.environ.get('SPACE_ID')

CUDA_AVAILABLE = torch.cuda.is_available()
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkΙ™ΙΉO'
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkΙ™ΙΉQ'

gr.set_static_paths(paths=["./front/dist"])

@spaces.GPU(duration=30)
def forward_gpu(ps, ref_s, speed):
    return models[True](ps, ref_s, speed)

def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
    text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
    pipeline = pipelines[voice[0]]
    pack = pipeline.load_voice(voice)
    use_gpu = use_gpu and CUDA_AVAILABLE
    for _, ps, _ in pipeline(text, voice, speed):
        ref_s = pack[len(ps)-1]
        try:
            if use_gpu:
                audio = forward_gpu(ps, ref_s, speed)
            else:
                audio = models[False](ps, ref_s, speed)
        except gr.exceptions.Error as e:
            if use_gpu:
                gr.Warning(str(e))
                gr.Info('Retrying with CPU. To avoid this error, change Hardware to CPU.')
                audio = models[False](ps, ref_s, speed)
            else:
                raise gr.Error(e)
        return (24000, audio.numpy()), ps
    return None, ''

# Arena API
def predict(text, voice='af_heart', speed=1):
    return generate_first(text, voice, speed, use_gpu=False)[0]

def tokenize_first(text, voice='af_heart'):
    pipeline = pipelines[voice[0]]
    for _, ps, _ in pipeline(text, voice):
        return ps
    return ''

def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
    text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
    pipeline = pipelines[voice[0]]
    pack = pipeline.load_voice(voice)
    use_gpu = use_gpu and CUDA_AVAILABLE
    first = True
    for _, ps, _ in pipeline(text, voice, speed):
        ref_s = pack[len(ps)-1]
        try:
            if use_gpu:
                audio = forward_gpu(ps, ref_s, speed)
            else:
                audio = models[False](ps, ref_s, speed)
        except gr.exceptions.Error as e:
            if use_gpu:
                gr.Warning(str(e))
                gr.Info('Switching to CPU')
                audio = models[False](ps, ref_s, speed)
            else:
                raise gr.Error(e)
        yield 24000, audio.numpy()
        if first:
            first = False
            yield 24000, torch.zeros(1).numpy()

CHOICES = {
'πŸ‡ΊπŸ‡Έ 🚺 Heart ❀️': 'af_heart',
'πŸ‡ΊπŸ‡Έ 🚺 Bella πŸ”₯': 'af_bella',
'πŸ‡ΊπŸ‡Έ 🚺 Nicole 🎧': 'af_nicole',
'πŸ‡ΊπŸ‡Έ 🚺 Aoede': 'af_aoede',
'πŸ‡ΊπŸ‡Έ 🚺 Kore': 'af_kore',
'πŸ‡ΊπŸ‡Έ 🚺 Sarah': 'af_sarah',
'πŸ‡ΊπŸ‡Έ 🚺 Nova': 'af_nova',
'πŸ‡ΊπŸ‡Έ 🚺 Sky': 'af_sky',
'πŸ‡ΊπŸ‡Έ 🚺 Alloy': 'af_alloy',
'πŸ‡ΊπŸ‡Έ 🚺 Jessica': 'af_jessica',
'πŸ‡ΊπŸ‡Έ 🚺 River': 'af_river',
'πŸ‡ΊπŸ‡Έ 🚹 Michael': 'am_michael',
'πŸ‡ΊπŸ‡Έ 🚹 Fenrir': 'am_fenrir',
'πŸ‡ΊπŸ‡Έ 🚹 Puck': 'am_puck',
'πŸ‡ΊπŸ‡Έ 🚹 Echo': 'am_echo',
'πŸ‡ΊπŸ‡Έ 🚹 Eric': 'am_eric',
'πŸ‡ΊπŸ‡Έ 🚹 Liam': 'am_liam',
'πŸ‡ΊπŸ‡Έ 🚹 Onyx': 'am_onyx',
'πŸ‡ΊπŸ‡Έ 🚹 Santa': 'am_santa',
'πŸ‡ΊπŸ‡Έ 🚹 Adam': 'am_adam',
'πŸ‡¬πŸ‡§ 🚺 Emma': 'bf_emma',
'πŸ‡¬πŸ‡§ 🚺 Isabella': 'bf_isabella',
'πŸ‡¬πŸ‡§ 🚺 Alice': 'bf_alice',
'πŸ‡¬πŸ‡§ 🚺 Lily': 'bf_lily',
'πŸ‡¬πŸ‡§ 🚹 George': 'bm_george',
'πŸ‡¬πŸ‡§ 🚹 Fable': 'bm_fable',
'πŸ‡¬πŸ‡§ 🚹 Lewis': 'bm_lewis',
'πŸ‡¬πŸ‡§ 🚹 Daniel': 'bm_daniel',
}
for v in CHOICES.values():
    pipelines[v[0]].load_voice(v)

TOKEN_NOTE = '''
πŸ’‘ Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kˈOkΙ™ΙΉO/)`

πŸ’¬ To adjust intonation, try punctuation `;:,.!?—…"()β€œβ€` or stress `ˈ` and `ˌ`

⬇️ Lower stress `[1 level](-1)` or `[2 levels](-2)`

⬆️ Raise stress 1 level `[or](+2)` 2 levels (only works on less stressed, usually short words)
'''

with gr.Blocks() as generate_tab:
    out_audio = gr.Audio(label='Output Audio', interactive=False, streaming=False, autoplay=True)
    generate_btn = gr.Button('Generate', variant='primary')
    with gr.Accordion('Output Tokens', open=True):
        out_ps = gr.Textbox(interactive=False, show_label=False, info='Tokens used to generate the audio, up to 510 context length.')
        tokenize_btn = gr.Button('Tokenize', variant='secondary')
        gr.Markdown(TOKEN_NOTE)
        predict_btn = gr.Button('Predict', variant='secondary', visible=False)

STREAM_NOTE = ['⚠️ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.']
if CHAR_LIMIT is not None:
    STREAM_NOTE.append(f'βœ‚οΈ Each stream is capped at {CHAR_LIMIT} characters.')
    STREAM_NOTE.append('πŸš€ Want more characters? You can [use Kokoro directly](https://huggingface.co/hexgrad/Kokoro-82M#usage) or duplicate this space:')
STREAM_NOTE = '\n\n'.join(STREAM_NOTE)

with gr.Blocks() as stream_tab:
    out_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True)
    with gr.Row():
        stream_btn = gr.Button('Stream', variant='primary')
        stop_btn = gr.Button('Stop', variant='stop')
    with gr.Accordion('Note', open=True):
        gr.Markdown(STREAM_NOTE)
        gr.DuplicateButton()

API_NAME = 'tts'


head = f'''
<script>
    window.SPACE_ID = '{SPACE_ID}';
    if (!localStorage.getItem('debug')) {{
        document.querySelector('body').classList = [];
        const rootDiv = document.createElement('div');
        rootDiv.id = 'root';
        document.body.appendChild(rootDiv);
    }}
</script>
'''

with open('./front/dist/index.html', 'r') as f:
    html = f.read()
    lines = html.split('\n')
    for i, line in enumerate(lines):
        if '<script' in line or 'stylesheet' in line:
            head += '\n'.join(lines[:i])

with gr.Blocks(head=head) as app:
    with gr.Row():
        with gr.Column():
            text = gr.Textbox(label='Input Text', info=f"Up to ~500 characters per Generate, or {'∞' if CHAR_LIMIT is None else CHAR_LIMIT} characters per Stream")
            voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
            speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
        with gr.Column():
            gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream'])
    generate_btn.click(fn=generate_first, inputs=[text, voice, speed], outputs=[out_audio, out_ps], api_name=API_NAME)
    tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps], api_name=API_NAME)
    stream_event = stream_btn.click(fn=generate_all, inputs=[text, voice, speed], outputs=[out_stream], api_name=API_NAME)
    stop_btn.click(fn=None, cancels=stream_event)
    predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio], api_name=API_NAME)

if __name__ == '__main__':
    app.queue(api_open=True).launch(show_api=True, ssr_mode=True)