File size: 4,819 Bytes
27237bb
 
 
 
 
 
 
 
 
489b79a
 
 
 
9e83fda
489b79a
9e83fda
489b79a
 
 
 
9e83fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489b79a
 
 
 
 
 
 
 
 
 
 
 
9e83fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489b79a
 
 
9e83fda
 
 
 
489b79a
 
 
 
9e83fda
 
 
 
 
 
 
 
 
489b79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27237bb
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
library_name: transformers
tags:
- unsloth
- trl
- sft
---
## Model Details

This model is finetuned on mutiple datasets related to ABC notation (mostly Irish data)



## CLI demo for 4-bit quantize 
```python
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, BitsAndBytesConfig
import torch
import torchaudio
import re

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4"
)

# Alpaca prompt template
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""


tokenizer = AutoTokenizer.from_pretrained("Seeker38/gemma-2-9b-it-abc-notation")

# model 4-bit quant
model = AutoModelForCausalLM.from_pretrained(
    "Seeker38/gemma-2-9b-it-abc-notation", 
    quantization_config=quantization_config,
    device_map="auto",
    resume_download=True
).eval()


generation_config = GenerationConfig(
    temperature=0.2,
    top_k=40,
    top_p=0.9,
    do_sample=True,
    num_beams=1,
    repetition_penalty=1.1,
    min_new_tokens=10,
    max_new_tokens=1536
)

instruction = """Create a musical composition using the given motif and adhering to the specified musical form represented by alphabet characters.
X:1
L:1/8
Q:3/8=90
M:6/8
K:A
['e cAA ABc dBB Tf2 e fdd', 'e fga']"""
# input_context = "'A', 'D', 'E7', 'A', 'E/G#', 'A', 'Bm', 'A7/C#', 'D', 'E7', 'A', 'A', 'D', 'A', 'A', 'D', 'A', 'A', 'D', 'A', 'D', 'A/D#', 'E', 'A', 'D', 'A', 'A', 'D', 'A', 'E7'"
input_context = ""

prompt = alpaca_prompt.format(
    instruction,    # instruction
    input_context,  # input
    "",            # output - leave this blank for generation!
)

# Tokenize input
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")

# Generate response with specified parameters
with torch.no_grad():
    outputs = model.generate(
        **inputs,
        max_new_tokens=2048,
        temperature=0.2,
        top_p=0.9,
        top_k=40,
        use_cache=True,
        do_sample=True,
        repetition_penalty=1.1,
        pad_token_id=tokenizer.eos_token_id
    )

result = tokenizer.batch_decode(outputs, skip_special_tokens=True)
print("Generated Response:")
print(result[0])

# to render abc notation, you need to install symusic
# pip install symusic
import re
from symusic import Score, Synthesizer

abc_notation = re.search(r'### Response:\s*(.*)', result[0], re.DOTALL).group(1).strip()

s = Score.from_abc(abc_notation)
audio = Synthesizer().render(s, stereo=True)
torchaudio.save('cm_music_piece.wav', torch.FloatTensor(audio), 44100)

from IPython.display import Audio, display

from pydub import AudioSegment
wav_link = "cm_music_piece.wav"
mp3_file = AudioSegment.from_wav(wav_link).export("cm_music_piece.mp3", format="mp3")

display(Audio(wav_link))
display(Audio('cm_music_piece.mp3'))
```


## Example Stable Prompts
Here some prompts that are tested to be stable. The convert code and prompt is from 🤗 [ChatMusician](https://huggingface.co/m-a-p/ChatMusician).

### Function: Chord Conditioned Music Generation
```
Develop a musical piece using the given chord progression.
'Dm', 'C', 'Dm', 'Dm', 'C', 'Dm', 'C', 'Dm'
```

### Function: Text2music
```
Develop a tune influenced by Bach's compositions.
```
```
Using ABC notation, recreate the given text as a musical score.
Meter C
Notes The parts are commonly interchanged.
Transcription 1997 by John Chambers
Key D
Note Length 1/8
Rhythm reel
```

### Function: Melody Harmonization

```
Construct smooth-flowing chord progressions for the supplied music.

|: BA | G2 g2"^(C)" edeg | B2 BA"^(D7)" BcBA | G2 g2 edeg | dBAG A2 BA |
G2 g2"^(C)" edeg | B2 BA B2 d2 | e2 ef e2 (3def | gedB A2 :: BA | G2 BG dGBe |
dBBA"^(D7)" B3 A | G2 BG dGBe | dBAG A4 | G2 BG dGBe | dBBA B3 d |
e2 ef e2 (3def | gedB A2 :|
```
```
Develop a series of chord pairings that amplify the harmonious elements in the given music piece.

E |: EAA ABc | Bee e2 d | cBA ABc | BEE E2 D | EAA ABc | Bee e2 d |
cBA ^GAB |1 A2 A A2 E :|2 A2 A GAB || c3 cdc | Bgg g2 ^g | aed cBA |
^GAB E^F^G | A^GA BAB | cde fed | cBA ^GAB |1 A2 A GAB :|2 \n A3 A2 ||
```

### Function: Musical Form Conditioned Music Generation

```
Develop a composition by incorporating elements from the given melodic structure.

Ternary, Sectional: Verse/Chorus/Bridge
```

### Function: Motif and Form Conditioned Music Generation

```
Create music by following the alphabetic representation of the assigned musical structure and the given motif.

Musical Form Input: AB

ABC Notation Music Input:
X:1
L:1/8
M:2/4
K:D
['d>ef>d g>ef>c d>ef>d c2 e2 d>ef>d g>ef>d', '(3(Ace) (3(Ace)']
```