diff --git a/web_demo.py b/web_demo.py index 89cb28e..bd25f7f 100755 --- a/web_demo.py +++ b/web_demo.py @@ -104,35 +104,35 @@ def _parse_text(text): def _launch_demo(args, model, tokenizer): - task_history = [] - def predict(_query, _chatbot): - print("User: " + _parse_text(_query)) + def predict(_query, _chatbot, _task_history): + print(f"User: {_parse_text(_query)}") _chatbot.append((_parse_text(_query), "")) full_response = "" - for response in model.chat_stream(tokenizer, _query, history=task_history): + for response in model.chat_stream(tokenizer, _query, history=_task_history): _chatbot[-1] = (_parse_text(_query), _parse_text(response)) yield _chatbot full_response = _parse_text(response) - task_history.append((_query, full_response)) - print("Qwen-7B-Chat: " + _parse_text(full_response)) + print(f"History: {_task_history}") + _task_history.append((_query, full_response)) + print(f"Qwen-7B-Chat: {_parse_text(full_response)}") - def regenerate(_chatbot): - if not task_history: + def regenerate(_chatbot, _task_history): + if not _task_history: yield _chatbot return - item = task_history.pop(-1) + item = _task_history.pop(-1) _chatbot.pop(-1) - yield from predict(item[0], _chatbot) + yield from predict(item[0], _chatbot, _task_history) def reset_user_input(): return gr.update(value="") - def reset_state(): - task_history.clear() + def reset_state(_task_history): + _task_history.clear() return [] with gr.Blocks() as demo: @@ -153,16 +153,17 @@ Qwen-7B-Chat chatbot = gr.Chatbot(label='Qwen-7B-Chat', 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 (้‡่ฏ•)") - submit_btn.click(predict, [query, chatbot], [chatbot], show_progress=True) + submit_btn.click(predict, [query, chatbot, task_history], [chatbot], show_progress=True) submit_btn.click(reset_user_input, [], [query]) - empty_btn.click(reset_state, outputs=[chatbot], show_progress=True) - regen_btn.click(regenerate, [chatbot], [chatbot], show_progress=True) + empty_btn.click(reset_state, [task_history], outputs=[chatbot], show_progress=True) + regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True) gr.Markdown("""\ Note: This demo is governed by the original license of Qwen-7B. \