Rezuwan commited on
Commit
17b1467
·
verified ·
1 Parent(s): 5f270c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -34
app.py CHANGED
@@ -1,52 +1,62 @@
1
  import os
 
 
2
  import tensorflow as tf
3
  from tensorflow.python.ops.numpy_ops import np_config
4
 
5
- # --------------------------
6
- # TensorFlow config + Spleeter install
7
- # --------------------------
8
  tf.config.run_functions_eagerly(True)
9
  np_config.enable_numpy_behavior()
10
 
 
 
 
11
  if not os.path.exists("/home/user/.spleeter_installed"):
12
- import install_spleeter
13
  open("/home/user/.spleeter_installed", "w").close()
14
 
15
- import tempfile
16
- import gradio as gr
17
-
18
-
19
- # --------------------------
20
- # Import Spleeter
21
- # --------------------------
22
  from spleeter.separator import Separator
23
 
24
- # --------------------------
25
  # Initialize separator
26
- # --------------------------
27
  separator = Separator("spleeter:2stems", multiprocess=False)
28
 
29
- # --------------------------
30
- # Separation function
31
- # --------------------------
32
-
33
  def separate_audio(audio_file):
34
- tmp = tempfile.mkdtemp()
35
- separator.separate_to_file(audio_file, tmp, codec="wav")
36
-
37
- base = os.path.splitext(os.path.basename(audio_file))[0]
38
- vocals_path = os.path.join(tmp, base, "vocals.wav")
39
- instr_path = os.path.join(tmp, base, "accompaniment.wav")
 
 
 
 
 
 
 
40
  return vocals_path, instr_path
41
 
42
- # --------------------------
43
- # Gradio Interface
44
- # --------------------------
45
- iface = gr.Interface(
46
- fn=separate_audio,
47
- inputs=gr.Audio(type="filepath"),
48
- outputs=[gr.Audio(), gr.Audio(), gr.Textbox()],
49
- title="Spleeter Vocal/Instrumental Separator"
50
- )
51
-
52
- iface.launch(share=True) # Added public link
 
 
 
 
 
 
 
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)