Safetensors
llama

Write music scores with llama

Try the model online: https://huggingface.co/spaces/dx2102/llama-midi

This model is finetuned from the Llama-3.2-1B language model.

It learns to write MIDI music scores with a text representation.

Optionally, the score title can also be used as a text prompt.

To use this model, you can simply take existing code and replace meta-llama/Llama-3.2-1B with dx2102/llama-midi.

import torch
from transformers import pipeline

pipe = pipeline(
    "text-generation", 
    model="dx2102/llama-midi", 
    torch_dtype=torch.bfloat16, 
    device_map="auto"
)

txt = pipe(
'''
Bach
pitch duration wait velocity instrument
'''.strip(),
    max_length=100,
    temperature=1.0,
    top_p=1.0,
)
print(txt)

To convert the text representation back to a midi file, try this:

# install this midi library
pip install symusic

import symusic

# For example
txt = '''pitch duration wait velocity instrument

71 1310 0 20 0
48 330 350 20 0
55 330 350 20 0
64 1310 690 20 0
74 660 690 20 0
69 1310 0 20 0
48 330 350 20 0
57 330 350 20 0
66 1310 690 20 0
67 330 350 20 0
69 330 350 20 0
71 1310 0 20 0
48 330 350 20 0
55 330 350 20 0
64 1310 690 20 0
74 660 690 20 0
69 1970 0 20 0
48 330 350 20 0
'''

def postprocess(txt, path):
    # assert txt.startswith(prompt)
    txt = txt.split('\n\n')[-1]

    tracks = {}

    now = 0
    # we need to ignore the invalid output by the model
    try:
        for line in txt.split('\n'):
            pitch, duration, wait, velocity, instrument = line.split()
            pitch, duration, wait, velocity = [int(x) for x in [pitch, duration, wait, velocity]]
            if instrument not in tracks:
                tracks[instrument] = symusic.core.TrackSecond()
                if instrument != 'drum':
                    tracks[instrument].program = int(instrument)
                else:
                    tracks[instrument].is_drum = True
            # Eg. Note(time=7.47, duration=5.25, pitch=43, velocity=64, ttype='Second')
            tracks[instrument].notes.append(symusic.core.NoteSecond(
                time=now/1000,
                duration=duration/1000,
                pitch=int(pitch),
                velocity=int(velocity * 4),
            ))
            now += wait
    except Exception as e:
        print('Postprocess: Ignored error:', e)
    
    print(f'Postprocess: Got {sum(len(track.notes) for track in tracks.values())} notes')

    try:
        score = symusic.Score(ttype='Second')
        score.tracks.extend(tracks.values())
        score.dump_midi(path)
    except Exception as e:
        print('Postprocess: Ignored postprocessing error:', e)

postprocess(txt, './result.mid')
Downloads last month
90
Safetensors
Model size
1.24B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for dx2102/llama-midi

Finetuned
(392)
this model
Quantizations
1 model

Datasets used to train dx2102/llama-midi

Space using dx2102/llama-midi 1