ray-006 commited on
Commit
10494c5
·
verified ·
1 Parent(s): 6ff1427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -2,82 +2,86 @@ import os
2
  import gradio as gr
3
  from audio_separator.separator import Separator
4
  import tempfile
5
- import shutil
6
 
7
- def separate_audio(input_file, model_name):
 
 
 
 
 
 
 
 
 
 
8
  if input_file is None:
9
  return [None] * 6
10
 
11
- # Create a temporary directory for this specific session
 
12
  temp_dir = tempfile.mkdtemp()
13
 
14
- # Initialize separator with the temp directory
15
  separator = Separator(output_dir=temp_dir)
16
- separator.load_model(model_filename=model_name)
17
 
18
- # Run separation
19
- # The library returns a list of filenames created in temp_dir
20
- output_files = separator.separate(input_file)
 
 
 
21
 
22
- # Map the generated files to the correct output slots
23
- # htdemucs_6s usually names files as: [input]_(Instrument).wav
 
 
 
 
24
  stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
25
  results = []
26
 
27
  for stem in stems:
28
- # Find the file in the temp directory that matches the stem name
29
- match = next((os.path.join(temp_dir, f) for f in output_files if stem in f), None)
 
30
  results.append(match)
31
 
 
32
  return tuple(results)
33
 
34
  # --- Gradio Interface ---
35
  with gr.Blocks() as demo:
36
- gr.Markdown("""
37
- # 🎵 AI 6-Stem Audio Separator
38
- Split your music into high-quality individual tracks using the **Demucs 6s** model.
39
- """)
40
 
41
  with gr.Row():
42
  with gr.Column(scale=1):
43
- input_audio = gr.Audio(
44
- label="Upload Audio File",
45
- type="filepath",
46
- interactive=True
47
- )
48
  model_dropdown = gr.Dropdown(
49
  choices=["htdemucs_6s.yaml"],
50
  value="htdemucs_6s.yaml",
51
  label="Select Model"
52
- )
53
- separate_btn = gr.Button("🚀 Separate Stems", variant="primary")
54
 
55
  with gr.Column(scale=2):
56
  with gr.Group():
57
- gr.Markdown("### 🔊 Separation Results")
58
  with gr.Row():
59
- output_vocals = gr.Audio(label="🎤 Vocals", type="filepath")
60
- output_drums = gr.Audio(label="🥁 Drums", type="filepath")
61
  with gr.Row():
62
- output_bass = gr.Audio(label="🎸 Bass", type="filepath")
63
- output_guitar = gr.Audio(label="🎸 Guitar", type="filepath")
64
  with gr.Row():
65
- output_piano = gr.Audio(label="🎹 Piano", type="filepath")
66
- output_other = gr.Audio(label="🎼 Other", type="filepath")
67
 
68
- # Action binding
69
  separate_btn.click(
70
  fn=separate_audio,
71
  inputs=[input_audio, model_dropdown],
72
- outputs=[
73
- output_vocals, output_drums, output_bass,
74
- output_other, output_guitar, output_piano
75
- ]
76
  )
77
 
78
- gr.Markdown("---")
79
- gr.Markdown("Note: Processing may take 1-2 minutes depending on file length and Space hardware.")
80
-
81
- # For Hugging Face Spaces, we don't use share=True
82
  if __name__ == "__main__":
83
- demo.launch(ssr_mode=False, theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"))
 
2
  import gradio as gr
3
  from audio_separator.separator import Separator
4
  import tempfile
5
+ import json
6
 
7
+ # Define your custom output names
8
+ output_names = {
9
+ "Vocals": "vocals_output",
10
+ "Drums": "drums_output",
11
+ "Bass": "bass_output",
12
+ "Other": "other_output",
13
+ "Guitar": "guitar_output",
14
+ "Piano": "piano_output",
15
+ }
16
+
17
+ def separate_audio(input_file, model_name, progress=gr.Progress(track_tqdm=True)):
18
  if input_file is None:
19
  return [None] * 6
20
 
21
+ # 1. Initialization Status
22
+ [cite_start]progress(0, desc="Initializing Separator...") [cite: 1]
23
  temp_dir = tempfile.mkdtemp()
24
 
25
+ # Initialize separator
26
  separator = Separator(output_dir=temp_dir)
 
27
 
28
+ # 2. Loading Model Status
29
+ [cite_start]progress(0.2, desc=f"Loading model: {model_name}...") [cite: 1]
30
+ [cite_start]separator.load_model(model_filename=model_name) [cite: 1]
31
+
32
+ # 3. Separation Status
33
+ [cite_start]progress(0.4, desc="Separating stems (this may take a minute)...") [cite: 2]
34
 
35
+ # Pass the custom output names to the separator
36
+ # The library expects a dictionary for custom_output_names
37
+ [cite_start]output_files = separator.separate(input_file, custom_output_names=output_names) [cite: 2]
38
+
39
+ # 4. Processing Results Status
40
+ [cite_start]progress(0.9, desc="Finalizing files...") [cite: 3]
41
  stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
42
  results = []
43
 
44
  for stem in stems:
45
+ # Since we renamed them, we look for the custom name in the output files
46
+ target_name = output_names[stem]
47
+ [cite_start]match = next((os.path.join(temp_dir, f) for f in output_files if target_name in f), None) [cite: 3]
48
  results.append(match)
49
 
50
+ progress(1.0, desc="Done!")
51
  return tuple(results)
52
 
53
  # --- Gradio Interface ---
54
  with gr.Blocks() as demo:
55
+ [cite_start]gr.Markdown("# 🎵 AI 6-Stem Audio Separator") [cite: 4]
 
 
 
56
 
57
  with gr.Row():
58
  with gr.Column(scale=1):
59
+ [cite_start]input_audio = gr.Audio(label="Upload Audio File", type="filepath", interactive=True) [cite: 4]
 
 
 
 
60
  model_dropdown = gr.Dropdown(
61
  choices=["htdemucs_6s.yaml"],
62
  value="htdemucs_6s.yaml",
63
  label="Select Model"
64
+ [cite_start]) [cite: 5]
65
+ [cite_start]separate_btn = gr.Button("🚀 Separate Stems", variant="primary") [cite: 5]
66
 
67
  with gr.Column(scale=2):
68
  with gr.Group():
69
+ [cite_start]gr.Markdown("### 🔊 Separation Results") [cite: 6]
70
  with gr.Row():
71
+ [cite_start]output_vocals = gr.Audio(label="🎤 Vocals", type="filepath") [cite: 6]
72
+ [cite_start]output_drums = gr.Audio(label="🥁 Drums", type="filepath") [cite: 7]
73
  with gr.Row():
74
+ [cite_start]output_bass = gr.Audio(label="🎸 Bass", type="filepath") [cite: 7]
75
+ [cite_start]output_guitar = gr.Audio(label="🎸 Guitar", type="filepath") [cite: 7]
76
  with gr.Row():
77
+ [cite_start]output_piano = gr.Audio(label="🎹 Piano", type="filepath") [cite: 8]
78
+ [cite_start]output_other = gr.Audio(label="🎼 Other", type="filepath") [cite: 8]
79
 
 
80
  separate_btn.click(
81
  fn=separate_audio,
82
  inputs=[input_audio, model_dropdown],
83
+ [cite_start]outputs=[output_vocals, output_drums, output_bass, output_other, output_guitar, output_piano] [cite: 9]
 
 
 
84
  )
85
 
 
 
 
 
86
  if __name__ == "__main__":
87
+ demo.launch()