File size: 2,545 Bytes
28f615e
 
 
84360fa
 
bbd44ac
48696cf
28f615e
92a0178
28f615e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdf37f5
 
 
 
 
 
bbd44ac
 
 
 
 
 
 
 
 
 
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
---
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)