Update app.py
Browse files
app.py
CHANGED
|
@@ -88,15 +88,11 @@ SUPPORTED_LANGUAGES = [
|
|
| 88 |
"te", # Telugu
|
| 89 |
]
|
| 90 |
|
| 91 |
-
# 定義預設說話者音訊檔案的路徑
|
| 92 |
-
# 請確保你已將 'speaker.wav' 檔案上傳到 Hugging Face Space 的根目錄
|
| 93 |
-
DEFAULT_SPEAKER_WAV = "speaker.wav"
|
| 94 |
|
| 95 |
-
def generate_speech(text, language):
|
| 96 |
"""
|
| 97 |
-
根據輸入文字
|
| 98 |
-
|
| 99 |
-
此版本使用預設的 speaker.wav 檔案。
|
| 100 |
"""
|
| 101 |
if model_load_error:
|
| 102 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
|
@@ -108,19 +104,17 @@ def generate_speech(text, language):
|
|
| 108 |
return None, "請輸入一些文字!"
|
| 109 |
if not language:
|
| 110 |
return None, "請選擇一個語言!"
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
if not os.path.exists(DEFAULT_SPEAKER_WAV):
|
| 114 |
-
return None, f"錯誤:預設語音參考檔案 ({DEFAULT_SPEAKER_WAV}) 未找到。請確保該檔案已上傳到您的 Hugging Face Space 的根目錄。"
|
| 115 |
|
| 116 |
output_file = None
|
| 117 |
try:
|
| 118 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 119 |
output_file = fp.name
|
| 120 |
|
| 121 |
-
print(f"正在為語言 '{language}' 生成語音,使用
|
| 122 |
-
# 將
|
| 123 |
-
tts.tts_to_file(text=text, language=language, speaker_wav=
|
| 124 |
print(f"語音已生成到:{output_file}")
|
| 125 |
|
| 126 |
return output_file, "語音生成成功!"
|
|
@@ -136,6 +130,19 @@ iface = gr.Interface(
|
|
| 136 |
inputs=[
|
| 137 |
gr.Textbox(lines=5, label="輸入文字", placeholder="請在這裡輸入你想要轉換成語音的文字..."),
|
| 138 |
gr.Dropdown(choices=SUPPORTED_LANGUAGES, label="選擇語言", value="en"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
],
|
| 140 |
outputs=[
|
| 141 |
gr.Audio(label="生成的語音", type="filepath"),
|
|
@@ -143,11 +150,9 @@ iface = gr.Interface(
|
|
| 143 |
],
|
| 144 |
title="Coqui TTS XTTS-v2 語音生成 (CPU)",
|
| 145 |
description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU 上運行會非常慢。首次運行時會下載模型。",
|
| 146 |
-
# 將 allow_flagging 替換為 flagging_mode 以消除 Gradio 警告
|
| 147 |
flagging_mode="never",
|
| 148 |
)
|
| 149 |
|
| 150 |
# 啟動 Gradio 應用
|
| 151 |
if __name__ == "__main__":
|
| 152 |
iface.launch()
|
| 153 |
-
|
|
|
|
| 88 |
"te", # Telugu
|
| 89 |
]
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
def generate_speech(text, language, speaker_audio_path):
|
| 93 |
"""
|
| 94 |
+
根據輸入文字、語言和語音參考檔案生成語音。
|
| 95 |
+
XTTS-v2 支援語音克隆,需要提供 speaker_wav。
|
|
|
|
| 96 |
"""
|
| 97 |
if model_load_error:
|
| 98 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
|
|
|
| 104 |
return None, "請輸入一些文字!"
|
| 105 |
if not language:
|
| 106 |
return None, "請選擇一個語言!"
|
| 107 |
+
if not speaker_audio_path: # 檢查是否提供了語音參考檔案
|
| 108 |
+
return None, "請上傳一個語音參考檔案 (WAV 格式) 來克隆語音!"
|
|
|
|
|
|
|
| 109 |
|
| 110 |
output_file = None
|
| 111 |
try:
|
| 112 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 113 |
output_file = fp.name
|
| 114 |
|
| 115 |
+
print(f"正在為語言 '{language}' 生成語音,使用語音參考檔案: {speaker_audio_path}...")
|
| 116 |
+
# 將上傳的語音檔案路徑傳遞給 speaker_wav 參數
|
| 117 |
+
tts.tts_to_file(text=text, language=language, speaker_wav=speaker_audio_path, file_path=output_file)
|
| 118 |
print(f"語音已生成到:{output_file}")
|
| 119 |
|
| 120 |
return output_file, "語音生成成功!"
|
|
|
|
| 130 |
inputs=[
|
| 131 |
gr.Textbox(lines=5, label="輸入文字", placeholder="請在這裡輸入你想要轉換成語音的文字..."),
|
| 132 |
gr.Dropdown(choices=SUPPORTED_LANGUAGES, label="選擇語言", value="en"),
|
| 133 |
+
# 新增一個 Audio 輸入組件,用於上傳語音參考檔案
|
| 134 |
+
gr.Audio(
|
| 135 |
+
type="filepath", # 接收檔案路徑
|
| 136 |
+
label="上傳語音參考檔案 (WAV)",
|
| 137 |
+
sources=["microphone", "upload"], # 允許從麥克風錄音或上傳檔案
|
| 138 |
+
waveform_options=gr.Audio.WaveformOptions(
|
| 139 |
+
skip_length=True,
|
| 140 |
+
show_controls=False,
|
| 141 |
+
show_labels=False,
|
| 142 |
+
create_waveform=True,
|
| 143 |
+
),
|
| 144 |
+
info="請上傳一個清晰的語音檔案 (建議 3-10 秒,WAV 格式) 來克隆語音。",
|
| 145 |
+
),
|
| 146 |
],
|
| 147 |
outputs=[
|
| 148 |
gr.Audio(label="生成的語音", type="filepath"),
|
|
|
|
| 150 |
],
|
| 151 |
title="Coqui TTS XTTS-v2 語音生成 (CPU)",
|
| 152 |
description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU 上運行會非常慢。首次運行時會下載模型。",
|
|
|
|
| 153 |
flagging_mode="never",
|
| 154 |
)
|
| 155 |
|
| 156 |
# 啟動 Gradio 應用
|
| 157 |
if __name__ == "__main__":
|
| 158 |
iface.launch()
|
|
|