Rezuwan commited on
Commit
3215ae9
·
verified ·
1 Parent(s): 17b1467

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -36
app.py CHANGED
@@ -1,62 +1,76 @@
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)
 
 
1
  import os
2
  import tempfile
3
+ import shutil
4
  import gradio as gr
5
  import tensorflow as tf
6
  from tensorflow.python.ops.numpy_ops import np_config
7
 
8
+ # -----------------------------
9
+ # TensorFlow setup
10
+ # -----------------------------
11
+ # Enable eager execution & NumPy behavior
12
  tf.config.run_functions_eagerly(True)
13
  np_config.enable_numpy_behavior()
14
 
15
+ # -----------------------------
16
  # Install Spleeter if missing
17
+ # -----------------------------
18
+ SPLEETER_FLAG = "/home/user/.spleeter_installed"
19
+ if not os.path.exists(SPLEETER_FLAG):
20
+ os.system("pip install tensorflow==2.12 spleeter==2.3.2")
21
+ open(SPLEETER_FLAG, "w").close()
22
 
23
  from spleeter.separator import Separator
24
 
25
+ # -----------------------------
26
  # Initialize separator
27
+ # -----------------------------
28
+ # 2 stems: vocals + accompaniment
29
+ separator = Separator("spleeter:2stems")
30
 
31
+ # -----------------------------
32
  # Audio separation function
33
+ # -----------------------------
34
  def separate_audio(audio_file):
35
+ # Create temporary directory for output
 
 
 
36
  tmp_dir = tempfile.mkdtemp()
37
 
38
+ try:
39
+ # Spleeter works best with file paths, not waveform arrays
40
+ separator.separate_to_file(audio_file, tmp_dir, codec="wav")
41
+
42
+ # Grab separated file paths
43
+ vocals_path = os.path.join(tmp_dir, os.path.basename(audio_file).replace(".wav", ""), "vocals.wav")
44
+ accompaniment_path = os.path.join(tmp_dir, os.path.basename(audio_file).replace(".wav", ""), "accompaniment.wav")
45
+
46
+ # Return file paths
47
+ return vocals_path, accompaniment_path
48
+ except Exception as e:
49
+ return str(e), None
50
+ finally:
51
+ # Note: We do NOT delete tmp_dir immediately, Gradio needs file paths
52
+ pass
53
 
54
+ # -----------------------------
55
  # Gradio UI
56
+ # -----------------------------
57
  with gr.Blocks() as iface:
58
+ gr.Markdown("## Audio Separator (Vocals + Accompaniment)")
59
 
60
+ audio_input = gr.Audio(label="Upload WAV/MP3 file", type="filepath")
61
+ output_vocals = gr.Audio(label="Vocals")
62
+ output_acc = gr.Audio(label="Accompaniment")
63
 
64
  separate_btn = gr.Button("Separate Audio")
65
+
66
+ separate_btn.click(
67
+ fn=separate_audio,
68
+ inputs=audio_input,
69
+ outputs=[output_vocals, output_acc]
70
+ )
71
 
72
+ # -----------------------------
73
+ # Launch app
74
+ # -----------------------------
75
+ if __name__ == "__main__":
76
+ iface.launch(server_name="0.0.0.0", server_port=7860)