Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
from TTS.config import load_config
|
4 |
import gradio as gr
|
5 |
-
|
6 |
from TTS.utils.manage import ModelManager
|
7 |
from TTS.utils.synthesizer import Synthesizer
|
|
|
|
|
8 |
|
9 |
-
MODEL_NAMES=[
|
10 |
"vits male1 (best)",
|
11 |
"vits female (best)",
|
12 |
"vits-male",
|
@@ -15,70 +16,78 @@ MODEL_NAMES=[
|
|
15 |
"glowtts-female",
|
16 |
"female tacotron2"
|
17 |
]
|
18 |
-
MAX_TXT_LEN = 800
|
19 |
-
model_path = os.getcwd() + "/best_model.pth"
|
20 |
-
config_path = os.getcwd() + "/config.json"
|
21 |
-
|
22 |
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
["vits female (best)","checkpoint_48000.pth","config-2.json",
|
28 |
-
|
29 |
-
["glowtts-
|
30 |
-
|
31 |
-
["
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
]
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
print("|> Downloading: ",directory)
|
40 |
-
download_url(
|
41 |
-
d[3]+d[1],directory,"best_model.pth"
|
42 |
-
)
|
43 |
-
download_url(
|
44 |
-
d[3]+d[2],directory,"config.json"
|
45 |
-
)
|
46 |
-
def tts(text: str,model_name: str):
|
47 |
-
if len(text) > MAX_TXT_LEN:
|
48 |
-
text = text[:MAX_TXT_LEN]
|
49 |
-
print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
|
50 |
-
print(text)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
synthesizer.save_wav(wavs, fp)
|
63 |
-
return fp.name
|
64 |
|
65 |
-
|
|
|
|
|
|
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
"""
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
["
|
74 |
-
["
|
75 |
-
["
|
76 |
-
["
|
77 |
-
["
|
78 |
-
["
|
|
|
79 |
]
|
|
|
|
|
|
|
80 |
iface = gr.Interface(
|
81 |
-
fn=tts,
|
82 |
inputs=[
|
83 |
gr.Textbox(
|
84 |
label="Text",
|
@@ -90,11 +99,11 @@ iface = gr.Interface(
|
|
90 |
value="vits-female",
|
91 |
),
|
92 |
],
|
93 |
-
outputs=gr.Audio(label="Output",type='filepath'),
|
94 |
examples=examples,
|
95 |
title="🗣️ Persian tts 🗣️",
|
96 |
description=description,
|
97 |
article=article,
|
98 |
live=False
|
99 |
)
|
100 |
-
iface.launch(share=False)
|
|
|
1 |
+
import tempfile
|
2 |
+
import os
|
|
|
3 |
import gradio as gr
|
4 |
+
from TTS.config import load_config
|
5 |
from TTS.utils.manage import ModelManager
|
6 |
from TTS.utils.synthesizer import Synthesizer
|
7 |
+
from TTS.utils.download import download_url
|
8 |
+
from concurrent.futures import ThreadPoolExecutor
|
9 |
|
10 |
+
MODEL_NAMES = [
|
11 |
"vits male1 (best)",
|
12 |
"vits female (best)",
|
13 |
"vits-male",
|
|
|
16 |
"glowtts-female",
|
17 |
"female tacotron2"
|
18 |
]
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
MAX_TXT_LEN = 800
|
21 |
+
MODELS_DIRECTORY = "models" # مسیر مدلها
|
22 |
|
23 |
+
modelInfo = [
|
24 |
+
["vits-male", "best_model_65633.pth", "config-0.json",
|
25 |
+
"https://huggingface.co/Kamtera/persian-tts-male-vits/resolve/main/"],
|
26 |
+
["vits female (best)", "checkpoint_48000.pth", "config-2.json",
|
27 |
+
"https://huggingface.co/Kamtera/persian-tts-female-vits/resolve/main/"],
|
28 |
+
["glowtts-male", "best_model_77797.pth", "config-1.json",
|
29 |
+
"https://huggingface.co/Kamtera/persian-tts-male-glow_tts/resolve/main/"],
|
30 |
+
["glowtts-female", "best_model.pth", "config.json",
|
31 |
+
"https://huggingface.co/Kamtera/persian-tts-female-glow_tts/resolve/main/"],
|
32 |
+
["vits male1 (best)", "checkpoint_88000.pth", "config.json",
|
33 |
+
"https://huggingface.co/Kamtera/persian-tts-male1-vits/resolve/main/"],
|
34 |
+
["vits female1", "checkpoint_50000.pth", "config.json",
|
35 |
+
"https://huggingface.co/Kamtera/persian-tts-female1-vits/resolve/main/"],
|
36 |
+
["female tacotron2", "checkpoint_313000.pth", "config-2.json",
|
37 |
+
"https://huggingface.co/Kamtera/persian-tts-female-tacotron2/resolve/main/"]
|
38 |
]
|
39 |
|
40 |
+
class PersianTTS:
|
41 |
+
def __init__(self):
|
42 |
+
self.model_manager = ModelManager(MODELS_DIRECTORY)
|
43 |
+
self.download_models()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
def download_models(self):
|
46 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
47 |
+
for model in modelInfo:
|
48 |
+
model_name, model_filename, config_filename, model_url = model
|
49 |
+
model_directory = os.path.join(MODELS_DIRECTORY, model_name)
|
50 |
+
if not os.path.exists(model_directory):
|
51 |
+
os.makedirs(model_directory)
|
52 |
+
print("|> Downloading: ", model_directory)
|
53 |
+
executor.submit(download_url, model_url + model_filename, model_directory, "best_model.pth")
|
54 |
+
executor.submit(download_url, model_url + config_filename, model_directory, "config.json")
|
|
|
|
|
55 |
|
56 |
+
def tts(self, text: str, model_name: str):
|
57 |
+
if len(text) > MAX_TXT_LEN:
|
58 |
+
text = text[:MAX_TXT_LEN]
|
59 |
+
print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
|
60 |
|
61 |
+
# synthesize
|
62 |
+
model_path, config_path = self.model_manager.get_model_paths(model_name)
|
63 |
+
synthesizer = Synthesizer(model_path, config_path)
|
64 |
+
if synthesizer is None:
|
65 |
+
raise NameError("model not found")
|
66 |
+
wavs = synthesizer.tts(text)
|
67 |
+
# return output
|
68 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
69 |
+
synthesizer.save_wav(wavs, fp)
|
70 |
+
return fp.name
|
71 |
+
|
72 |
+
description = """
|
73 |
|
|
|
74 |
|
75 |
+
"""
|
76 |
+
article = ""
|
77 |
+
examples = [
|
78 |
+
["و خداوند شما را با ارسال روح در جسم زندگانی و حیات بخشید", "vits-male"],
|
79 |
+
["تاجر تو چه تجارت می کنی ، تو را چه که چه تجارت می کنم؟", "vits female (best)"],
|
80 |
+
["شیش سیخ جیگر سیخی شیش هزار", "vits female (best)"],
|
81 |
+
["سه شیشه شیر ، سه سیر سرشیر", "vits female (best)"],
|
82 |
+
["دزدی دزدید ز بز دزدی بزی ، عجب دزدی که دزدید ز بز دزدی بزی", "vits male1 (best)"],
|
83 |
+
["مثنوی یکی از قالب های شعری است ک هر بیت قافیه ی جداگانه دارد", "vits female1"],
|
84 |
+
["در گلو ماند خس او سالها، چیست آن خس مهر جاه و مالها", "vits male1 (best)"],
|
85 |
]
|
86 |
+
|
87 |
+
persian_tts = PersianTTS()
|
88 |
+
|
89 |
iface = gr.Interface(
|
90 |
+
fn=persian_tts.tts,
|
91 |
inputs=[
|
92 |
gr.Textbox(
|
93 |
label="Text",
|
|
|
99 |
value="vits-female",
|
100 |
),
|
101 |
],
|
102 |
+
outputs=gr.Audio(label="Output", type='filepath'),
|
103 |
examples=examples,
|
104 |
title="🗣️ Persian tts 🗣️",
|
105 |
description=description,
|
106 |
article=article,
|
107 |
live=False
|
108 |
)
|
109 |
+
iface.launch(share=False)
|