Rezuwan commited on
Commit
bcdcaa8
·
verified ·
1 Parent(s): 3eca7a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -41
app.py CHANGED
@@ -1,60 +1,39 @@
1
  import gradio as gr
2
  from spleeter.separator import Separator
3
  from spleeter.audio.adapter import AudioAdapter
4
- import os
5
  import tempfile
 
6
 
7
- # Path to the pretrained models inside your HF Space
8
  MODEL_PATH = "pretrained_models/2stems"
9
 
10
- # Load Spleeter once (much faster for multiple users)
11
- separator = Separator(f"{MODEL_PATH}")
12
-
13
  audio_loader = AudioAdapter.default()
14
 
15
-
16
  def separate_audio(audio_file):
17
  if audio_file is None:
18
- return None, None, "Please upload an audio file."
19
 
20
- # Load audio
21
  waveform, sample_rate = audio_loader.load(audio_file)
22
-
23
- # Separate stems
24
  prediction = separator.separate(waveform)
25
 
26
- # Temporary output files
27
- tmp_dir = tempfile.mkdtemp()
28
- vocal_path = os.path.join(tmp_dir, "vocals.wav")
29
- instrumental_path = os.path.join(tmp_dir, "instrumental.wav")
30
-
31
- # Save outputs
32
- audio_loader.save(vocal_path, prediction["vocals"], sample_rate)
33
- audio_loader.save(instrumental_path, prediction["accompaniment"], sample_rate)
34
-
35
- return vocal_path, instrumental_path, "Done!"
36
-
37
-
38
- # Gradio Interface
39
- with gr.Blocks() as demo:
40
- gr.Markdown("# 🎵 Vocal & Instrumental Separator (Spleeter 2-Stems)")
41
- gr.Markdown("Upload a song and get **vocals** and **instrumental** extracted!")
42
-
43
- with gr.Row():
44
- input_audio = gr.Audio(type="filepath", label="Upload Audio")
45
-
46
- with gr.Row():
47
- vocal_output = gr.Audio(label="Vocals")
48
- instru_output = gr.Audio(label="Instrumental")
49
 
50
- status = gr.Textbox(label="Status")
 
51
 
52
- run_btn = gr.Button("Separate")
53
 
54
- run_btn.click(
55
- separate_audio,
56
- inputs=input_audio,
57
- outputs=[vocal_output, instru_output, status]
58
- )
 
 
 
 
 
59
 
60
- demo.launch()
 
1
  import gradio as gr
2
  from spleeter.separator import Separator
3
  from spleeter.audio.adapter import AudioAdapter
 
4
  import tempfile
5
+ import os
6
 
 
7
  MODEL_PATH = "pretrained_models/2stems"
8
 
9
+ separator = Separator(MODEL_PATH)
 
 
10
  audio_loader = AudioAdapter.default()
11
 
 
12
  def separate_audio(audio_file):
13
  if audio_file is None:
14
+ return None, None, "Upload an audio file."
15
 
 
16
  waveform, sample_rate = audio_loader.load(audio_file)
 
 
17
  prediction = separator.separate(waveform)
18
 
19
+ tmpdir = tempfile.mkdtemp()
20
+ vocals_path = os.path.join(tmpdir, "vocals.wav")
21
+ instr_path = os.path.join(tmpdir, "instrumental.wav")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ audio_loader.save(vocals_path, prediction["vocals"], sample_rate)
24
+ audio_loader.save(instr_path, prediction["accompaniment"], sample_rate)
25
 
26
+ return vocals_path, instr_path, "Done!"
27
 
28
+ iface = gr.Interface(
29
+ fn=separate_audio,
30
+ inputs=gr.Audio(type="filepath", label="Upload Audio"),
31
+ outputs=[
32
+ gr.Audio(label="Vocals"),
33
+ gr.Audio(label="Instrumental"),
34
+ gr.Textbox(label="Status")
35
+ ],
36
+ title="🎵 Spleeter Vocal/Instrumental Separator"
37
+ )
38
 
39
+ iface.launch()