kkkgg commited on
Commit
58775ce
·
1 Parent(s): b8cb388

Create app.py

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