Spaces:
Runtime error
Runtime error
kind of working
Browse filesFiles changed (2)
- app.py+12-6
- audiocraft/models/musicgen.py+19-6
58 unmodified lines59606162636411 unmodified lines7677787980818227 unmodified lines11011111211311411511611711811912012116 unmodified lines13813914014114214314439 unmodified lines18418518618718818919058 unmodified linesdef _do_predictions(texts, melodies, duration, **gen_kwargs):MODEL.set_generation_params(duration=duration, **gen_kwargs)print("new batch", len(texts), texts, [None if m is None else (m[0], m[1].shape) for m in melodies])be = time.time()11 unmodified linesmelody = convert_audio(melody, sr, target_sr, target_ac)processed_melodies.append(melody)if processed_melodies.any():outputs = MODEL.generate_with_chroma(descriptions=texts,melody_wavs=processed_melodies,27 unmodified linesdef predict_full(model, text, melody, duration, topk, topp, temperature, cfg_coef):topk = int(topk)load_model(model)if duration > MODEL.lm.cfg.dataset.segment_duration:raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")outs = _do_predictions([text], [melody], duration,topk=topk, top_p=topp, temperature=temperature, cfg_coef=cfg_coef)return outs[0]16 unmodified lineswith gr.Row():model = gr.Radio(["melody", "medium", "small", "large"], label="Model", value="melody", interactive=True)with gr.Row():duration = gr.Slider(minimum=1, maximum=30, value=10, label="Duration", interactive=True)with gr.Row():topk = gr.Number(label="Top-k", value=250, interactive=True)topp = gr.Number(label="Top-p", value=0, interactive=True)39 unmodified lines### More detailsThe model will generate a short music extract based on the description you provided.You can generate up to 30 seconds of audio.We present 4 model variations:1. Melody -- a music generation model capable of generating music condition on text and melody inputs. **Note**, you can also use text only.58 unmodified lines59606162636465666711 unmodified lines7980818283848527 unmodified lines11311411511611711811912012112216 unmodified lines13914014114214314414539 unmodified lines18518618718818919019119219319419519658 unmodified linesdef _do_predictions(texts, melodies, duration, **gen_kwargs):if duration > MODEL.lm.cfg.dataset.segment_duration:raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")MODEL.set_generation_params(duration=duration, **gen_kwargs)print("new batch", len(texts), texts, [None if m is None else (m[0], m[1].shape) for m in melodies])be = time.time()11 unmodified linesmelody = convert_audio(melody, sr, target_sr, target_ac)processed_melodies.append(melody)if any(m is not None for m in processed_melodies):outputs = MODEL.generate_with_chroma(descriptions=texts,melody_wavs=processed_melodies,27 unmodified linesdef predict_full(model, text, melody, duration, topk, topp, temperature, cfg_coef):topk = int(topk)load_model(model)outs = _do_predictions([text], [melody], duration,top_k=topk, top_p=topp, temperature=temperature, cfg_coef=cfg_coef)return outs[0]16 unmodified lineswith gr.Row():model = gr.Radio(["melody", "medium", "small", "large"], label="Model", value="melody", interactive=True)with gr.Row():duration = gr.Slider(minimum=1, maximum=120, value=10, label="Duration", interactive=True)with gr.Row():topk = gr.Number(label="Top-k", value=250, interactive=True)topp = gr.Number(label="Top-p", value=0, interactive=True)39 unmodified lines### More detailsThe model will generate a short music extract based on the description you provided.The model can generate up to 30 seconds of audio in one pass. It is now possibleto extend the generation by feeding back the end of the previous chunk of audio.This can take a long time, and the model might lose consistency. The model might alsodecide at arbitrary positions that the song ends.**WARNING:** Choosing long durations will take a long time to generate (2min might take ~10min).We present 4 model variations:1. Melody -- a music generation model capable of generating music condition on text and melody inputs. **Note**, you can also use text only.
44 unmodified lines45464748495076 unmodified lines127128129130131132141 unmodified lines27427527627727827916 unmodified lines29629729829930030130230330430530614 unmodified lines32132232332432532632732832933033133233333444 unmodified linesself.device = next(iter(lm.parameters())).deviceself.generation_params: dict = {}self.set_generation_params(duration=15) # 15 seconds by defaultif self.device.type == 'cpu':self.autocast = TorchAutocast(enabled=False)else:76 unmodified lines'two_step_cfg': two_step_cfg,}def generate_unconditional(self, num_samples: int, progress: bool = False) -> torch.Tensor:"""Generate samples in an unconditional manner.141 unmodified linescurrent_gen_offset: int = 0def _progress_callback(generated_tokens: int, tokens_to_generate: int):print(f'{current_gen_offset + generated_tokens: 6d} / {total_gen_len: 6d}', end='\r')if prompt_tokens is not None:16 unmodified lines# melody conditioning etc.ref_wavs = [attr.wav['self_wav'] for attr in attributes]all_tokens = []if prompt_tokens is not None:all_tokens.append(prompt_tokens)time_offset = 0.while time_offset < self.duration:chunk_duration = min(self.duration - time_offset, self.max_duration)max_gen_len = int(chunk_duration * self.frame_rate)for attr, ref_wav in zip(attributes, ref_wavs):14 unmodified linesgen_tokens = self.lm.generate(prompt_tokens, attributes,callback=callback, max_gen_len=max_gen_len, **self.generation_params)stride_tokens = int(self.frame_rate * self.extend_stride)if prompt_tokens is None:all_tokens.append(gen_tokens)else:all_tokens.append(gen_tokens[:, :, prompt_tokens.shape[-1]:])prompt_tokens = gen_tokens[:, :, stride_tokens]current_gen_offset += stride_tokenstime_offset += self.extend_stridegen_tokens = torch.cat(all_tokens, dim=-1)44 unmodified lines4546474849505176 unmodified lines128129130131132133134135136141 unmodified lines27827928028128228328428528628716 unmodified lines30430530630730830931031131231331431531631731831932014 unmodified lines33533633733833934034134234334434534634744 unmodified linesself.device = next(iter(lm.parameters())).deviceself.generation_params: dict = {}self.set_generation_params(duration=15) # 15 seconds by defaultself._progress_callback: tp.Optional[tp.Callable[[int, int], None]] = Noneif self.device.type == 'cpu':self.autocast = TorchAutocast(enabled=False)else:76 unmodified lines'two_step_cfg': two_step_cfg,}def set_custom_progress_callback(self, progress_callback: tp.Optional[tp.Callable[[int, int], None]] = None):self._progress_callback = progress_callbackdef generate_unconditional(self, num_samples: int, progress: bool = False) -> torch.Tensor:"""Generate samples in an unconditional manner.141 unmodified linescurrent_gen_offset: int = 0def _progress_callback(generated_tokens: int, tokens_to_generate: int):generated_tokens += current_gen_offsetif self._progress_callback is not None:self._progress_callback(generated_tokens, total_gen_len)else:print(f'{current_gen_offset + generated_tokens: 6d} / {total_gen_len: 6d}', end='\r')if prompt_tokens is not None:16 unmodified lines# melody conditioning etc.ref_wavs = [attr.wav['self_wav'] for attr in attributes]all_tokens = []if prompt_tokens is None:prompt_length = 0else:all_tokens.append(prompt_tokens)prompt_length = prompt_tokens.shape[-1]stride_tokens = int(self.frame_rate * self.extend_stride)while current_gen_offset + prompt_length < total_gen_len:time_offset = current_gen_offset / self.frame_ratechunk_duration = min(self.duration - time_offset, self.max_duration)max_gen_len = int(chunk_duration * self.frame_rate)for attr, ref_wav in zip(attributes, ref_wavs):14 unmodified linesgen_tokens = self.lm.generate(prompt_tokens, attributes,callback=callback, max_gen_len=max_gen_len, **self.generation_params)if prompt_tokens is None:all_tokens.append(gen_tokens)else:all_tokens.append(gen_tokens[:, :, prompt_tokens.shape[-1]:])prompt_tokens = gen_tokens[:, :, stride_tokens:]prompt_length = prompt_tokens.shape[-1]current_gen_offset += stride_tokensgen_tokens = torch.cat(all_tokens, dim=-1)
app.py+12-6
audiocraft/models/musicgen.py+19-6