-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_client.py
161 lines (136 loc) · 5.82 KB
/
gradio_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import gradio as gr
import mdtex2html
import re
def clean_response(text):
# 移除结尾的 '</s', 'USER', '<endoftext>'
return re.sub(r'</s$|USER$|<endoftext$', '', text)
def postprocess(self, y):
if y is None:
return []
for i, (message, response) in enumerate(y):
y[i] = (
None if message is None else mdtex2html.convert(message),
None if response is None else mdtex2html.convert(response),
)
return y
gr.Chatbot.postprocess = postprocess
def _parse_text(text):
lines = text.split("\n")
lines = [line for line in lines if line != ""]
count = 0
for i, line in enumerate(lines):
if "```" in line:
count += 1
items = line.split("`")
if count % 2 == 1:
lines[i] = f'<pre><code class="language-{items[-1]}">'
else:
lines[i] = f"<br></code></pre>"
else:
if i > 0:
if count % 2 == 1:
line = line.replace("`", r"\`")
line = line.replace("<", "<")
line = line.replace(">", ">")
line = line.replace(" ", " ")
line = line.replace("*", "*")
line = line.replace("_", "_")
line = line.replace("-", "-")
line = line.replace(".", ".")
line = line.replace("!", "!")
line = line.replace("(", "(")
line = line.replace(")", ")")
line = line.replace("$", "$")
lines[i] = "<br>" + line
text = "".join(lines)
return text
def main(
*,
model: str='Qwen/Qwen-14B',
share: bool=False,
inbrowser: bool=False,
server_port: int=8080,
server_name: str="127.0.0.1",
):
from openai_client import chat_compeletion_openai_stream, LOG
def predict(_query, _chatbot, _task_history, temperature,max_tokens,topp,topk,presence_penalty,frequency_penalty):
if topk == 0:
topk = -1
if topp == 0:
topp = 1
LOG.info(f"History: {_task_history}")
LOG.info(f"User: {_parse_text(_query)}")
_chatbot.append((_parse_text(_query), ""))
full_response = ""
messages = []
for his in _task_history:
messages.append({'role': 'user', 'content': his[0]})
messages.append({'role': 'assistant', 'content': his[1]})
messages.append({'role': 'user', 'content': _query})
for response in chat_compeletion_openai_stream(
model,
messages,
temperature=temperature,
max_tokens=max_tokens,
top_p=topp,
top_k=topk,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty
):
_chatbot[-1] = (_parse_text(_query), _parse_text(clean_response(response)))
yield _chatbot
full_response = response
_task_history.append((_query, full_response))
LOG.info(f"CCIIP-GPT: {clean_response(full_response)}")
def regenerate(_chatbot, _task_history):
if not _task_history:
yield _chatbot
return
item = _task_history.pop(-1)
_chatbot.pop(-1)
yield from predict(item[0], _chatbot, _task_history)
def reset_user_input():
return gr.update(value="")
def reset_state(_chatbot, _task_history):
_task_history.clear()
_chatbot.clear()
return _chatbot
with gr.Blocks() as demo:
gr.Markdown("""<center><font size=8>CCIIP LAB ChatBOT</center>""")
chatbot = gr.Chatbot(label='CCIIP-GPT', elem_classes="control-height")
query = gr.Textbox(lines=2, label='Input')
task_history = gr.State([])
with gr.Row():
empty_btn = gr.Button("🧹 Clear History (清除历史)")
submit_btn = gr.Button("🚀 Submit (发送)")
regen_btn = gr.Button("🤔️ Regenerate (重试)")
with gr.Column():
with gr.Accordion("⚙️ Advanced Settings (进阶设置)", open=False):
temperature = gr.components.Slider(minimum=0, maximum=1, value=0.7, label="Temperature (=0 greedy; otherwise do sample)")
max_tokens = gr.components.Slider(
minimum=1, maximum=2000, step=1, value=1024, label="Max Tokens"
)
topp = gr.components.Slider(minimum=0, maximum=1, value=1.0, label="Top p (do sample)")
topk = gr.components.Slider(minimum=-1, maximum=100, step=1, value=-1, label="Top k (do sample)")
presence_penalty = gr.components.Slider(
minimum=-2.0, maximum=2.0, step=0.1, value=0.0, label="Presence Penalty"
)
frequency_penalty = gr.components.Slider(
minimum=-2.0, maximum=2.0, step=0.1, value=0.0, label="Frequency Penalty"
)
# n = gr.components.Slider(minimum=1, maximum=10, step=1, value=4, label="Beams Number") + best_of
# submit_btn.click(predict, [query, chatbot, task_history], [chatbot], show_progress=True)
submit_btn.click(predict, [query, chatbot, task_history,temperature,max_tokens,topp,topk,presence_penalty,frequency_penalty], [chatbot], show_progress=True)
submit_btn.click(reset_user_input, [], [query])
empty_btn.click(reset_state, [chatbot, task_history], outputs=[chatbot], show_progress=True)
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True)
demo.queue().launch(
share=share,
inbrowser=inbrowser,
server_port=server_port,
server_name=server_name,
)
if __name__ == '__main__':
# debug : use jurigged (auto reload)
import defopt
defopt.run(main)