Pecorized commited on
Commit
eee15fe
·
1 Parent(s): 0328da5
Files changed (1) hide show
  1. app.py +12 -104
app.py CHANGED
@@ -60,79 +60,6 @@
60
 
61
 
62
 
63
- # import os
64
- # import gradio as gr
65
- # from scipy.io.wavfile import write
66
- # import subprocess
67
- # import torch
68
-
69
- # # Assuming audio_separator is available in your environment
70
- # from audio_separator import Separator
71
-
72
- # def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
73
- # # Initially, show the loading GIF
74
- # loading_gif_path = "7RwF.gif"
75
- # gr.Image(loading_gif_path,visible=True)
76
-
77
- # os.makedirs("out", exist_ok=True)
78
- # audio_path = 'test.wav'
79
- # write(audio_path, audio[0], audio[1])
80
- # device = 'cuda' if torch.cuda.is_available() else 'cpu'
81
- # print(f"Using device: {device}")
82
-
83
- # try:
84
- # command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
85
- # process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
86
- # print("Demucs script output:", process.stdout.decode())
87
- # except subprocess.CalledProcessError as e:
88
- # print("Error in Demucs script:", e.stderr.decode())
89
- # return [gr.Audio(visible=False)] * 8 + [loading_gif_path]
90
-
91
- # try:
92
- # separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=device=='cuda', output_format='wav')
93
- # primary_stem_path, secondary_stem_path = separator.separate()
94
- # except Exception as e:
95
- # print("Error in custom separation:", str(e))
96
- # return [gr.Audio(visible=False)] * 8 + [loading_gif_path]
97
-
98
- # stem_paths = {
99
- # "vocals": "./out/htdemucs_6s/test/vocals.wav" if vocals else None,
100
- # "bass": "./out/htdemucs_6s/test/bass.wav" if bass else None,
101
- # "drums": "./out/htdemucs_6s/test/drums.wav" if drums else None,
102
- # "other": "./out/htdemucs_6s/test/other.wav" if other else None,
103
- # "piano": "./out/htdemucs_6s/test/piano.wav" if piano else None,
104
- # "guitar": "./out/htdemucs_6s/test/guitar.wav" if guitar else None,
105
- # "lead_vocals": primary_stem_path if lead_vocals else None,
106
- # "backing_vocals": secondary_stem_path if backing_vocals else None
107
- # }
108
-
109
- # # Once processing is done, hide the GIF by returning a transparent image
110
- # gr.Image(visible=False)
111
- # return [gr.Audio(stem_paths[stem], visible=bool(stem_paths[stem])) for stem in stem_paths]
112
-
113
- # # Define checkboxes for each stem
114
- # checkbox_labels = ["Full Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]
115
- # checkboxes = [gr.components.Checkbox(label=label) for label in checkbox_labels]
116
-
117
- # # Gradio Interface
118
- # title = "Source Separation Demo"
119
- # description = "Music Source Separation in the Waveform Domain. Upload your audio to begin."
120
- # iface = gr.Interface(
121
- # inference,
122
- # [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
123
- # [gr.Audio(label=label, visible=False) for label in checkbox_labels],
124
- # title=title,
125
- # description=description,
126
- # )
127
-
128
- # iface.launch()
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
  import os
137
  import gradio as gr
138
  from scipy.io.wavfile import write
@@ -142,37 +69,31 @@ import torch
142
  # Assuming audio_separator is available in your environment
143
  from audio_separator import Separator
144
 
145
- # Global variable to track the display state of the GIF
146
- show_loading_gif = True
147
-
148
- def start_loading():
149
- global show_loading_gif
150
- show_loading_gif = True
151
 
152
- def stop_loading():
153
- global show_loading_gif
154
- show_loading_gif = False
155
 
