fb700 commited on
Commit
e1d93c3
·
1 Parent(s): 64f9b17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -57
app.py CHANGED
@@ -1,70 +1,101 @@
1
  from transformers import AutoModel, AutoTokenizer
2
- import streamlit as st
3
- from streamlit_chat import message
4
-
5
-
6
- st.set_page_config(
7
- page_title="帛凡 ChatGLM-6b-fitness-RLHF 演示",
8
- page_icon=":robot:"
9
- )
10
-
11
-
12
- @st.cache_resource
13
- def get_model():
14
- tokenizer = AutoTokenizer.from_pretrained("fb700/chatglm-fitness-RLHF/chatglm_rlhf", trust_remote_code=True)
15
- #model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
16
- model = AutoModel.from_pretrained("D:\glm\chatglm_webui\chatglm-6b", trust_remote_code=True).quantize(8).half().cuda()
17
- model = model.eval()
18
- return tokenizer, model
19
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- MAX_TURNS = 20
22
- MAX_BOXES = MAX_TURNS * 2
23
 
24
 
25
- def predict(input, max_length, top_p, temperature, history=None):
26
- tokenizer, model = get_model()
27
- if history is None:
28
- history = []
29
 
30
- with container:
31
- if len(history) > 0:
32
- for i, (query, response) in enumerate(history):
33
- message(query, avatar_style="big-smile", key=str(i) + "_user")
34
- message(response, avatar_style="bottts", key=str(i))
35
 
36
- message(input, avatar_style="big-smile", key=str(len(history)) + "_user")
37
- st.write("AI正在回复:")
38
- with st.empty():
39
- for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
40
- temperature=temperature):
41
- query, response = history[-1]
42
- st.write(response)
43
 
44
- return history
45
 
 
 
46
 
47
- container = st.container()
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # create a prompt text for the text generation
50
- prompt_text = st.text_area(label="用户命令输入",
51
- height = 100,
52
- placeholder="请在这儿输入您的命令")
53
 
54
- max_length = st.sidebar.slider(
55
- 'max_length', 0, 40960, 20480, step=1
56
- )
57
- top_p = st.sidebar.slider(
58
- 'top_p', 0.0, 1.0, 0.6, step=0.01
59
- )
60
- temperature = st.sidebar.slider(
61
- 'temperature', 0.0, 1.0, 0.95, step=0.01
62
- )
63
 
64
- if 'state' not in st.session_state:
65
- st.session_state['state'] = []
66
 
67
- if st.button("发送", key="predict"):
68
- with st.spinner("AI正在思考,请稍等........"):
69
- # text generation
70
- st.session_state["state"] = predict(prompt_text, max_length, top_p, temperature, st.session_state["state"])
 
1
  from transformers import AutoModel, AutoTokenizer
2
+ import gradio as gr
3
+ import mdtex2html
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("fb700/chatglm-fitness-RLHF/chatglm_rlhf", trust_remote_code=True)
6
+ model = AutoModel.from_pretrained("fb700/chatglm-fitness-RLHF/chatglm_rlhf", trust_remote_code=True).quantize(8).half().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):
60
+ chatbot.append((parse_text(input), ""))
61
+ for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
62
+ temperature=temperature):
63
+ chatbot[-1] = (parse_text(input), parse_text(response))
64
 
65
+ yield chatbot, history
 
66
 
67
 
68
+ def reset_user_input():
69
+ return gr.update(value='')
 
 
70
 
 
 
 
 
 
71
 
72
+ def reset_state():
73
+ return [], []
 
 
 
 
 
74
 
 
75
 
76
+ with gr.Blocks() as demo:
77
+ gr.HTML("""<h1 align="center">帛凡 ChatGLM-6b-fitness-RLHF 演示</h1>""")
78
 
79
+ chatbot = gr.Chatbot()
80
+ with gr.Row():
81
+ with gr.Column(scale=4):
82
+ with gr.Column(scale=12):
83
+ user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
84
+ container=False)
85
+ with gr.Column(min_width=32, scale=1):
86
+ submitBtn = gr.Button("Submit", variant="primary")
87
+ with gr.Column(scale=1):
88
+ emptyBtn = gr.Button("Clear History")
89
+ max_length = gr.Slider(0, 409600, value=2048, step=1.0, label="Maximum length", interactive=True)
90
+ top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)
91
+ temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
92
 
93
+ history = gr.State([])
 
 
 
94
 
95
+ submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history],
96
+ show_progress=True)
97
+ submitBtn.click(reset_user_input, [], [user_input])
 
 
 
 
 
 
98
 
99
+ emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
 
100
 
101
+ demo.queue().launch(share=True, inbrowser=True)