mirror of https://github.com/THUDM/ChatGLM-6B
add parse_text
parent
6148d6d6ac
commit
119caa15ef
37
web_demo3.py
37
web_demo3.py
|
@ -23,11 +23,44 @@ def postprocess(self, y):
|
||||||
gr.Chatbot.postprocess = postprocess
|
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'<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 predict(input, chatbot, max_length, top_p, temperature, history):
|
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,
|
for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
|
||||||
temperature=temperature):
|
temperature=temperature):
|
||||||
chatbot[-1] = (input, response)
|
chatbot[-1] = (parse_text(input), parse_text(response))
|
||||||
yield chatbot, history
|
yield chatbot, history
|
||||||
|
|
||||||
def reset_user_input():
|
def reset_user_input():
|
||||||
|
|
Loading…
Reference in New Issue