-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
161 lines (133 loc) · 4.55 KB
/
inference.py
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
import argparse
import json
import os
import re
import subprocess
import sys
from math import ceil
import torch
from datasets import Dataset, load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
GPT2LMHeadModel,
GPT2TokenizerFast,
Pipeline,
TextGenerationPipeline,
pipeline,
)
from peft import PeftModel, PeftConfig
import utils
from config import get_cfg_defaults
INSTRUMENTS = [
"distorted0",
"distorted1",
"distorted2",
"clean0",
"clean1",
"bass",
"leads",
"pads",
"drums",
]
def is_in_instruments(note, instruments):
for instr in instruments:
if instr in note:
return True
return False
def get_bad_words(tokenizer, vocab, instruments=INSTRUMENTS):
if instruments == INSTRUMENTS:
return
unwanted_instr = set(INSTRUMENTS).difference(instruments)
bad_notes = [s for s in vocab if is_in_instruments(s, unwanted_instr)]
bad_words_ids = tokenizer(bad_notes, add_special_tokens=False).input_ids
return bad_words_ids
def postprocess(text, all_tokens, append_end=True, sep="\n"):
text = re.sub("new_measure", " new_measure ", text)
text = text.strip()
tokens = text.split()
filtered_tokens = [t for t in tokens if t in all_tokens]
if append_end and filtered_tokens[-1] != "end":
filtered_tokens.append("end")
processed_text = f"{sep}".join(filtered_tokens)
return processed_text
def generate_piece(generator, warm_up_tabs, max_length, overlap, all_tokens):
current_input = warm_up_tabs
generated_tabs = generator(current_input)[0]["generated_text"]
generated_tabs = postprocess(generated_tabs, all_tokens, append_end=False, sep=" ")
while len(generated_tabs.split()) < max_length:
print(len(generated_tabs.split()))
current_input = " ".join(generated_tabs.split()[-overlap:])
current_gen = generator(current_input)[0]["generated_text"]
current_gen = " ".join(current_gen.split()[overlap:])
current_gen = postprocess(current_gen, all_tokens, append_end=False, sep=" ")
generated_tabs += current_gen
generated_tabs = postprocess(generated_tabs, all_tokens)
return generated_tabs
def generate_gp(
generated_text,
output_path=".output/",
):
txt_path = os.path.join(output_path, "input.txt")
with open(txt_path, "w") as f:
f.write(generated_text)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
type=str,
help=".yml config path",
)
parser.add_argument(
"--input-path",
type=str,
default="/mnt/e/Data/DadaGP-v1.1/B/Beatles (The)/Beatles (The) - Here Comes The Sun (3).gp4.tokens.txt",
)
parser.add_argument("--output-path", type=str, default="./output/")
parser.add_argument("--n-warm-up", type=int, default=128)
parser.add_argument("--max-length", type=int, default=1024)
parser.add_argument("--overlap", type=int, default=128)
parser.add_argument("--instruments", nargs="+", default=INSTRUMENTS)
return parser.parse_args()
def main():
args = parse_arguments()
cfg = get_cfg_defaults()
print(f"Device: {torch.device(0)}")
if os.path.exists(args.config):
cfg.merge_from_file(args.config)
all_tokens = json.load(open(os.path.join(cfg.INPUT, "_DadaGP_all_tokens.json")))
input_piece = utils.read_tokens(args.input_path)
warm_up = " ".join(input_piece.split()[: args.n_warm_up])
print(f"Prompt: {warm_up}")
if cfg.DATA.EXTEND_TOKENIZER:
tokenizer = utils.get_tokenizer(extend=all_tokens)
else:
tokenizer = utils.get_tokenizer()
if cfg.USE_PEFT:
config = PeftConfig.from_pretrained(os.path.join(cfg.CKPT_PATH, "adapter_model"))
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, os.path.join(cfg.CKPT_PATH, "adapter_model"))
else:
model = GPT2LMHeadModel.from_pretrained(cfg.CKPT_PATH)
tab_generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_length=1024,
device=torch.device(0),
bad_words_ids=get_bad_words(
tokenizer,
all_tokens,
instruments=args.instruments,
),
)
generated_text = generate_piece(
tab_generator,
warm_up,
max_length=args.max_length,
overlap=args.overlap,
all_tokens=all_tokens,
)
generate_gp(generated_text, args.output_path)
if __name__ == "__main__":
main()