Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import os
|
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
# --- 打印 Gradio 版本以供診斷 ---
|
| 8 |
-
# 再次強調:請務必檢查日誌中這行打印出的 Gradio 版本,這對診斷問題根源至關重要。
|
| 9 |
print(f"Gradio version at runtime: {gr.__version__}")
|
| 10 |
# ---
|
| 11 |
|
|
@@ -92,11 +91,15 @@ SUPPORTED_LANGUAGES = [
|
|
| 92 |
"te", # Telugu
|
| 93 |
]
|
| 94 |
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
"""
|
| 98 |
-
|
| 99 |
-
XTTS-v2
|
| 100 |
"""
|
| 101 |
if model_load_error:
|
| 102 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
|
@@ -108,16 +111,19 @@ def generate_speech(text, language, speaker_audio_path):
|
|
| 108 |
return None, "請輸入一些文字!"
|
| 109 |
if not language:
|
| 110 |
return None, "請選擇一個語言!"
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
output_file = None
|
| 115 |
try:
|
| 116 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 117 |
output_file = fp.name
|
| 118 |
|
| 119 |
-
print(f"正在為語言 '{language}'
|
| 120 |
-
|
|
|
|
| 121 |
print(f"語音已生成到:{output_file}")
|
| 122 |
|
| 123 |
return output_file, "語音生成成功!"
|
|
@@ -133,27 +139,20 @@ iface = gr.Interface(
|
|
| 133 |
inputs=[
|
| 134 |
gr.Textbox(lines=5, label="輸入文字", placeholder="請在這裡輸入你想要轉換成語音的文字..."),
|
| 135 |
gr.Dropdown(choices=SUPPORTED_LANGUAGES, label="選擇語言", value="en"),
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# show_controls=False,
|
| 144 |
-
# show_labels=False,
|
| 145 |
-
# create_waveform=True,
|
| 146 |
-
# ),
|
| 147 |
-
# 移除 info 參數
|
| 148 |
-
# info="請上傳一個清晰的語音檔案 (建議 3-10 秒,WAV 格式) 來克隆語音。",
|
| 149 |
-
),
|
| 150 |
],
|
| 151 |
outputs=[
|
| 152 |
gr.Audio(label="生成的語音", type="filepath"),
|
| 153 |
gr.Textbox(label="狀態")
|
| 154 |
],
|
| 155 |
-
title="Coqui TTS XTTS-v2 語音生成 (CPU)",
|
| 156 |
-
description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU
|
| 157 |
flagging_mode="never",
|
| 158 |
)
|
| 159 |
|
|
|
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
# --- 打印 Gradio 版本以供診斷 ---
|
|
|
|
| 8 |
print(f"Gradio version at runtime: {gr.__version__}")
|
| 9 |
# ---
|
| 10 |
|
|
|
|
| 91 |
"te", # Telugu
|
| 92 |
]
|
| 93 |
|
| 94 |
+
# 定義預設說話者音訊檔案的路徑
|
| 95 |
+
# 請確保你已將 'speaker.wav' 檔案上傳到 Hugging Face Space 的根目錄
|
| 96 |
+
DEFAULT_SPEAKER_WAV = "speaker.wav"
|
| 97 |
|
| 98 |
+
|
| 99 |
+
def generate_speech(text, language): # 移除 speaker_audio_path 參數
|
| 100 |
"""
|
| 101 |
+
根據輸入文字和語言生成語音。
|
| 102 |
+
此版本使用 XTTS-v2 模型,並使用預設的 speaker.wav 檔案作為語音參考。
|
| 103 |
"""
|
| 104 |
if model_load_error:
|
| 105 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
|
|
|
| 111 |
return None, "請輸入一些文字!"
|
| 112 |
if not language:
|
| 113 |
return None, "請選擇一個語言!"
|
| 114 |
+
|
| 115 |
+
# 檢查預設說話者音訊檔案是否存在
|
| 116 |
+
if not os.path.exists(DEFAULT_SPEAKER_WAV):
|
| 117 |
+
return None, f"錯誤:預設語音參考檔案 ({DEFAULT_SPEAKER_WAV}) 未找到。請確保該檔案已上傳到您的 Hugging Face Space 的根目錄。"
|
| 118 |
|
| 119 |
output_file = None
|
| 120 |
try:
|
| 121 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 122 |
output_file = fp.name
|
| 123 |
|
| 124 |
+
print(f"正在為語言 '{language}' 生成語音,使用預設語音參考檔案: {DEFAULT_SPEAKER_WAV}...")
|
| 125 |
+
# 將 DEFAULT_SPEAKER_WAV 作為 speaker_wav 參數傳遞
|
| 126 |
+
tts.tts_to_file(text=text, language=language, speaker_wav=DEFAULT_SPEAKER_WAV, file_path=output_file)
|
| 127 |
print(f"語音已生成到:{output_file}")
|
| 128 |
|
| 129 |
return output_file, "語音生成成功!"
|
|
|
|
| 139 |
inputs=[
|
| 140 |
gr.Textbox(lines=5, label="輸入文字", placeholder="請在這裡輸入你想要轉換成語音的文字..."),
|
| 141 |
gr.Dropdown(choices=SUPPORTED_LANGUAGES, label="選擇語言", value="en"),
|
| 142 |
+
# 移除語音參考檔案上傳組件
|
| 143 |
+
# gr.Audio(
|
| 144 |
+
# type="filepath",
|
| 145 |
+
# label="上傳語音參考檔案 (WAV)",
|
| 146 |
+
# sources=["microphone", "upload"],
|
| 147 |
+
# info="請上傳一個清晰的語音檔案 (建議 3-10 秒,WAV 格式) 來克隆語音。",
|
| 148 |
+
# ),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
],
|
| 150 |
outputs=[
|
| 151 |
gr.Audio(label="生成的語音", type="filepath"),
|
| 152 |
gr.Textbox(label="狀態")
|
| 153 |
],
|
| 154 |
+
title="Coqui TTS XTTS-v2 語音生成 (CPU - 使用預設語音)",
|
| 155 |
+
description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU 上運行會非常慢。語音將使用預設的 speaker.wav 檔案進行克隆。",
|
| 156 |
flagging_mode="never",
|
| 157 |
)
|
| 158 |
|