156
- def process_audio(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
157
- # Audio processing logic
158
  os.makedirs("out", exist_ok=True)
159
  audio_path = 'test.wav'
160
  write(audio_path, audio[0], audio[1])
161
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
162
 
163
  try:
164
  command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
165
  process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
166
  except subprocess.CalledProcessError as e:
167
- return None # Handle the error appropriately
 
168
 
169
  try:
170
  separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=device=='cuda', output_format='wav')
171
  primary_stem_path, secondary_stem_path = separator.separate()
172
  except Exception as e:
173
- return None # Handle the error appropriately
 
174
 
175
- # Generate paths for the stems
176
  stem_paths = {
177
  "vocals": "./out/htdemucs_6s/test/vocals.wav" if vocals else None,
178
  "bass": "./out/htdemucs_6s/test/bass.wav" if bass else None,
@@ -184,23 +105,9 @@ def process_audio(audio, vocals, bass, drums, other, piano, guitar, lead_vocals,
184
  "backing_vocals": secondary_stem_path if backing_vocals else None
185
  }
186
 
187
- return [stem_paths[stem] for stem in stem_paths]
188
 
189
- def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
190
- global show_loading_gif
191
-
192
- # Start loading
193
- start_loading()
194
-
195
- # Call the main processing function
196
- audio_outputs = process_audio(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals)
197
-
198
- # Stop loading
199
- stop_loading()
200
-
201
- # Return the outputs along with the loading GIF state
202
- loading_gif_path = "7RwF.gif" if show_loading_gif else ""
203
- return loading_gif_path, *audio_outputs
204
 
205
  # Define checkboxes for each stem
206
  checkbox_labels = ["Full Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]
@@ -212,10 +119,11 @@ description = "Music Source Separation in the Waveform Domain. Upload your audio
212
  iface = gr.Interface(
213
  inference,
214
  [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
215
- [gr.Image()] + [gr.Audio(label=label, visible=False) for label in checkbox_labels],
216
  title=title,
217
  description=description,
218
  )
219
 
220
  iface.launch()
221
 
 
 
60
 
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  import os
64
  import gradio as gr
65
  from scipy.io.wavfile import write
 
69
  # Assuming audio_separator is available in your environment
70
  from audio_separator import Separator
71
 
72
+ def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
73
+ status_message = "Processing..."
 
 
 
 
74
 
 
 
 
75
 
 
 
76
  os.makedirs("out", exist_ok=True)
77
  audio_path = 'test.wav'
78
  write(audio_path, audio[0], audio[1])
79
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
80
+ print(f"Using device: {device}")
81
 
82
  try:
83
  command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
84
  process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
85
+ print("Demucs script output:", process.stdout.decode())
86
  except subprocess.CalledProcessError as e:
87
+ print("Error in Demucs script:", e.stderr.decode())
88
+ return [gr.Audio(visible=False)] * 8 + [loading_gif_path]
89
 
90
  try:
91
  separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=device=='cuda', output_format='wav')
92
  primary_stem_path, secondary_stem_path = separator.separate()
93
  except Exception as e:
94
+ print("Error in custom separation:", str(e))
95
+ return [gr.Audio(visible=False)] * 8 + [loading_gif_path]
96
 
 
97
  stem_paths = {
98
  "vocals": "./out/htdemucs_6s/test/vocals.wav" if vocals else None,
99
  "bass": "./out/htdemucs_6s/test/bass.wav" if bass else None,
 
105
  "backing_vocals": secondary_stem_path if backing_vocals else None
106
  }
107
 
108
+ # Once processing is done, hide the GIF by returning a transparent image
109
 
110
+ return [gr.Audio(stem_paths[stem], visible=bool(stem_paths[stem])) for stem in stem_paths], "Done! Successfully processed."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  # Define checkboxes for each stem
113
  checkbox_labels = ["Full Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]
 
119
  iface = gr.Interface(
120
  inference,
121
  [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
122
+ [gr.Audio(label=label, visible=False) for label in checkbox_labels] + [gr.Label()],
123
  title=title,
124
  description=description,
125
  )
126
 
127
  iface.launch()
128
 
129
+