赞
踩
文章首发及后续更新:https://mwhls.top/4500.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!
本来这篇是为了打比赛写的,写着写着发现两个问题,AI部署连续几篇,等我比赛打完再发模型都不知道更新到哪个版本了。所以就直接发了。题图随便放个,我之后部属个文生图让它生成个。嘻嘻,蹭个热度
有问题推荐去 GitHub Issue,当然评论问我也行。2023/05/06更新:增加 API 调用及
conda create --name ChatGLM python=3.8
conda activate ChatGLM
pip install -r requirements.txt
conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.3 -c pytorch
pretrain_path
指的是模型文件夹的绝对路径。pretrain_path = r"F:\0_DATA\1_DATA\CODE\PYTHON\202304_RJB_C4\ChatGLM\chatglm-6b-int8"
tokenizer = AutoTokenizer.from_pretrained(pretrain_path, trust_remote_code=True)
model = AutoModel.from_pretrained(pretrain_path, trust_remote_code=True).half().cuda()
from transformers import AutoModel, AutoTokenizer import gradio as gr import mdtex2html import os pretrain_path = "chatglm-6b-int8" pretrain_path = os.path.abspath(pretrain_path) tokenizer = AutoTokenizer.from_pretrained(pretrain_path, trust_remote_code=True) model = AutoModel.from_pretrained(pretrain_path, trust_remote_code=True).half().cuda() model = model.eval() """Override Chatbot.postprocess""" # 原来self还可以这样用,学习了 def postprocess(self, y): if y is None: return [] for i, (message, response) in enumerate(y): y[i] = ( None if message is None else mdtex2html.convert((message)), None if response is None else mdtex2html.convert(response), ) return y gr.Chatbot.postprocess = postprocess def parse_text(text): # markdown代码转html,我猜我博客的插件也是这样 """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/""" lines = text.split("\n") lines = [line for line in lines if line != ""] count = 0 for i, line in enumerate(lines): if "```" in line: count += 1 items = line.split('`') if count % 2 == 1: lines[i] = f'<pre><code class="language-{items[-1]}">' else: lines[i] = f'<br></code></pre>' else: if i > 0: if count % 2 == 1: line = line.replace("`", "\`") line = line.replace("<", "<") line = line.replace(">", ">") line = line.replace(" ", " ") line = line.replace("*", "*") line = line.replace("_", "_") line = line.replace("-", "-") line = line.replace(".", ".") line = line.replace("!", "!") line = line.replace("(", "(") line = line.replace(")", ")") line = line.replace("$", "$") lines[i] = "<br>"+line text = "".join(lines) return text # def set_mode(temperatrue_value, top_p_value): # return gr.Slider.update(value=temperatrue_value), gr.update(value=top_p_value) def set_mode(radio_mode): # 不理解,为什么点击按钮就不能传过去,用这玩意就可以传 mode = {"创造性": [0.95, 0.7], "精准": [0.01, 0.01]} return gr.Slider.update(value=mode[radio_mode][0]), gr.update(value=mode[radio_mode][1]) def predict(input, chatbot, max_length, top_p, temperature, history): # 新增一项 chatbot.append((parse_text(input), "")) for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p, temperature=temperature): # 新增的那项修改为推理结果 chatbot[-1] = (parse_text(input), parse_text(response)) yield chatbot, history def reset_user_input(): return gr.update(value='') def reset_state(): return [], [] with gr.Blocks() as demo: gr.HTML("""<h1 align="center">ChatGLM</h1>""") # 聊天记录器 chatbot = gr.Chatbot() # 同一行内,第一行 with gr.Row(): # 第一列 with gr.Column(scale=4): # 第一列第一行,输入框 with gr.Column(scale=12): user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style( container=False) # 第一列第二行,提交按钮 with gr.Column(min_width=32, scale=1): submitBtn = gr.Button("Submit", variant="primary") # 第二列 with gr.Column(scale=1): with gr.Row(): # with gr.Column(): # button_accuracy_mode = gr.Button("精准") # with gr.Column(): # button_creative_mode = gr.Button("创造性") radio_mode = gr.Radio(label="对话模式", choices=["精准", "创造性"], show_label=False) # 靠右的四个输入 emptyBtn = gr.Button("清除历史") max_length = gr.Slider(0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True) top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True) temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True) # 历史记录 history = gr.State([]) # 按钮点击后,执行predict,并将第二个参数[...]传给predict,将predict结果传给第三个参数[...] submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history], show_progress=True) submitBtn.click(reset_user_input, [], [user_input]) # 将reset_state的结果传给outputs emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True) radio_mode.change(set_mode, radio_mode, [temperature, top_p]) # button_accuracy_mode.click(set_mode, [0.95, 0.7], outputs=[temperature, top_p]) # button_creative_mode.click(set_mode, [0.01, 0.01], outputs=[temperature, top_p]) # 这玩意debug不会用啊 demo.queue().launch(share=False, inbrowser=False)
host='0.0.0.0'
为 host='localhost'
。 uvicorn.run(app, host='localhost', port=8000, workers=1)
if __name__ == '__main__':
pretrain_path = "chatglm-6b-int8"
pretrain_path = os.path.abspath(pretrain_path)
tokenizer = AutoTokenizer.from_pretrained(pretrain_path, trust_remote_code=True)
model = AutoModel.from_pretrained(pretrain_path, trust_remote_code=True).half().cuda()
model.eval()
uvicorn.run(app, host='localhost', port=8000, workers=1)
import requests
url = "http://localhost:8000"
data = {"prompt": "你好", "history": []}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=data, headers=headers)
print(response.text)
chcp 65001
title 闻达
set "PYTHON=F:\0_DATA\2_CODE\Anaconda\envs\ChatGLM\python.exe "
:end
闻达要安装 fess,但是懒人包好像没有?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。