# coding=utf-8 # Copyright 2023 WisdomShell Inc. All Rights Reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # This code is based on Qwen's web Demo. It has been modified from # its original forms to accommodate CodeShell. # Copyright (c) Alibaba Cloud. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. """A simple web interactive chat demo based on gradio.""" import os from argparse import ArgumentParser import gradio as gr import mdtex2html import torch from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation import GenerationConfig DEFAULT_CKPT_PATH = 'WisdomShell/CodeShell-7B-Chat-int4' def _load_model_tokenizer(args): tokenizer = AutoTokenizer.from_pretrained( 'WisdomShell/CodeShell-7B-Chat-int4', trust_remote_code=True, resume_download=True, ) model = AutoModelForCausalLM.from_pretrained( 'WisdomShell/CodeShell-7B-Chat-int4', trust_remote_code=True, resume_download=True, torch_dtype=torch.bfloat16 ).eval() config = GenerationConfig.from_pretrained( 'WisdomShell/CodeShell-7B-Chat-int4', trust_remote_code=True, resume_download=True, ) return model, tokenizer, config 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): 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("`", r"\`")
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] = "