Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,6 @@ import os
|
|
7 |
import torch
|
8 |
|
9 |
# 載入 FastSpeech2 模型
|
10 |
-
# SpeechBrain 提供了預訓練的 FastSpeech2 模型,可以直接從 Hugging Face Hub 載入。
|
11 |
-
# savedir 參數指定了模型下載後儲存的本地路徑。
|
12 |
-
# run_opts={"device":"cpu"} 確保模型在 CPU 上運行。
|
13 |
fastspeech2 = FastSpeech2.from_hparams(
|
14 |
source="speechbrain/tts-fastspeech2-ljspeech",
|
15 |
savedir="pretrained_models/tts-fastspeech2-ljspeech",
|
@@ -17,8 +14,6 @@ fastspeech2 = FastSpeech2.from_hparams(
|
|
17 |
)
|
18 |
|
19 |
# 載入聲碼器 (Vocoder)
|
20 |
-
# FastSpeech2 輸出的是梅爾頻譜圖 (mel-spectrogram),需要聲碼器將其轉換為可聽的音訊。
|
21 |
-
# 我們使用 HiFi-GAN 作為聲碼器。
|
22 |
hifi_gan = HIFIGAN.from_hparams(
|
23 |
source="speechbrain/tts-hifigan-ljspeech",
|
24 |
savedir="pretrained_models/tts-hifigan-ljspeech",
|
@@ -29,36 +24,47 @@ def synthesize_speech(text):
|
|
29 |
"""
|
30 |
將輸入文字轉換為語音。
|
31 |
"""
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
wav = hifi_gan.decode_batch(mel_outputs).squeeze(1)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
torchaudio.save(output_file, torch.tensor(audio_numpy).unsqueeze(0), 16000)
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# 創建 Gradio 介面
|
58 |
iface = gr.Interface(
|
59 |
fn=synthesize_speech,
|
60 |
inputs=gr.Textbox(lines=2, placeholder="請輸入您想要合成的文字..."),
|
61 |
-
outputs=
|
|
|
|
|
|
|
62 |
title="FastSpeech2 文字轉語音 (CPU)",
|
63 |
description="這是一個使用 SpeechBrain 的 FastSpeech2 模型在 Hugging Face Spaces 的 CPU 上進行文字轉語音的演示。由於在 CPU 上運行,合成速度可能會較慢。",
|
64 |
examples=[
|
|
|
7 |
import torch
|
8 |
|
9 |
# 載入 FastSpeech2 模型
|
|
|
|
|
|
|
10 |
fastspeech2 = FastSpeech2.from_hparams(
|
11 |
source="speechbrain/tts-fastspeech2-ljspeech",
|
12 |
savedir="pretrained_models/tts-fastspeech2-ljspeech",
|
|
|
14 |
)
|
15 |
|
16 |
# 載入聲碼器 (Vocoder)
|
|
|
|
|
17 |
hifi_gan = HIFIGAN.from_hparams(
|
18 |
source="speechbrain/tts-hifigan-ljspeech",
|
19 |
savedir="pretrained_models/tts-hifigan-ljspeech",
|
|
|
24 |
"""
|
25 |
將輸入文字轉換為語音。
|
26 |
"""
|
27 |
+
# 檢查輸入文字是否為空或只包含空白字元
|
28 |
+
if not text or text.strip() == "":
|
29 |
+
# 返回一個錯誤訊息或空音訊,而不是直接 None
|
30 |
+
# Gradio 介面會顯示這個錯誤訊息
|
31 |
+
return None, "請輸入有效的文字進行語音合成。"
|
32 |
|
33 |
+
try:
|
34 |
+
# 將文字編碼為梅爾頻譜圖
|
35 |
+
mel_outputs, durations, pitch, energy = fastspeech2.encode_text(
|
36 |
+
[text], pace=1.0
|
37 |
+
)
|
38 |
|
39 |
+
# 使用聲碼器將梅爾頻譜圖轉換為音訊波形
|
40 |
+
wav = hifi_gan.decode_batch(mel_outputs).squeeze(1)
|
|
|
41 |
|
42 |
+
# 將音訊張量轉換為 NumPy 陣列
|
43 |
+
audio_numpy = wav.cpu().numpy().flatten()
|
44 |
|
45 |
+
# 定義輸出檔案路徑
|
46 |
+
output_file = "output.wav"
|
47 |
|
48 |
+
# 將音訊保存為 WAV 檔案
|
49 |
+
torchaudio.save(output_file, torch.tensor(audio_numpy).unsqueeze(0), 16000)
|
|
|
50 |
|
51 |
+
return output_file, "語音合成成功!"
|
52 |
+
|
53 |
+
except IndexError as e:
|
54 |
+
# 捕獲特定的 IndexError,並提供更詳細的錯誤訊息
|
55 |
+
return None, f"語音合成失敗:處理文字時發生錯誤 (IndexError)。請嘗試不同的文字。錯誤詳情: {e}"
|
56 |
+
except Exception as e:
|
57 |
+
# 捕獲其他所有可能的錯誤
|
58 |
+
return None, f"語音合成失敗:發生未知錯誤。錯誤詳情: {e}"
|
59 |
|
60 |
# 創建 Gradio 介面
|
61 |
iface = gr.Interface(
|
62 |
fn=synthesize_speech,
|
63 |
inputs=gr.Textbox(lines=2, placeholder="請輸入您想要合成的文字..."),
|
64 |
+
outputs=[
|
65 |
+
gr.Audio(type="filepath", label="合成語音"),
|
66 |
+
gr.Textbox(label="狀態訊息") # 新增一個文字框來顯示狀態或錯誤訊息
|
67 |
+
],
|
68 |
title="FastSpeech2 文字轉語音 (CPU)",
|
69 |
description="這是一個使用 SpeechBrain 的 FastSpeech2 模型在 Hugging Face Spaces 的 CPU 上進行文字轉語音的演示。由於在 CPU 上運行,合成速度可能會較慢。",
|
70 |
examples=[
|