[Python] python - generate music from text

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    please, give me some suggestions

    To explain, I type "Sleep Music For Deep Sleep" and it will return a wav file as https://www.youtube.com/watch?v=1wAdQhFJy54 or I give a wav file, it will return the same

    Now here is what I have tried:


    • https://github.com/facebookresearch/audiocraft but it has a Low Quality

      import torchaudio
      from audiocraft.models import MusicGen

      au_crmode = MusicGen.get_pretrained(pre, cache_dir=pret_loca)
      au_crmode.set_generation_params(duration=kwargs['thoigian'])

      melody, sr = torchaudio.load('./176_183.wav')
      # generates using the melody from the given audio and the provided descriptions.
      wav = au_crmode.generate_with_chroma(input_text, melody[None].expand(3, -1, -1), sr)

      for idx, one_wav in enumerate(wav):
      audio_write(f'{idx}', one_wav.cpu(), au_crmode.sample_rate, strategy="loudness")

    or

    processor = AutoProcessor.from_pretrained(pretra[0], cache_dir=pret_loca)
    model = MusicgenForConditionalGeneration.from_pretrained(pretra[0], cache_dir=pret_loca)

    sample_length = 30
    num_tokens = sample_length * model.config.audio_encoder.frame_rate + 3
    inputs = processor(
    text=input_text,
    padding=True,
    return_tensors="pt",
    ).to(device)
    audio_values = model.generate(
    **inputs,
    max_new_tokens=num_tokens, # defines the length of the generated music piece
    guidance_scale=g_scale, # controls the creativity level # The guidance_scale is used in classifier free guidance...Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt
    do_sample=True, # enables stochastic sampling, making the generation process more creative # sampling leads to significantly better results than greedy (do_sample=False)
    )


    KeyError: 'hubert_pretraining'
    ​

    with HuBERT Base or


    'NoneType' object has no attribute 'task'
    ​

    with HuBERT Large

    any ideals

    Continue reading...

Compartilhe esta Página