akbarazimifar commited on
Commit
2070617
·
verified ·
1 Parent(s): 08ebb72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
13
+ "vits female1",
14
+ "glowtts-male",
15
+ "glowtts-female",
16
+ "female tacotron2"
17
+ ]
18
+
19
+ MAX_TXT_LEN = 800
20
+ MODELS_DIRECTORY = os.path.join(os.path.dirname(__file__), "models")
21
+
22
+ modelInfo = [
23
+ ["vits-male", "best_model_65633.pth", "config-0.json",
24
+ "https://huggingface.co/Kamtera/persian-tts-male-vits/resolve/main/"],
25
+ ["vits female (best)", "checkpoint_48000.pth", "config-2.json",
26
+ "https://huggingface.co/Kamtera/persian-tts-female-vits/resolve/main/"],
27
+ ["glowtts-male", "best_model_77797.pth", "config-1.json",
28
+ "https://huggingface.co/Kamtera/persian-tts-male-glow_tts/resolve/main/"],
29
+ ["glowtts-female", "best_model.pth", "config.json",
30
+ "https://huggingface.co/Kamtera/persian-tts-female-glow_tts/resolve/main/"],
31
+ ["vits male1 (best)", "checkpoint_88000.pth", "config.json",
32
+ "https://huggingface.co/Kamtera/persian-tts-male1-vits/resolve/main/"],
33
+ ["vits female1", "checkpoint_50000.pth", "config.json",
34
+ "https://huggingface.co/Kamtera/persian-tts-female1-vits/resolve/main/"],
35
+ ["female tacotron2", "checkpoint_313000.pth", "config-2.json",
36
+ "https://huggingface.co/Kamtera/persian-tts-female-tacotron2/resolve/main/"]
37
+ ]
38
+
39
+ for model in modelInfo:
40
+ model_name, model_filename, config_filename, model_url = model
41
+ model_directory = os.path.join(MODELS_DIRECTORY, model_name)
42
+ if not os.path.exists(model_directory):
43
+ os.makedirs(model_directory)
44
+ print("|> Downloading: ", model_directory)
45
+ download_url(model_url + model_filename, model_directory, "best_model.pth")
46
+ download_url(model_url + config_filename, model_directory, "config.json")
47
+
48
+
49
+ def tts(text: str, model_name: str):
50
+ if len(text) > MAX_TXT_LEN:
51
+ text = text[:MAX_TXT_LEN]
52
+ print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
53
+
54
+ # synthesize
55
+ model_directory = os.path.join(MODELS_DIRECTORY, model_name)
56
+ model_path = os.path.join(model_directory, "best_model.pth")
57
+ config_path = os.path.join(model_directory, "config.json")
58
+ synthesizer = Synthesizer(model_path, config_path)
59
+ if synthesizer is None:
60
+ raise NameError("model not found")
61
+ wavs = synthesizer.tts(text)
62
+ # return output
63
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
64
+ synthesizer.save_wav(wavs, fp)
65
+ return fp.name
66
+
67
+
68
+ description = """
69
+ This is a demo of persian text to speech model.
70
+
71
+ **Github : https://github.com/karim23657/Persian-tts-coqui **
72
+
73
+ Models can be found here: <br>
74
+
75
+ |Model|Dataset|
76
+ |----|------|
77
+ |[vits female (best)](https://huggingface.co/Kamtera/persian-tts-female-vits)|[persian-tts-dataset-famale](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset-famale)|
78
+ |[vits male1 (best)](https://huggingface.co/Kamtera/persian-tts-male1-vits)|[persian-tts-dataset-male](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset-male)|
79
+ |[vits female1](https://huggingface.co/Kamtera/persian-tts-female1-vits)|[ParsiGoo](https://github.com/karim23657/ParsiGoo)|
80
+ |[vits male](https://huggingface.co/Kamtera/persian-tts-male-vits)|[persian-tts-dataset](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset)|
81
+ |[glowtts female](https://huggingface.co/Kamtera/persian-tts-female-glow_tts)|[persian-tts-dataset-famale](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset-famale)|
82
+ |[glowtts male](https://huggingface.co/Kamtera/persian-tts-male-glow_tts)|[persian-tts-dataset](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset)|
83
+ |[tacotron2 female](https://huggingface.co/Kamtera/persian-tts-female-tacotron2)|[persian-tts-dataset-famale](https://www.kaggle.com/datasets/magnoliasis/persian-tts-dataset-famale)|
84
+
85
+
86
+ """
87
+ article = ""
88
+ examples = [
89
+ ["و خداوند شما را با ارسال روح در جسم زندگانی و حیات بخشید", "vits-male"],
90
+ ["تاجر تو چه تجارت می کنی ، تو را چه که چه تجارت می کنم؟", "vits female (best)"],
91
+ ["شیش سیخ جیگر سیخی شیش هزار", "vits female (best)"],
92
+ ["سه شیشه شیر ، سه سیر سرشیر", "vits female (best)"],
93
+ ["دزدی دزدید ز بز دزدی بزی ، عجب دزدی که دزدید ز بز دزدی بزی", "vits male1 (best)"],
94
+ ["مثنوی یکی از قالب های شعری است ک هر بیت قافیه ی جداگانه دارد", "vits female1"],
95
+ ["در گلو ماند خس او سالها، چیست آن خس مهر جاه و مالها", "vits male1 (best)"],
96
+ ]
97
+
98
+ iface = gr.Interface(
99
+ fn=tts,
100
+ inputs=[
101
+ gr.Textbox(
102
+ label="Text",
103
+ value="زندگی فقط یک بار است؛ از آن به خوبی استفاده کن",
104
+ ),
105
+ gr.Radio(
106
+ label="Pick a TTS Model ",
107
+ choices=MODEL_NAMES,
108
+ value="vits-female",
109
+ ),
110
+ ],
111
+ outputs=gr.Audio(label="Output", type='filepath'),
112
+ examples=examples,
113
+ title="🗣️ Persian tts 🗣️",
114
+ description=description,
115
+ article=article,
116
+ live=False
117
+ )
118
+ iface.launch(share=False)