Music-Split / app.py
amirgame197's picture
Update app.py
b8e4d8f verified
raw
history blame
2.34 kB
import gradio as gr
import os
import random
import subprocess
def separate_audio(audio_path, stem_count):
print(f"{audio_path=}")
head, tail = os.path.split(audio_path)
gradio_temp_path = head
audio_filename = tail.split('.')[0]
print(f"{gradio_temp_path=}")
print(f"{audio_filename=}")
print(f"{stem_count=}")
# Construct the spleeter command
command = f"spleeter separate -p spleeter:{stem_count}stems -o {gradio_temp_path} {audio_path}"
command = command.split()
print(f"{command=}")
# Run the spleeter command and capture the output
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"stdout: {result.stdout.decode('utf-8')}")
print(f"stderr: {result.stderr.decode('utf-8')}")
# Check if the command executed successfully
if result.returncode != 0:
print(f"Error executing command: {result.returncode}")
return []
# Generate a random number for unique folder naming
randomnumber = str(random.randint(111111111, 999999999))
paths = []
# Define the output paths for the separated stems
if stem_count == 2:
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/accompaniment.wav"))
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/vocals.wav"))
elif stem_count == 4 or stem_count == 5:
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/vocals.wav"))
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/drums.wav"))
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/bass.wav"))
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/other.wav"))
if stem_count == 5:
paths.append(os.path.join(gradio_temp_path, f"separated_audio/{audio_filename}/piano.wav"))
# Return the audio file paths as a list of Audio components
return [gr.Audio(path) for path in paths]
# Define the Gradio interface
iface = gr.Interface(
fn=separate_audio,
inputs=[gr.Audio(type="filepath"), gr.Radio([2, 4, 5])],
outputs=[gr.Audio() for _ in range(max_stem_count)] # Adjust the range according to the max number of stems
)
# Launch the interface
iface.launch()