Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tempfile
|
| 4 |
+
import shutil
|
| 5 |
+
from audio_separator.separator import Separator
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
def separate_audio(input_file, model_name, progress=gr.Progress()):
|
| 13 |
+
if input_file is None:
|
| 14 |
+
return None, None
|
| 15 |
+
|
| 16 |
+
# 1. Use a temporary directory for outputs to keep HF Spaces clean
|
| 17 |
+
# HF Spaces has a writable /tmp directory which is ideal for ephemeral files
|
| 18 |
+
temp_output_dir = tempfile.mkdtemp()
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
progress(0, desc="Initializing Separator...")
|
| 22 |
+
# Initialize separator pointing to the temp directory
|
| 23 |
+
separator = Separator(output_dir=temp_output_dir)
|
| 24 |
+
|
| 25 |
+
progress(0.2, desc=f"Loading model: {model_name}...")
|
| 26 |
+
separator.load_model(model_filename=model_name)
|
| 27 |
+
|
| 28 |
+
progress(0.4, desc="Separating audio (this may take a minute)...")
|
| 29 |
+
# Run separation
|
| 30 |
+
output_files = separator.separate(input_file)
|
| 31 |
+
|
| 32 |
+
vocals = None
|
| 33 |
+
instrumental = None
|
| 34 |
+
|
| 35 |
+
progress(0.9, desc="Finalizing files...")
|
| 36 |
+
for file in output_files:
|
| 37 |
+
# Construct full path from the temp directory
|
| 38 |
+
full_path = os.path.join(temp_output_dir, file)
|
| 39 |
+
if "Vocals" in file:
|
| 40 |
+
vocals = full_path
|
| 41 |
+
elif "Instrumental" in file:
|
| 42 |
+
instrumental = full_path
|
| 43 |
+
|
| 44 |
+
return instrumental, vocals
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.error(f"Separation failed: {e}")
|
| 48 |
+
return None, None
|
| 49 |
+
|
| 50 |
+
# --- Gradio Interface ---
|
| 51 |
+
|
| 52 |
+
# Using a custom theme and better layout
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Audio Separator") as demo:
|
| 54 |
+
gr.Markdown("""
|
| 55 |
+
# 🎵 AI Audio Separator
|
| 56 |
+
Upload a song to split it into **Vocals** and **Instrumental** using state-of-the-art AI models.
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
+
# Input with waveform visualization enabled
|
| 62 |
+
input_audio = gr.Audio(
|
| 63 |
+
label="Upload Audio",
|
| 64 |
+
type="filepath",
|
| 65 |
+
waveform_options=gr.WaveformOptions(
|
| 66 |
+
waveform_color="#9ca3af",
|
| 67 |
+
waveform_progress_color="#3b82f6",
|
| 68 |
+
show_controls=True,
|
| 69 |
+
)
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
model_dropdown = gr.Dropdown(
|
| 73 |
+
choices=[
|
| 74 |
+
"model_bs_roformer_ep_317_sdr_12.9755.ckpt",
|
| 75 |
+
"UVR_MDXNET_KARA_2.onnx",
|
| 76 |
+
"UVR-MDX-NET-Inst_HQ_4.onnx"
|
| 77 |
+
],
|
| 78 |
+
value="model_bs_roformer_ep_317_sdr_12.9755.ckpt",
|
| 79 |
+
label="Select AI Model"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
separate_btn = gr.Button("🚀 Start Separation", variant="primary")
|
| 83 |
+
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
# Output components with waveforms for a better listening experience
|
| 86 |
+
output_inst = gr.Audio(
|
| 87 |
+
label="Instrumental (Output)",
|
| 88 |
+
interactive=False,
|
| 89 |
+
waveform_options=gr.WaveformOptions(waveform_color="#f59e0b")
|
| 90 |
+
)
|
| 91 |
+
output_vocals = gr.Audio(
|
| 92 |
+
label="Vocals (Output)",
|
| 93 |
+
interactive=False,
|
| 94 |
+
waveform_options=gr.WaveformOptions(waveform_color="#10b981")
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Click event with progress bar integration
|
| 98 |
+
separate_btn.click(
|
| 99 |
+
fn=separate_audio,
|
| 100 |
+
inputs=[input_audio, model_dropdown],
|
| 101 |
+
outputs=[output_inst, output_vocals]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
gr.Markdown("---")
|
| 105 |
+
gr.Markdown("Note: High-quality models may take longer to process on CPU hardware.")
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch()
|