kevinwang676 commited on
Commit
346fc97
·
1 Parent(s): 4721aa1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel, AutoTokenizer, AutoConfig
2
+ import gradio as gr
3
+ import mdtex2html
4
+ import torch
5
+ import os
6
+
7
+ #CHECKPOINT_PATH=f'output_lh/checkpoint-600'
8
+ tokenizer = AutoTokenizer.from_pretrained("chatglm3-6b", trust_remote_code=True)
9
+ config = AutoConfig.from_pretrained("chatglm3-6b", trust_remote_code=True, pre_seq_len=128)
10
+ model = AutoModel.from_pretrained("chatglm3-6b", config=config, trust_remote_code=True)
11
+
12
+ model = model.half().float()
13
+
14
+ model = model.eval()
15
+
16
+
17
+ """Override Chatbot.postprocess"""
18
+
19
+
20
+ def postprocess(self, y):
21
+ if y is None:
22
+ return []
23
+ for i, (message, response) in enumerate(y):
24
+ y[i] = (
25
+ None if message is None else mdtex2html.convert((message)),
26
+ None if response is None else mdtex2html.convert(response),
27
+ )
28
+ return y
29
+
30
+
31
+ gr.Chatbot.postprocess = postprocess
32
+
33
+
34
+ def parse_text(text):
35
+ """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
36
+ lines = text.split("\n")
37
+ lines = [line for line in lines if line != ""]
38
+ count = 0
39
+ for i, line in enumerate(lines):
40
+ if "```" in line:
41
+ count += 1
42
+ items = line.split('`')
43
+ if count % 2 == 1:
44
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
45
+ else:
46
+ lines[i] = f'<br></code></pre>'
47
+ else:
48
+ if i > 0:
49
+ if count % 2 == 1:
50
+ line = line.replace("`", "\`")
51
+ line = line.replace("<", "&lt;")
52
+ line = line.replace(">", "&gt;")
53
+ line = line.replace(" ", "&nbsp;")
54
+ line = line.replace("*", "&ast;")
55
+ line = line.replace("_", "&lowbar;")
56
+ line = line.replace("-", "&#45;")
57
+ line = line.replace(".", "&#46;")
58
+ line = line.replace("!", "&#33;")
59
+ line = line.replace("(", "&#40;")
60
+ line = line.replace(")", "&#41;")
61
+ line = line.replace("$", "&#36;")
62
+ lines[i] = "<br>"+line
63
+ text = "".join(lines)
64
+ return text
65
+
66
+
67
+ def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
68
+ chatbot.append((parse_text(input), ""))
69
+ for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
70
+ return_past_key_values=True,
71
+ max_length=max_length, top_p=top_p,
72
+ temperature=temperature):
73
+ chatbot[-1] = (parse_text(input), parse_text(response))
74
+
75
+ yield chatbot, history, past_key_values
76
+
77
+
78
+ def reset_user_input():
79
+ return gr.update(value='')
80
+
81
+
82
+ def reset_state():
83
+ return [], [], None
84
+
85
+
86
+ response, history = model.chat(tokenizer, "你是谁", history=[])
87
+ response, history = model.chat(tokenizer, "你好呀", history=history)
88
+ response, history = model.chat(tokenizer, "早上好", history=history)
89
+
90
+
91
+ with gr.Blocks() as demo:
92
+ gr.HTML("""<h1 align="center">ChatGLM2-6B</h1>""")
93
+
94
+ chatbot = gr.Chatbot()
95
+ with gr.Row():
96
+ with gr.Column(scale=4):
97
+ with gr.Column(scale=12):
98
+ user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
99
+ container=False)
100
+ with gr.Column(min_width=32, scale=1):
101
+ submitBtn = gr.Button("Submit", variant="primary")
102
+ with gr.Column(scale=1):
103
+ emptyBtn = gr.Button("Clear History")
104
+ max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
105
+ top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
106
+ temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
107
+
108
+ history = gr.State([])
109
+ past_key_values = gr.State(None)
110
+
111
+ submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
112
+ [chatbot, history, past_key_values], show_progress=True)
113
+ submitBtn.click(reset_user_input, [], [user_input])
114
+
115
+ emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
116
+
117
+ demo.launch(show_error=True)