Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,54 +84,32 @@ def separate_audio(input_audio):
|
|
| 84 |
temp_wav = None
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
logger.info(f"[{process_id}] 🔁 轉換標準音檔格式...")
|
| 97 |
-
temp_wav = convert_to_wav(input_audio)
|
| 98 |
-
validate_audio(temp_wav)
|
| 99 |
-
|
| 100 |
-
# 2️⃣ 建立輸出目錄
|
| 101 |
-
out_dir = tempfile.mkdtemp()
|
| 102 |
-
outfilename = os.path.join(out_dir, "output.wav")
|
| 103 |
-
logger.info(f"[{process_id}] 📁 建立臨時輸出目錄: {out_dir}")
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
logger.info(f"[{process_id}] 🧠 開始執行語音分離...")
|
| 107 |
sep_files = dpt_sep_process(temp_wav, model=model, outfilename=outfilename)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
for f in sep_files:
|
| 111 |
if not os.path.exists(f):
|
| 112 |
-
raise gr.Error(f"❌
|
| 113 |
-
validate_audio(f)
|
| 114 |
|
| 115 |
-
logger.info(f"[{process_id}] ✅ 處理成功完成")
|
| 116 |
return sep_files
|
| 117 |
|
| 118 |
-
except RuntimeError as e:
|
| 119 |
-
if "CUDA out of memory" in str(e):
|
| 120 |
-
logger.error(f"[{process_id}] 💥 CUDA 記憶體不足")
|
| 121 |
-
raise gr.Error("⚠️ 記憶體不足,請上傳較短的音檔") from e
|
| 122 |
-
else:
|
| 123 |
-
raise
|
| 124 |
except Exception as e:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
# 清理臨時檔案
|
| 129 |
-
if temp_wav and os.path.exists(temp_wav):
|
| 130 |
-
try:
|
| 131 |
-
os.unlink(temp_wav)
|
| 132 |
-
logger.info(f"[{process_id}] 🧹 臨時檔案已清理")
|
| 133 |
-
except Exception as clean_err:
|
| 134 |
-
logger.warning(f"[{process_id}] ⚠️ 清理失敗: {str(clean_err)}")
|
| 135 |
|
| 136 |
# 🎯 description 內容(轉為 HTML)
|
| 137 |
description_html = """
|
|
@@ -175,9 +153,10 @@ AUDIO_INPUT = gr.Audio(
|
|
| 175 |
max_length=180 # 最大 3 分鐘
|
| 176 |
)
|
| 177 |
|
|
|
|
| 178 |
AUDIO_OUTPUTS = [
|
| 179 |
-
gr.Audio(label="🗣️ 語音軌道 1", type="filepath"),
|
| 180 |
-
gr.Audio(label="🗣️ 語音軌道 2", type="filepath")
|
| 181 |
]
|
| 182 |
|
| 183 |
# 🚀 啟動應用程式
|
|
|
|
| 84 |
temp_wav = None
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
# 使用固定臨時目錄而非隨機生成
|
| 88 |
+
tmp_dir = "/tmp/gradio_outputs"
|
| 89 |
+
os.makedirs(tmp_dir, exist_ok=True)
|
| 90 |
|
| 91 |
+
# 建立唯一辨識子目錄
|
| 92 |
+
process_id = datetime.now().strftime("%Y%m%d%H%M%S%f")
|
| 93 |
+
output_dir = os.path.join(tmp_dir, process_id)
|
| 94 |
+
os.makedirs(output_dir)
|
| 95 |
|
| 96 |
+
# 輸出路徑改為固定結構
|
| 97 |
+
outfilename = os.path.join(output_dir, "output.wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# 執行分離
|
|
|
|
| 100 |
sep_files = dpt_sep_process(temp_wav, model=model, outfilename=outfilename)
|
| 101 |
|
| 102 |
+
# 確保輸出檔案存在
|
| 103 |
for f in sep_files:
|
| 104 |
if not os.path.exists(f):
|
| 105 |
+
raise gr.Error(f"❌ 缺失輸出檔案: {f}")
|
|
|
|
| 106 |
|
|
|
|
| 107 |
return sep_files
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
+
# 清理失敗時保留臨時資料夾以利除錯
|
| 111 |
+
logger.error(f"⚠️ 處理失敗: {str(e)}")
|
| 112 |
+
raise gr.Error("❌ 處理失敗,請檢查日誌") from e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# 🎯 description 內容(轉為 HTML)
|
| 115 |
description_html = """
|
|
|
|
| 153 |
max_length=180 # 最大 3 分鐘
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# 修改 Gradio 輸出設定
|
| 157 |
AUDIO_OUTPUTS = [
|
| 158 |
+
gr.Audio(label="🗣️ 語音軌道 1", type="filepath", format="wav"),
|
| 159 |
+
gr.Audio(label="🗣️ 語音軌道 2", type="filepath", format="wav")
|
| 160 |
]
|
| 161 |
|
| 162 |
# 🚀 啟動應用程式
|