Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.python.ops.numpy_ops import np_config
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import atexit
|
| 6 |
|
|
@@ -16,49 +18,49 @@ if not os.path.exists("/home/user/.spleeter_installed"):
|
|
| 16 |
|
| 17 |
|
| 18 |
# Now import normal modules
|
| 19 |
-
import gradio as gr
|
| 20 |
from spleeter.separator import Separator
|
| 21 |
from spleeter.audio.adapter import AudioAdapter
|
| 22 |
-
import tempfile
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
separator = Separator("spleeter:2stems", multiprocess=False)
|
| 26 |
audio_loader = AudioAdapter.default()
|
| 27 |
|
| 28 |
def separate_audio(audio_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Load audio as NumPy array
|
| 30 |
waveform, sample_rate = audio_loader.load(audio_file, sample_rate=44100)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Separate stems
|
| 34 |
pred = separator.separate(waveform)
|
| 35 |
|
| 36 |
-
# Save results
|
| 37 |
-
|
| 38 |
-
vocals_path = os.path.join(
|
| 39 |
-
instr_path = os.path.join(
|
| 40 |
|
| 41 |
audio_loader.save(vocals_path, pred["vocals"], sample_rate)
|
| 42 |
audio_loader.save(instr_path, pred["accompaniment"], sample_rate)
|
| 43 |
|
| 44 |
return vocals_path, instr_path, "Done!"
|
| 45 |
|
| 46 |
-
#
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=separate_audio,
|
| 49 |
-
inputs=gr.Audio(type="filepath"),
|
| 50 |
-
outputs=[gr.Audio(), gr.Audio(), gr.Textbox()],
|
| 51 |
-
title="Spleeter Vocal/Instrumental Separator"
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
# Ensure clean shutdown
|
| 55 |
-
def cleanup():
|
| 56 |
-
try:
|
| 57 |
-
iface.close()
|
| 58 |
-
except Exception:
|
| 59 |
-
pass
|
| 60 |
-
|
| 61 |
-
atexit.register(cleanup)
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.python.ops.numpy_ops import np_config
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import tempfile
|
| 6 |
import numpy as np
|
| 7 |
import atexit
|
| 8 |
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# Now import normal modules
|
|
|
|
| 21 |
from spleeter.separator import Separator
|
| 22 |
from spleeter.audio.adapter import AudioAdapter
|
|
|
|
| 23 |
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Initialize Spleeter separator
|
| 32 |
separator = Separator("spleeter:2stems", multiprocess=False)
|
| 33 |
audio_loader = AudioAdapter.default()
|
| 34 |
|
| 35 |
def separate_audio(audio_file):
|
| 36 |
+
"""
|
| 37 |
+
Separate the vocals and instrumental from the input audio file.
|
| 38 |
+
Returns filepaths for the separated stems and a status message.
|
| 39 |
+
"""
|
| 40 |
# Load audio as NumPy array
|
| 41 |
waveform, sample_rate = audio_loader.load(audio_file, sample_rate=44100)
|
| 42 |
+
|
| 43 |
+
# Separate stems using Spleeter
|
|
|
|
| 44 |
pred = separator.separate(waveform)
|
| 45 |
|
| 46 |
+
# Save results in a temporary directory
|
| 47 |
+
tmp_dir = tempfile.mkdtemp()
|
| 48 |
+
vocals_path = os.path.join(tmp_dir, "vocals.wav")
|
| 49 |
+
instr_path = os.path.join(tmp_dir, "instrumental.wav")
|
| 50 |
|
| 51 |
audio_loader.save(vocals_path, pred["vocals"], sample_rate)
|
| 52 |
audio_loader.save(instr_path, pred["accompaniment"], sample_rate)
|
| 53 |
|
| 54 |
return vocals_path, instr_path, "Done!"
|
| 55 |
|
| 56 |
+
# Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=separate_audio,
|
| 59 |
+
inputs=gr.Audio(type="filepath", label="Input Audio"),
|
| 60 |
+
outputs=[gr.Audio(label="Vocals"), gr.Audio(label="Instrumental"), gr.Textbox(label="Status")],
|
| 61 |
+
title="Spleeter Vocal/Instrumental Separator",
|
| 62 |
+
description="Upload an audio file to separate it into vocals and instrumental tracks."
|
| 63 |
)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
iface.launch()
|