thecollabagepatch commited on
Commit
3c1e68c
1 Parent(s): 40a916f

gary on gary

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -11,6 +11,7 @@ from audiocraft.data.audio import audio_write
11
  from pydub import AudioSegment
12
  import spaces
13
  import tempfile
 
14
 
15
  # Check if CUDA is available
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -198,7 +199,7 @@ def continue_music(input_audio_path, prompt_duration, musicgen_model, num_iterat
198
  # Prepare the audio slice for generation
199
  prompt_waveform = preprocess_audio(prompt_waveform)
200
 
201
- # Load the model and set generation parameters as before
202
  model_continue = MusicGen.get_pretrained(musicgen_model.split(" ")[0])
203
  model_continue.set_generation_params(
204
  use_sampling=True,
@@ -209,32 +210,39 @@ def continue_music(input_audio_path, prompt_duration, musicgen_model, num_iterat
209
  cfg_coef=3
210
  )
211
 
212
- all_audio_files = []
 
 
 
213
  for i in range(num_iterations):
214
  output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
215
- output = output.cpu() # Ensure the output is on CPU for further processing
216
  if len(output.size()) > 2:
217
  output = output.squeeze()
218
 
219
  filename_without_extension = f'continue_{i}'
220
  filename_with_extension = f'{filename_without_extension}.wav'
 
 
221
  audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
222
- all_audio_files.append(filename_with_extension)
 
 
223
 
224
- # Combine all audio files as before
225
- combined_audio = AudioSegment.empty()
226
- for filename in all_audio_files:
227
- combined_audio += AudioSegment.from_wav(filename)
228
 
229
  combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
230
  combined_audio.export(combined_audio_filename, format="mp3")
231
 
232
- # Clean up temporary files
233
- for filename in all_audio_files:
234
- os.remove(filename)
235
 
236
  return combined_audio_filename
237
 
 
 
238
  # Define the expandable sections
239
  musiclang_blurb = """
240
  ## musiclang
@@ -289,9 +297,9 @@ with gr.Blocks() as iface:
289
  ], value="thepatch/vanya_ai_dnb_0.1 (small)")
290
  num_iterations = gr.Slider(label="Number of Iterations", minimum=1, maximum=3, step=1, value=3)
291
  generate_music_button = gr.Button("Generate Music")
292
- output_audio = gr.Audio(label="Generated Music")
293
  continue_button = gr.Button("Continue Generating Music")
294
- continue_output_audio = gr.Audio(label="Continued Music Output")
295
 
296
  # Connecting the components
297
  generate_midi_button.click(generate_midi, inputs=[seed, use_chords, chord_progression, bpm], outputs=[midi_audio])
 
11
  from pydub import AudioSegment
12
  import spaces
13
  import tempfile
14
+ from pydub import AudioSegment
15
 
16
  # Check if CUDA is available
17
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
199
  # Prepare the audio slice for generation
200
  prompt_waveform = preprocess_audio(prompt_waveform)
201
 
202
+ # Load the model and set generation parameters
203
  model_continue = MusicGen.get_pretrained(musicgen_model.split(" ")[0])
204
  model_continue.set_generation_params(
205
  use_sampling=True,
 
210
  cfg_coef=3
211
  )
212
 
213
+ original_audio = AudioSegment.from_mp3(input_audio_path)
214
+ all_audio_files = [original_audio] # Start with the original audio
215
+ file_paths_for_cleanup = [] # List to track generated file paths for cleanup
216
+
217
  for i in range(num_iterations):
218
  output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
219
+ output = output.cpu() # Move the output tensor back to CPU
220
  if len(output.size()) > 2:
221
  output = output.squeeze()
222
 
223
  filename_without_extension = f'continue_{i}'
224
  filename_with_extension = f'{filename_without_extension}.wav'
225
+ correct_filename_extension = f'{filename_without_extension}.wav.wav' # Apply the workaround for audio_write
226
+
227
  audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
228
+ new_audio_segment = AudioSegment.from_wav(correct_filename_extension)
229
+ all_audio_files.append(new_audio_segment)
230
+ file_paths_for_cleanup.append(correct_filename_extension) # Add to cleanup list
231
 
232
+ # Combine all audio files into one continuous segment
233
+ combined_audio = sum(all_audio_files)
 
 
234
 
235
  combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
236
  combined_audio.export(combined_audio_filename, format="mp3")
237
 
238
+ # Clean up temporary files using the list of file paths
239
+ for file_path in file_paths_for_cleanup:
240
+ os.remove(file_path)
241
 
242
  return combined_audio_filename
243
 
244
+
245
+
246
  # Define the expandable sections
247
  musiclang_blurb = """
248
  ## musiclang
 
297
  ], value="thepatch/vanya_ai_dnb_0.1 (small)")
298
  num_iterations = gr.Slider(label="Number of Iterations", minimum=1, maximum=3, step=1, value=3)
299
  generate_music_button = gr.Button("Generate Music")
300
+ output_audio = gr.Audio(label="Generated Music", type="filepath")
301
  continue_button = gr.Button("Continue Generating Music")
302
+ continue_output_audio = gr.Audio(label="Continued Music Output", type="filepath")
303
 
304
  # Connecting the components
305
  generate_midi_button.click(generate_midi, inputs=[seed, use_chords, chord_progression, bpm], outputs=[midi_audio])