shukdevdatta123 commited on
Commit
8eb4a72
·
verified ·
1 Parent(s): 951219d

Create abc.txt

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