trlfpyfa commited on
Commit
c235bca
·
verified ·
1 Parent(s): b9d7413

Update app.py

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