Update README.md
Browse files
README.md
CHANGED
|
@@ -19,8 +19,45 @@ pip install mlx-lm
|
|
| 19 |
```
|
| 20 |
|
| 21 |
```python
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
model, tokenizer = load("mlx-community/ChatMusician-hf-4bit-mlx")
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
```
|
|
|
|
| 19 |
```
|
| 20 |
|
| 21 |
```python
|
| 22 |
+
import re
|
| 23 |
+
from string import Template
|
| 24 |
+
from mlx_lm.utils import generate, load
|
| 25 |
+
|
| 26 |
+
prompt_template = Template("Human: ${inst} </s> Assistant: ")
|
| 27 |
+
|
| 28 |
|
| 29 |
model, tokenizer = load("mlx-community/ChatMusician-hf-4bit-mlx")
|
| 30 |
+
|
| 31 |
+
instruction = """"Develop a musical piece using the given chord progression.
|
| 32 |
+
'Dm', 'C', 'Dm', 'Dm', 'C', 'Dm', 'C', 'Dm'
|
| 33 |
+
"""
|
| 34 |
+
prompt = prompt_template.safe_substitute({"inst": instruction})
|
| 35 |
+
|
| 36 |
+
response = generate(
|
| 37 |
+
model=model,
|
| 38 |
+
tokenizer=tokenizer,
|
| 39 |
+
prompt=prompt,
|
| 40 |
+
temp=0.6,
|
| 41 |
+
top_p=0.9,
|
| 42 |
+
max_tokens=1000,
|
| 43 |
+
repetition_penalty=1.1,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# pip install symusic
|
| 47 |
+
from symusic import Score, Synthesizer
|
| 48 |
+
import wave
|
| 49 |
+
|
| 50 |
+
abc_pattern = r"(X:\d+\n(?:[^\n]*\n)+)"
|
| 51 |
+
abc_notation = re.findall(abc_pattern, response + "\n")[0]
|
| 52 |
+
s = Score.from_abc(abc_notation)
|
| 53 |
+
audio = Synthesizer().render(s, stereo=True)
|
| 54 |
+
|
| 55 |
+
sample_rate = 44100
|
| 56 |
+
audio = (audio * 32767).astype("int16")
|
| 57 |
+
with wave.open("cm_music_piece.wav", "w") as wf:
|
| 58 |
+
wf.setnchannels(2)
|
| 59 |
+
wf.setsampwidth(2)
|
| 60 |
+
wf.setframerate(sample_rate)
|
| 61 |
+
wf.writeframes(audio.tobytes())
|
| 62 |
+
|
| 63 |
```
|