Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
INTROTXT = """# StyleTTS 2
|
2 |
+
|
3 |
+
[Paper](https://arxiv.org/abs/2306.07691) - [Samples](https://styletts2.github.io/) - [Code](https://github.com/yl4579/StyleTTS2) - [Discord](https://discord.gg/ha8sxdG2K4)
|
4 |
+
|
5 |
+
A free demo of StyleTTS 2. **I am not affiliated with the StyleTTS 2 Authors.**
|
6 |
+
|
7 |
+
**Before using this demo, you agree to inform the listeners that the speech samples are synthesized by the pre-trained models, unless you have the permission to use the voice you synthesize. That is, you agree to only use voices whose speakers grant the permission to have their voice cloned, either directly or by license before making synthesized voices public, or you have to publicly announce that these voices are synthesized if you do not have the permission to use these voices.**
|
8 |
+
|
9 |
+
Is there a long queue on this space? Duplicate it and add a more powerful GPU to skip the wait! **Note: Thank you to Hugging Face for their generous GPU grant program!**
|
10 |
+
|
11 |
+
**NOTE: StyleTTS 2 does better on longer texts.** For example, making it say "hi" will produce a lower-quality result than making it say a longer phrase.
|
12 |
+
|
13 |
+
**NOTE: StyleTTS 2 is _currently_ English-only. Join the Discord for updates on multilingual training.**
|
14 |
+
"""
|
15 |
+
import gradio as gr
|
16 |
+
import styletts2importable
|
17 |
+
import ljspeechimportable
|
18 |
+
import torch
|
19 |
+
import os
|
20 |
+
from txtsplit import txtsplit
|
21 |
+
import numpy as np
|
22 |
+
import pickle
|
23 |
+
theme = gr.themes.Base(
|
24 |
+
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
|
25 |
+
)
|
26 |
+
voicelist = ['f-us-1', 'f-us-2', 'f-us-3', 'f-us-4', 'm-us-1', 'm-us-2', 'm-us-3', 'm-us-4']
|
27 |
+
voices = {}
|
28 |
+
import phonemizer
|
29 |
+
global_phonemizer = phonemizer.backend.EspeakBackend(language='en-us', preserve_punctuation=True, with_stress=True)
|
30 |
+
# todo: cache computed style, load using pickle
|
31 |
+
# if os.path.exists('voices.pkl'):
|
32 |
+
# with open('voices.pkl', 'rb') as f:
|
33 |
+
# voices = pickle.load(f)
|
34 |
+
# else:
|
35 |
+
for v in voicelist:
|
36 |
+
voices[v] = styletts2importable.compute_style(f'voices/{v}.wav')
|
37 |
+
# def synthesize(text, voice, multispeakersteps):
|
38 |
+
# if text.strip() == "":
|
39 |
+
# raise gr.Error("You must enter some text")
|
40 |
+
# # if len(global_phonemizer.phonemize([text])) > 300:
|
41 |
+
# if len(text) > 300:
|
42 |
+
# raise gr.Error("Text must be under 300 characters")
|
43 |
+
# v = voice.lower()
|
44 |
+
# # return (24000, styletts2importable.inference(text, voices[v], alpha=0.3, beta=0.7, diffusion_steps=7, embedding_scale=1))
|
45 |
+
# return (24000, styletts2importable.inference(text, voices[v], alpha=0.3, beta=0.7, diffusion_steps=multispeakersteps, embedding_scale=1))
|
46 |
+
if not torch.cuda.is_available(): INTROTXT += "\n\n### You are on a CPU-only system, inference will be much slower.\n\nYou can use the [online demo](https://huggingface.co/spaces/styletts2/styletts2) for fast inference."
|
47 |
+
def synthesize(text, voice, lngsteps, password, progress=gr.Progress()):
|
48 |
+
if text.strip() == "":
|
49 |
+
raise gr.Error("You must enter some text")
|
50 |
+
if len(text) > 50000:
|
51 |
+
raise gr.Error("Text must be <50k characters")
|
52 |
+
print("*** saying ***")
|
53 |
+
print(text)
|
54 |
+
print("*** end ***")
|
55 |
+
texts = txtsplit(text)
|
56 |
+
v = voice.lower()
|
57 |
+
audios = []
|
58 |
+
for t in progress.tqdm(texts):
|
59 |
+
print(t)
|
60 |
+
audios.append(styletts2importable.inference(t, voices[v], alpha=0.3, beta=0.7, diffusion_steps=lngsteps, embedding_scale=1))
|
61 |
+
return (24000, np.concatenate(audios))
|
62 |
+
# def longsynthesize(text, voice, lngsteps, password, progress=gr.Progress()):
|
63 |
+
# if password == os.environ['ACCESS_CODE']:
|
64 |
+
# if text.strip() == "":
|
65 |
+
# raise gr.Error("You must enter some text")
|
66 |
+
# if lngsteps > 25:
|
67 |
+
# raise gr.Error("Max 25 steps")
|
68 |
+
# if lngsteps < 5:
|
69 |
+
# raise gr.Error("Min 5 steps")
|
70 |
+
# texts = split_and_recombine_text(text)
|
71 |
+
# v = voice.lower()
|
72 |
+
# audios = []
|
73 |
+
# for t in progress.tqdm(texts):
|
74 |
+
# audios.append(styletts2importable.inference(t, voices[v], alpha=0.3, beta=0.7, diffusion_steps=lngsteps, embedding_scale=1))
|
75 |
+
# return (24000, np.concatenate(audios))
|
76 |
+
# else:
|
77 |
+
# raise gr.Error('Wrong access code')
|
78 |
+
def clsynthesize(text, voice, vcsteps, embscale, alpha, beta, progress=gr.Progress()):
|
79 |
+
# if text.strip() == "":
|
80 |
+
# raise gr.Error("You must enter some text")
|
81 |
+
# # if global_phonemizer.phonemize([text]) > 300:
|
82 |
+
# if len(text) > 400:
|
83 |
+
# raise gr.Error("Text must be under 400 characters")
|
84 |
+
# # return (24000, styletts2importable.inference(text, styletts2importable.compute_style(voice), alpha=0.3, beta=0.7, diffusion_steps=20, embedding_scale=1))
|
85 |
+
# return (24000, styletts2importable.inference(text, styletts2importable.compute_style(voice), alpha=0.3, beta=0.7, diffusion_steps=vcsteps, embedding_scale=1))
|
86 |
+
if text.strip() == "":
|
87 |
+
raise gr.Error("You must enter some text")
|
88 |
+
if len(text) > 50000:
|
89 |
+
raise gr.Error("Text must be <50k characters")
|
90 |
+
if embscale > 1.3 and len(text) < 20:
|
91 |
+
gr.Warning("WARNING: You entered short text, you may get static!")
|
92 |
+
print("*** saying ***")
|
93 |
+
print(text)
|
94 |
+
print("*** end ***")
|
95 |
+
texts = txtsplit(text)
|
96 |
+
audios = []
|
97 |
+
# vs = styletts2importable.compute_style(voice)
|
98 |
+
vs = styletts2importable.compute_style(voice)
|
99 |
+
# print(vs)
|
100 |
+
for t in progress.tqdm(texts):
|
101 |
+
audios.append(styletts2importable.inference(t, vs, alpha=alpha, beta=beta, diffusion_steps=vcsteps, embedding_scale=embscale))
|
102 |
+
# audios.append(styletts2importable.inference(t, vs, diffusion_steps=10, alpha=0.3, beta=0.7, embedding_scale=5))
|
103 |
+
return (24000, np.concatenate(audios))
|
104 |
+
def ljsynthesize(text, steps, progress=gr.Progress()):
|
105 |
+
# if text.strip() == "":
|
106 |
+
# raise gr.Error("You must enter some text")
|
107 |
+
# # if global_phonemizer.phonemize([text]) > 300:
|
108 |
+
# if len(text) > 400:
|
109 |
+
# raise gr.Error("Text must be under 400 characters")
|
110 |
+
noise = torch.randn(1,1,256).to('cuda' if torch.cuda.is_available() else 'cpu')
|
111 |
+
# return (24000, ljspeechimportable.inference(text, noise, diffusion_steps=7, embedding_scale=1))
|
112 |
+
if text.strip() == "":
|
113 |
+
raise gr.Error("You must enter some text")
|
114 |
+
if len(text) > 150000:
|
115 |
+
raise gr.Error("Text must be <150k characters")
|
116 |
+
print("*** saying ***")
|
117 |
+
print(text)
|
118 |
+
print("*** end ***")
|
119 |
+
texts = txtsplit(text)
|
120 |
+
audios = []
|
121 |
+
for t in progress.tqdm(texts):
|
122 |
+
audios.append(ljspeechimportable.inference(t, noise, diffusion_steps=steps, embedding_scale=1))
|
123 |
+
return (24000, np.concatenate(audios))
|
124 |
+
|
125 |
+
|
126 |
+
with gr.Blocks() as vctk:
|
127 |
+
with gr.Row():
|
128 |
+
with gr.Column(scale=1):
|
129 |
+
inp = gr.Textbox(label="Text", info="What would you like StyleTTS 2 to read? It works better on full sentences.", interactive=True)
|
130 |
+
voice = gr.Dropdown(voicelist, label="Voice", info="Select a default voice.", value='m-us-2', interactive=True)
|
131 |
+
multispeakersteps = gr.Slider(minimum=3, maximum=15, value=3, step=1, label="Diffusion Steps", info="Theoretically, higher should be better quality but slower, but we cannot notice a difference. Try with lower steps first - it is faster", interactive=True)
|
132 |
+
# use_gruut = gr.Checkbox(label="Use alternate phonemizer (Gruut) - Experimental")
|
133 |
+
with gr.Column(scale=1):
|
134 |
+
btn = gr.Button("Synthesize", variant="primary")
|
135 |
+
audio = gr.Audio(interactive=False, label="Synthesized Audio", waveform_options={'waveform_progress_color': '#3C82F6'})
|
136 |
+
btn.click(synthesize, inputs=[inp, voice, multispeakersteps], outputs=[audio], concurrency_limit=4)
|
137 |
+
with gr.Blocks() as clone:
|
138 |
+
with gr.Row():
|
139 |
+
with gr.Column(scale=1):
|
140 |
+
clinp = gr.Textbox(label="Text", info="What would you like StyleTTS 2 to read? It works better on full sentences.", interactive=True)
|
141 |
+
clvoice = gr.Audio(label="Voice", interactive=True, type='filepath', max_length=300, waveform_options={'waveform_progress_color': '#3C82F6'})
|
142 |
+
vcsteps = gr.Slider(minimum=3, maximum=20, value=20, step=1, label="Diffusion Steps", info="Theoretically, higher should be better quality but slower, but we cannot notice a difference. Try with lower steps first - it is faster", interactive=True)
|
143 |
+
embscale = gr.Slider(minimum=1, maximum=10, value=1, step=0.1, label="Embedding Scale (READ WARNING BELOW)", info="Defaults to 1. WARNING: If you set this too high and generate text that's too short you will get static!", interactive=True)
|
144 |
+
alpha = gr.Slider(minimum=0, maximum=1, value=0.3, step=0.1, label="Alpha", info="Defaults to 0.3", interactive=True)
|
145 |
+
beta = gr.Slider(minimum=0, maximum=1, value=0.7, step=0.1, label="Beta", info="Defaults to 0.7", interactive=True)
|
146 |
+
with gr.Column(scale=1):
|
147 |
+
clbtn = gr.Button("Synthesize", variant="primary")
|
148 |
+
claudio = gr.Audio(interactive=False, label="Synthesized Audio", waveform_options={'waveform_progress_color': '#3C82F6'})
|
149 |
+
clbtn.click(clsynthesize, inputs=[clinp, clvoice, vcsteps, embscale, alpha, beta], outputs=[claudio], concurrency_limit=4)
|
150 |
+
# with gr.Blocks() as longText:
|
151 |
+
# with gr.Row():
|
152 |
+
# with gr.Column(scale=1):
|
153 |
+
# lnginp = gr.Textbox(label="Text", info="What would you like StyleTTS 2 to read? It works better on full sentences.", interactive=True)
|
154 |
+
# lngvoice = gr.Dropdown(voicelist, label="Voice", info="Select a default voice.", value='m-us-1', interactive=True)
|
155 |
+
# lngsteps = gr.Slider(minimum=5, maximum=25, value=10, step=1, label="Diffusion Steps", info="Higher = better quality, but slower", interactive=True)
|
156 |
+
# lngpwd = gr.Textbox(label="Access code", info="This feature is in beta. You need an access code to use it as it uses more resources and we would like to prevent abuse")
|
157 |
+
# with gr.Column(scale=1):
|
158 |
+
# lngbtn = gr.Button("Synthesize", variant="primary")
|
159 |
+
# lngaudio = gr.Audio(interactive=False, label="Synthesized Audio")
|
160 |
+
# lngbtn.click(longsynthesize, inputs=[lnginp, lngvoice, lngsteps, lngpwd], outputs=[lngaudio], concurrency_limit=4)
|
161 |
+
with gr.Blocks() as lj:
|
162 |
+
with gr.Row():
|
163 |
+
with gr.Column(scale=1):
|
164 |
+
ljinp = gr.Textbox(label="Text", info="What would you like StyleTTS 2 to read? It works better on full sentences.", interactive=True)
|
165 |
+
ljsteps = gr.Slider(minimum=3, maximum=20, value=3, step=1, label="Diffusion Steps", info="Theoretically, higher should be better quality but slower, but we cannot notice a difference. Try with lower steps first - it is faster", interactive=True)
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
ljbtn = gr.Button("Synthesize", variant="primary")
|
168 |
+
ljaudio = gr.Audio(interactive=False, label="Synthesized Audio", waveform_options={'waveform_progress_color': '#3C82F6'})
|
169 |
+
ljbtn.click(ljsynthesize, inputs=[ljinp, ljsteps], outputs=[ljaudio], concurrency_limit=4)
|
170 |
+
with gr.Blocks(title="StyleTTS 2", css="footer{display:none !important}", theme=theme) as demo:
|
171 |
+
gr.Markdown(INTROTXT)
|
172 |
+
gr.DuplicateButton("Duplicate Space")
|
173 |
+
# gr.TabbedInterface([vctk, clone, lj, longText], ['Multi-Voice', 'Voice Cloning', 'LJSpeech', 'Long Text [Beta]'])
|
174 |
+
gr.TabbedInterface([vctk, clone, lj], ['Multi-Voice', 'Voice Cloning', 'LJSpeech', 'Long Text [Beta]'])
|
175 |
+
gr.Markdown("""
|
176 |
+
Demo by [mrfakename](https://twitter.com/realmrfakename). I am not affiliated with the StyleTTS 2 authors.
|
177 |
+
|
178 |
+
Run this demo locally using Docker:
|
179 |
+
|
180 |
+
```bash
|
181 |
+
docker run -it -p 7860:7860 --platform=linux/amd64 --gpus all registry.hf.space/styletts2-styletts2:latest python app.py
|
182 |
+
```
|
183 |
+
""") # Please do not remove this line.
|
184 |
+
if __name__ == "__main__":
|
185 |
+
# demo.queue(api_open=False, max_size=15).launch(show_api=False)
|
186 |
+
demo.queue(api_open=False, max_size=15).launch(show_api=False)
|
187 |
+
|