| --- | |
| license: apache-2.0 | |
| language: Chinese | |
| Usage:把files and versions里的chatglm-lora.pt下载到本地,然后依次按下面的操作 | |
| # 安装依赖 | |
| pip install protobuf==3.20.0 transformers==4.26.1 peft cpm_kernels icetk -q | |
| # 导入库 | |
| from transformers import AutoTokenizer,AutoModel | |
| import torch | |
| from peft import get_peft_model, LoraConfig, TaskType | |
| # 加载模型 | |
| model = AutoModel.from_pretrained( | |
| "yuanzhoulvpi/chatglm6b-dddd", trust_remote_code=True).half().cuda() | |
| peft_config = LoraConfig( | |
| task_type=TaskType.CAUSAL_LM, | |
| inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1, | |
| target_modules=['query_key_value',], | |
| ) | |
| model = get_peft_model(model, peft_config) | |
| # 在这里加载lora模型,注意修改chekpoint | |
| peft_path = "home/checkpoint-800/chatglm-lora.pt" | |
| model.load_state_dict(torch.load(peft_path), strict=False) | |
| model.eval() | |
| # 构建tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("yuanzhoulvpi/chatglm6b-dddd", trust_remote_code=True) | |
| # 模型输出 | |
| text ="天空为什么是蓝色的?" | |
| with torch.autocast("cuda"): | |
| res, history = model.chat(tokenizer=tokenizer, query=text,max_length=300) | |
| print(res) | |
| 天空看起来蓝色是由于大气散射的结果。当太阳光线穿过大气层时,会被大气分子散射。这些分子包括氧、氮和碳氢化合物,它们吸收较短波长的光,如红色和橙色,而较长波长的光,如蓝色和紫色,则被散射得更多。 | |
| 因此,当太阳光线照射到大气层时,较短波长的光线,如红色和橙色,被散射得更多,我们看到的天空呈现出蓝色。 | |
| 天空的蓝色是由于大气散射的结果。当太阳光线穿过大气层时,会被大气分子散射。这些分子包括氧、氮和碳氢化合物,它们吸收较短波长的光,如红色和橙色,而较长波长的光,如蓝色和紫色,则被散射得更多。 | |
| 因此,当太阳光线照射到大气层时,较短波长的光线,如红色和橙色,被散射得更多,我们看到的天空呈现出蓝色。 | |
| # 如果用Gradio界面输出,可加下面语句 | |
| title = "Chat with ChatGLM" | |
| description = "Chat with ChatGLM, a Chinese pre-trained dialogue generation model." | |
| gr.Interface(fn=chat, | |
| inputs=gr.inputs.Textbox(lines=1, label="Your message:"), | |
| outputs=gr.outputs.Textbox(label="Response:"), | |
| title=title, | |
| description=description).launch(share=True) | |