WinterGYC
commited on
Commit
•
38f2963
1
Parent(s):
d409029
Init
Browse files- README.md +1 -1
- app.py +118 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Baichuan 13B Chat Int8 Gradio
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Baichuan 13B Chat Int8 Gradio
|
3 |
+
emoji: ⚡
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModel, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import mdtex2html
|
4 |
+
from utils import load_model_on_gpus
|
5 |
+
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
"baichuan-inc/Baichuan-13B-Chat",
|
8 |
+
torch_dtype=torch.float16,
|
9 |
+
device_map="auto",
|
10 |
+
trust_remote_code=True
|
11 |
+
)
|
12 |
+
model.generation_config = GenerationConfig.from_pretrained(
|
13 |
+
"baichuan-inc/Baichuan-13B-Chat"
|
14 |
+
)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
16 |
+
"baichuan-inc/Baichuan-13B-Chat",
|
17 |
+
use_fast=False,
|
18 |
+
trust_remote_code=True
|
19 |
+
)
|
20 |
+
model = model.quantize(8).cuda()
|
21 |
+
model = model.eval()
|
22 |
+
|
23 |
+
"""Override Chatbot.postprocess"""
|
24 |
+
|
25 |
+
|
26 |
+
def postprocess(self, y):
|
27 |
+
if y is None:
|
28 |
+
return []
|
29 |
+
for i, (message, response) in enumerate(y):
|
30 |
+
y[i] = (
|
31 |
+
None if message is None else mdtex2html.convert((message)),
|
32 |
+
None if response is None else mdtex2html.convert(response),
|
33 |
+
)
|
34 |
+
return y
|
35 |
+
|
36 |
+
|
37 |
+
gr.Chatbot.postprocess = postprocess
|
38 |
+
|
39 |
+
|
40 |
+
def parse_text(text):
|
41 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
42 |
+
lines = text.split("\n")
|
43 |
+
lines = [line for line in lines if line != ""]
|
44 |
+
count = 0
|
45 |
+
for i, line in enumerate(lines):
|
46 |
+
if "```" in line:
|
47 |
+
count += 1
|
48 |
+
items = line.split('`')
|
49 |
+
if count % 2 == 1:
|
50 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
51 |
+
else:
|
52 |
+
lines[i] = f'<br></code></pre>'
|
53 |
+
else:
|
54 |
+
if i > 0:
|
55 |
+
if count % 2 == 1:
|
56 |
+
line = line.replace("`", "\`")
|
57 |
+
line = line.replace("<", "<")
|
58 |
+
line = line.replace(">", ">")
|
59 |
+
line = line.replace(" ", " ")
|
60 |
+
line = line.replace("*", "*")
|
61 |
+
line = line.replace("_", "_")
|
62 |
+
line = line.replace("-", "-")
|
63 |
+
line = line.replace(".", ".")
|
64 |
+
line = line.replace("!", "!")
|
65 |
+
line = line.replace("(", "(")
|
66 |
+
line = line.replace(")", ")")
|
67 |
+
line = line.replace("$", "$")
|
68 |
+
lines[i] = "<br>"+line
|
69 |
+
text = "".join(lines)
|
70 |
+
return text
|
71 |
+
|
72 |
+
|
73 |
+
def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
|
74 |
+
chatbot.append((parse_text(input), ""))
|
75 |
+
for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
|
76 |
+
return_past_key_values=True,
|
77 |
+
max_length=max_length, top_p=top_p,
|
78 |
+
temperature=temperature):
|
79 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
80 |
+
|
81 |
+
yield chatbot, history, past_key_values
|
82 |
+
|
83 |
+
|
84 |
+
def reset_user_input():
|
85 |
+
return gr.update(value='')
|
86 |
+
|
87 |
+
|
88 |
+
def reset_state():
|
89 |
+
return [], [], None
|
90 |
+
|
91 |
+
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
gr.HTML("""<h1 align="center">BaiChuan-13B-Int8</h1>""")
|
94 |
+
|
95 |
+
chatbot = gr.Chatbot()
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column(scale=4):
|
98 |
+
with gr.Column(scale=12):
|
99 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
|
100 |
+
container=False)
|
101 |
+
with gr.Column(min_width=32, scale=1):
|
102 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
103 |
+
with gr.Column(scale=1):
|
104 |
+
emptyBtn = gr.Button("Clear History")
|
105 |
+
max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
|
106 |
+
top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
|
107 |
+
temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
|
108 |
+
|
109 |
+
history = gr.State([])
|
110 |
+
past_key_values = gr.State(None)
|
111 |
+
|
112 |
+
submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
|
113 |
+
[chatbot, history, past_key_values], show_progress=True)
|
114 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
115 |
+
|
116 |
+
emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
|
117 |
+
|
118 |
+
demo.queue().launch(share=False, inbrowser=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
colorama
|
3 |
+
cpm_kernels
|
4 |
+
sentencepiece
|
5 |
+
transformers_stream_generator
|