glm-emotion / README.md
Jerome2046's picture
Update README.md
dcd0e32 verified
|
raw
history blame
1.23 kB
metadata
language:
  - zh
pipeline_tag: text-classification

以ChatGLM2-6B作为基座模型再训练。 对文本进行情感分析。

加载代码:

!git clone https://huggingface.co/Jerome2046/glm-emotion

import torch
import os
from transformers import AutoConfig, AutoModel, AutoTokenizer

# 载入Tokenizer与模型
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
config = AutoConfig.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True, pre_seq_len=128)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", config=config, trust_remote_code=True)
# 载入预训练参数
prefix_state_dict = torch.load(os.path.join("glm-emotion", "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.half().cuda().eval()

response, history = model.chat(tokenizer, "说出'到北京五环25分钟车程,938离我住处只有300米,饮食一条街更是北京饮食街的翻版'此文段表达的情绪类型", history=[])
print(response)