Spaces:
Sleeping
Sleeping
Commit
·
76c5192
1
Parent(s):
f9457b6
no more duration params
Browse files
app.py
CHANGED
|
@@ -18,8 +18,21 @@ def preprocess_audio(waveform):
|
|
| 18 |
waveform_np = waveform.cpu().squeeze().numpy()
|
| 19 |
return torch.from_numpy(waveform_np).unsqueeze(0).to(device)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# @spaces.GPU(duration=10)
|
| 22 |
-
# def generate_drum_sample()
|
| 23 |
# model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 24 |
# model.set_generation_params(duration=10)
|
| 25 |
# wav = model.generate_unconditional(1).squeeze(0)
|
|
@@ -31,140 +44,140 @@ def preprocess_audio(waveform):
|
|
| 31 |
|
| 32 |
# return filename_with_extension
|
| 33 |
|
| 34 |
-
@spaces.GPU(duration=10)
|
| 35 |
-
def continue_drum_sample(existing_audio_path):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
@spaces.GPU(duration=120)
|
| 73 |
-
def generate_music(wav_filename, prompt_duration, musicgen_model, output_duration):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
|
| 107 |
-
@spaces.GPU(duration=120)
|
| 108 |
-
def continue_music(input_audio_path, prompt_duration, musicgen_model, output_duration):
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
|
| 169 |
# Define the expandable sections (keeping your existing content)
|
| 170 |
musicgen_micro_blurb = """
|
|
@@ -266,9 +279,9 @@ with gr.Blocks() as iface:
|
|
| 266 |
|
| 267 |
# Connecting the components
|
| 268 |
# generate_button.click(generate_drum_sample, outputs=[drum_audio])
|
| 269 |
-
continue_drum_sample_button.click(continue_drum_sample, inputs=[drum_audio], outputs=[drum_audio])
|
| 270 |
-
generate_music_button.click(generate_music, inputs=[drum_audio, prompt_duration, musicgen_model, output_duration], outputs=[output_audio])
|
| 271 |
-
continue_button.click(continue_music, inputs=[output_audio, prompt_duration, musicgen_model, output_duration], outputs=continue_output_audio)
|
| 272 |
|
| 273 |
if __name__ == "__main__":
|
| 274 |
iface.launch()
|
|
|
|
| 18 |
waveform_np = waveform.cpu().squeeze().numpy()
|
| 19 |
return torch.from_numpy(waveform_np).unsqueeze(0).to(device)
|
| 20 |
|
| 21 |
+
@spaces.GPU
|
| 22 |
+
def generate_drum_sample() -> str:
|
| 23 |
+
model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 24 |
+
model.set_generation_params(duration=10)
|
| 25 |
+
wav = model.generate_unconditional(1).squeeze(0)
|
| 26 |
+
|
| 27 |
+
filename_without_extension = f'jungle'
|
| 28 |
+
filename_with_extension = f'{filename_without_extension}.wav'
|
| 29 |
+
|
| 30 |
+
audio_write(filename_without_extension, wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 31 |
+
|
| 32 |
+
return filename_with_extension
|
| 33 |
+
|
| 34 |
# @spaces.GPU(duration=10)
|
| 35 |
+
# def generate_drum_sample():
|
| 36 |
# model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 37 |
# model.set_generation_params(duration=10)
|
| 38 |
# wav = model.generate_unconditional(1).squeeze(0)
|
|
|
|
| 44 |
|
| 45 |
# return filename_with_extension
|
| 46 |
|
| 47 |
+
# @spaces.GPU(duration=10)
|
| 48 |
+
# def continue_drum_sample(existing_audio_path):
|
| 49 |
+
# if existing_audio_path is None:
|
| 50 |
+
# return None
|
| 51 |
|
| 52 |
+
# existing_audio, sr = torchaudio.load(existing_audio_path)
|
| 53 |
+
# existing_audio = existing_audio.to(device)
|
| 54 |
|
| 55 |
+
# prompt_duration = 2
|
| 56 |
+
# output_duration = 10
|
| 57 |
|
| 58 |
+
# num_samples = int(prompt_duration * sr)
|
| 59 |
+
# if existing_audio.shape[1] < num_samples:
|
| 60 |
+
# raise ValueError("The existing audio is too short for the specified prompt duration.")
|
| 61 |
|
| 62 |
+
# start_sample = existing_audio.shape[1] - num_samples
|
| 63 |
+
# prompt_waveform = existing_audio[..., start_sample:]
|
| 64 |
|
| 65 |
+
# model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 66 |
+
# model.set_generation_params(duration=output_duration)
|
| 67 |
|
| 68 |
+
# output = model.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 69 |
+
# output = output.to(device)
|
| 70 |
|
| 71 |
+
# if output.dim() == 3:
|
| 72 |
+
# output = output.squeeze(0)
|
| 73 |
|
| 74 |
+
# if output.dim() == 1:
|
| 75 |
+
# output = output.unsqueeze(0)
|
| 76 |
|
| 77 |
+
# combined_audio = torch.cat((existing_audio, output), dim=1)
|
| 78 |
+
# combined_audio = combined_audio.cpu()
|
| 79 |
|
| 80 |
+
# combined_file_path = f'./continued_jungle_{random.randint(1000, 9999)}.wav'
|
| 81 |
+
# torchaudio.save(combined_file_path, combined_audio, sr)
|
| 82 |
|
| 83 |
+
# return combined_file_path
|
| 84 |
|
| 85 |
+
# @spaces.GPU(duration=120)
|
| 86 |
+
# def generate_music(wav_filename, prompt_duration, musicgen_model, output_duration):
|
| 87 |
+
# if wav_filename is None:
|
| 88 |
+
# return None
|
| 89 |
|
| 90 |
+
# song, sr = torchaudio.load(wav_filename)
|
| 91 |
+
# song = song.to(device)
|
| 92 |
+
|
| 93 |
+
# model_name = musicgen_model.split(" ")[0]
|
| 94 |
+
# model_continue = MusicGen.get_pretrained(model_name)
|
| 95 |
+
|
| 96 |
+
# model_continue.set_generation_params(
|
| 97 |
+
# use_sampling=True,
|
| 98 |
+
# top_k=250,
|
| 99 |
+
# top_p=0.0,
|
| 100 |
+
# temperature=1.0,
|
| 101 |
+
# duration=output_duration,
|
| 102 |
+
# cfg_coef=3
|
| 103 |
+
# )
|
| 104 |
+
|
| 105 |
+
# prompt_waveform = song[..., :int(prompt_duration * sr)]
|
| 106 |
+
# prompt_waveform = preprocess_audio(prompt_waveform)
|
| 107 |
|
| 108 |
+
# output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 109 |
+
# output = output.cpu()
|
| 110 |
|
| 111 |
+
# if len(output.size()) > 2:
|
| 112 |
+
# output = output.squeeze()
|
| 113 |
|
| 114 |
+
# filename_without_extension = f'continued_music'
|
| 115 |
+
# filename_with_extension = f'{filename_without_extension}.wav'
|
| 116 |
+
# audio_write(filename_without_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 117 |
|
| 118 |
+
# return filename_with_extension
|
| 119 |
|
| 120 |
+
# @spaces.GPU(duration=120)
|
| 121 |
+
# def continue_music(input_audio_path, prompt_duration, musicgen_model, output_duration):
|
| 122 |
+
# if input_audio_path is None:
|
| 123 |
+
# return None
|
| 124 |
|
| 125 |
+
# song, sr = torchaudio.load(input_audio_path)
|
| 126 |
+
# song = song.to(device)
|
| 127 |
|
| 128 |
+
# model_continue = MusicGen.get_pretrained(musicgen_model.split(" ")[0])
|
| 129 |
+
# model_continue.set_generation_params(
|
| 130 |
+
# use_sampling=True,
|
| 131 |
+
# top_k=250,
|
| 132 |
+
# top_p=0.0,
|
| 133 |
+
# temperature=1.0,
|
| 134 |
+
# duration=output_duration,
|
| 135 |
+
# cfg_coef=3
|
| 136 |
+
# )
|
| 137 |
|
| 138 |
+
# original_audio = AudioSegment.from_mp3(input_audio_path)
|
| 139 |
+
# current_audio = original_audio
|
| 140 |
|
| 141 |
+
# file_paths_for_cleanup = []
|
| 142 |
|
| 143 |
+
# for i in range(1):
|
| 144 |
+
# num_samples = int(prompt_duration * sr)
|
| 145 |
+
# if current_audio.duration_seconds * 1000 < prompt_duration * 1000:
|
| 146 |
+
# raise ValueError("The prompt_duration is longer than the current audio length.")
|
| 147 |
|
| 148 |
+
# start_time = current_audio.duration_seconds * 1000 - prompt_duration * 1000
|
| 149 |
+
# prompt_audio = current_audio[start_time:]
|
| 150 |
|
| 151 |
+
# prompt_bytes = prompt_audio.export(format="wav").read()
|
| 152 |
+
# prompt_waveform, _ = torchaudio.load(io.BytesIO(prompt_bytes))
|
| 153 |
+
# prompt_waveform = prompt_waveform.to(device)
|
| 154 |
|
| 155 |
+
# prompt_waveform = preprocess_audio(prompt_waveform)
|
| 156 |
|
| 157 |
+
# output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 158 |
+
# output = output.cpu()
|
| 159 |
|
| 160 |
+
# if len(output.size()) > 2:
|
| 161 |
+
# output = output.squeeze()
|
| 162 |
|
| 163 |
+
# filename_without_extension = f'continue_{i}'
|
| 164 |
+
# filename_with_extension = f'{filename_without_extension}.wav'
|
| 165 |
+
# correct_filename_extension = f'{filename_without_extension}.wav.wav'
|
| 166 |
|
| 167 |
+
# audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 168 |
+
# generated_audio_segment = AudioSegment.from_wav(correct_filename_extension)
|
| 169 |
|
| 170 |
+
# current_audio = current_audio[:start_time] + generated_audio_segment
|
| 171 |
|
| 172 |
+
# file_paths_for_cleanup.append(correct_filename_extension)
|
| 173 |
|
| 174 |
+
# combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
|
| 175 |
+
# current_audio.export(combined_audio_filename, format="mp3")
|
| 176 |
|
| 177 |
+
# for file_path in file_paths_for_cleanup:
|
| 178 |
+
# os.remove(file_path)
|
| 179 |
|
| 180 |
+
# return combined_audio_filename
|
| 181 |
|
| 182 |
# Define the expandable sections (keeping your existing content)
|
| 183 |
musicgen_micro_blurb = """
|
|
|
|
| 279 |
|
| 280 |
# Connecting the components
|
| 281 |
# generate_button.click(generate_drum_sample, outputs=[drum_audio])
|
| 282 |
+
# continue_drum_sample_button.click(continue_drum_sample, inputs=[drum_audio], outputs=[drum_audio])
|
| 283 |
+
# generate_music_button.click(generate_music, inputs=[drum_audio, prompt_duration, musicgen_model, output_duration], outputs=[output_audio])
|
| 284 |
+
# continue_button.click(continue_music, inputs=[output_audio, prompt_duration, musicgen_model, output_duration], outputs=continue_output_audio)
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
| 287 |
iface.launch()
|