Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,62 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.python.ops.numpy_ops import np_config
|
| 4 |
|
| 5 |
-
# -------------------------
|
| 6 |
-
# TensorFlow
|
| 7 |
-
# -------------------------
|
| 8 |
tf.config.run_functions_eagerly(True)
|
| 9 |
np_config.enable_numpy_behavior()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
if not os.path.exists("/home/user/.spleeter_installed"):
|
| 12 |
-
import install_spleeter
|
| 13 |
open("/home/user/.spleeter_installed", "w").close()
|
| 14 |
|
| 15 |
-
import tempfile
|
| 16 |
-
import gradio as gr
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# --------------------------
|
| 20 |
-
# Import Spleeter
|
| 21 |
-
# --------------------------
|
| 22 |
from spleeter.separator import Separator
|
| 23 |
|
| 24 |
-
# -------------------------
|
| 25 |
# Initialize separator
|
| 26 |
-
# -------------------------
|
| 27 |
separator = Separator("spleeter:2stems", multiprocess=False)
|
| 28 |
|
| 29 |
-
# -------------------------
|
| 30 |
-
#
|
| 31 |
-
# -------------------------
|
| 32 |
-
|
| 33 |
def separate_audio(audio_file):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return vocals_path, instr_path
|
| 41 |
|
| 42 |
-
# -------------------------
|
| 43 |
-
# Gradio
|
| 44 |
-
# -------------------------
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 configuration
|
| 9 |
+
# -------------------------
|
| 10 |
tf.config.run_functions_eagerly(True)
|
| 11 |
np_config.enable_numpy_behavior()
|
| 12 |
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Install Spleeter if missing
|
| 15 |
+
# -------------------------
|
| 16 |
if not os.path.exists("/home/user/.spleeter_installed"):
|
| 17 |
+
import install_spleeter # assumes install_spleeter.py is available
|
| 18 |
open("/home/user/.spleeter_installed", "w").close()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from spleeter.separator import Separator
|
| 21 |
|
| 22 |
+
# -------------------------
|
| 23 |
# Initialize separator
|
| 24 |
+
# -------------------------
|
| 25 |
separator = Separator("spleeter:2stems", multiprocess=False)
|
| 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 |
+
# Use Spleeter's file-based separation
|
| 38 |
+
separator.separate_to_file(audio_file, tmp_dir, codec="wav")
|
| 39 |
+
|
| 40 |
+
base_name = os.path.splitext(os.path.basename(audio_file))[0]
|
| 41 |
+
vocals_path = os.path.join(tmp_dir, base_name, "vocals.wav")
|
| 42 |
+
instr_path = os.path.join(tmp_dir, base_name, "accompaniment.wav")
|
| 43 |
+
|
| 44 |
return vocals_path, instr_path
|
| 45 |
|
| 46 |
+
# -------------------------
|
| 47 |
+
# Gradio UI
|
| 48 |
+
# -------------------------
|
| 49 |
+
with gr.Blocks() as iface:
|
| 50 |
+
gr.Markdown("## Audio Source Separation (Vocals & Instrumentals)")
|
| 51 |
+
|
| 52 |
+
audio_input = gr.Audio(label="Upload Audio", type="filepath")
|
| 53 |
+
vocals_output = gr.Audio(label="Vocals", type="filepath")
|
| 54 |
+
instr_output = gr.Audio(label="Instrumental", type="filepath")
|
| 55 |
+
|
| 56 |
+
separate_btn = gr.Button("Separate Audio")
|
| 57 |
+
separate_btn.click(separate_audio, inputs=audio_input, outputs=[vocals_output, instr_output])
|
| 58 |
+
|
| 59 |
+
# -------------------------
|
| 60 |
+
# Launch Gradio app
|
| 61 |
+
# -------------------------
|
| 62 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|