Browse Source

add parse_text

pull/373/head
tuteng0915 2 years ago
parent
commit
119caa15ef
  1. 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("<", "&lt;")
line = line.replace(">", "&gt;")
line = line.replace(" ", "&nbsp;")
line = line.replace("*", "&ast;")
line = line.replace("_", "&lowbar;")
line = line.replace("-", "&#45;")
line = line.replace(".", "&#46;")
line = line.replace("!", "&#33;")
line = line.replace("(", "&#40;")
line = line.replace(")", "&#41;")
line = line.replace("$", "&#36;")
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…
Cancel
Save