Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
from scipy.io.wavfile import write
|
| 6 |
+
from audio_separator.separator import Separator
|
| 7 |
+
|
| 8 |
+
# Initialize the Separator
|
| 9 |
+
separator = Separator()
|
| 10 |
+
|
| 11 |
+
def separate_audio(audio_file):
|
| 12 |
+
if audio_file is None:
|
| 13 |
+
raise ValueError("Please upload an audio file.")
|
| 14 |
+
|
| 15 |
+
# Load the separator model
|
| 16 |
+
separator.load_model()
|
| 17 |
+
|
| 18 |
+
# Generate a random file name for the output
|
| 19 |
+
random_id = str(random.randint(10000, 99999))
|
| 20 |
+
output_file = f"outputs/{random_id}.wav"
|
| 21 |
+
|
| 22 |
+
# Create the output directory if it doesn't exist
|
| 23 |
+
os.makedirs("outputs", exist_ok=True)
|
| 24 |
+
|
| 25 |
+
# Save the uploaded audio file to the output directory
|
| 26 |
+
write(output_file, audio_file[0], audio_file[1])
|
| 27 |
+
|
| 28 |
+
# Perform the separation
|
| 29 |
+
output_files = separator.separate(output_file)
|
| 30 |
+
|
| 31 |
+
# List the separated files
|
| 32 |
+
files_list = []
|
| 33 |
+
for file in os.listdir("outputs"):
|
| 34 |
+
if re.search(random_id, file):
|
| 35 |
+
files_list.append(os.path.join("outputs", file))
|
| 36 |
+
|
| 37 |
+
# Ensure there are at least two separated stems
|
| 38 |
+
if len(files_list) < 2:
|
| 39 |
+
raise ValueError("Error: Separation did not produce enough stems.")
|
| 40 |
+
|
| 41 |
+
stem1_file = files_list[0]
|
| 42 |
+
stem2_file = files_list[1]
|
| 43 |
+
|
| 44 |
+
return stem1_file}, stem2_file
|
| 45 |
+
|
| 46 |
+
# Define the Gradio Blocks interface
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("# Audio Separator")
|
| 49 |
+
gr.Markdown("Upload an audio file to separate it into different components.")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
audio_input = gr.Audio(label="Upload Audio", type="numpy")
|
| 54 |
+
separate_button = gr.Button("Separate")
|
| 55 |
+
|
| 56 |
+
with gr.Column():
|
| 57 |
+
output_1 = gr.Audio(label="Separation Output 1")
|
| 58 |
+
output_2 = gr.Audio(label="Separation Output 2")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Set up button click event
|
| 62 |
+
separate_button.click(fn=separate_audio, inputs=audio_input, outputs=output_1, output_2)
|
| 63 |
+
|
| 64 |
+
# Launch the interface
|
| 65 |
+
demo.launch()
|