File size: 2,453 Bytes
a37cef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import subprocess
import os

def audio_model_inference(files, output_folder, model_path, denoise, margin, chunks, n_fft, dim_t, dim_f):
    # 构建命令行调用字符串
    cmd = f"separate.py {' '.join(files)}"
    if output_folder:
        cmd += f" -o {output_folder}"
    if model_path:
        cmd += f" -m {model_path}"
    if denoise:
        cmd += " -d"
    if margin:
        cmd += f" -M {margin}"
    if chunks:
        cmd += f" -c {chunks}"
    if n_fft:
        cmd += f" -F {n_fft}"
    if dim_t:
        cmd += f" -t {dim_t}"
    if dim_f:
        cmd += f" -f {dim_f}"
    
    # 执行命令行调用
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    
    # 检查命令是否成功执行
    if result.returncode != 0:
        return f"Error: {result.stderr}"
    
    # 读取输出文件
    vocals_file = f"{os.path.splitext(os.path.basename(files[0]))[0]}_vocals.wav"
    no_vocals_file = f"{os.path.splitext(os.path.basename(files[0]))[0]}_no_vocals.wav"
    vocals_path = os.path.join(output_folder, vocals_file)
    no_vocals_path = os.path.join(output_folder, no_vocals_file)
    
    # 确保文件存在
    if not os.path.exists(vocals_path) or not os.path.exists(no_vocals_path):
        return "Error: Output files not found."
    
    # 读取音频文件
    vocals_audio = open(vocals_path, 'rb').read()
    no_vocals_audio = open(no_vocals_path, 'rb').read()
    
    return (vocals_audio, no_vocals_audio)

# Gradio 界面组件
inputs = [
    gr.inputs.File(label="Source Audio Files", type='file', file_count='multiple'),
    gr.inputs.Textbox(label="Output Folder", default="output/"),
    gr.inputs.Textbox(label="Model Path", default="model.onnx"),
    gr.inputs.Checkbox(label="Enable Denoising", default=False),
    gr.inputs.Number(label="Margin", default=0.1),
    gr.inputs.Number(label="Chunk Size", default=1024),
    gr.inputs.Number(label="FFT Size", default=2048),
    gr.inputs.Number(label="Time Dimension", default=512),
    gr.inputs.Number(label="Frequency Dimension", default=64)
]

outputs = [gr.outputs.Audio(label="Vocals"), gr.outputs.Audio(label="No Vocals")]

# 创建界面
iface = gr.Interface(
    fn=audio_model_inference,
    inputs=inputs,
    outputs=outputs,
    title="Audio Separation Model",
    description="Upload audio files and configure parameters to process them using the audio separation model."
)

iface.launch()