Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from spleeter.separator import Separator
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import random2
|
5 |
+
|
6 |
+
# Initiate a file separator with 2 stems (instruments and vocals)
|
7 |
+
separator = Separator('spleeter:2stems')
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
# Gradio function to split audio stems and return their filepaths
|
12 |
+
def extract_stems(audio):
|
13 |
+
|
14 |
+
# initiate a folder for splitted files
|
15 |
+
foldername = str(random2.randrange(100000000))
|
16 |
+
|
17 |
+
# Separate audio input. Synchronous is true to wait for the end of split before going further
|
18 |
+
separator.separate_to_file(audio, foldername, filename_format= foldername + "/{instrument}.wav", synchronous=True)
|
19 |
+
|
20 |
+
# To get full filepath, need to clean the input audio filepath (ex : /tmp/audioexample.mp3)
|
21 |
+
filepath = audio[5:] # remove first five chars aka /tmp/
|
22 |
+
filepath = filepath[:max([idx for idx, x in enumerate(filepath) if x == '.'])] # remove extension
|
23 |
+
|
24 |
+
vocals = f"./output/"+ foldername +"/vocals.wav"
|
25 |
+
accompaniment = f"./output/"+ foldername +"/accompaniment.wav"
|
26 |
+
|
27 |
+
return vocals, accompaniment
|
28 |
+
|
29 |
+
# Launch a Gradio interface
|
30 |
+
# Input is an audio file, returning his filepath
|
31 |
+
# Output is an also audio files
|
32 |
+
|
33 |
+
title = "Demo: Deezer Spleeter / Audio separation library"
|
34 |
+
description = "This demo is a basic interface for <a href='https://research.deezer.com/projects/spleeter.html' target='_blank'>Deezer Spleeter</a>. It uses the Spleeter library for separate audio file in two stems : accompaniments and vocals."
|
35 |
+
examples = [["examples/" + mp3] for mp3 in os.listdir("examples/")]
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=extract_stems,
|
39 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
40 |
+
outputs=[gr.Audio(label="Vocals stem", source="upload", type="filepath"), gr.Audio(label="Accompaniment stem", source="upload", type="filepath")],
|
41 |
+
title=title,
|
42 |
+
description=description,
|
43 |
+
examples=examples,
|
44 |
+
allow_flagging="never",
|
45 |
+
cache_examples=False
|
46 |
+
)
|
47 |
+
|
48 |
+
demo.launch(share=True)
|