akbarazimifar commited on
Commit
328f593
·
verified ·
1 Parent(s): e0bb698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -69
app.py CHANGED
@@ -1,13 +1,12 @@
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,78 +15,69 @@ MODEL_NAMES = [
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,11 +89,11 @@ iface = gr.Interface(
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)
 
1
+
2
+ import tempfile ,os
 
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
  "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
+ from TTS.utils.download import download_url
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
+ for d in modelInfo:
36
+ directory=d[0]
37
+ if not os.path.exists(directory):
38
+ os.makedirs(directory)
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
+ # synthesize
54
+ synthesizer = Synthesizer(
55
+ model_name+"/best_model.pth", model_name+"/config.json"
56
+ )
57
+ if synthesizer is None:
58
+ raise NameError("model not found")
59
+ wavs = synthesizer.tts(text)
60
+ # return output
61
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
62
+ synthesizer.save_wav(wavs, fp)
63
+ return fp.name
64
 
 
65
 
66
+ description="""
67
 
68
  """
69
+ article= ""
70
+ examples=[
71
+ ["و خداوند شما را با ارسال روح در جسم زندگانی و حیات بخشید","vits-male"],
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
  label="Text",
 
89
  value="vits-female",
90
  ),
91
  ],
92
+ outputs=gr.Audio(label="Output",type='filepath'),
93
  examples=examples,
94
  title="🗣️ Persian tts 🗣️",
95
  description=description,
96
  article=article,
97
  live=False
98
  )
99
+ iface.launch(share=False)