nakas commited on
Commit
25a0e09
·
1 Parent(s): 49b009f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -6
app.py CHANGED
@@ -11,6 +11,8 @@ from concurrent.futures import Future
11
  import torch
12
  import gradio as gr
13
  import pydub
 
 
14
 
15
  from audiocraft.data.audio_utils import convert_audio
16
  from audiocraft.data.audio import audio_write
@@ -119,12 +121,10 @@ def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
119
  audio_write(
120
  file.name, output, MODEL.sample_rate, strategy="loudness",
121
  loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
122
- out_files.append(file.name) # Store the filename as a string
123
  file_cleaner.add(file.name)
124
- res = [out_file for out_file in out_files]
125
  for file in res:
126
- if isinstance(file, Future): # Check if it's a Future object
127
- file = file.result() # Extract the filename from the Future object
128
  file_cleaner.add(file)
129
  print("batch finished", len(texts), time.time() - be)
130
  print("Tempfiles currently stored: ", len(file_cleaner.files))
@@ -171,6 +171,28 @@ def toggle_audio_src(choice):
171
  return gr.update(source="upload", value=None, label="File")
172
 
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  def ui_full(launch_kwargs):
175
  with gr.Blocks() as interface:
176
  gr.Markdown(
@@ -193,6 +215,7 @@ def ui_full(launch_kwargs):
193
  with gr.Row():
194
  submit = gr.Button("Submit")
195
  # Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license.
 
196
  with gr.Row():
197
  model = gr.Radio(["melody", "medium", "small", "large"],
198
  label="Model", value="melody", interactive=True)
@@ -204,7 +227,7 @@ def ui_full(launch_kwargs):
204
  temperature = gr.Number(label="Temperature", value=1.0, interactive=True)
205
  cfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, interactive=True)
206
  with gr.Column():
207
- output = gr.Audio(label="Generated Music")
208
  submit.click(predict_full,
209
  inputs=[model, text, melody, duration, topk, topp, temperature, cfg_coef],
210
  outputs=[output])
@@ -298,7 +321,7 @@ def ui_batched(launch_kwargs):
298
  with gr.Row():
299
  submit = gr.Button("Generate")
300
  with gr.Column():
301
- output = gr.Audio(label="Generated Music")
302
  submit.click(predict_batched, inputs=[text, melody],
303
  outputs=[output], batch=True, max_batch_size=MAX_BATCH_SIZE)
304
  radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)
@@ -343,6 +366,46 @@ def ui_batched(launch_kwargs):
343
  demo.queue(max_size=8 * 4).launch(**launch_kwargs)
344
 
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  if __name__ == "__main__":
347
  parser = argparse.ArgumentParser()
348
  parser.add_argument(
 
11
  import torch
12
  import gradio as gr
13
  import pydub
14
+ from scipy.io.wavfile import write
15
+ import subprocess
16
 
17
  from audiocraft.data.audio_utils import convert_audio
18
  from audiocraft.data.audio import audio_write
 
121
  audio_write(
122
  file.name, output, MODEL.sample_rate, strategy="loudness",
123
  loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
124
+ out_files.append(pool.submit(make_waveform, file.name))
125
  file_cleaner.add(file.name)
126
+ res = [out_file.result() for out_file in out_files]
127
  for file in res:
 
 
128
  file_cleaner.add(file)
129
  print("batch finished", len(texts), time.time() - be)
130
  print("Tempfiles currently stored: ", len(file_cleaner.files))
 
171
  return gr.update(source="upload", value=None, label="File")
172
 
173
 
174
+ def inference(audio):
175
+ os.makedirs("out", exist_ok=True)
176
+ write('test.wav', audio[0], audio[1])
177
+
178
+ command = "python3 -m demucs.separate -n mdx_extra_q -d cpu test.wav -o out"
179
+ process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
180
+ print("Demucs script output:", process.stdout.decode())
181
+
182
+ # Check if files exist before returning
183
+ files = ["./out/mdx_extra_q/test/vocals.wav",
184
+ "./out/mdx_extra_q/test/bass.wav",
185
+ "./out/mdx_extra_q/test/drums.wav",
186
+ "./out/mdx_extra_q/test/other.wav"]
187
+ for file in files:
188
+ if not os.path.isfile(file):
189
+ print(f"File not found: {file}")
190
+ else:
191
+ print(f"File exists: {file}")
192
+
193
+ return files
194
+
195
+
196
  def ui_full(launch_kwargs):
197
  with gr.Blocks() as interface:
198
  gr.Markdown(
 
215
  with gr.Row():
216
  submit = gr.Button("Submit")
217
  # Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license.
218
+ _ = gr.Button("Interrupt").click(fn=interrupt, queue=False)
219
  with gr.Row():
220
  model = gr.Radio(["melody", "medium", "small", "large"],
221
  label="Model", value="melody", interactive=True)
 
227
  temperature = gr.Number(label="Temperature", value=1.0, interactive=True)
228
  cfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, interactive=True)
229
  with gr.Column():
230
+ output = gr.Video(label="Generated Music")
231
  submit.click(predict_full,
232
  inputs=[model, text, melody, duration, topk, topp, temperature, cfg_coef],
233
  outputs=[output])
 
321
  with gr.Row():
322
  submit = gr.Button("Generate")
323
  with gr.Column():
324
+ output = gr.Video(label="Generated Music")
325
  submit.click(predict_batched, inputs=[text, melody],
326
  outputs=[output], batch=True, max_batch_size=MAX_BATCH_SIZE)
327
  radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)
 
366
  demo.queue(max_size=8 * 4).launch(**launch_kwargs)
367
 
368
 
369
+ def convert_to_audio(file_path):
370
+ sound = pydub.AudioSegment.from_file(file_path)
371
+ samples = sound.get_array_of_samples()
372
+ sample_rate = sound.frame_rate
373
+ audio = np.array(samples).reshape((-1, 2))
374
+ audio = audio.T / 32768.0
375
+ return audio, sample_rate
376
+
377
+
378
+ def separate_audio(source_file_path, target_folder_path):
379
+ command = f"python3 -m demucs.separate -n demucs_extra -d cpu {source_file_path} -o {target_folder_path}"
380
+ subprocess.call(command, shell=True)
381
+
382
+
383
+ def separate_music_audio(audio_path):
384
+ audio, sample_rate = convert_to_audio(audio_path)
385
+ audio_file_name = os.path.basename(audio_path)
386
+ temp_dir = f"./temp/{audio_file_name}"
387
+ os.makedirs(temp_dir, exist_ok=True)
388
+ temp_audio_path = os.path.join(temp_dir, audio_file_name)
389
+ write(temp_audio_path, sample_rate, audio.T)
390
+ separate_audio(temp_audio_path, temp_dir)
391
+ return temp_dir
392
+
393
+
394
+ def postprocess_audio(audio_dir):
395
+ separated_audio_files = {}
396
+ for file in os.listdir(audio_dir):
397
+ if file.endswith(".wav"):
398
+ instrument = file.split(".")[0]
399
+ separated_audio_files[instrument] = os.path.join(audio_dir, file)
400
+ return separated_audio_files
401
+
402
+
403
+ def separate_music(audio_path):
404
+ audio_dir = separate_music_audio(audio_path)
405
+ separated_audio_files = postprocess_audio(audio_dir)
406
+ return separated_audio_files
407
+
408
+
409
  if __name__ == "__main__":
410
  parser = argparse.ArgumentParser()
411
  parser.add_argument(