-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm_test.py
257 lines (230 loc) · 11.5 KB
/
llm_test.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
250
251
252
253
254
import os
import struct
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import json
def get_key_llm(text, model, tokenizer, device, name, gen_description=True):
with torch.inference_mode():
answer_list = []
description_dict = {}
for part in range(0, len(text["text"]) // 6000 + 1):
context = text["text"][part*6000:(part+1)*6000]
user_text = f'I have the following document:\n\n{context}\n\nBased on the information above, extract the keywords that best describe the topic of the text.\nMake sure to only extract keywords that appear in the text.\nUse the following format separated by commas:\n<keywords>\nAnswer in Russian.'
#user_text = f'I have the following document:\n\n{context}\n\nBased on the information above, extract the most important topics that best describe the text to understand.\n.\nUse the following format separated by commas:\n<keywords>\nAnswer in Russian.'
chat = [
{"role": "system", "content": "The following is a conversation with an AI Large Language Model. The AI has been trained to answer questions, provide recommendations, and help with decision making. The AI follows user requests. The AI thinks outside the box."},
{"role": "user", "content": user_text},
]
input_tokens = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=False)
print(f'Length of chat tokens (chat tokens + answer must be below 4096): {len(input_tokens)}')
input_len = len(input_tokens)
input_tokens = torch.LongTensor([input_tokens]).to(device=device)
out = model.generate(
input_ids=input_tokens,
do_sample=False,
max_new_tokens=400,
repetition_penalty=1.2,
#eos_token_id=[32000, 28705],
)
answer = tokenizer.batch_decode(out[:,input_len:], skip_special_tokens=True)[0]
print(answer)
answer_list.append(str.strip(answer))
if gen_description:
for keyword in str.split(str.strip(answer), ','):
description_dict[keyword] = get_description_llm(keyword, context, model, tokenizer, device)
with open(f'{str.replace(name, ".mp3", "_keywords.json")}', 'w', encoding='utf-8') as jsf:
json.dump(answer_list, jsf, ensure_ascii=False, indent=4)
if gen_description:
with open(f'{str.replace(name, ".mp3", "_descriptions.json")}', 'w', encoding='utf-8') as jsf:
json.dump(description_dict, jsf, ensure_ascii=False, indent=4)
answer_full_list = []
for ans in answer_list:
split_ans = str.split(ans, ',')
for s in split_ans:
if str.replace(str.strip(s), '.', '') not in answer_full_list:
answer_full_list.append(str.replace(str.strip(s), '.', ''))
return answer_full_list, description_dict
def get_key_stage2_llm(keywords, text, model, tokenizer, device, name):
with torch.inference_mode():
answer_list = []
for part in range(0, len(text["text"]) // 6000 + 1):
context = text["text"][part * 6000:(part + 1) * 6000]
keywords_string = ' [\n'
for k in keywords:
keywords_string = keywords_string + ' ' + '"' + k + '",\n'
keywords_string = keywords_string[:-2] + '\n]'
#print(keywords_string)
#keywords_string = '''
'''
[
"проверка четности",
"итеративные процессы",
"бесконечный цикл",
"элементы",
"чтение файла",
"язык программирования c-sharp",
"гири",
"специальности",
"быстро рекомендация",
"английский язык",
"методы вывода.",
"алгоритмы",
"язык программирования c#",
"переменные",
"участок память",
"кодировка",
"разработка программного обеспечения",
"условные операторы",
"итерационные процессы",
"циклические структуры",
"упражнения",
"автоматизированное тестирование.",
"представить пример",
"массив array",
"averagedialedtemperatures сократить",
"структура данных",
"csharp цикл",
"именование объектов",
"массив формирование",
"блок-схема",
"индекс элемента массива",
"задачи",
"сообщения об ошибках",
"изучение документации",
"заполнение массива",
"деление по модулю",
"utf-8",
"типы данных",
"запустить проект",
"основные характеристики",
"условия",
"тестировщик программного обеспечения",
"среднее значение",
"язык c-sharp",
"c-sharp",
"итерации",
"функциональные свойства",
"элемент массив",
"весовые гири",
"создание массива",
"length раз",
"элементы массива",
"ключевые слова foreach",
"цикл for",
"вывод на экран",
"технический английский язык",
"среднее арифметическое",
"переменные счетчиков",
"операторы сравнения",
"репозиторий",
"максимальное значение",
"обращение к элементам",
"язык программирования",
"массивы",
"форматирование кода",
"индексация",
"utf8 unicode",
"блок-схемы",
"арифметические операторы",
"операции ввода-вывода",
"циклы",
"цикл while",
"алгоритмическое мышление",
"суммирование"
]
'''
#print(keywords_string)
#print(keywords_string[0:15])
#print('-----------------------')
#print(keywords_string)
user_text = f'You are a student who is studying computer science. I have the following keywords:\n\n{keywords_string}\n\nfrom the lesson:\n\n{context}\n\nBased on the both keywords and the text of the lesson above, pick three most important terms and output them.\nMake sure to only extract keywords that appear in the text.\nUse the following format separated by commas:\n<keywords>\nAnswer in Russian.'
chat = [
{"role": "system",
"content": "The following is a conversation with an AI Large Language Model. The AI has been trained to answer questions, provide recommendations, and help with decision making. The AI follows user requests. The AI thinks outside the box."},
{"role": "user", "content": user_text},
]
input_tokens = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=False)
input_tokens = input_tokens + tokenizer.encode('\n[')
print(f'Length of chat tokens (chat tokens + answer must be below 4096): {len(input_tokens)}')
input_len = len(input_tokens)
input_tokens = torch.LongTensor([input_tokens]).to(device=device)
out = model.generate(
input_ids=input_tokens,
do_sample=False,
max_new_tokens=400,
repetition_penalty=1.2,
# eos_token_id=[32000, 28705],
)
answer = tokenizer.batch_decode(out[:, input_len:], skip_special_tokens=True)[0]
if '```' in answer:
answer = str.split(answer, '```')[0]
if 'In English' in answer:
answer = str.split(answer, 'In English')[0]
print(f'answer: {answer}')
answer_split = str.split(answer, '\n')
chars_to_remove = ['[', ']', '"', ',', '.', '\'']
#print(answer_split)
added = 0
for ans in answer_split:
#cleaned = str.strip(ans)
#cleaned = ans[4:]
#for c in chars_to_remove:
# cleaned = str.replace(cleaned, c, '')
#print(ans)
if '"' in ans:
cleaned = str.split(ans, '"')[1]
#print(cleaned)
if not cleaned in answer_list and len(cleaned) > 0 and added < 3:
print(cleaned)
answer_list.append(cleaned)
added = added + 1
with open(f'{str.replace(name, ".mp3", "_keywords_cleaned.json")}', 'w', encoding='utf-8') as jsf:
json.dump(answer_list, jsf, ensure_ascii=False, indent=4)
return answer_list
def get_description_llm(keyword, text, model, tokenizer, device):
with torch.inference_mode():
user_text = f'I have the following document:\n\n{text}\n\nBased on the information above, give me a definition of the {keyword}.\nMake sure to only use definition that appear in the text.\nAnswer in Russian.'
chat = [
{"role": "system", "content": "The following is a conversation with an AI Large Language Model. The AI has been trained to answer questions, provide recommendations, and help with decision making. The AI follows user requests. The AI thinks outside the box."},
{"role": "user", "content": user_text},
]
input_tokens = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=False)
input_len = len(input_tokens)
input_tokens = torch.LongTensor([input_tokens]).to(device=device)
out = model.generate(
input_ids=input_tokens,
do_sample=False,
max_new_tokens=400,
repetition_penalty=1.2,
)
answer = tokenizer.batch_decode(out[:, input_len:], skip_special_tokens=True)[0]
print(f'answer for {keyword}: {answer}')
return answer
def get_model_and_tokenizer():
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
# local cached file, non-docker run
if os.path.exists('backend/docker/models/Mistral-7B-OpenOrca/'):
repo_path = "backend/docker/models/Mistral-7B-OpenOrca/"
# docker run
elif os.path.exists('models/Mistral-7B-OpenOrca/'):
repo_path = "models/Mistral-7B-OpenOrca/"
# default to huggingface hub download
else:
repo_path = "Open-Orca/Mistral-7B-OpenOrca"
print(f'Loading LLM from {repo_path}')
model = AutoModelForCausalLM.from_pretrained(repo_path, device_map=device,
torch_dtype=torch.float16, use_flash_attention_2=True)
tokenizer = AutoTokenizer.from_pretrained(repo_path)
return model, tokenizer, device
if __name__ == "__main__":
model, tokenizer, device = get_model_and_tokenizer()
# mac
if os.path.exists('./train_data/audiofiles/'):
filepath = './train_data/audiofiles/'
# win
else:
filepath = 'train_data/audiofiles/'
for name in os.listdir(filepath):
with open(str.replace(name, ".mp3", ".json"), 'r', encoding='utf-8') as jsf:
text = json.load(jsf)
get_key_llm(text, model, tokenizer, device)