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