JunzhaoSun commited on
Commit
4c32703
β€’
1 Parent(s): 8337ae6
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel, AutoTokenizer
2
+ import gradio as gr
3
+ import mdtex2html
4
+
5
+ # CPUζŽ¨η†ιƒ¨η½²οΌŒε¦‚ζžœζ˜―GPUζŽ¨η†ε―δ»₯η›΄ζŽ₯使用 β€œTHUDM/chatglm-6b”
6
+ checkpoint = "THUDM/chatglm-6b-int4"
7
+ # checkpoint = "/innev/open-ai/huggingface/models/THUDM/chatglm-6b-int4"
8
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True)
9
+ model = AutoModel.from_pretrained(checkpoint, trust_remote_code=True).float()
10
+ # model = AutoModel.from_pretrained(checkpoint, trust_remote_code=True).half().to('mps')
11
+ model = model.eval()
12
+
13
+ """Override Chatbot.postprocess"""
14
+
15
+
16
+ def postprocess(self, y):
17
+ if y is None:
18
+ return []
19
+ for i, (message, response) in enumerate(y):
20
+ y[i] = (
21
+ None if message is None else mdtex2html.convert((message)),
22
+ None if response is None else mdtex2html.convert(response),
23
+ )
24
+ return y
25
+
26
+
27
+ gr.Chatbot.postprocess = postprocess
28
+
29
+
30
+ def parse_text(text):
31
+ """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
32
+ lines = text.split("\n")
33
+ lines = [line for line in lines if line != ""]
34
+ count = 0
35
+ for i, line in enumerate(lines):
36
+ if "```" in line:
37
+ count += 1
38
+ items = line.split('`')
39
+ if count % 2 == 1:
40
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
41
+ else:
42
+ lines[i] = f'<br></code></pre>'
43
+ else:
44
+ if i > 0:
45
+ if count % 2 == 1:
46
+ line = line.replace("`", "\`")
47
+ line = line.replace("<", "&lt;")
48
+ line = line.replace(">", "&gt;")
49
+ line = line.replace(" ", "&nbsp;")
50
+ line = line.replace("*", "&ast;")
51
+ line = line.replace("_", "&lowbar;")
52
+ line = line.replace("-", "&#45;")
53
+ line = line.replace(".", "&#46;")
54
+ line = line.replace("!", "&#33;")
55
+ line = line.replace("(", "&#40;")
56
+ line = line.replace(")", "&#41;")
57
+ line = line.replace("$", "&#36;")
58
+ lines[i] = "<br>"+line
59
+ text = "".join(lines)
60
+ return text
61
+
62
+
63
+ def predict(input, chatbot, max_length, top_p, temperature, history):
64
+ chatbot.append((parse_text(input), ""))
65
+ for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
66
+ temperature=temperature):
67
+ chatbot[-1] = (parse_text(input), parse_text(response))
68
+
69
+ yield chatbot, history
70
+
71
+
72
+ def reset_user_input():
73
+ return gr.update(value='')
74
+
75
+
76
+ def reset_state():
77
+ return [], []
78
+
79
+
80
+ with gr.Blocks() as demo:
81
+ gr.HTML("""<h1 align="center">ChatGLM</h1>""")
82
+
83
+ chatbot = gr.Chatbot()
84
+ with gr.Row():
85
+ with gr.Column(scale=4):
86
+ with gr.Column(scale=12):
87
+ user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
88
+ container=False)
89
+ with gr.Column(min_width=32, scale=1):
90
+ submitBtn = gr.Button("Submit", variant="primary")
91
+ with gr.Column(scale=1):
92
+ emptyBtn = gr.Button("Clear History")
93
+ max_length = gr.Slider(0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True)
94
+ top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)
95
+ temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
96
+
97
+ history = gr.State([])
98
+
99
+ submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history],
100
+ show_progress=True)
101
+ submitBtn.click(reset_user_input, [], [user_input])
102
+
103
+ emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
104
+
105
+ demo.queue().launch(share=False, inbrowser=True)