# import gradio as gr # model_name = "models/THUDM/chatglm2-6b-int4" # gr.load(model_name).lauch() # %%writefile demo-4bit.py from transformers import AutoModel, AutoTokenizer import gradio as gr import mdtex2html model_name = "THUDM/chatglm2-6b" tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) # model = AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda() # 按需修改,目前只支持 4/8 bit 量化 # model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).quantize(4).cuda() import torch has_cuda = torch.cuda.is_available() # has_cuda = False # force cpu if has_cuda: model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4",trust_remote_code=True).cuda() # 3.92 else: model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4",trust_remote_code=True).float() model = model.eval() """Override Chatbot.postprocess""" 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): """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'
'
else:
lines[i] = f'
'
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] = "