Load the nearest character after deleting a character

Instead of the first.
This commit is contained in:
oobabooga 2024-01-09 06:24:27 -08:00
parent ba87b9993d
commit 123f27a3c5
3 changed files with 15 additions and 5 deletions

View File

@ -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) {

View File

@ -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):

View File

@ -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'))