fitsum2017 commited on
Commit
ca04799
·
verified ·
1 Parent(s): 0bff9c4

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -202
app.py DELETED
@@ -1,202 +0,0 @@
1
- import spaces
2
- from kokoro import KModel, KPipeline
3
- import gradio as gr
4
- import os
5
- import random
6
- import torch
7
-
8
- IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/')
9
- CUDA_AVAILABLE = torch.cuda.is_available()
10
- if not IS_DUPLICATE:
11
- import kokoro
12
- import misaki
13
- print('DEBUG', kokoro.__version__, CUDA_AVAILABLE, misaki.__version__)
14
-
15
- CHAR_LIMIT = None if IS_DUPLICATE else 5000
16
- models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
17
- pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
18
- pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
19
- pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
20
-
21
- @spaces.GPU(duration=30)
22
- def forward_gpu(ps, ref_s, speed):
23
- return models[True](ps, ref_s, speed)
24
-
25
- def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
26
- text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
27
- pipeline = pipelines[voice[0]]
28
- pack = pipeline.load_voice(voice)
29
- use_gpu = use_gpu and CUDA_AVAILABLE
30
- for _, ps, _ in pipeline(text, voice, speed):
31
- ref_s = pack[len(ps)-1]
32
- try:
33
- if use_gpu:
34
- audio = forward_gpu(ps, ref_s, speed)
35
- else:
36
- audio = models[False](ps, ref_s, speed)
37
- except gr.exceptions.Error as e:
38
- if use_gpu:
39
- gr.Warning(str(e))
40
- gr.Info('Retrying with CPU. To avoid this error, change Hardware to CPU.')
41
- audio = models[False](ps, ref_s, speed)
42
- else:
43
- raise gr.Error(e)
44
- return (24000, audio.numpy()), ps
45
- return None, ''
46
-
47
- # Arena API
48
- def predict(text, voice='af_heart', speed=1):
49
- return generate_first(text, voice, speed, use_gpu=False)[0]
50
-
51
- def tokenize_first(text, voice='af_heart'):
52
- pipeline = pipelines[voice[0]]
53
- for _, ps, _ in pipeline(text, voice):
54
- return ps
55
- return ''
56
-
57
- def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
58
- text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
59
- pipeline = pipelines[voice[0]]
60
- pack = pipeline.load_voice(voice)
61
- use_gpu = use_gpu and CUDA_AVAILABLE
62
- first = True
63
- for _, ps, _ in pipeline(text, voice, speed):
64
- ref_s = pack[len(ps)-1]
65
- try:
66
- if use_gpu:
67
- audio = forward_gpu(ps, ref_s, speed)
68
- else:
69
- audio = models[False](ps, ref_s, speed)
70
- except gr.exceptions.Error as e:
71
- if use_gpu:
72
- gr.Warning(str(e))
73
- gr.Info('Switching to CPU')
74
- audio = models[False](ps, ref_s, speed)
75
- else:
76
- raise gr.Error(e)
77
- yield 24000, audio.numpy()
78
- if first:
79
- first = False
80
- yield 24000, torch.zeros(1).numpy()
81
-
82
- with open('en.txt', 'r') as r:
83
- random_quotes = [line.strip() for line in r]
84
-
85
- def get_random_quote():
86
- return random.choice(random_quotes)
87
-
88
- def get_gatsby():
89
- with open('gatsby5k.md', 'r') as r:
90
- return r.read().strip()
91
-
92
- def get_frankenstein():
93
- with open('frankenstein5k.md', 'r') as r:
94
- return r.read().strip()
95
-
96
- CHOICES = {
97
- '🇺🇸 🚺 Heart ❤️': 'af_heart',
98
- '🇺🇸 🚺 Bella 🔥': 'af_bella',
99
- '🇺🇸 🚺 Nicole 🎧': 'af_nicole',
100
- '🇺🇸 🚺 Aoede': 'af_aoede',
101
- '🇺🇸 🚺 Kore': 'af_kore',
102
- '🇺🇸 🚺 Sarah': 'af_sarah',
103
- '🇺🇸 🚺 Nova': 'af_nova',
104
- '🇺🇸 🚺 Sky': 'af_sky',
105
- '🇺🇸 🚺 Alloy': 'af_alloy',
106
- '🇺🇸 🚺 Jessica': 'af_jessica',
107
- '🇺🇸 🚺 River': 'af_river',
108
- '🇺🇸 🚹 Michael': 'am_michael',
109
- '🇺🇸 🚹 Fenrir': 'am_fenrir',
110
- '🇺🇸 🚹 Puck': 'am_puck',
111
- '🇺🇸 🚹 Echo': 'am_echo',
112
- '🇺🇸 🚹 Eric': 'am_eric',
113
- '🇺🇸 🚹 Liam': 'am_liam',
114
- '🇺🇸 🚹 Onyx': 'am_onyx',
115
- '🇺🇸 🚹 Santa': 'am_santa',
116
- '🇺🇸 🚹 Adam': 'am_adam',
117
- '🇬🇧 🚺 Emma': 'bf_emma',
118
- '🇬🇧 🚺 Isabella': 'bf_isabella',
119
- '🇬🇧 🚺 Alice': 'bf_alice',
120
- '🇬🇧 🚺 Lily': 'bf_lily',
121
- '🇬🇧 🚹 George': 'bm_george',
122
- '🇬🇧 🚹 Fable': 'bm_fable',
123
- '🇬🇧 🚹 Lewis': 'bm_lewis',
124
- '🇬🇧 🚹 Daniel': 'bm_daniel',
125
- }
126
- for v in CHOICES.values():
127
- pipelines[v[0]].load_voice(v)
128
-
129
- TOKEN_NOTE = '''
130
- 💡 Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kˈOkəɹO/)`
131
-
132
- 💬 To adjust intonation, try punctuation `;:,.!?—…"()“”` or stress `ˈ` and `ˌ`
133
-
134
- ⬇️ Lower stress `[1 level](-1)` or `[2 levels](-2)`
135
-
136
- ⬆️ Raise stress 1 level `[or](+2)` 2 levels (only works on less stressed, usually short words)
137
- '''
138
-
139
- with gr.Blocks() as generate_tab:
140
- out_audio = gr.Audio(label='Output Audio', interactive=False, streaming=False, autoplay=True)
141
- generate_btn = gr.Button('Generate', variant='primary')
142
- with gr.Accordion('Output Tokens', open=True):
143
- out_ps = gr.Textbox(interactive=False, show_label=False, info='Tokens used to generate the audio, up to 510 context length.')
144
- tokenize_btn = gr.Button('Tokenize', variant='secondary')
145
- gr.Markdown(TOKEN_NOTE)
146
- predict_btn = gr.Button('Predict', variant='secondary', visible=False)
147
-
148
- STREAM_NOTE = ['⚠️ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.']
149
- if CHAR_LIMIT is not None:
150
- STREAM_NOTE.append(f'✂️ Each stream is capped at {CHAR_LIMIT} characters.')
151
- STREAM_NOTE.append('🚀 Want more characters? You can [use Kokoro directly](https://huggingface.co/hexgrad/Kokoro-82M#usage) or duplicate this space:')
152
- STREAM_NOTE = '\n\n'.join(STREAM_NOTE)
153
-
154
- with gr.Blocks() as stream_tab:
155
- out_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True)
156
- with gr.Row():
157
- stream_btn = gr.Button('Stream', variant='primary')
158
- stop_btn = gr.Button('Stop', variant='stop')
159
- with gr.Accordion('Note', open=True):
160
- gr.Markdown(STREAM_NOTE)
161
- gr.DuplicateButton()
162
-
163
- BANNER_TEXT = '''
164
- [***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M)
165
-
166
- This demo only showcases English, but you can directly use the model to access other languages.
167
- '''
168
- API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
169
- API_NAME = None if API_OPEN else False
170
- with gr.Blocks() as app:
171
- with gr.Row():
172
- gr.Markdown(BANNER_TEXT, container=True)
173
- with gr.Row():
174
- with gr.Column():
175
- 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")
176
- with gr.Row():
177
- voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
178
- use_gpu = gr.Dropdown(
179
- [('ZeroGPU 🚀', True), ('CPU 🐌', False)],
180
- value=CUDA_AVAILABLE,
181
- label='Hardware',
182
- info='GPU is usually faster, but has a usage quota',
183
- interactive=CUDA_AVAILABLE
184
- )
185
- speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
186
- random_btn = gr.Button('🎲 Random Quote 💬', variant='secondary')
187
- with gr.Row():
188
- gatsby_btn = gr.Button('🥂 Gatsby 📕', variant='secondary')
189
- frankenstein_btn = gr.Button('💀 Frankenstein 📗', variant='secondary')
190
- with gr.Column():
191
- gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream'])
192
- random_btn.click(fn=get_random_quote, inputs=[], outputs=[text], api_name=API_NAME)
193
- gatsby_btn.click(fn=get_gatsby, inputs=[], outputs=[text], api_name=API_NAME)
194
- frankenstein_btn.click(fn=get_frankenstein, inputs=[], outputs=[text], api_name=API_NAME)
195
- generate_btn.click(fn=generate_first, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps], api_name=API_NAME)
196
- tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps], api_name=API_NAME)
197
- stream_event = stream_btn.click(fn=generate_all, inputs=[text, voice, speed, use_gpu], outputs=[out_stream], api_name=API_NAME)
198
- stop_btn.click(fn=None, cancels=stream_event)
199
- predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio], api_name=API_NAME)
200
-
201
- if __name__ == '__main__':
202
- app.queue(api_open=API_OPEN).launch(show_api=API_OPEN, ssr_mode=True)