Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,12 @@
|
|
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,85 +15,85 @@ 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 |
-
modelInfo=[
|
26 |
-
["vits-male","best_model_65633.pth","config-0.json","https://huggingface.co/Kamtera/persian-tts-male-vits/resolve/main/"],
|
27 |
-
["vits female (best)","checkpoint_48000.pth","config-2.json","https://huggingface.co/Kamtera/persian-tts-female-vits/resolve/main/"],
|
28 |
-
["glowtts-male","best_model_77797.pth","config-1.json","https://huggingface.co/Kamtera/persian-tts-male-glow_tts/resolve/main/"],
|
29 |
-
["glowtts-female","best_model.pth","config.json","https://huggingface.co/Kamtera/persian-tts-female-glow_tts/resolve/main/"],
|
30 |
-
["vits male1 (best)","checkpoint_88000.pth","config.json","https://huggingface.co/Kamtera/persian-tts-male1-vits/resolve/main/"],
|
31 |
-
["vits female1","checkpoint_50000.pth","config.json","https://huggingface.co/Kamtera/persian-tts-female1-vits/resolve/main/"],
|
32 |
-
["female tacotron2","checkpoint_313000.pth","config-2.json","https://huggingface.co/Kamtera/persian-tts-female-tacotron2/resolve/main/"]
|
33 |
]
|
34 |
|
35 |
-
|
36 |
-
|
|
|
37 |
if not os.path.exists(directory):
|
38 |
os.makedirs(directory)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
43 |
-
|
44 |
-
|
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 |
-
if
|
58 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
wavs = synthesizer.tts(text)
|
60 |
-
|
|
|
61 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
62 |
-
synthesizer.save_wav(wavs, fp)
|
63 |
return fp.name
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
"""
|
69 |
-
|
70 |
-
examples=[
|
71 |
-
["و خداوند شما را با ارسال روح در جسم زندگانی و حیات بخشید","vits
|
72 |
-
["تاجر تو چه تجارت می کنی ، تو را چه که چه تجارت می کنم؟","vits female (best)"],
|
73 |
-
["شیش سیخ جیگر سیخی شیش هزار","vits female (best)"],
|
74 |
-
["سه شیشه شیر ، سه سیر سرشیر","vits female (best)"],
|
75 |
-
["دزدی دزدید ز بز دزدی بزی ، عجب دزدی که دزدید ز بز دزدی بزی","vits male1 (best)"],
|
76 |
-
["مثنوی یکی از قالب های شعری است ک هر بیت قافیه ی جداگانه دارد","vits female1"],
|
77 |
-
["در گلو ماند خس او سالها، چیست آن خس مهر جاه و مالها","vits male1 (best)"],
|
78 |
]
|
|
|
79 |
iface = gr.Interface(
|
80 |
fn=tts,
|
81 |
inputs=[
|
82 |
-
gr.Textbox(
|
83 |
-
|
84 |
-
value="زندگی فقط یک بار است؛ از آن به خوبی استفاده کن",
|
85 |
-
),
|
86 |
-
gr.Radio(
|
87 |
-
label="Pick a TTS Model ",
|
88 |
-
choices=MODEL_NAMES,
|
89 |
-
value="vits-female",
|
90 |
-
),
|
91 |
],
|
92 |
-
outputs=gr.Audio(label="Output",type='filepath'),
|
93 |
examples=examples,
|
94 |
-
title="🗣️ Persian
|
95 |
description=description,
|
96 |
-
article=
|
97 |
live=False
|
98 |
)
|
99 |
-
|
|
|
|
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 |
|
9 |
+
MODEL_NAMES = [
|
10 |
"vits male1 (best)",
|
11 |
"vits female (best)",
|
12 |
"vits-male",
|
|
|
15 |
"glowtts-female",
|
16 |
"female tacotron2"
|
17 |
]
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
MAX_TXT_LEN = 800
|
20 |
|
21 |
+
# Define model information
|
22 |
+
modelInfo = [
|
23 |
+
["vits-male", "best_model_65633.pth", "config-0.json", "https://huggingface.co/Kamtera/persian-tts-male-vits/resolve/main/"],
|
24 |
+
["vits female (best)", "checkpoint_48000.pth", "config-2.json", "https://huggingface.co/Kamtera/persian-tts-female-vits/resolve/main/"],
|
25 |
+
["glowtts-male", "best_model_77797.pth", "config-1.json", "https://huggingface.co/Kamtera/persian-tts-male-glow_tts/resolve/main/"],
|
26 |
+
["glowtts-female", "best_model.pth", "config.json", "https://huggingface.co/Kamtera/persian-tts-female-glow_tts/resolve/main/"],
|
27 |
+
["vits male1 (best)", "checkpoint_88000.pth", "config.json", "https://huggingface.co/Kamtera/persian-tts-male1-vits/resolve/main/"],
|
28 |
+
["vits female1", "checkpoint_50000.pth", "config.json", "https://huggingface.co/Kamtera/persian-tts-female1-vits/resolve/main/"],
|
29 |
+
["female tacotron2", "checkpoint_313000.pth", "config-2.json", "https://huggingface.co/Kamtera/persian-tts-female-tacotron2/resolve/main/"]
|
30 |
]
|
31 |
|
32 |
+
# Download model files if not exist
|
33 |
+
for model in modelInfo:
|
34 |
+
directory = model[0]
|
35 |
if not os.path.exists(directory):
|
36 |
os.makedirs(directory)
|
37 |
+
if not os.path.exists(os.path.join(directory, "best_model.pth")):
|
38 |
+
print(f"|> Downloading model: {directory}")
|
39 |
+
download_url(model[3] + model[1], directory, "best_model.pth")
|
40 |
+
if not os.path.exists(os.path.join(directory, "config.json")):
|
41 |
+
print(f"|> Downloading config: {directory}")
|
42 |
+
download_url(model[3] + model[2], directory, "config.json")
|
43 |
+
|
44 |
+
def tts(text: str, model_name: str):
|
45 |
if len(text) > MAX_TXT_LEN:
|
46 |
text = text[:MAX_TXT_LEN]
|
47 |
print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
|
48 |
print(text)
|
|
|
49 |
|
50 |
+
model_dir = model_name.replace(" ", "_")
|
51 |
+
model_path = os.path.join(model_dir, "best_model.pth")
|
52 |
+
config_path = os.path.join(model_dir, "config.json")
|
53 |
+
|
54 |
+
if not os.path.exists(model_path) or not os.path.exists(config_path):
|
55 |
+
raise FileNotFoundError(f"Model or config not found for {model_name}")
|
56 |
+
|
57 |
+
# Load config
|
58 |
+
config = load_config(config_path)
|
59 |
+
|
60 |
+
# Create the synthesizer
|
61 |
+
synthesizer = Synthesizer(model_path, config)
|
62 |
+
|
63 |
+
# Synthesize text
|
64 |
wavs = synthesizer.tts(text)
|
65 |
+
|
66 |
+
# Save the output to a temporary file
|
67 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
68 |
+
synthesizer.save_wav(wavs, fp.name)
|
69 |
return fp.name
|
70 |
|
71 |
+
description = """
|
72 |
+
A Persian Text-to-Speech (TTS) demo using various models.
|
|
|
73 |
"""
|
74 |
+
|
75 |
+
examples = [
|
76 |
+
["و خداوند شما را با ارسال روح در جسم زندگانی و حیات بخشید", "vits male1 (best)"],
|
77 |
+
["تاجر تو چه تجارت می کنی ، تو را چه که چه تجارت می کنم؟", "vits female (best)"],
|
78 |
+
["شیش سیخ جیگر سیخی شیش هزار", "vits female (best)"],
|
79 |
+
["سه شیشه شیر ، سه سیر سرشیر", "vits female (best)"],
|
80 |
+
["دزدی دزدید ز بز دزدی بزی ، عجب دزدی که دزدید ز بز دزدی بزی", "vits male1 (best)"],
|
81 |
+
["مثنوی یکی از قالب های شعری است ک هر بیت قافیه ی جداگانه دارد", "vits female1"],
|
82 |
+
["در گلو ماند خس او سالها، چیست آن خس مهر جاه و مالها", "vits male1 (best)"],
|
83 |
]
|
84 |
+
|
85 |
iface = gr.Interface(
|
86 |
fn=tts,
|
87 |
inputs=[
|
88 |
+
gr.Textbox(label="Text", value="زندگی فقط یک بار است؛ از آن به خوبی استفاده کن"),
|
89 |
+
gr.Radio(label="Pick a TTS Model", choices=MODEL_NAMES, value="vits female (best)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
],
|
91 |
+
outputs=gr.Audio(label="Output", type='filepath'),
|
92 |
examples=examples,
|
93 |
+
title="🗣️ Persian TTS 🗣️",
|
94 |
description=description,
|
95 |
+
article="",
|
96 |
live=False
|
97 |
)
|
98 |
+
|
99 |
+
iface.launch(share=False)
|