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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Iterator
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import run
|
7 |
+
|
8 |
+
DEFAULT_SYSTEM_PROMPT = """\
|
9 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
10 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\
|
11 |
+
"""
|
12 |
+
MAX_MAX_NEW_TOKENS = 2048
|
13 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
14 |
+
MAX_INPUT_TOKEN_LENGTH = 4000
|
15 |
+
|
16 |
+
DESCRIPTION = """
|
17 |
+
# Baichuan-13B-Chat
|
18 |
+
"""
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
def clear_and_save_textbox(message: str) -> tuple[str, str]:
|
24 |
+
return '', message
|
25 |
+
|
26 |
+
|
27 |
+
def display_input(message: str,
|
28 |
+
history: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
29 |
+
history.append((message, ''))
|
30 |
+
return history
|
31 |
+
|
32 |
+
|
33 |
+
def delete_prev_fn(
|
34 |
+
history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
|
35 |
+
try:
|
36 |
+
message, _ = history.pop()
|
37 |
+
except IndexError:
|
38 |
+
message = ''
|
39 |
+
return history, message or ''
|
40 |
+
|
41 |
+
|
42 |
+
def generate(
|
43 |
+
message: str,
|
44 |
+
history_with_input: list[tuple[str, str]],
|
45 |
+
system_prompt: str,
|
46 |
+
max_new_tokens: int,
|
47 |
+
temperature: float,
|
48 |
+
top_p: float,
|
49 |
+
top_k: int,
|
50 |
+
) -> Iterator[list[tuple[str, str]]]:
|
51 |
+
if max_new_tokens > MAX_MAX_NEW_TOKENS:
|
52 |
+
raise ValueError
|
53 |
+
|
54 |
+
history = history_with_input[:-1]
|
55 |
+
generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
|
56 |
+
try:
|
57 |
+
first_response = next(generator)
|
58 |
+
yield history + [(message, first_response)]
|
59 |
+
except StopIteration:
|
60 |
+
yield history + [(message, '')]
|
61 |
+
for response in generator:
|
62 |
+
yield history + [(message, response)]
|
63 |
+
|
64 |
+
|
65 |
+
def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
|
66 |
+
generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 8192, 1, 0.95, 50)
|
67 |
+
for x in generator:
|
68 |
+
pass
|
69 |
+
return '', x
|
70 |
+
|
71 |
+
|
72 |
+
def check_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
|
73 |
+
a = 1
|
74 |
+
|
75 |
+
|
76 |
+
with gr.Blocks(css='style.css') as demo:
|
77 |
+
gr.Markdown(DESCRIPTION)
|
78 |
+
gr.DuplicateButton(value='Duplicate Space for private use',
|
79 |
+
elem_id='duplicate-button')
|
80 |
+
|
81 |
+
with gr.Group():
|
82 |
+
chatbot = gr.Chatbot(label='Chatbot')
|
83 |
+
with gr.Row():
|
84 |
+
textbox = gr.Textbox(
|
85 |
+
container=False,
|
86 |
+
show_label=False,
|
87 |
+
placeholder='Type a message...',
|
88 |
+
scale=10,
|
89 |
+
)
|
90 |
+
submit_button = gr.Button('Submit',
|
91 |
+
variant='primary',
|
92 |
+
scale=1,
|
93 |
+
min_width=0)
|
94 |
+
with gr.Row():
|
95 |
+
retry_button = gr.Button('🔄 Retry', variant='secondary')
|
96 |
+
undo_button = gr.Button('↩️ Undo', variant='secondary')
|
97 |
+
clear_button = gr.Button('🗑️ Clear', variant='secondary')
|
98 |
+
|
99 |
+
saved_input = gr.State()
|
100 |
+
|
101 |
+
with gr.Accordion(label='Advanced options', open=False):
|
102 |
+
system_prompt = gr.Textbox(label='System prompt',
|
103 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
104 |
+
lines=6)
|
105 |
+
max_new_tokens = gr.Slider(
|
106 |
+
label='Max new tokens',
|
107 |
+
minimum=1,
|
108 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
109 |
+
step=1,
|
110 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
111 |
+
)
|
112 |
+
temperature = gr.Slider(
|
113 |
+
label='Temperature',
|
114 |
+
minimum=0.1,
|
115 |
+
maximum=4.0,
|
116 |
+
step=0.1,
|
117 |
+
value=1.0,
|
118 |
+
)
|
119 |
+
top_p = gr.Slider(
|
120 |
+
label='Top-p (nucleus sampling)',
|
121 |
+
minimum=0.05,
|
122 |
+
maximum=1.0,
|
123 |
+
step=0.05,
|
124 |
+
value=0.95,
|
125 |
+
)
|
126 |
+
top_k = gr.Slider(
|
127 |
+
label='Top-k',
|
128 |
+
minimum=1,
|
129 |
+
maximum=1000,
|
130 |
+
step=1,
|
131 |
+
value=50,
|
132 |
+
)
|
133 |
+
|
134 |
+
gr.Examples(
|
135 |
+
examples=[
|
136 |
+
'用中文回答,When is the best time to visit Beijing, and do you have any suggestions for me?',
|
137 |
+
'用英文回答,特朗普是谁?',
|
138 |
+
'Hello there! How are you doing?',
|
139 |
+
'Can you explain briefly to me what is the Python programming language?',
|
140 |
+
'Explain the plot of Cinderella in a sentence.',
|
141 |
+
'How many hours does it take a man to eat a Helicopter?',
|
142 |
+
"Write a 100-word article on 'Benefits of Open-Source in AI research'",
|
143 |
+
],
|
144 |
+
inputs=textbox,
|
145 |
+
outputs=[textbox, chatbot],
|
146 |
+
fn=process_example,
|
147 |
+
cache_examples=True,
|
148 |
+
)
|
149 |
+
|
150 |
+
gr.Markdown(LICENSE)
|
151 |
+
|
152 |
+
textbox.submit(
|
153 |
+
fn=clear_and_save_textbox,
|
154 |
+
inputs=textbox,
|
155 |
+
outputs=[textbox, saved_input],
|
156 |
+
api_name=False,
|
157 |
+
queue=False,
|
158 |
+
).then(
|
159 |
+
fn=display_input,
|
160 |
+
inputs=[saved_input, chatbot],
|
161 |
+
outputs=chatbot,
|
162 |
+
api_name=False,
|
163 |
+
queue=False,
|
164 |
+
).then(
|
165 |
+
fn=check_input_token_length,
|
166 |
+
inputs=[saved_input, chatbot, system_prompt],
|
167 |
+
api_name=False,
|
168 |
+
queue=False,
|
169 |
+
).success(
|
170 |
+
fn=generate,
|
171 |
+
inputs=[
|
172 |
+
saved_input,
|
173 |
+
chatbot,
|
174 |
+
system_prompt,
|
175 |
+
max_new_tokens,
|
176 |
+
temperature,
|
177 |
+
top_p,
|
178 |
+
top_k,
|
179 |
+
],
|
180 |
+
outputs=chatbot,
|
181 |
+
api_name=False,
|
182 |
+
)
|
183 |
+
|
184 |
+
button_event_preprocess = submit_button.click(
|
185 |
+
fn=clear_and_save_textbox,
|
186 |
+
inputs=textbox,
|
187 |
+
outputs=[textbox, saved_input],
|
188 |
+
api_name=False,
|
189 |
+
queue=False,
|
190 |
+
).then(
|
191 |
+
fn=display_input,
|
192 |
+
inputs=[saved_input, chatbot],
|
193 |
+
outputs=chatbot,
|
194 |
+
api_name=False,
|
195 |
+
queue=False,
|
196 |
+
).then(
|
197 |
+
fn=check_input_token_length,
|
198 |
+
inputs=[saved_input, chatbot, system_prompt],
|
199 |
+
api_name=False,
|
200 |
+
queue=False,
|
201 |
+
).success(
|
202 |
+
fn=generate,
|
203 |
+
inputs=[
|
204 |
+
saved_input,
|
205 |
+
chatbot,
|
206 |
+
system_prompt,
|
207 |
+
max_new_tokens,
|
208 |
+
temperature,
|
209 |
+
top_p,
|
210 |
+
top_k,
|
211 |
+
],
|
212 |
+
outputs=chatbot,
|
213 |
+
api_name=False,
|
214 |
+
)
|
215 |
+
|
216 |
+
retry_button.click(
|
217 |
+
fn=delete_prev_fn,
|
218 |
+
inputs=chatbot,
|
219 |
+
outputs=[chatbot, saved_input],
|
220 |
+
api_name=False,
|
221 |
+
queue=False,
|
222 |
+
).then(
|
223 |
+
fn=display_input,
|
224 |
+
inputs=[saved_input, chatbot],
|
225 |
+
outputs=chatbot,
|
226 |
+
api_name=False,
|
227 |
+
queue=False,
|
228 |
+
).then(
|
229 |
+
fn=generate,
|
230 |
+
inputs=[
|
231 |
+
saved_input,
|
232 |
+
chatbot,
|
233 |
+
system_prompt,
|
234 |
+
max_new_tokens,
|
235 |
+
temperature,
|
236 |
+
top_p,
|
237 |
+
top_k,
|
238 |
+
],
|
239 |
+
outputs=chatbot,
|
240 |
+
api_name=False,
|
241 |
+
)
|
242 |
+
|
243 |
+
undo_button.click(
|
244 |
+
|
245 |
+
fn=delete_prev_fn,
|
246 |
+
inputs=chatbot,
|
247 |
+
outputs=[chatbot, saved_input],
|
248 |
+
api_name=False,
|
249 |
+
queue=False,
|
250 |
+
).then(
|
251 |
+
fn=lambda x: x,
|
252 |
+
inputs=[saved_input],
|
253 |
+
outputs=textbox,
|
254 |
+
api_name=False,
|
255 |
+
queue=False,
|
256 |
+
)
|
257 |
+
|
258 |
+
clear_button.click(
|
259 |
+
fn=lambda: ([], ''),
|
260 |
+
outputs=[chatbot, saved_input],
|
261 |
+
queue=False,
|
262 |
+
api_name=False,
|
263 |
+
)
|
264 |
+
|
265 |
+
demo.queue(max_size=20).launch()
|