Update web_demo.py

pull/155/head
haofanurusai 2023-03-19 17:16:35 +08:00
parent 649e3f280a
commit 88ddfdd5dc
1 changed files with 14 additions and 4 deletions

View File

@ -13,17 +13,27 @@ def predict(input, max_length, top_p, temperature, history=None):
if history is None: if history is None:
history = [] history = []
response = '' response = ''
updates = []
for query, response in history:
updates.append(gr.update(visible=True, value="用户:" + query))
updates.append(gr.update(visible=True, value="ChatGLM-6B" + response))
flag = True
for delta, seq, history in model.chat_stream(tokenizer, input, history, max_length=max_length, top_p=top_p, for delta, seq, history in model.chat_stream(tokenizer, input, history, max_length=max_length, top_p=top_p,
temperature=temperature): temperature=temperature):
updates = []
response += delta response += delta
if flag:
updates.append(gr.update(visible=True, value="用户:" + input)) updates.append(gr.update(visible=True, value="用户:" + input))
updates.append(gr.update(visible=True, value="ChatGLM-6B" + response)) updates.append(gr.update(visible=True, value="ChatGLM-6B" + response))
flag = False
else:
updates[-2]=gr.update(visible=True, value="用户:" + input)
updates[-1]=gr.update(visible=True, value="ChatGLM-6B" + response)
if len(updates) < MAX_BOXES: if len(updates) < MAX_BOXES:
updates = updates + [gr.Textbox.update(visible=False)] * (MAX_BOXES - len(updates)) updates = updates + [gr.Textbox.update(visible=False)] * (MAX_BOXES - len(updates))
yield [history] + updates yield [history] + updates
with gr.Blocks() as demo: with gr.Blocks() as demo:
state = gr.State([]) state = gr.State([])
text_boxes = [] text_boxes = []