Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ import tempfile
|
|
| 6 |
import datetime
|
| 7 |
import shutil
|
| 8 |
import re
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Coqui TTS 授權同意 ---
|
| 11 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
|
@@ -25,11 +27,9 @@ except Exception as e:
|
|
| 25 |
print(f"警告:無法將安全全局變數加入 PyTorch 白名單: {e}")
|
| 26 |
print("如果遇到模型載入錯誤,請檢查 PyTorch 和 TTS 庫版本。")
|
| 27 |
|
| 28 |
-
# --- 設備選擇 ---
|
| 29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
print(f"使用設備: {device}")
|
| 31 |
|
| 32 |
-
# --- 全局變數 ---
|
| 33 |
tts = None
|
| 34 |
model_load_error = None
|
| 35 |
|
|
@@ -60,62 +60,78 @@ try:
|
|
| 60 |
except Exception as e:
|
| 61 |
model_load_error = f"載入 Coqui TTS XTTS-v2 模型時發生錯誤: {e}"
|
| 62 |
|
| 63 |
-
def generate_speech(text, language, uploaded_speaker_audio_path
|
| 64 |
if model_load_error:
|
| 65 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
status_message
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def list_saved_audio_files():
|
| 121 |
audio_files = []
|
|
@@ -183,6 +199,4 @@ with gr.Blocks(title="Coqui TTS XTTS-v2 語音生成") as demo:
|
|
| 183 |
demo.load(list_uploaded_reference_files, outputs=[saved_uploaded_ref_files_output])
|
| 184 |
refresh_uploaded_ref_button.click(list_uploaded_reference_files, outputs=[saved_uploaded_ref_files_output])
|
| 185 |
|
| 186 |
-
demo.
|
| 187 |
-
if __name__ == "__main__":
|
| 188 |
-
demo.launch()
|
|
|
|
| 6 |
import datetime
|
| 7 |
import shutil
|
| 8 |
import re
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
# --- Coqui TTS 授權同意 ---
|
| 13 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
|
|
|
| 27 |
print(f"警告:無法將安全全局變數加入 PyTorch 白名單: {e}")
|
| 28 |
print("如果遇到模型載入錯誤,請檢查 PyTorch 和 TTS 庫版本。")
|
| 29 |
|
|
|
|
| 30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
print(f"使用設備: {device}")
|
| 32 |
|
|
|
|
| 33 |
tts = None
|
| 34 |
model_load_error = None
|
| 35 |
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
model_load_error = f"載入 Coqui TTS XTTS-v2 模型時發生錯誤: {e}"
|
| 62 |
|
| 63 |
+
def generate_speech(text, language, uploaded_speaker_audio_path):
|
| 64 |
if model_load_error:
|
| 65 |
return None, f"應用程式啟動錯誤:{model_load_error}"
|
| 66 |
|
| 67 |
+
steps = [
|
| 68 |
+
"檢查模型狀態",
|
| 69 |
+
"檢查輸入",
|
| 70 |
+
"處理語音參考檔案",
|
| 71 |
+
"生成語音",
|
| 72 |
+
"儲存語音檔案",
|
| 73 |
+
"完成"
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
# tqdm 進度條會出現在 logs/終端機
|
| 77 |
+
for i, step in enumerate(tqdm(steps, desc="語音生成流程", ncols=80)):
|
| 78 |
+
if step == "檢查模型狀態":
|
| 79 |
+
if tts is None:
|
| 80 |
+
return None, "TTS 模型未成功載入,無法生成語音。"
|
| 81 |
+
time.sleep(0.1)
|
| 82 |
+
elif step == "檢查輸入":
|
| 83 |
+
if not text:
|
| 84 |
+
return None, "請輸入一些文字!"
|
| 85 |
+
if not language:
|
| 86 |
+
return None, "請選擇一個語言!"
|
| 87 |
+
time.sleep(0.1)
|
| 88 |
+
elif step == "處理語音參考檔案":
|
| 89 |
+
global speaker_wav_to_use
|
| 90 |
+
speaker_wav_to_use = None
|
| 91 |
+
global status_message
|
| 92 |
+
status_message = ""
|
| 93 |
+
if uploaded_speaker_audio_path:
|
| 94 |
+
speaker_wav_to_use = uploaded_speaker_audio_path
|
| 95 |
+
try:
|
| 96 |
+
timestamp_ref = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 97 |
+
original_ext = os.path.splitext(uploaded_speaker_audio_path)[1]
|
| 98 |
+
saved_ref_file_name = f"{timestamp_ref}_uploaded_ref{original_ext}"
|
| 99 |
+
saved_ref_file_path = os.path.join(SAVE_UPLOADED_REFERENCES_DIR, saved_ref_file_name)
|
| 100 |
+
shutil.copy(uploaded_speaker_audio_path, saved_ref_file_path)
|
| 101 |
+
status_message += f"參考語音已儲存到:{saved_ref_file_path}\n"
|
| 102 |
+
except Exception as e:
|
| 103 |
+
status_message += f"警告:儲存參考語音失敗: {e}\n"
|
| 104 |
+
else:
|
| 105 |
+
speaker_wav_to_use = DEFAULT_SPEAKER_WAV
|
| 106 |
+
if not os.path.exists(speaker_wav_to_use):
|
| 107 |
+
return None, f"錯誤:預設語音參考檔案 ({DEFAULT_SPEAKER_WAV}) 未找到。請上傳一個檔案或確保預設檔案存在。"
|
| 108 |
+
time.sleep(0.1)
|
| 109 |
+
elif step == "生成語音":
|
| 110 |
+
global output_file
|
| 111 |
+
output_file = None
|
| 112 |
+
try:
|
| 113 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 114 |
+
output_file = fp.name
|
| 115 |
+
tts.tts_to_file(text=text, language=language, speaker_wav=speaker_wav_to_use, file_path=output_file)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
if output_file and os.path.exists(output_file):
|
| 118 |
+
os.remove(output_file)
|
| 119 |
+
return None, f"生成語音失敗: {e}"
|
| 120 |
+
elif step == "儲存語音檔案":
|
| 121 |
+
try:
|
| 122 |
+
timestamp_gen = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 123 |
+
sanitized_text = sanitize_filename(text)
|
| 124 |
+
saved_file_name = f"{timestamp_gen}_{language}_{sanitized_text}.wav"
|
| 125 |
+
saved_file_path = os.path.join(SAVE_GENERATED_AUDIO_DIR, saved_file_name)
|
| 126 |
+
shutil.copy(output_file, saved_file_path)
|
| 127 |
+
status_message += f"語音生成成功!已儲存為:{saved_file_path}"
|
| 128 |
+
except Exception as e:
|
| 129 |
+
return None, f"儲存語音檔案失敗: {e}"
|
| 130 |
+
elif step == "完成":
|
| 131 |
+
pass
|
| 132 |
+
# 每步都可加 sleep 模擬耗時(真實環境下可移除)
|
| 133 |
+
time.sleep(0.1)
|
| 134 |
+
return output_file, status_message
|
| 135 |
|
| 136 |
def list_saved_audio_files():
|
| 137 |
audio_files = []
|
|
|
|
| 199 |
demo.load(list_uploaded_reference_files, outputs=[saved_uploaded_ref_files_output])
|
| 200 |
refresh_uploaded_ref_button.click(list_uploaded_reference_files, outputs=[saved_uploaded_ref_files_output])
|
| 201 |
|
| 202 |
+
demo.launch()
|
|
|
|
|
|