Update README.md
Browse files
README.md
CHANGED
|
@@ -11,17 +11,40 @@ This model is finetuned on mutiple datasets related to ABC notation (mostly Iris
|
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-
## CLI demo
|
| 15 |
```python
|
| 16 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 17 |
import torch
|
| 18 |
import torchaudio
|
| 19 |
import re
|
| 20 |
-
from string import Template
|
| 21 |
-
prompt_template = Template("Human: ${inst} </s> Assistant: ")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
generation_config = GenerationConfig(
|
| 27 |
temperature=0.2,
|
|
@@ -34,30 +57,62 @@ generation_config = GenerationConfig(
|
|
| 34 |
max_new_tokens=1536
|
| 35 |
)
|
| 36 |
|
| 37 |
-
instruction = """
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# to render abc notation, you need to install symusic
|
| 53 |
# pip install symusic
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
abc_pattern = r'(X:\d+\n(?:[^\n]*\n)+)'
|
| 57 |
-
abc_notation = re.findall(abc_pattern, response+'\n')[0]
|
| 58 |
s = Score.from_abc(abc_notation)
|
| 59 |
audio = Synthesizer().render(s, stereo=True)
|
| 60 |
torchaudio.save('cm_music_piece.wav', torch.FloatTensor(audio), 44100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
```
|
| 62 |
|
| 63 |
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
+
## CLI demo for 4-bit quantize
|
| 15 |
```python
|
| 16 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, BitsAndBytesConfig
|
| 17 |
import torch
|
| 18 |
import torchaudio
|
| 19 |
import re
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
quantization_config = BitsAndBytesConfig(
|
| 22 |
+
load_in_4bit=True,
|
| 23 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 24 |
+
bnb_4bit_use_double_quant=True,
|
| 25 |
+
bnb_4bit_quant_type="nf4"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Alpaca prompt template
|
| 29 |
+
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.
|
| 30 |
+
### Instruction:
|
| 31 |
+
{}
|
| 32 |
+
### Input:
|
| 33 |
+
{}
|
| 34 |
+
### Response:
|
| 35 |
+
{}"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained("Seeker38/gemma-2-9b-it-abc-notation")
|
| 39 |
+
|
| 40 |
+
# model 4-bit quant
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
+
"Seeker38/gemma-2-9b-it-abc-notation",
|
| 43 |
+
quantization_config=quantization_config,
|
| 44 |
+
device_map="auto",
|
| 45 |
+
resume_download=True
|
| 46 |
+
).eval()
|
| 47 |
+
|
| 48 |
|
| 49 |
generation_config = GenerationConfig(
|
| 50 |
temperature=0.2,
|
|
|
|
| 57 |
max_new_tokens=1536
|
| 58 |
)
|
| 59 |
|
| 60 |
+
instruction = """Create a musical composition using the given motif and adhering to the specified musical form represented by alphabet characters.
|
| 61 |
+
X:1
|
| 62 |
+
L:1/8
|
| 63 |
+
Q:3/8=90
|
| 64 |
+
M:6/8
|
| 65 |
+
K:A
|
| 66 |
+
['e cAA ABc dBB Tf2 e fdd', 'e fga']"""
|
| 67 |
+
# 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'"
|
| 68 |
+
input_context = ""
|
| 69 |
+
|
| 70 |
+
prompt = alpaca_prompt.format(
|
| 71 |
+
instruction, # instruction
|
| 72 |
+
input_context, # input
|
| 73 |
+
"", # output - leave this blank for generation!
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Tokenize input
|
| 77 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
| 78 |
+
|
| 79 |
+
# Generate response with specified parameters
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model.generate(
|
| 82 |
+
**inputs,
|
| 83 |
+
max_new_tokens=2048,
|
| 84 |
+
temperature=0.2,
|
| 85 |
+
top_p=0.9,
|
| 86 |
+
top_k=40,
|
| 87 |
+
use_cache=True,
|
| 88 |
+
do_sample=True,
|
| 89 |
+
repetition_penalty=1.1,
|
| 90 |
+
pad_token_id=tokenizer.eos_token_id
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
result = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 94 |
+
print("Generated Response:")
|
| 95 |
+
print(result[0])
|
| 96 |
|
| 97 |
# to render abc notation, you need to install symusic
|
| 98 |
# pip install symusic
|
| 99 |
+
import re
|
| 100 |
+
from symusic import Score, Synthesizer
|
| 101 |
+
|
| 102 |
+
abc_notation = re.search(r'### Response:\s*(.*)', result[0], re.DOTALL).group(1).strip()
|
| 103 |
|
|
|
|
|
|
|
| 104 |
s = Score.from_abc(abc_notation)
|
| 105 |
audio = Synthesizer().render(s, stereo=True)
|
| 106 |
torchaudio.save('cm_music_piece.wav', torch.FloatTensor(audio), 44100)
|
| 107 |
+
|
| 108 |
+
from IPython.display import Audio, display
|
| 109 |
+
|
| 110 |
+
from pydub import AudioSegment
|
| 111 |
+
wav_link = "cm_music_piece.wav"
|
| 112 |
+
mp3_file = AudioSegment.from_wav(wav_link).export("cm_music_piece.mp3", format="mp3")
|
| 113 |
+
|
| 114 |
+
display(Audio(wav_link))
|
| 115 |
+
display(Audio('cm_music_piece.mp3'))
|
| 116 |
```
|
| 117 |
|
| 118 |
|