from transformers import AutoModel, AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True) model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).half().cuda() model = model.eval() def beginchat(input, history=None): if history is None: history = [] response, history = model.chat(tokenizer, input, history) return history, history with gr.Blocks() as chatglm2bot: gr.Markdown('''## ChatGLM2-6B - chatbot demo''') state = gr.State([]) chatbot = gr.Chatbot([], elem_id="chatbot").style(height=400) with gr.Row(): with gr.Column(scale=4): txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) with gr.Column(scale=1): button = gr.Button("Generate") txt.submit(beginchat, [txt, state], [chatbot, state]) button.click(beginchat, [txt, state], [chatbot, state]) chatglm2bot.queue().launch()