Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import librosa
|
| 3 |
import numpy as np
|
| 4 |
from spleeter.separator import Separator
|
| 5 |
-
import threading
|
| 6 |
-
import queue
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import soundfile as sf
|
| 9 |
-
|
| 10 |
-
# Load the song
|
| 11 |
-
def load_audio(file_path):
|
| 12 |
-
signal, sr = librosa.load(file_path, sr=None)
|
| 13 |
-
return signal, sr
|
| 14 |
|
| 15 |
# Spleeter separation
|
| 16 |
-
def spleeter_separate(audio
|
| 17 |
separator = Separator('spleeter:5stems')
|
| 18 |
prediction = separator.separate(audio)
|
| 19 |
return prediction['vocals'], prediction['accompaniment'], prediction['bass'], prediction['drums'], prediction['other']
|
|
@@ -27,92 +19,32 @@ def adjust_volume(stems, volumes):
|
|
| 27 |
return adjusted_stems
|
| 28 |
|
| 29 |
# Function to handle the separation and volume adjustment
|
| 30 |
-
def process_audio(
|
| 31 |
-
|
| 32 |
-
stems = spleeter_separate(audio, sr)
|
| 33 |
adjusted_stems = adjust_volume(stems, volumes)
|
| 34 |
-
result_queue.put(adjusted_stems)
|
| 35 |
-
|
| 36 |
-
# Multithreaded processing
|
| 37 |
-
def process_audio_threaded(file_path, volumes, result_queue):
|
| 38 |
-
thread = threading.Thread(target=process_audio, args=(file_path, volumes, result_queue))
|
| 39 |
-
thread.start()
|
| 40 |
-
|
| 41 |
-
# Gradio interface
|
| 42 |
-
def separate_audio(file, vocals, accompaniment, bass, drums, other):
|
| 43 |
-
file_path = file[0]
|
| 44 |
-
|
| 45 |
-
volumes = [vocals, accompaniment, bass, drums, other]
|
| 46 |
-
|
| 47 |
-
result_queue = queue.Queue()
|
| 48 |
-
process_audio_threaded(file_path, volumes, result_queue)
|
| 49 |
-
adjusted_stems = result_queue.get()
|
| 50 |
-
|
| 51 |
-
# Reconstruct the audio with adjusted stems
|
| 52 |
reconstructed_audio = sum(adjusted_stems)
|
| 53 |
-
|
| 54 |
return reconstructed_audio.astype(np.float32)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
def
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
librosa.display.waveplot(signal, sr=sr)
|
| 63 |
-
plt.title('Waveform')
|
| 64 |
-
|
| 65 |
-
# Spectrogram plot
|
| 66 |
-
plt.subplot(2, 1, 2)
|
| 67 |
-
plt.specgram(signal, NFFT=2048, Fs=2, Fc=0, noverlap=128, cmap='inferno', sides='default', mode='default', scale='dB')
|
| 68 |
-
plt.title('Spectrogram')
|
| 69 |
-
|
| 70 |
-
plt.tight_layout()
|
| 71 |
-
plt.suptitle(title, fontsize=14)
|
| 72 |
-
plt.show()
|
| 73 |
|
| 74 |
iface = gr.Interface(
|
| 75 |
fn=separate_audio,
|
| 76 |
inputs=[
|
| 77 |
-
gr.inputs.Audio(label="Audio file"),
|
| 78 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Vocals"),
|
| 79 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Accompaniment"),
|
| 80 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Bass"),
|
| 81 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Drums"),
|
| 82 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Other")
|
| 83 |
],
|
| 84 |
-
outputs=gr.outputs.Audio(
|
| 85 |
title="Song Stem Separation",
|
| 86 |
-
description="Isolate vocals, accompaniment, bass, and drums of any song using the Spleeter model."
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# Add visualizations and support for different audio formats
|
| 90 |
-
def visualize_and_save_audio(inputs, output):
|
| 91 |
-
audio_file = inputs[0]
|
| 92 |
-
vocals = inputs[1]
|
| 93 |
-
accompaniment = inputs[2]
|
| 94 |
-
bass = inputs[3]
|
| 95 |
-
drums = inputs[4]
|
| 96 |
-
other = inputs[5]
|
| 97 |
-
|
| 98 |
-
# Load the original audio file
|
| 99 |
-
signal, sr = librosa.load(audio_file.name, sr=None)
|
| 100 |
-
|
| 101 |
-
# Plot waveform and spectrogram of the original audio
|
| 102 |
-
plot_audio(signal, sr, "Original Audio")
|
| 103 |
-
|
| 104 |
-
# Save the separated audio to a file
|
| 105 |
-
output_file = "separated_audio.wav"
|
| 106 |
-
sf.write(output_file, output.astype(np.float32), sr)
|
| 107 |
-
|
| 108 |
-
# Load the separated audio file
|
| 109 |
-
separated_signal, separated_sr = librosa.load(output_file, sr=None)
|
| 110 |
-
|
| 111 |
-
# Plot waveform and spectrogram of the separated audio
|
| 112 |
-
plot_audio(separated_signal, separated_sr, "Separated Audio")
|
| 113 |
-
|
| 114 |
-
# Provide feedback to the user
|
| 115 |
-
print("Audio separated successfully! Separated audio saved to 'separated_audio.wav'.")
|
| 116 |
-
|
| 117 |
iface.launch()
|
| 118 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from spleeter.separator import Separator
|
|
|
|
|
|
|
|
|
|
| 4 |
import soundfile as sf
|
| 5 |
+
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Spleeter separation
|
| 8 |
+
def spleeter_separate(audio):
|
| 9 |
separator = Separator('spleeter:5stems')
|
| 10 |
prediction = separator.separate(audio)
|
| 11 |
return prediction['vocals'], prediction['accompaniment'], prediction['bass'], prediction['drums'], prediction['other']
|
|
|
|
| 19 |
return adjusted_stems
|
| 20 |
|
| 21 |
# Function to handle the separation and volume adjustment
|
| 22 |
+
def process_audio(audio, volumes):
|
| 23 |
+
stems = spleeter_separate(audio)
|
|
|
|
| 24 |
adjusted_stems = adjust_volume(stems, volumes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
reconstructed_audio = sum(adjusted_stems)
|
|
|
|
| 26 |
return reconstructed_audio.astype(np.float32)
|
| 27 |
|
| 28 |
+
# Gradio interface
|
| 29 |
+
def separate_audio(audio, vocals, accompaniment, bass, drums, other):
|
| 30 |
+
audio = np.frombuffer(base64.b64decode(audio), dtype=np.float32)
|
| 31 |
+
volumes = [vocals, accompaniment, bass, drums, other]
|
| 32 |
+
reconstructed_audio = process_audio(audio, volumes)
|
| 33 |
+
return base64.b64encode(reconstructed_audio.tobytes()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=separate_audio,
|
| 37 |
inputs=[
|
| 38 |
+
gr.inputs.Audio(label="Audio file", type="file", accept="audio/*"),
|
| 39 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Vocals"),
|
| 40 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Accompaniment"),
|
| 41 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Bass"),
|
| 42 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Drums"),
|
| 43 |
gr.inputs.Slider(0.0, 1.0, step=0.1, label="Other")
|
| 44 |
],
|
| 45 |
+
outputs=gr.outputs.Audio(label="Separated Audio", type="base64"),
|
| 46 |
title="Song Stem Separation",
|
| 47 |
+
description="Isolate vocals, accompaniment, bass, and drums of any song using the Spleeter model."
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
iface.launch()
|
|
|