-
Notifications
You must be signed in to change notification settings - Fork 25
/
chat.py
141 lines (111 loc) · 3.69 KB
/
chat.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
import os
import transformers
import torch
from peft import PeftModel
import fire
def load_model(base_model, device_map="auto"):
global model, tokenizer, generator
print("Loading "+base_model+"...")
if device_map == "zero":
device_map = "balanced_low_0"
# config
gpu_count = torch.cuda.device_count()
print('gpu_count', gpu_count)
tokenizer = transformers.LlamaTokenizer.from_pretrained(base_model)
model = transformers.LlamaForCausalLM.from_pretrained(
base_model,
device_map="auto",
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
load_in_8bit=False,
cache_dir="cache"
).cuda()
generator = model.generate
def load_lora_model(base_model, lora_model, device_map="auto"):
global model, tokenizer, generator
print("Loading "+base_model+"...")
if device_map == "zero":
device_map = "balanced_low_0"
# config
gpu_count = torch.cuda.device_count()
print('gpu_count', gpu_count)
tokenizer = transformers.LlamaTokenizer.from_pretrained(base_model)
model = transformers.LlamaForCausalLM.from_pretrained(
base_model,
device_map="auto",
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
load_in_8bit=False,
cache_dir="cache"
).cuda()
print("Loading "+lora_model+"...")
model = PeftModel.from_pretrained(
model,
lora_model,
torch_dtype=torch.float16
)
generator = model.generate
def go(act_as):
invitation = "HackMentor: "
human_invitation = "User: "
# input
msg = input(human_invitation)
print("")
history.append(human_invitation + msg)
fulltext = act_as + "\n\n".join(history) + "\n\n" + invitation
generated_text = ""
gen_in = tokenizer(fulltext, return_tensors="pt").input_ids.cuda()
in_tokens = len(gen_in)
with torch.no_grad():
# no_lora-model
generated_ids = generator(
input_ids = gen_in,
max_new_tokens=2048,
use_cache=True,
pad_token_id=tokenizer.eos_token_id,
num_return_sequences=1,
do_sample=True,
repetition_penalty=1.1,
temperature=0.6,
top_k = 50,
top_p = 1.0,
early_stopping=True,
)
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] # for some reason, batch_decode returns an array of one element?
text_without_prompt = generated_text[len(fulltext):]
response = text_without_prompt
response = response.split(human_invitation)[0]
response.strip()
print(invitation + response)
print("")
history.append(invitation + response)
def chat(
# model/data params
base_model: str = "",
use_lora: bool = True,
lora_model: str = "",
):
print(
f"Chat model load with params:\n"
f"base_model: {base_model}\n"
f"use_lora: {use_lora}\n"
f"lora_model: {lora_model}\n"
)
global model, tokenizer, generator, history
os.environ["CUDA_VISIBLE_DEVICES"]="0"
if use_lora:
if load_model == "":
raise ValueError("Please enter lora_model location, or set use_lora to False!")
load_lora_model(base_model, lora_model)
else:
load_model(base_model)
First_chat = "HackMentor: I am HackMentor, what cybersecurity questions do you have?\n"
print(First_chat)
history = []
history.append(First_chat)
act_as = "You are an intelligent assistant in the field of cyber security\
that mainly helps with resolving cybersecurity claims. \n\n"
while True:
go(act_as)
if __name__ == "__main__":
fire.Fire(chat)