From 7ef241435719df1555cf5bdb9d70a6dbddad7b71 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:38:20 -0700 Subject: [PATCH] UI: Make the file saving dialogs more robust --- modules/ui_file_saving.py | 64 ++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/modules/ui_file_saving.py b/modules/ui_file_saving.py index 4b5ac7c6..b696b655 100644 --- a/modules/ui_file_saving.py +++ b/modules/ui_file_saving.py @@ -2,6 +2,7 @@ import gradio as gr from modules import chat, presets, shared, ui, utils from modules.utils import gradio +import traceback def create_ui(): @@ -68,31 +69,67 @@ def create_event_handlers(): shared.gradio['delete_character_cancel'].click(lambda: gr.update(visible=False), None, gradio('character_deleter'), show_progress=False) +def handle_save_preset_confirm_click(filename, contents): + try: + utils.save_file(f"presets/{filename}.yaml", contents) + available_presets = utils.get_available_presets() + output = gr.update(choices=available_presets, value=filename), + except Exception: + output = gr.update() + traceback.print_exc() + + return [ + output, + gr.update(visible=False) + ] + + def handle_save_confirm_click(root, filename, contents): - utils.save_file(root + filename, contents) + try: + utils.save_file(root + filename, contents) + except Exception: + traceback.print_exc() + return gr.update(visible=False) def handle_delete_confirm_click(root, filename): - utils.delete_file(root + filename) + try: + utils.delete_file(root + filename) + except Exception: + traceback.print_exc() + return gr.update(visible=False) def handle_save_character_confirm_click(name2, greeting, context, character_picture, filename): - chat.save_character(name2, greeting, context, character_picture, filename) - available_characters = utils.get_available_characters() + try: + chat.save_character(name2, greeting, context, character_picture, filename) + available_characters = utils.get_available_characters() + output = gr.update(choices=available_characters, value=filename), + except Exception: + output = gr.update() + traceback.print_exc() return [ - gr.update(choices=available_characters, value=filename), + output, gr.update(visible=False) ] def handle_delete_character_confirm_click(character): - index = str(utils.get_available_characters().index(character)) - chat.delete_character(character) - output = chat.update_character_menu_after_deletion(index) - return [output, gr.update(visible=False)] + try: + index = str(utils.get_available_characters().index(character)) + chat.delete_character(character) + output = chat.update_character_menu_after_deletion(index) + except Exception: + output = gr.update() + traceback.print_exc() + + return [ + output, + gr.update(visible=False) + ] def handle_save_preset_click(state): @@ -104,15 +141,6 @@ def handle_save_preset_click(state): ] -def handle_save_preset_confirm_click(filename, contents): - utils.save_file(f"presets/{filename}.yaml", contents) - available_presets = utils.get_available_presets() - return [ - gr.update(choices=available_presets, value=filename), - gr.update(visible=False) - ] - - def handle_delete_preset_click(preset): return [ f"{preset}.yaml",