File size: 1,324 Bytes
e589a86
c63d66d
 
 
617bcb0
8e9ced2
617bcb0
8e9ced2
617bcb0
8e9ced2
c63d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b626efe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
from transformers import AutoModel, AutoTokenizer
import gradio as gr

use_cpu = os.environ.get("USE_CPU", "all")
tokenizer = AutoTokenizer.from_pretrained("./THUDM/chatglm-6b", trust_remote_code=True)
if not use_cpu:
    model = AutoModel.from_pretrained("./THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
else:
    model = AutoModel.from_pretrained("./THUDM/chatglm-6b", trust_remote_code=True).bfloat16()
model = model.eval()

def predict(input, history=None):
    if history is None:
        history = []
    response, history = model.chat(tokenizer, input, history)
    return history, history


with gr.Blocks() as demo:
    gr.Markdown('''## ChatGLM-6B - unofficial demo
    Unnoficial demo of the [ChatGLM-6B](https://github.com/THUDM/ChatGLM-6B/blob/main/README_en.md) model, trained on 1T tokens of English and Chinese
    ''')
    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(predict, [txt, state], [chatbot, state])
    button.click(predict, [txt, state], [chatbot, state])
demo.queue().launch()