|
--- |
|
license: other |
|
--- |
|
``` |
|
from transformers import AutoConfig, AutoModel, AutoTokenizer |
|
import os |
|
import torch |
|
|
|
# 载入Tokenizer |
|
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) |
|
config = AutoConfig.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True, pre_seq_len=128) |
|
model = AutoModel.from_pretrained("THUDM/chatglm-6b", config=config, trust_remote_code=True) |
|
prefix_state_dict = torch.load(os.path.join("./Adjust_ChatGLM_6B/", "pytorch_model.bin")) |
|
new_prefix_state_dict = {} |
|
for k, v in prefix_state_dict.items(): |
|
if k.startswith("transformer.prefix_encoder."): |
|
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v |
|
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict) |
|
model = model.quantize(4) |
|
model = model.half().cuda() |
|
model.transformer.prefix_encoder.float() |
|
model = model.eval() |
|
|
|
response, history = model.chat(tokenizer, "生成衬衣的广告词", history=[]) |
|
print(response) |
|
``` |