actboy commited on
Commit
9deaf7a
1 Parent(s): 77452f9

cpu ChatGLM-6B

Browse files
Files changed (2) hide show
  1. app.py +46 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel, AutoTokenizer
2
+ import gradio as gr
3
+
4
+ tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
5
+ # model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
6
+ model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float()
7
+ model = model.eval()
8
+
9
+ MAX_TURNS = 20
10
+ MAX_BOXES = MAX_TURNS * 2
11
+
12
+
13
+ def predict(input, max_length, top_p, temperature, history=None):
14
+ if history is None:
15
+ history = []
16
+ for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
17
+ temperature=temperature):
18
+ updates = []
19
+ for query, response in history:
20
+ updates.append(gr.update(visible=True, value="用户:" + query))
21
+ updates.append(gr.update(visible=True, value="ChatGLM-6B:" + response))
22
+ if len(updates) < MAX_BOXES:
23
+ updates = updates + [gr.Textbox.update(visible=False)] * (MAX_BOXES - len(updates))
24
+ yield [history] + updates
25
+
26
+
27
+ with gr.Blocks() as demo:
28
+ state = gr.State([])
29
+ text_boxes = []
30
+ for i in range(MAX_BOXES):
31
+ if i % 2 == 0:
32
+ text_boxes.append(gr.Markdown(visible=False, label="提问:"))
33
+ else:
34
+ text_boxes.append(gr.Markdown(visible=False, label="回复:"))
35
+
36
+ with gr.Row():
37
+ with gr.Column(scale=4):
38
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter", lines=11).style(
39
+ container=False)
40
+ with gr.Column(scale=1):
41
+ max_length = gr.Slider(0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True)
42
+ top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)
43
+ temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
44
+ button = gr.Button("Generate")
45
+ button.click(predict, [txt, max_length, top_p, temperature, state], [state] + text_boxes)
46
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ protobuf>=3.19.5,<3.20.1
2
+ transformers>=4.26.1
3
+ icetk
4
+ cpm_kernels
5
+ torch>=1.10
6
+ gradio