forked from SakuraLLM/SakuraLLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_novel.py
249 lines (214 loc) · 9.79 KB
/
translate_novel.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from dacite import from_dict
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
import time
import re
from tqdm import tqdm
import utils
import utils.cli
import utils.model as M
import utils.consts as consts
total_token = 0
generation_time = 0
def add_token_cnt(cnt):
global total_token
total_token += cnt
def add_time(time):
global generation_time
generation_time += time
def get_novel_text_list(data_path, text_length):
data_list = list()
with open(data_path, 'r', encoding="utf-8") as f:
data = f.read()
data = data.strip()
data_raw = re.sub('\n+', '\n', data)
print(f"text total words: {len(data_raw)}")
data = data_raw.strip().split("\n")
i = 0
while i < len(data):
r = text_length
text = ""
while len(text) < r:
if i >= len(data):
break
if len(text) > max(- len(data[i]) + r, 0):
break
else:
text += data[i] + "\n"
i += 1
text = text.strip()
data_list.append(text)
return data_raw, data_list
def get_model_response(model: AutoModelForCausalLM, tokenizer: AutoTokenizer, prompt: str, model_version: str, generation_config: GenerationConfig, text_length: int, llama_cpp: bool):
backup_generation_config_stage2 = GenerationConfig(
temperature=0.1,
top_p=0.3,
top_k=40,
num_beams=1,
bos_token_id=1,
eos_token_id=2,
pad_token_id=0,
max_new_tokens=text_length,
min_new_tokens=1,
do_sample=True,
repetition_penalty=1.0,
frequency_penalty=0.05
)
backup_generation_config_stage3 = GenerationConfig(
temperature=0.1,
top_p=0.3,
top_k=40,
num_beams=1,
bos_token_id=1,
eos_token_id=2,
pad_token_id=0,
max_new_tokens=text_length,
min_new_tokens=1,
do_sample=True,
repetition_penalty=1.0,
frequency_penalty=0.2
)
backup_generation_config = [backup_generation_config_stage2, backup_generation_config_stage3]
if llama_cpp:
def generate(model, generation_config):
if "frequency_penalty" in generation_config.__dict__.keys():
output = model(prompt, max_tokens=generation_config.__dict__['max_new_tokens'], temperature=generation_config.__dict__['temperature'], top_p=generation_config.__dict__['top_p'], repeat_penalty=generation_config.__dict__['repetition_penalty'], frequency_penalty=generation_config.__dict__['frequency_penalty'])
else:
output = model(prompt, max_tokens=generation_config.__dict__['max_new_tokens'], temperature=generation_config.__dict__['temperature'], top_p=generation_config.__dict__['top_p'], repeat_penalty=generation_config.__dict__['repetition_penalty'])
return output
stage = 0
output = generate(model, generation_config)
while output['usage']['completion_tokens'] == text_length:
stage += 1
if stage > 2:
print("model degeneration cannot be avoided.")
break
print("model degeneration detected, retrying...")
output = generate(model, backup_generation_config[stage-1])
response = output['choices'][0]['text']
return response
# llm sharp backend
# elif use_llm_sharp:
# raise NotImplementedError
# import System
# import llm_sharp
# def generate(model, generation_config):
# history = System.Collections.Generic.List[System.ValueTuple[System.String, System.String]]()
# g = llm_sharp.LLM.Pretrained.GenerationConfig()
# g.temperature = generation_config.__dict__['temperature']
# g.top_p = generation_config.__dict__['top_p']
# g.max_generated_tokens = generation_config.__dict__['max_new_tokens']
# output = model.chat(history, prompt, g)
# output_ret = ""
# cnt = 0
# for o in output:
# output_ret += o
# cnt += 1
# add_token_cnt(cnt)
# return output_ret
# response = generate(model, generation_config)
# return response
generation = model.generate(**tokenizer(prompt, return_tensors="pt").to(model.device), generation_config=generation_config)[0]
if len(generation) > text_length:
stage = 0
while utils.detect_degeneration(list(generation), model_version):
stage += 1
if stage > 2:
print("model degeneration cannot be avoided.")
break
generation = model.generate(**tokenizer(prompt, return_tensors="pt").to(model.device), generation_config=backup_generation_config[stage-1])[0]
response = tokenizer.decode(generation)
output = utils.split_response(response, model_version)
return output
# FIXME(kuriko): I dont know how refactor to this, QAQ. just provide an example.
def get_model_response(model: M.SakuraModel, prompt: str, generation_config: GenerationConfig):
backup_generation_config_stage2 = GenerationConfig( temperature=0.1, top_p=0.3, top_k=40, num_beams=1, bos_token_id=1, eos_token_id=2, pad_token_id=0, max_new_tokens=2 * text_length, min_new_tokens=1, do_sample=True, repetition_penalty=1.0, frequency_penalty=0.05)
backup_generation_config_stage3 = GenerationConfig( temperature=0.1, top_p=0.3, top_k=40, num_beams=1, bos_token_id=1, eos_token_id=2, pad_token_id=0, max_new_tokens=2 * text_length, min_new_tokens=1, do_sample=True, repetition_penalty=1.0, frequency_penalty=0.2)
backup_generation_config = [backup_generation_config_stage2, backup_generation_config_stage3]
# Use the sync one
output: M.SakuraModel.ModelResponse = model.completion(prompt, generation_config)
if llama_cpp:
return output.text
# FIXME(kuriko): QAQ
if len(generation) > 2 * text_length:
stage = 0
while utils.detect_degeneration(list(generation), model_version):
stage += 1
if stage > 2:
print("model degeneration cannot be avoided.")
break
output: M.SakuraModel.ModelResponse = model.completion(prompt, backup_generation_config[stage-1])
return output.text
def get_compare_text(source_text, translated_text):
source_text_list = source_text.strip().split("\n")
translated_text_list = translated_text.strip().split("\n")
output_text = ""
if len(source_text_list) != len(translated_text_list):
print(f"error occurred when output compared text(length of source is {len(source_text_list)} while length of translated is {len(translated_text_list)}), fallback to output only translated text.")
# for i in range(len(source_text_list)):
# try:
# tmp = translated_text_list[i]
# except Exception as e:
# tmp = ""
# output_text += source_text_list[i] + "\n" + tmp + "\n\n"
return translated_text
else:
for i in range(len(source_text_list)):
output_text += source_text_list[i] + "\n" + translated_text_list[i] + "\n\n"
output_text = output_text.strip()
return output_text
def main():
def extra_args(parser):
parser.add_argument("--data_path", type=str, default="data.txt", help="file path of the text you want to translate.")
parser.add_argument("--output_path", type=str, default="data_translated.txt", help="save path of the text model translated.")
parser.add_argument("--compare_text", action="store_true", help="whether to output with both source text and translated text in order to compare.")
parser.add_argument("--text_length", type=int, default=512, help="input max length in each inference.")
args = utils.cli.parse_args(do_validation=True, add_extra_args_fn=extra_args)
import coloredlogs
coloredlogs.install(level="INFO")
cfg = from_dict(data_class=M.SakuraModelConfig, data=args.__dict__)
sakura_model = M.SakuraModel(cfg=cfg)
generation_config = GenerationConfig(
temperature=0.1,
top_p=0.3,
top_k=40,
num_beams=1,
bos_token_id=1,
eos_token_id=2,
pad_token_id=0,
max_new_tokens=512,
min_new_tokens=1,
do_sample=True
)
print("translating...")
with open(args.output_path, 'w', encoding='utf-8') as f_w:
start = time.time()
data_raw, data_list = get_novel_text_list(args.data_path, args.text_length)
data = ""
for d in tqdm(data_list):
prompt = consts.get_prompt(
input=d,
model_name=sakura_model.cfg.model_name,
model_version=sakura_model.cfg.model_version,
model_quant=sakura_model.cfg.model_quant,
)
#FIXME(kuriko): refactor this to sakura_model.completion()
output = get_model_response(
sakura_model.model,
sakura_model.tokenizer,
prompt,
sakura_model.cfg.model_version,
generation_config,
sakura_model.cfg.text_length,
sakura_model.cfg.llama_cpp,
)
data += output.strip() + "\n"
end = time.time()
print("translation completed, used time: ", generation_time, end-start, ", total tokens: ", total_token, ", speed: ", total_token/(end-start), " token/s")
print("saving...")
if args.compare_text:
f_w.write(get_compare_text(data_raw, data))
else:
f_w.write(data)
print("completed.")
if __name__ == "__main__":
main()