From d5330406fa3d6e337d63579a51340cd36338817f Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:53:03 -0700 Subject: [PATCH] Add a rename menu for chat histories --- css/main.css | 8 ++++++++ modules/chat.py | 15 +++++++++++++++ modules/ui_chat.py | 20 +++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/css/main.css b/css/main.css index c5ee7159..50a0ee0b 100644 --- a/css/main.css +++ b/css/main.css @@ -595,3 +595,11 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* { list-style-position: outside !important; margin-top: 6px !important; } + +#past-chats-row { + margin-bottom: calc( -1 * var(--layout-gap) ); +} + +#rename-row label { + margin-top: var(--layout-gap); +} diff --git a/modules/chat.py b/modules/chat.py index 2b04dec6..334693ab 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -418,6 +418,21 @@ def save_history(history, unique_id, character, mode): f.write(json.dumps(history, indent=4)) +def rename_history(old_id, new_id, character, mode): + if shared.args.multi_user: + return + + old_p = get_history_file_path(old_id, character, mode) + new_p = get_history_file_path(new_id, character, mode) + if new_p.parent != old_p.parent: + logger.error(f"The following path is not allowed: {new_p}.") + elif new_p == old_p: + logger.info("The provided path is identical to the old one.") + else: + logger.info(f"Renaming {old_p} to {new_p}") + old_p.rename(new_p) + + def find_all_histories(state): if shared.args.multi_user: return [''] diff --git a/modules/ui_chat.py b/modules/ui_chat.py index 1e091c7d..4ec93ecb 100644 --- a/modules/ui_chat.py +++ b/modules/ui_chat.py @@ -62,12 +62,18 @@ def create_ui(): shared.gradio['send-chat-to-default'] = gr.Button('Send to default') shared.gradio['send-chat-to-notebook'] = gr.Button('Send to notebook') - with gr.Row(): + with gr.Row(elem_id='past-chats-row'): shared.gradio['unique_id'] = gr.Dropdown(label='Past chats', elem_classes=['slim-dropdown']) + shared.gradio['rename_chat'] = gr.Button('Rename', elem_classes='refresh-button') shared.gradio['delete_chat'] = gr.Button('🗑️', elem_classes='refresh-button') shared.gradio['delete_chat-cancel'] = gr.Button('Cancel', visible=False, elem_classes='refresh-button') shared.gradio['delete_chat-confirm'] = gr.Button('Confirm', variant='stop', visible=False, elem_classes='refresh-button') + with gr.Row(elem_id='rename-row'): + shared.gradio['rename_to'] = gr.Textbox(label='Rename to:', placeholder='New name', visible=False, elem_classes=['no-background']) + shared.gradio['rename_to-cancel'] = gr.Button('Cancel', visible=False, elem_classes='refresh-button') + shared.gradio['rename_to-confirm'] = gr.Button('Confirm', visible=False, elem_classes='refresh-button') + with gr.Row(): shared.gradio['start_with'] = gr.Textbox(label='Start reply with', placeholder='Sure thing!', value=shared.settings['start_with']) @@ -238,6 +244,18 @@ def create_event_handlers(): lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')).then( lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, gradio(clear_arr)) + shared.gradio['rename_chat'].click( + lambda x: x, gradio('unique_id'), gradio('rename_to')).then( + lambda: [gr.update(visible=True)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False) + + shared.gradio['rename_to-cancel'].click( + lambda: [gr.update(visible=False)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False) + + shared.gradio['rename_to-confirm'].click( + chat.rename_history, gradio('unique_id', 'rename_to', 'character_menu', 'mode'), None).then( + lambda: [gr.update(visible=False)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False).then( + lambda x, y: gr.update(choices=chat.find_all_histories(x), value=y), gradio('interface_state', 'rename_to'), gradio('unique_id')) + shared.gradio['load_chat_history'].upload( ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( chat.start_new_chat, gradio('interface_state'), gradio('history')).then(