{"title": "ChatGLM2-6B Chatbot with Gradio UI - Stream Chat and Markdown Support", "description": "This code implements a ChatGLM2-6B chatbot with a Gradio user interface, enabling streaming chat conversations and Markdown support for richer text rendering. Features include adjustable parameters for maximum length, top_p, and temperature, along with a user-friendly interface.", "keywords": "ChatGLM2-6B, Chatbot, Gradio, Stream Chat, Markdown, Python, NLP, Text Generation, AI, Machine Learning", "content": "import gradio as gr\nimport mdtex2html\nfrom utils import load_model_on_gpus\nfrom transformers import AutoModel, AutoTokenizer\n\ntokenizer = AutoTokenizer.from_pretrained("D:\work\test\ChatGLM2-6B\THUDM\chatglm2-6b", trust_remote_code=True)\nmodel = AutoModel.from_pretrained("D:\work\test\ChatGLM2-6B\THUDM\chatglm2-6b", trust_remote_code=True,from_tf=True).cuda()\n# 多显卡支持,使用下面两行代替上面一行,将num_gpus改为你实际的显卡数量\n# from utils import load_model_on_gpus\n# model = load_model_on_gpus("THUDM/chatglm2-6b", num_gpus=2)\nmodel = model.eval()\n\n"""Override Chatbot.postprocess"""\n\ndef postprocess(self, y):\n if y is None:\n return []\n for i, (message, response) in enumerate(y):\n message_text = None if message is None else mdtex2html.convert("".join(message))\n response_text = None if response is None else mdtex2html.convert("".join(response))\n y[i] = (message_text, response_text)\n return y\n\ngr.Chatbot.postprocess = postprocess\n\ndef parse_text(text):\n """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""\n lines = text.split("\n")\n lines = [line for line in lines if line != ""]\n count = 0\n for i, line in enumerate(lines):\n if "```" in line:\n count += 1\n items = line.split('')\n if count % 2 == 1:\n lines[i] = f'<pre><code class="language-{items[-1]}">'\n else:\n lines[i] = f'<br></code></pre>'\n else:\n if i > 0:\n if count % 2 == 1:\n line = line.replace("," "`")\n line = line.replace("<", "<")\n line = line.replace(">", ">")\n line = line.replace(" ", " ")\n line = line.replace("*", "*")\n line = line.replace("_", "_")\n line = line.replace("-", "-")\n line = line.replace(".", ".")\n line = line.replace("!", "!")\n line = line.replace("(", "(")\n line = line.replace(")", ")")\n line = line.replace("$", "$")\n lines[i] = "
"+line\n text = "".join(lines)\n return text\n\ndef predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):\n chatbot.append((parse_text(input), ""))\n for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,\n return_past_key_values=True,\n max_length=max_length, top_p=top_p,\n temperature=temperature):\n chatbot[-1] = (parse_text(input), parse_text(response))\n\n yield chatbot, history, past_key_values\n\ndef reset_user_input():\n return gr.update(value='')\n\ndef reset_state():\n return [], [], None\n\nwith gr.Blocks() as demo:\n gr.HTML("""

ChatGLM2-6B

""")\n\n chatbot = gr.Chatbot()\n with gr.Row():\n with gr.Column(scale=4):\n with gr.Column(scale=12):\n user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(\n container=False)\n with gr.Column(min_width=32, scale=1):\n submitBtn = gr.Button("Submit", variant="primary")\n with gr.Column(scale=1):\n emptyBtn = gr.Button("Clear History")\n max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)\n top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)\n temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)\n\n history = gr.State([])\n past_key_values = gr.State(None)\n\n submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],\n [chatbot, history, past_key_values], show_progress=True)\n submitBtn.click(reset_user_input, [], [user_input])\n\n emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)\n\ndemo.queue().launch(share=False, inbrowser=True)\n


原文地址: https://www.cveoy.top/t/topic/p9bP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录