Seeker38 commited on
Commit
9e83fda
·
verified ·
1 Parent(s): 489b79a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +78 -23
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
- tokenizer = AutoTokenizer.from_pretrained("Seeker38/gemma-2-9b-it-abc-notation", trust_remote_code=True)
24
- model = AutoModelForCausalLM.from_pretrained("Seeker38/gemma-2-9b-it-abc-notation", torch_dtype=torch.float16, device_map="cuda", resume_download=True).eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = """Develop a musical piece using the given chord progression.
38
- 'Dm', 'C', 'Dm', 'Dm', 'C', 'Dm', 'C', 'Dm'
39
- """
40
-
41
- prompt = prompt_template.safe_substitute({"inst": instruction})
42
- inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
43
- response = model.generate(
44
- input_ids=inputs["input_ids"].to(model.device),
45
- attention_mask=inputs['attention_mask'].to(model.device),
46
- eos_token_id=tokenizer.eos_token_id,
47
- generation_config=generation_config,
48
- )
49
- response = tokenizer.decode(response[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
50
- print(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # to render abc notation, you need to install symusic
53
  # pip install symusic
54
- from symusic import Score, Synthesizer, BuiltInSF3, dump_wav
 
 
 
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