File size: 1,958 Bytes
0f7d4a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import random
import re
from scipy.io.wavfile import write
from audio_separator.separator import Separator

# Initialize the Separator
separator = Separator()

def separate_audio(audio_file):
    if audio_file is None:
        raise ValueError("Please upload an audio file.")

    # Load the separator model
    separator.load_model()

    # Generate a random file name for the output
    random_id = str(random.randint(10000, 99999))
    output_file = f"outputs/{random_id}.wav"
    
    # Create the output directory if it doesn't exist
    os.makedirs("outputs", exist_ok=True)
    
    # Save the uploaded audio file to the output directory
    write(output_file, audio_file[0], audio_file[1])

    # Perform the separation
    output_files = separator.separate(output_file)
    
    # List the separated files
    files_list = []
    for file in os.listdir("outputs"):
        if re.search(random_id, file):
            files_list.append(os.path.join("outputs", file))
    
    # Ensure there are at least two separated stems
    if len(files_list) < 2:
        raise ValueError("Error: Separation did not produce enough stems.")
        
    stem1_file = files_list[0]
    stem2_file = files_list[1]

    return stem1_file}, stem2_file

# Define the Gradio Blocks interface
with gr.Blocks() as demo:
    gr.Markdown("# Audio Separator")
    gr.Markdown("Upload an audio file to separate it into different components.")
    
    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(label="Upload Audio", type="numpy")
            separate_button = gr.Button("Separate")
        
        with gr.Column():
            output_1 = gr.Audio(label="Separation Output 1")
            output_2 = gr.Audio(label="Separation Output 2")
    
    
    # Set up button click event
    separate_button.click(fn=separate_audio, inputs=audio_input, outputs=output_1, output_2)

# Launch the interface
demo.launch()