import gradio as gr import torch import os import soundfile as sf import numpy as np import librosa import warnings import tempfile from DPTNet_eval.DPTNet_quant_sep import load_dpt_model, dpt_sep_process # 過濾警告訊息 warnings.filterwarnings("ignore", category=UserWarning) warnings.filterwarnings("ignore", category=FutureWarning) # 加載模型(全局變量) model = load_dpt_model() def separate_audio(input_wav): """處理音訊分離的主要函數""" try: # 步驟 1:讀取音訊並標準化格式 data, sr = librosa.load(input_wav, sr=None, mono=True) # 步驟 2:強制重採樣到 16kHz if sr != 16000: data = librosa.resample(data, orig_sr=sr, target_sr=16000) sr = 16000 # 步驟 3:生成唯一臨時檔案 with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file: temp_wav = tmp_file.name sf.write(temp_wav, data, sr, subtype='PCM_16') # 步驟 4:執行語音分離 outfilename = "output.wav" dpt_sep_process(temp_wav, model=model, outfilename=outfilename) # 步驟 5:清理臨時檔案 os.remove(temp_wav) # 步驟 6:驗證輸出檔案存在 output_files = [ outfilename.replace('.wav', '_sep1.wav'), outfilename.replace('.wav', '_sep2.wav') ] if not all(os.path.exists(f) for f in output_files): raise gr.Error("分離過程中發生錯誤,請檢查輸入檔案格式!") return output_files except Exception as e: # 錯誤處理 error_msg = f"處理失敗:{str(e)}" raise gr.Error(error_msg) from e # 🎯 你提供的 description 內容(已轉為 HTML) description_html = """
上傳一段混音音檔,自動分離出兩個人的聲音