# coding=utf-8 # Implements user interface in browser for fine-tuned models. # Usage: python web_demo.py --model_name_or_path path_to_model --checkpoint_dir path_to_checkpoint import mdtex2html import gradio as gr from threading import Thread from utils import ( Template, load_pretrained, prepare_infer_args, get_logits_processor ) from transformers import TextIteratorStreamer from transformers.utils.versions import require_version require_version("gradio>=3.30.0", "To fix: pip install gradio>=3.30.0") model_args, data_args, finetuning_args, generating_args = prepare_infer_args() model, tokenizer = load_pretrained(model_args, finetuning_args) prompt_template = Template(data_args.prompt_template) def postprocess(self, y): r""" Overrides Chatbot.postprocess """ 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] = "
".format(items[-1])
else:
lines[i] = "
"
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] = "