Spaces:
Sleeping
Sleeping
DeepLearning101
commited on
Commit
•
0fb6ffc
1
Parent(s):
54bd4a5
Update app.py
Browse files
app.py
CHANGED
@@ -14,26 +14,28 @@ from loguru import logger
|
|
14 |
ssl._create_default_https_context = ssl._create_unverified_context
|
15 |
import nltk
|
16 |
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
nltk.download('averaged_perceptron_tagger')
|
21 |
|
22 |
from parrots import TextToSpeech
|
23 |
|
|
|
24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
25 |
logger.info(f"device: {device}")
|
26 |
half = True if device == "cuda" else False
|
27 |
|
|
|
28 |
m = TextToSpeech(speaker_model_path="DeepLearning101/GPT-SoVITS_TWMAN", speaker_name="TWMAN", device=device, half=half)
|
29 |
-
m.predict(text="台灣南波萬。Taiwan Number One.", text_language="auto", output_path="output_audio.wav")
|
30 |
-
assert os.path.exists("output_audio.wav"), "output_audio.wav not found"
|
31 |
-
|
32 |
|
|
|
33 |
def get_text_hash(text: str):
|
34 |
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
35 |
|
36 |
-
|
37 |
def do_tts_wav_predict(text: str, output_path: str = None):
|
38 |
if output_path is None:
|
39 |
output_path = f"output_audio_{get_text_hash(text)}.wav"
|
@@ -41,30 +43,35 @@ def do_tts_wav_predict(text: str, output_path: str = None):
|
|
41 |
m.predict(text, text_language="auto", output_path=output_path)
|
42 |
return output_path
|
43 |
|
44 |
-
|
45 |
with gr.Blocks(title="TTS WebUI") as app:
|
46 |
-
gr.Markdown(
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
with gr.Group():
|
58 |
-
gr.Markdown(
|
59 |
with gr.Row():
|
60 |
-
text = gr.Textbox(label="想語音合成的文字(100字以内)
|
61 |
inference_button = gr.Button("語音合成", variant="primary")
|
62 |
output = gr.Audio(label="合成的語音")
|
|
|
|
|
63 |
inference_button.click(
|
64 |
do_tts_wav_predict,
|
65 |
[text],
|
66 |
[output],
|
67 |
)
|
68 |
|
|
|
69 |
app.queue(max_size=10)
|
70 |
-
app.launch(inbrowser=True)
|
|
|
14 |
ssl._create_default_https_context = ssl._create_unverified_context
|
15 |
import nltk
|
16 |
|
17 |
+
# 檢查是否已下載資源,若未下載則進行下載
|
18 |
+
nltk_data_path = os.path.expanduser('~/nltk_data')
|
19 |
+
if not os.path.exists(os.path.join(nltk_data_path, 'corpora/cmudict.zip')):
|
20 |
+
nltk.download('cmudict', download_dir=nltk_data_path)
|
21 |
|
22 |
+
if not os.path.exists(os.path.join(nltk_data_path, 'taggers/averaged_perceptron_tagger.zip')):
|
23 |
+
nltk.download('averaged_perceptron_tagger', download_dir=nltk_data_path)
|
24 |
|
25 |
from parrots import TextToSpeech
|
26 |
|
27 |
+
# 設定裝置與模式
|
28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
29 |
logger.info(f"device: {device}")
|
30 |
half = True if device == "cuda" else False
|
31 |
|
32 |
+
# 初始化語音合成模型
|
33 |
m = TextToSpeech(speaker_model_path="DeepLearning101/GPT-SoVITS_TWMAN", speaker_name="TWMAN", device=device, half=half)
|
|
|
|
|
|
|
34 |
|
35 |
+
# 用於檢查和生成語音的音訊檔案
|
36 |
def get_text_hash(text: str):
|
37 |
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
38 |
|
|
|
39 |
def do_tts_wav_predict(text: str, output_path: str = None):
|
40 |
if output_path is None:
|
41 |
output_path = f"output_audio_{get_text_hash(text)}.wav"
|
|
|
43 |
m.predict(text, text_language="auto", output_path=output_path)
|
44 |
return output_path
|
45 |
|
46 |
+
# 建立 Gradio WebUI
|
47 |
with gr.Blocks(title="TTS WebUI") as app:
|
48 |
+
gr.Markdown("""
|
49 |
+
# 線上語音合成 (TWMAN)
|
50 |
+
#### 請嚴格遵守法規,發布二創作品請標註本專案作者及連結,並標註生成工具 GPT-SoVITS AI!
|
51 |
+
⚠️ 注意:在線生成可能較慢,建議在本地進行推理。
|
52 |
+
|
53 |
+
更多相關內容:
|
54 |
+
- [語音處理技術](https://www.twman.org/AI/ASR)
|
55 |
+
- [語音處理常見問題](https://blog.twman.org/2021/04/ASR.html)
|
56 |
+
- [Parrots專案](https://github.com/shibing624/parrots)
|
57 |
+
- [模型使用說明](https://github.com/RVC-Boss/GPT-SoVITS)
|
58 |
+
""")
|
59 |
+
|
60 |
+
# 設定語音合成輸入與按鈕
|
61 |
with gr.Group():
|
62 |
+
gr.Markdown("*請在下方輸入要進行語音合成的文字*")
|
63 |
with gr.Row():
|
64 |
+
text = gr.Textbox(label="想語音合成的文字 (100字以内)", value="床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。", placeholder="請輸入您想要的文字", lines=3)
|
65 |
inference_button = gr.Button("語音合成", variant="primary")
|
66 |
output = gr.Audio(label="合成的語音")
|
67 |
+
|
68 |
+
# 設定按鈕點擊事件
|
69 |
inference_button.click(
|
70 |
do_tts_wav_predict,
|
71 |
[text],
|
72 |
[output],
|
73 |
)
|
74 |
|
75 |
+
# 啟動 Gradio 應用
|
76 |
app.queue(max_size=10)
|
77 |
+
app.launch(share=True, inbrowser=True)
|