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