Views
No views yet
m-a-p/ChatMusician.
Refer to the original model card for more details on the model.pip install mlx-lm1import re
2from string import Template
3from mlx_lm.utils import generate, load
4
5prompt_template = Template("Human: ${inst} </s> Assistant: ")
6
7
8model, tokenizer = load("mlx-community/ChatMusician-hf-4bit-mlx")
9
10instruction = """"Develop a musical piece using the given chord progression.
11'Dm', 'C', 'Dm', 'Dm', 'C', 'Dm', 'C', 'Dm'
12"""
13prompt = prompt_template.safe_substitute({"inst": instruction})
14
15response = generate(
16 model=model,
17 tokenizer=tokenizer,
18 prompt=prompt,
19 temp=0.6,
20 top_p=0.9,
21 max_tokens=1000,
22 repetition_penalty=1.1,
23)
24
25# pip install symusic
26from symusic import Score, Synthesizer
27import wave
28
29abc_pattern = r"(X:\d+\n(?:[^\n]*\n)+)"
30abc_notation = re.findall(abc_pattern, response + "\n")[0]
31s = Score.from_abc(abc_notation)
32audio = Synthesizer().render(s, stereo=True)
33
34sample_rate = 44100
35audio = (audio * 32767).astype("int16")
36with wave.open("cm_music_piece.wav", "w") as wf:
37 wf.setnchannels(2)
38 wf.setsampwidth(2)
39 wf.setframerate(sample_rate)
40 wf.writeframes(audio.tobytes())
41