From 8265d45db88cbead324ec3bda1fcd6e1325c2f65 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 11 Apr 2023 22:21:41 -0300 Subject: [PATCH] Add send dummy message/reply buttons Useful for starting a new reply. --- modules/chat.py | 17 ++++++++++++++++- server.py | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/modules/chat.py b/modules/chat.py index e922e7c..1b3e27d 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -171,7 +171,7 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False): shared.history['visible'].append(['', '']) if _continue: - sep = list(map(lambda x: ' ' if x[-1] != ' ' else '', last_reply)) + sep = list(map(lambda x: ' ' if len(x) > 0 and x[-1] != ' ' else '', last_reply)) shared.history['internal'][-1] = [text, f'{last_reply[0]}{sep[0]}{reply}'] shared.history['visible'][-1] = [visible_text, f'{last_reply[1]}{sep[1]}{visible_reply}'] else: @@ -267,6 +267,21 @@ def replace_last_reply(text, name1, name2, mode): return chat_html_wrapper(shared.history['visible'], name1, name2, mode) +def send_dummy_message(text, name1, name2, mode): + shared.history['visible'].append([text, '']) + shared.history['internal'].append([apply_extensions(text, "input"), '']) + return chat_html_wrapper(shared.history['visible'], name1, name2, mode) + + +def send_dummy_reply(text, name1, name2, mode): + if len(shared.history['visible']) > 0 and not shared.history['visible'][-1][1] == '': + shared.history['visible'].append(['', '']) + shared.history['internal'].append(['', '']) + shared.history['visible'][-1][1] = text + shared.history['internal'][-1][1] = apply_extensions(text, "input") + return chat_html_wrapper(shared.history['visible'], name1, name2, mode) + + def clear_html(): return chat_html_wrapper([], "", "") diff --git a/server.py b/server.py index 06bec5d..1491db9 100644 --- a/server.py +++ b/server.py @@ -399,13 +399,15 @@ def create_interface(): shared.gradio['Continue'] = gr.Button('Continue') shared.gradio['Impersonate'] = gr.Button('Impersonate') with gr.Row(): - shared.gradio['Copy last reply'] = gr.Button('Copy last reply') + shared.gradio['Send dummy message'] = gr.Button('Send dummy message') + shared.gradio['Send dummy reply'] = gr.Button('Send dummy reply') shared.gradio['Replace last reply'] = gr.Button('Replace last reply') - shared.gradio['Remove last'] = gr.Button('Remove last') - + shared.gradio['Copy last reply'] = gr.Button('Copy last reply') + with gr.Row(): shared.gradio['Clear history'] = gr.Button('Clear history') shared.gradio['Clear history-confirm'] = gr.Button('Confirm', variant="stop", visible=False) shared.gradio['Clear history-cancel'] = gr.Button('Cancel', visible=False) + shared.gradio['Remove last'] = gr.Button('Remove last') shared.gradio["mode"] = gr.Radio(choices=["cai-chat", "chat", "instruct"], value="cai-chat", label="Mode") shared.gradio["Instruction templates"] = gr.Dropdown(choices=get_available_instruction_templates(), label="Instruction template", value="None", visible=False, info="Change this according to the model/LoRA that you are using.") @@ -502,6 +504,16 @@ def create_interface(): lambda x: '', shared.gradio['textbox'], shared.gradio['textbox'], show_progress=False).then( chat.save_history, shared.gradio['mode'], None, show_progress=False) + shared.gradio['Send dummy message'].click( + chat.send_dummy_message, [shared.gradio[k] for k in ['textbox', 'name1', 'name2', 'mode']], shared.gradio['display'], show_progress=shared.args.no_stream).then( + lambda x: '', shared.gradio['textbox'], shared.gradio['textbox'], show_progress=False).then( + chat.save_history, shared.gradio['mode'], None, show_progress=False) + + shared.gradio['Send dummy reply'].click( + chat.send_dummy_reply, [shared.gradio[k] for k in ['textbox', 'name1', 'name2', 'mode']], shared.gradio['display'], show_progress=shared.args.no_stream).then( + lambda x: '', shared.gradio['textbox'], shared.gradio['textbox'], show_progress=False).then( + chat.save_history, shared.gradio['mode'], None, show_progress=False) + shared.gradio['Clear history-confirm'].click( lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, clear_arr).then( chat.clear_chat_log, [shared.gradio[k] for k in ['name1', 'name2', 'greeting', 'mode']], shared.gradio['display']).then(