kevinwang676
commited on
Commit
•
2355aad
1
Parent(s):
b72cc9e
Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,99 @@ from text.cleaner import clean_text
|
|
25 |
import gradio as gr
|
26 |
import webbrowser
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
net_g = None
|
30 |
|
@@ -119,9 +212,31 @@ if __name__ == "__main__":
|
|
119 |
gr.Markdown("### <center>🍻 - 更多精彩应用,尽在[滔滔AI](http://www.talktalkai.com);滔滔AI,为爱滔滔!💕</center>")
|
120 |
with gr.Accordion("绫华", open=True):
|
121 |
gr.Markdown(image_markdown)
|
122 |
-
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
|
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
text = gr.TextArea(label="Text", placeholder="Input Text Here",
|
|
|
25 |
import gradio as gr
|
26 |
import webbrowser
|
27 |
|
28 |
+
# ChatGLM2
|
29 |
+
|
30 |
+
from transformers import AutoModel, AutoTokenizer, AutoConfig
|
31 |
+
import gradio as gr
|
32 |
+
import mdtex2html
|
33 |
+
import torch
|
34 |
+
import os
|
35 |
+
|
36 |
+
CHECKPOINT_PATH=f'checkpoint-600'
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained("chatglm2-6b", trust_remote_code=True)
|
38 |
+
config = AutoConfig.from_pretrained("chatglm2-6b", trust_remote_code=True, pre_seq_len=128)
|
39 |
+
model = AutoModel.from_pretrained("chatglm2-6b", config=config, trust_remote_code=True)
|
40 |
+
prefix_state_dict = torch.load(os.path.join(CHECKPOINT_PATH, "pytorch_model.bin"))
|
41 |
+
new_prefix_state_dict = {}
|
42 |
+
for k, v in prefix_state_dict.items():
|
43 |
+
if k.startswith("transformer.prefix_encoder."):
|
44 |
+
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
|
45 |
+
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
|
46 |
+
|
47 |
+
model = model.half().cuda()
|
48 |
+
model.transformer.prefix_encoder.float()
|
49 |
+
model = model.eval()
|
50 |
+
|
51 |
+
|
52 |
+
"""Override Chatbot.postprocess"""
|
53 |
+
|
54 |
+
|
55 |
+
def postprocess(self, y):
|
56 |
+
if y is None:
|
57 |
+
return []
|
58 |
+
for i, (message, response) in enumerate(y):
|
59 |
+
y[i] = (
|
60 |
+
None if message is None else mdtex2html.convert((message)),
|
61 |
+
None if response is None else mdtex2html.convert(response),
|
62 |
+
)
|
63 |
+
return y
|
64 |
+
|
65 |
+
|
66 |
+
gr.Chatbot.postprocess = postprocess
|
67 |
+
|
68 |
+
|
69 |
+
def parse_text(text):
|
70 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
71 |
+
lines = text.split("\n")
|
72 |
+
lines = [line for line in lines if line != ""]
|
73 |
+
count = 0
|
74 |
+
for i, line in enumerate(lines):
|
75 |
+
if "```" in line:
|
76 |
+
count += 1
|
77 |
+
items = line.split('`')
|
78 |
+
if count % 2 == 1:
|
79 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
80 |
+
else:
|
81 |
+
lines[i] = f'<br></code></pre>'
|
82 |
+
else:
|
83 |
+
if i > 0:
|
84 |
+
if count % 2 == 1:
|
85 |
+
line = line.replace("`", "\`")
|
86 |
+
line = line.replace("<", "<")
|
87 |
+
line = line.replace(">", ">")
|
88 |
+
line = line.replace(" ", " ")
|
89 |
+
line = line.replace("*", "*")
|
90 |
+
line = line.replace("_", "_")
|
91 |
+
line = line.replace("-", "-")
|
92 |
+
line = line.replace(".", ".")
|
93 |
+
line = line.replace("!", "!")
|
94 |
+
line = line.replace("(", "(")
|
95 |
+
line = line.replace(")", ")")
|
96 |
+
line = line.replace("$", "$")
|
97 |
+
lines[i] = "<br>"+line
|
98 |
+
text = "".join(lines)
|
99 |
+
return text
|
100 |
+
|
101 |
+
|
102 |
+
def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
|
103 |
+
chatbot.append((parse_text(input), ""))
|
104 |
+
for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
|
105 |
+
return_past_key_values=True,
|
106 |
+
max_length=max_length, top_p=top_p,
|
107 |
+
temperature=temperature):
|
108 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
109 |
+
|
110 |
+
yield chatbot, history, past_key_values
|
111 |
+
|
112 |
+
|
113 |
+
def reset_user_input():
|
114 |
+
return gr.update(value='')
|
115 |
+
|
116 |
+
|
117 |
+
def reset_state():
|
118 |
+
return [], [], None
|
119 |
+
|
120 |
+
# Bert-VITS2
|
121 |
|
122 |
net_g = None
|
123 |
|
|
|
212 |
gr.Markdown("### <center>🍻 - 更多精彩应用,尽在[滔滔AI](http://www.talktalkai.com);滔滔AI,为爱滔滔!💕</center>")
|
213 |
with gr.Accordion("绫华", open=True):
|
214 |
gr.Markdown(image_markdown)
|
|
|
215 |
|
216 |
+
chatbot = gr.Chatbot()
|
217 |
+
with gr.Row():
|
218 |
+
with gr.Column(scale=4):
|
219 |
+
with gr.Column(scale=12):
|
220 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
|
221 |
+
container=False)
|
222 |
+
with gr.Column(min_width=32, scale=1):
|
223 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
224 |
+
with gr.Column(scale=1):
|
225 |
+
emptyBtn = gr.Button("Clear History")
|
226 |
+
max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
|
227 |
+
top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
|
228 |
+
temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
|
229 |
+
|
230 |
+
history = gr.State([])
|
231 |
+
past_key_values = gr.State(None)
|
232 |
+
|
233 |
+
submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
|
234 |
+
[chatbot, history, past_key_values], show_progress=True)
|
235 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
236 |
+
|
237 |
+
emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
|
238 |
|
239 |
+
|
240 |
with gr.Row():
|
241 |
with gr.Column():
|
242 |
text = gr.TextArea(label="Text", placeholder="Input Text Here",
|