Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
| 5 |
from tensorflow.python.ops.numpy_ops import np_config
|
| 6 |
|
| 7 |
-
# -------------------------
|
| 8 |
-
# TensorFlow
|
| 9 |
-
# -------------------------
|
|
|
|
| 10 |
tf.config.run_functions_eagerly(True)
|
| 11 |
np_config.enable_numpy_behavior()
|
| 12 |
|
| 13 |
-
# -------------------------
|
| 14 |
# Install Spleeter if missing
|
| 15 |
-
# -------------------------
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
from spleeter.separator import Separator
|
| 21 |
|
| 22 |
-
# -------------------------
|
| 23 |
# Initialize separator
|
| 24 |
-
# -------------------------
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
# -------------------------
|
| 28 |
# Audio separation function
|
| 29 |
-
# -------------------------
|
| 30 |
def separate_audio(audio_file):
|
| 31 |
-
|
| 32 |
-
Takes an audio file path, separates vocals & accompaniment,
|
| 33 |
-
and returns paths to the resulting WAV files.
|
| 34 |
-
"""
|
| 35 |
tmp_dir = tempfile.mkdtemp()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# -------------------------
|
| 47 |
# Gradio UI
|
| 48 |
-
# -------------------------
|
| 49 |
with gr.Blocks() as iface:
|
| 50 |
-
gr.Markdown("## Audio
|
| 51 |
|
| 52 |
-
audio_input = gr.Audio(label="Upload
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
separate_btn = gr.Button("Separate Audio")
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
# -------------------------
|
| 60 |
-
# Launch
|
| 61 |
-
# -------------------------
|
| 62 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
+
import shutil
|
| 4 |
import gradio as gr
|
| 5 |
import tensorflow as tf
|
| 6 |
from tensorflow.python.ops.numpy_ops import np_config
|
| 7 |
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# TensorFlow setup
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Enable eager execution & NumPy behavior
|
| 12 |
tf.config.run_functions_eagerly(True)
|
| 13 |
np_config.enable_numpy_behavior()
|
| 14 |
|
| 15 |
+
# -----------------------------
|
| 16 |
# Install Spleeter if missing
|
| 17 |
+
# -----------------------------
|
| 18 |
+
SPLEETER_FLAG = "/home/user/.spleeter_installed"
|
| 19 |
+
if not os.path.exists(SPLEETER_FLAG):
|
| 20 |
+
os.system("pip install tensorflow==2.12 spleeter==2.3.2")
|
| 21 |
+
open(SPLEETER_FLAG, "w").close()
|
| 22 |
|
| 23 |
from spleeter.separator import Separator
|
| 24 |
|
| 25 |
+
# -----------------------------
|
| 26 |
# Initialize separator
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# 2 stems: vocals + accompaniment
|
| 29 |
+
separator = Separator("spleeter:2stems")
|
| 30 |
|
| 31 |
+
# -----------------------------
|
| 32 |
# Audio separation function
|
| 33 |
+
# -----------------------------
|
| 34 |
def separate_audio(audio_file):
|
| 35 |
+
# Create temporary directory for output
|
|
|
|
|
|
|
|
|
|
| 36 |
tmp_dir = tempfile.mkdtemp()
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
# Spleeter works best with file paths, not waveform arrays
|
| 40 |
+
separator.separate_to_file(audio_file, tmp_dir, codec="wav")
|
| 41 |
+
|
| 42 |
+
# Grab separated file paths
|
| 43 |
+
vocals_path = os.path.join(tmp_dir, os.path.basename(audio_file).replace(".wav", ""), "vocals.wav")
|
| 44 |
+
accompaniment_path = os.path.join(tmp_dir, os.path.basename(audio_file).replace(".wav", ""), "accompaniment.wav")
|
| 45 |
+
|
| 46 |
+
# Return file paths
|
| 47 |
+
return vocals_path, accompaniment_path
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return str(e), None
|
| 50 |
+
finally:
|
| 51 |
+
# Note: We do NOT delete tmp_dir immediately, Gradio needs file paths
|
| 52 |
+
pass
|
| 53 |
|
| 54 |
+
# -----------------------------
|
| 55 |
# Gradio UI
|
| 56 |
+
# -----------------------------
|
| 57 |
with gr.Blocks() as iface:
|
| 58 |
+
gr.Markdown("## Audio Separator (Vocals + Accompaniment)")
|
| 59 |
|
| 60 |
+
audio_input = gr.Audio(label="Upload WAV/MP3 file", type="filepath")
|
| 61 |
+
output_vocals = gr.Audio(label="Vocals")
|
| 62 |
+
output_acc = gr.Audio(label="Accompaniment")
|
| 63 |
|
| 64 |
separate_btn = gr.Button("Separate Audio")
|
| 65 |
+
|
| 66 |
+
separate_btn.click(
|
| 67 |
+
fn=separate_audio,
|
| 68 |
+
inputs=audio_input,
|
| 69 |
+
outputs=[output_vocals, output_acc]
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# -----------------------------
|
| 73 |
+
# Launch app
|
| 74 |
+
# -----------------------------
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|