From 123f27a3c5e72499fcb44e07e76fbf4271a3fbc9 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 9 Jan 2024 06:24:27 -0800 Subject: [PATCH] Load the nearest character after deleting a character Instead of the first. --- css/main.css | 1 + modules/chat.py | 12 ++++++++++-- modules/ui_file_saving.py | 7 ++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/css/main.css b/css/main.css index a3b7ded9..43ad82b2 100644 --- a/css/main.css +++ b/css/main.css @@ -193,6 +193,7 @@ button { max-width: 500px; background-color: var(--input-background-fill); border: var(--input-border-width) solid var(--input-border-color) !important; + padding: 10px; } .file-saver > :first-child > :nth-child(2) { diff --git a/modules/chat.py b/modules/chat.py index 188453d3..265d220a 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -14,6 +14,7 @@ from jinja2.sandbox import ImmutableSandboxedEnvironment from PIL import Image import modules.shared as shared +from modules import utils from modules.extensions import apply_extensions from modules.html_generator import chat_html_wrapper, make_thumbnail from modules.logging_colors import logger @@ -526,15 +527,22 @@ def load_history_after_deletion(state, idx): if shared.args.multi_user: return start_new_chat(state) + idx = min(int(idx), len(histories) - 1) histories = find_all_histories(state) if len(histories) > 0: - history = load_history(histories[int(idx)], state['character_menu'], state['mode']) + history = load_history(histories[idx], state['character_menu'], state['mode']) else: history = start_new_chat(state) histories = find_all_histories(state) - return history, gr.update(choices=histories, value=histories[int(idx)]) + return history, gr.update(choices=histories, value=histories[idx]) + + +def update_character_menu_after_deletion(idx): + characters = utils.get_available_characters() + idx = min(int(idx), len(characters) - 1) + return gr.update(choices=characters, value=characters[idx]) def load_history(unique_id, character, mode): diff --git a/modules/ui_file_saving.py b/modules/ui_file_saving.py index 39ab41d4..9e034495 100644 --- a/modules/ui_file_saving.py +++ b/modules/ui_file_saving.py @@ -34,8 +34,8 @@ def create_ui(): with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['character_deleter']: gr.Markdown('Confirm the character deletion?') with gr.Row(): - shared.gradio['delete_character_confirm'] = gr.Button('Delete', elem_classes="small-button", variant='stop', interactive=not mu) shared.gradio['delete_character_cancel'] = gr.Button('Cancel', elem_classes="small-button") + shared.gradio['delete_character_confirm'] = gr.Button('Delete', elem_classes="small-button", variant='stop', interactive=not mu) # Preset saver with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['preset_saver']: @@ -64,9 +64,10 @@ def create_event_handlers(): lambda x: gr.update(choices=utils.get_available_characters(), value=x), gradio('save_character_filename'), gradio('character_menu')) shared.gradio['delete_character_confirm'].click( + lambda x: str(utils.get_available_characters().index(x)), gradio('character_menu'), gradio('temporary_text')).then( chat.delete_character, gradio('character_menu'), None).then( - lambda: gr.update(visible=False), None, gradio('character_deleter')).then( - lambda: gr.update(choices=(characters := utils.get_available_characters()), value=characters[0]), None, gradio('character_menu')) + chat.update_character_menu_after_deletion, gradio('temporary_text'), gradio('character_menu')).then( + lambda: gr.update(visible=False), None, gradio('character_deleter')) shared.gradio['save_character_cancel'].click(lambda: gr.update(visible=False), None, gradio('character_saver')) shared.gradio['delete_character_cancel'].click(lambda: gr.update(visible=False), None, gradio('character_deleter'))