From 955b841d0fdcb36abc8b91988e6514e966d247c0 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Wed, 19 Apr 2023 10:59:20 -0400 Subject: [PATCH] fix temp folder --- javascript/black-orange.css | 2 +- modules/shared.py | 4 ++-- modules/ui_tempdir.py | 26 +++++++------------------- webui.py | 1 + 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/javascript/black-orange.css b/javascript/black-orange.css index 9b50d30df..cc9a200c9 100644 --- a/javascript/black-orange.css +++ b/javascript/black-orange.css @@ -83,7 +83,7 @@ svg.feather.feather-image, .feather .feather-image { display: none } #refresh_sd_model_checkpoint { height: 40px; margin-left: -14px; background: #333333; box-shadow: none; } #refresh_txt2img_styles, #refresh_img2img_styles, #open_folder_txt2img, #open_folder_img2img, #open_folder_extras, #footer, #style_pos_col, #style_neg_col, #roll_col, #save_zip_txt2img, #save_zip_img2img, #extras_upscaler_2, #extras_upscaler_2_visibility, #txt2img_res_switch_btn, #img2img_res_switch_btn, #txt2img_seed_resize_from_w, #txt2img_seed_resize_from_h, #txt2img_tiling { display: none; } #save-animation { border-radius: 0 !important; margin-bottom: 16px; background-color: #111111; } -#script_list { padding: 4px; margin-top: 20px; } +#script_list { padding: 4px; margin-top: 20px; margin-bottom: 20px; } #settings > div.flex-wrap { width: 15em; } #tab_extensions table { background-color: #222222; } #txt2img_actions_column, #img2img_actions_column { min-width: 260px !important; max-width: 260px !important; } diff --git a/modules/shared.py b/modules/shared.py index ebf886c2d..cc5237556 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -263,6 +263,8 @@ options_templates.update(options_section(('sd', "Stable Diffusion"), { })) options_templates.update(options_section(('system-paths', "System Paths"), { + "temp_dir": OptionInfo("", "Directory for temporary images; leave empty for default"), + "clean_temp_dir_at_start": OptionInfo(True, "Cleanup non-default temporary directory when starting webui"), "ckpt_dir": OptionInfo(os.path.join(paths.models_path, 'Stable-diffusion'), "Path to directory with stable diffusion checkpoints"), "vae_dir": OptionInfo(os.path.join(paths.models_path, 'VAE'), "Path to directory with VAE files"), "embeddings_dir": OptionInfo(os.path.join(paths.models_path, 'embeddings'), "Embeddings directory for textual inversion"), @@ -309,8 +311,6 @@ options_templates.update(options_section(('saving-images', "Image options"), { "use_save_to_dirs_for_ui": OptionInfo(False, "When using \"Save\" button, save images to a subdirectory"), "directories_filename_pattern": OptionInfo("[date]", "Directory name pattern", component_args=hide_dirs), "directories_max_prompt_words": OptionInfo(8, "Max prompt words for [prompt_words] pattern", gr.Slider, {"minimum": 1, "maximum": 20, "step": 1, **hide_dirs}), - "temp_dir": OptionInfo("", "Directory for temporary images; leave empty for default"), - "clean_temp_dir_at_start": OptionInfo(True, "Cleanup non-default temporary directory when starting webui"), })) options_templates.update(options_section(('saving-paths', "Image Paths"), { diff --git a/modules/ui_tempdir.py b/modules/ui_tempdir.py index 21945235e..bef3cdeba 100644 --- a/modules/ui_tempdir.py +++ b/modules/ui_tempdir.py @@ -2,11 +2,8 @@ import os import tempfile from collections import namedtuple from pathlib import Path - import gradio as gr - from PIL import PngImagePlugin - from modules import shared @@ -16,7 +13,6 @@ Savedfile = namedtuple("Savedfile", ["name"]) def register_tmp_file(gradio, filename): if hasattr(gradio, 'temp_file_sets'): # gradio 3.15 gradio.temp_file_sets[0] = gradio.temp_file_sets[0] | {os.path.abspath(filename)} - if hasattr(gradio, 'temp_dirs'): # gradio 3.9 gradio.temp_dirs = gradio.temp_dirs | {os.path.abspath(os.path.dirname(filename))} @@ -24,31 +20,25 @@ def register_tmp_file(gradio, filename): def check_tmp_file(gradio, filename): if hasattr(gradio, 'temp_file_sets'): return any([filename in fileset for fileset in gradio.temp_file_sets]) - if hasattr(gradio, 'temp_dirs'): return any(Path(temp_dir).resolve() in Path(filename).resolve().parents for temp_dir in gradio.temp_dirs) - return False -def save_pil_to_file(pil_image, dir=None): +def save_pil_to_file(pil_image, dir=None): # pylint: disable=redefined-builtin already_saved_as = getattr(pil_image, 'already_saved_as', None) if already_saved_as and os.path.isfile(already_saved_as): register_tmp_file(shared.demo, already_saved_as) - file_obj = Savedfile(already_saved_as) return file_obj - if shared.opts.temp_dir != "": dir = shared.opts.temp_dir - use_metadata = False metadata = PngImagePlugin.PngInfo() for key, value in pil_image.info.items(): if isinstance(key, str) and isinstance(value, str): metadata.add_text(key, value) use_metadata = True - file_obj = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir=dir) pil_image.save(file_obj, pnginfo=(metadata if use_metadata else None)) return file_obj @@ -59,11 +49,11 @@ gr.processing_utils.save_pil_to_file = save_pil_to_file def on_tmpdir_changed(): - if shared.opts.temp_dir == "" or shared.demo is None: + if shared.opts.temp_dir == "": return - - os.makedirs(shared.opts.temp_dir, exist_ok=True) - + if not os.path.isdir(shared.opts.temp_dir): + print('Creating temporary folder:', shared.opts.temp_dir) + os.makedirs(shared.opts.temp_dir, exist_ok=True) register_tmp_file(shared.demo, os.path.join(shared.opts.temp_dir, "x")) @@ -71,12 +61,10 @@ def cleanup_tmpdr(): temp_dir = shared.opts.temp_dir if temp_dir == "" or not os.path.isdir(temp_dir): return - - for root, dirs, files in os.walk(temp_dir, topdown=False): + for root, _dirs, files in os.walk(temp_dir, topdown=False): for name in files: _, extension = os.path.splitext(name) - if extension != ".png": + if extension != ".png" and extension != ".jpg" and extension != ".webp": continue - filename = os.path.join(root, name) os.remove(filename) diff --git a/webui.py b/webui.py index c512cf093..bd1b776ea 100644 --- a/webui.py +++ b/webui.py @@ -153,6 +153,7 @@ def create_api(app): def start_ui(): initialize() + ui_tempdir.on_tmpdir_changed() if shared.opts.clean_temp_dir_at_start: ui_tempdir.cleanup_tmpdr() startup_timer.record("cleanup")