From 119caa15ef98de6faf3c66e82fa900f9b21b505c Mon Sep 17 00:00:00 2001 From: tuteng0915 Date: Mon, 3 Apr 2023 23:31:30 +0800 Subject: [PATCH 1/2] add parse_text --- web_demo3.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/web_demo3.py b/web_demo3.py index d6a62ec..80ffce9 100644 --- a/web_demo3.py +++ b/web_demo3.py @@ -23,11 +23,44 @@ def postprocess(self, y): gr.Chatbot.postprocess = postprocess +def parse_text(text): + """revise 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] = "
"+line + text = "".join(lines) + return text + + def predict(input, chatbot, max_length, top_p, temperature, history): - chatbot.append((input, "")) + 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] = (input, response) + chatbot[-1] = (parse_text(input), parse_text(response)) yield chatbot, history def reset_user_input(): From d21f891a76e9df2da7d3e0f6e5c5d28ef1dde337 Mon Sep 17 00:00:00 2001 From: tuteng0915 Date: Mon, 3 Apr 2023 23:36:18 +0800 Subject: [PATCH 2/2] add parse_text --- web_demo3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_demo3.py b/web_demo3.py index 80ffce9..ad5ba11 100644 --- a/web_demo3.py +++ b/web_demo3.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type import mdtex2html tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) -model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).quantize(8).half().cuda() +model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda() model = model.eval() # MAX_TURNS = 20 @@ -24,7 +24,7 @@ gr.Chatbot.postprocess = postprocess def parse_text(text): - """revise from https://github.com/GaiZhenbiao/ChuanhuChatGPT/""" + """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/""" lines = text.split("\n") lines = [line for line in lines if line != ""] count = 0