From 70335d1474c8cdb27ca2baee388977089dff6719 Mon Sep 17 00:00:00 2001 From: steveyourcreativepeople Date: Fri, 19 Jan 2024 21:27:35 -0500 Subject: [PATCH 1/9] Added options to set the Gradio cache path and clear cache on launch. --- launch.py | 28 +++++++++++++++++++++++++++- modules/config.py | 11 +++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/launch.py b/launch.py index e98045f6d6..bcff91e248 100644 --- a/launch.py +++ b/launch.py @@ -1,6 +1,7 @@ import os import sys import ssl +import shutil print('[System ARGV] ' + str(sys.argv)) @@ -22,12 +23,37 @@ from modules.launch_util import is_installed, run, python, run_pip, requirements_met from modules.model_loader import load_file_from_url from modules.config import path_checkpoints, path_loras, path_vae_approx, path_fooocus_expansion, \ - checkpoint_downloads, path_embeddings, embeddings_downloads, lora_downloads + checkpoint_downloads, path_embeddings, embeddings_downloads, lora_downloads, path_cache, clear_cache REINSTALL_ALL = False TRY_INSTALL_XFORMERS = False +print(str(path_cache)) + +def cleanup_temp(folder_path): + try: + shutil.rmtree(folder_path) + print("All subfolders deleted successfully.") + except Exception as e: + print(f"Error: {e}") + +if bool(path_cache): + try: + os.makedirs(path_cache, exist_ok=True) + os.environ['GRADIO_DATA_DIR'] = path_cache + print("Using a custom path for cache.") + except Exception as e: + print(f"Error: {e}\nUsing the default path for Gradio cache.") + clear_cache = False + if bool(clear_cache): + cleanup_temp(path_cache) + else: + print("Cache is not cleared on load.") +else: + print("Using the default path for Gradio cache.") + if bool(clear_cache): + print('You must use a custom path to clear cache on load.\nAdd a path to path_cache in config.txt') def prepare_environment(): torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121") diff --git a/modules/config.py b/modules/config.py index c7af33dbfa..9af67cd1dd 100644 --- a/modules/config.py +++ b/modules/config.py @@ -315,6 +315,17 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ ], validator=lambda x: isinstance(x, list) and all(isinstance(v, str) for v in x) ) +path_cache = get_config_item_or_set_default( + key='path_cache', + default_value='', + validator=lambda x: isinstance(x, str), + disable_empty_as_none=True +) +clear_cache = get_config_item_or_set_default( + key='clear_cache', + default_value=False, + validator=lambda x: isinstance(x, bool) +) example_inpaint_prompts = [[x] for x in example_inpaint_prompts] From a3ed3d3a7e51b7539627e9dbfa06f43e998917f9 Mon Sep 17 00:00:00 2001 From: steveyourcreativepeople Date: Fri, 19 Jan 2024 21:49:17 -0500 Subject: [PATCH 2/9] Renamed cache to temp --- launch.py | 32 +++++++++++++++----------------- modules/config.py | 8 ++++---- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/launch.py b/launch.py index bcff91e248..e5d5a653a0 100644 --- a/launch.py +++ b/launch.py @@ -23,37 +23,35 @@ from modules.launch_util import is_installed, run, python, run_pip, requirements_met from modules.model_loader import load_file_from_url from modules.config import path_checkpoints, path_loras, path_vae_approx, path_fooocus_expansion, \ - checkpoint_downloads, path_embeddings, embeddings_downloads, lora_downloads, path_cache, clear_cache + checkpoint_downloads, path_embeddings, embeddings_downloads, lora_downloads, gradio_temp_path, gradio_temp_clear REINSTALL_ALL = False TRY_INSTALL_XFORMERS = False -print(str(path_cache)) def cleanup_temp(folder_path): try: shutil.rmtree(folder_path) - print("All subfolders deleted successfully.") + print("Gradio Temp deleted successfully.") except Exception as e: - print(f"Error: {e}") + print(f"Error: {e}\nGradio Temp not deleted.") -if bool(path_cache): +if bool(gradio_temp_path): try: - os.makedirs(path_cache, exist_ok=True) - os.environ['GRADIO_DATA_DIR'] = path_cache - print("Using a custom path for cache.") + os.makedirs(gradio_temp_path, exist_ok=True) + os.environ['GRADIO_DATA_DIR'] = gradio_temp_path + print("Using custom path for Gradio Temp.") except Exception as e: - print(f"Error: {e}\nUsing the default path for Gradio cache.") - clear_cache = False - if bool(clear_cache): - cleanup_temp(path_cache) - else: - print("Cache is not cleared on load.") + print(f"Error: {e}\nUsing default path for Gradio Temp.") + gradio_temp_clear = False + if bool(gradio_temp_clear): + cleanup_temp(gradio_temp_path) else: - print("Using the default path for Gradio cache.") - if bool(clear_cache): - print('You must use a custom path to clear cache on load.\nAdd a path to path_cache in config.txt') + print("Using default path for Gradio Temp.") + if bool(gradio_temp_clear): + print('You must use a custom path to clear Gradio Temp on load.\nAdd a path to gradio_temp_path in config.txt') + def prepare_environment(): torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121") diff --git a/modules/config.py b/modules/config.py index 9af67cd1dd..63babe494b 100644 --- a/modules/config.py +++ b/modules/config.py @@ -315,14 +315,14 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ ], validator=lambda x: isinstance(x, list) and all(isinstance(v, str) for v in x) ) -path_cache = get_config_item_or_set_default( - key='path_cache', +gradio_temp_path = get_config_item_or_set_default( + key='gradio_temp_path', default_value='', validator=lambda x: isinstance(x, str), disable_empty_as_none=True ) -clear_cache = get_config_item_or_set_default( - key='clear_cache', +gradio_temp_clear = get_config_item_or_set_default( + key='gradio_temp_clear', default_value=False, validator=lambda x: isinstance(x, bool) ) From 889c63db5749d32f60523b4944977472c037b289 Mon Sep 17 00:00:00 2001 From: steveyourcreativepeople Date: Fri, 19 Jan 2024 22:06:00 -0500 Subject: [PATCH 3/9] clear temp --- launch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launch.py b/launch.py index e5d5a653a0..7a44816b8e 100644 --- a/launch.py +++ b/launch.py @@ -40,7 +40,7 @@ def cleanup_temp(folder_path): if bool(gradio_temp_path): try: os.makedirs(gradio_temp_path, exist_ok=True) - os.environ['GRADIO_DATA_DIR'] = gradio_temp_path + os.environ['GRADIO_TEMP_DIR'] = gradio_temp_path print("Using custom path for Gradio Temp.") except Exception as e: print(f"Error: {e}\nUsing default path for Gradio Temp.") From 15b9d82ef8953d983ca78e727ae1fee027366414 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 16:36:59 +0100 Subject: [PATCH 4/9] feat: do not delete temp folder but only clean content also use fallback to system temp dir see https://github.com/gradio-app/gradio/blob/6683ab2589f9d8658e1f51acc1b7526edce988d3/gradio/utils.py#L1151 --- launch.py | 37 +++++++++++++++---------------------- modules/launch_util.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/launch.py b/launch.py index e4ed1fba3d..74d3b7aede 100644 --- a/launch.py +++ b/launch.py @@ -1,7 +1,7 @@ import os import sys import ssl -import shutil +import tempfile print('[System ARGV] ' + str(sys.argv)) @@ -16,26 +16,17 @@ ssl._create_default_https_context = ssl._create_unverified_context - import platform import fooocus_version from build_launcher import build_launcher -from modules.launch_util import is_installed, run, python, run_pip, requirements_met +from modules.launch_util import is_installed, run, python, run_pip, requirements_met, delete_folder_content from modules.model_loader import load_file_from_url - REINSTALL_ALL = False TRY_INSTALL_XFORMERS = False -def cleanup_temp(folder_path): - try: - shutil.rmtree(folder_path) - print("Gradio Temp deleted successfully.") - except Exception as e: - print(f"Error: {e}\nGradio Temp not deleted.") - def prepare_environment(): torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121") torch_command = os.environ.get('TORCH_COMMAND', @@ -85,7 +76,6 @@ def ini_args(): build_launcher() args = ini_args() - if args.gpu_device_id is not None: os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_device_id) print("Set device to:", args.gpu_device_id) @@ -93,20 +83,23 @@ def ini_args(): from modules import config -if config.gradio_temp_path != '': +if config.gradio_temp_path == '': + config.gradio_temp_path = os.path.join(tempfile.gettempdir(), 'gradio') +else: try: os.makedirs(config.gradio_temp_path, exist_ok=True) os.environ['GRADIO_TEMP_DIR'] = config.gradio_temp_path - print("Using custom path for Gradio Temp.") + print(f"Using Gradio temp dir {config.gradio_temp_path}") except Exception as e: - print(f"Error: {e}\nUsing default path for Gradio Temp.") - gradio_temp_clear = False - if config.gradio_temp_clear: - cleanup_temp(config.gradio_temp_path) -else: - print("Using default path for Gradio Temp.") - if config.gradio_temp_clear: - print("You must use a custom path to clear Gradio Temp on load.\nAdd a path to gradio_temp_path in config.txt") + print(f"[Cleanup] Using default Gradio temp dir. Reason: {e}\n") + +if config.gradio_temp_clear: + print(f'[Cleanup] Attempting to delete content of Gradio temp dir {config.gradio_temp_path}') + result = delete_folder_content(config.gradio_temp_path, '[Cleanup] ') + if result: + print("[Cleanup] Cleanup successful") + else: + print(f"[Cleanup] Failed to delete content of Gradio temp dir.") def download_models(): diff --git a/modules/launch_util.py b/modules/launch_util.py index b483d5158c..ab1aadaa42 100644 --- a/modules/launch_util.py +++ b/modules/launch_util.py @@ -1,6 +1,7 @@ import os import importlib import importlib.util +import shutil import subprocess import sys import re @@ -101,3 +102,19 @@ def requirements_met(requirements_file): return True + +def delete_folder_content(folder, prefix=None): + result = True + + for filename in os.listdir(folder): + file_path = os.path.join(folder, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except Exception as e: + print(f'{prefix}Failed to delete {file_path}. Reason: {e}') + result = False + + return result \ No newline at end of file From 23fef1d3bdc83ad1a05a9511f0609a8bcfaceade Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 16:37:57 +0100 Subject: [PATCH 5/9] refactor: code cleanup --- launch.py | 2 +- modules/config.py | 2 +- modules/launch_util.py | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/launch.py b/launch.py index 74d3b7aede..00f9acabd9 100644 --- a/launch.py +++ b/launch.py @@ -67,6 +67,7 @@ def prepare_environment(): 'https://huggingface.co/lllyasviel/misc/resolve/main/xl-to-v1_interposer-v3.1.safetensors') ] + def ini_args(): from args_manager import args return args @@ -140,5 +141,4 @@ def download_models(): download_models() - from webui import * diff --git a/modules/config.py b/modules/config.py index 911afd3e8b..8c509695cc 100644 --- a/modules/config.py +++ b/modules/config.py @@ -117,7 +117,7 @@ def get_path_output() -> str: global config_dict path_output = get_dir_or_set_default('path_outputs', '../outputs/', make_directory=True) if args_manager.args.output_path: - print(f'[CONFIG] Overriding config value path_outputs with {args_manager.args.output_path}') + print(f'Overriding config value path_outputs with {args_manager.args.output_path}') config_dict['path_outputs'] = path_output = args_manager.args.output_path return path_output diff --git a/modules/launch_util.py b/modules/launch_util.py index ab1aadaa42..370dc04895 100644 --- a/modules/launch_util.py +++ b/modules/launch_util.py @@ -10,9 +10,6 @@ import packaging.version from packaging.requirements import Requirement - - - logging.getLogger("torch.distributed.nn").setLevel(logging.ERROR) # sshh... logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage()) From 5ec3aeb63e6b2c62fdeaa4c6f16e1db7a2bf1713 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 18:54:04 +0100 Subject: [PATCH 6/9] feat: unify arg --temp-path and new temp_path config value --- args_manager.py | 3 --- launch.py | 24 +++++++----------------- modules/config.py | 36 +++++++++++++++++++++++++++++++----- modules/private_logger.py | 2 +- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/args_manager.py b/args_manager.py index c7c1b7ab11..8c3e191827 100644 --- a/args_manager.py +++ b/args_manager.py @@ -49,7 +49,4 @@ if args_parser.args.disable_in_browser: args_parser.args.in_browser = False -if args_parser.args.temp_path is None: - args_parser.args.temp_path = os.path.join(gettempdir(), 'Fooocus') - args = args_parser.args diff --git a/launch.py b/launch.py index 00f9acabd9..b3b06d6ec2 100644 --- a/launch.py +++ b/launch.py @@ -1,7 +1,6 @@ import os -import sys import ssl -import tempfile +import sys print('[System ARGV] ' + str(sys.argv)) @@ -81,26 +80,17 @@ def ini_args(): os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_device_id) print("Set device to:", args.gpu_device_id) - from modules import config -if config.gradio_temp_path == '': - config.gradio_temp_path = os.path.join(tempfile.gettempdir(), 'gradio') -else: - try: - os.makedirs(config.gradio_temp_path, exist_ok=True) - os.environ['GRADIO_TEMP_DIR'] = config.gradio_temp_path - print(f"Using Gradio temp dir {config.gradio_temp_path}") - except Exception as e: - print(f"[Cleanup] Using default Gradio temp dir. Reason: {e}\n") - -if config.gradio_temp_clear: - print(f'[Cleanup] Attempting to delete content of Gradio temp dir {config.gradio_temp_path}') - result = delete_folder_content(config.gradio_temp_path, '[Cleanup] ') +os.environ['GRADIO_TEMP_DIR'] = config.temp_path + +if config.temp_path_cleanup_on_launch: + print(f'[Cleanup] Attempting to delete content of temp dir {config.temp_path}') + result = delete_folder_content(config.temp_path, '[Cleanup] ') if result: print("[Cleanup] Cleanup successful") else: - print(f"[Cleanup] Failed to delete content of Gradio temp dir.") + print(f"[Cleanup] Failed to delete content of temp dir.") def download_models(): diff --git a/modules/config.py b/modules/config.py index 8c509695cc..d30864f095 100644 --- a/modules/config.py +++ b/modules/config.py @@ -3,6 +3,7 @@ import math import numbers import args_manager +import tempfile import modules.flags import modules.sdxl_styles @@ -10,6 +11,7 @@ from modules.util import get_files_from_folder, makedirs_with_log from modules.flags import Performance, MetadataScheme + def get_config_path(key, default_value): env = os.getenv(key) if env is not None and isinstance(env, str): @@ -18,6 +20,7 @@ def get_config_path(key, default_value): else: return os.path.abspath(default_value) + config_path = get_config_path('config_path', "./config.txt") config_example_path = get_config_path('config_example_path', "config_modification_tutorial.txt") config_dict = {} @@ -122,6 +125,27 @@ def get_path_output() -> str: return path_output +def get_temp_path(path: str | None) -> str: + default_temp_path = os.path.join(tempfile.gettempdir(), 'gradio') + + if args_manager.args.temp_path: + path = args_manager.args.temp_path + + if path != '': + try: + if not os.path.isabs(path): + path = os.path.abspath(path) + os.makedirs(path, exist_ok=True) + print(f'Using temp path {path}') + return path + except Exception as e: + print(f'Could not create temp path {path}. Reason: {e}') + print(f'Using default temp path {default_temp_path} instead.') + + os.makedirs(default_temp_path, exist_ok=True) + return default_temp_path + + def get_dir_or_set_default(key, default_value, as_array=False, make_directory=False): global config_dict, visited_keys, always_save_keys @@ -178,6 +202,7 @@ def get_dir_or_set_default(key, default_value, as_array=False, make_directory=Fa path_fooocus_expansion = get_dir_or_set_default('path_fooocus_expansion', '../models/prompt_expansion/fooocus_expansion') path_outputs = get_path_output() + def get_config_item_or_set_default(key, default_value, validator, disable_empty_as_none=False): global config_dict, visited_keys @@ -406,18 +431,19 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ default_value='', validator=lambda x: isinstance(x, str) ) -gradio_temp_path = get_config_item_or_set_default( - key='gradio_temp_path', +temp_path = get_config_item_or_set_default( + key='temp_path', default_value='', validator=lambda x: isinstance(x, str), - disable_empty_as_none=True ) -gradio_temp_clear = get_config_item_or_set_default( - key='gradio_temp_clear', +temp_path_cleanup_on_launch = get_config_item_or_set_default( + key='temp_path_cleanup_on_launch', default_value=False, validator=lambda x: isinstance(x, bool) ) +temp_path = get_temp_path(temp_path) + example_inpaint_prompts = [[x] for x in example_inpaint_prompts] config_dict["default_loras"] = default_loras = default_loras[:default_max_lora_number] + [['None', 1.0] for _ in range(default_max_lora_number - len(default_loras))] diff --git a/modules/private_logger.py b/modules/private_logger.py index 01e570a7d9..73fdda5484 100644 --- a/modules/private_logger.py +++ b/modules/private_logger.py @@ -21,7 +21,7 @@ def get_current_html_path(output_format=None): def log(img, metadata, metadata_parser: MetadataParser | None = None, output_format=None) -> str: - path_outputs = args_manager.args.temp_path if args_manager.args.disable_image_log else modules.config.path_outputs + path_outputs = modules.config.temp_path if args_manager.args.disable_image_log else modules.config.path_outputs output_format = output_format if output_format else modules.config.default_output_format date_string, local_temp_filename, only_name = generate_temp_filename(folder=path_outputs, extension=output_format) os.makedirs(os.path.dirname(local_temp_filename), exist_ok=True) From bcd9fadd4dd4c9faecd9f06ee7744d2329d69b54 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 18:54:49 +0100 Subject: [PATCH 7/9] feat: change default temp dir from gradio to fooocus --- modules/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/config.py b/modules/config.py index d30864f095..7f97c9fe3e 100644 --- a/modules/config.py +++ b/modules/config.py @@ -126,7 +126,7 @@ def get_path_output() -> str: def get_temp_path(path: str | None) -> str: - default_temp_path = os.path.join(tempfile.gettempdir(), 'gradio') + default_temp_path = os.path.join(tempfile.gettempdir(), 'fooocus') if args_manager.args.temp_path: path = args_manager.args.temp_path From f473fe9eaba678e1eece874706866d2412397567 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 19:19:18 +0100 Subject: [PATCH 8/9] refactor: move temp path method definition and configs --- modules/config.py | 63 ++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/modules/config.py b/modules/config.py index 7f97c9fe3e..c16fbde165 100644 --- a/modules/config.py +++ b/modules/config.py @@ -125,27 +125,6 @@ def get_path_output() -> str: return path_output -def get_temp_path(path: str | None) -> str: - default_temp_path = os.path.join(tempfile.gettempdir(), 'fooocus') - - if args_manager.args.temp_path: - path = args_manager.args.temp_path - - if path != '': - try: - if not os.path.isabs(path): - path = os.path.abspath(path) - os.makedirs(path, exist_ok=True) - print(f'Using temp path {path}') - return path - except Exception as e: - print(f'Could not create temp path {path}. Reason: {e}') - print(f'Using default temp path {default_temp_path} instead.') - - os.makedirs(default_temp_path, exist_ok=True) - return default_temp_path - - def get_dir_or_set_default(key, default_value, as_array=False, make_directory=False): global config_dict, visited_keys, always_save_keys @@ -231,6 +210,36 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ return default_value +def get_temp_path(path: str | None, default_path: str) -> str: + if args_manager.args.temp_path: + path = args_manager.args.temp_path + + if path != '' and path != default_path: + try: + if not os.path.isabs(path): + path = os.path.abspath(path) + os.makedirs(path, exist_ok=True) + print(f'Using temp path {path}') + return path + except Exception as e: + print(f'Could not create temp path {path}. Reason: {e}') + print(f'Using default temp path {default_path} instead.') + + os.makedirs(default_path, exist_ok=True) + return default_path + + +default_temp_path = os.path.join(tempfile.gettempdir(), 'fooocus') +temp_path = get_temp_path(get_config_item_or_set_default( + key='temp_path', + default_value=default_temp_path, + validator=lambda x: isinstance(x, str), +), default_temp_path) +temp_path_cleanup_on_launch = get_config_item_or_set_default( + key='temp_path_cleanup_on_launch', + default_value=True, + validator=lambda x: isinstance(x, bool) +) default_base_model_name = get_config_item_or_set_default( key='default_model', default_value='model.safetensors', @@ -431,18 +440,6 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ default_value='', validator=lambda x: isinstance(x, str) ) -temp_path = get_config_item_or_set_default( - key='temp_path', - default_value='', - validator=lambda x: isinstance(x, str), -) -temp_path_cleanup_on_launch = get_config_item_or_set_default( - key='temp_path_cleanup_on_launch', - default_value=False, - validator=lambda x: isinstance(x, bool) -) - -temp_path = get_temp_path(temp_path) example_inpaint_prompts = [[x] for x in example_inpaint_prompts] From 7241f10157d3c104a50a84d96cee0f3f78bb52f9 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Sun, 10 Mar 2024 19:24:38 +0100 Subject: [PATCH 9/9] feat: rename get_temp_path to init_temp_path --- modules/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/config.py b/modules/config.py index c16fbde165..543bba5ef0 100644 --- a/modules/config.py +++ b/modules/config.py @@ -210,7 +210,7 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_ return default_value -def get_temp_path(path: str | None, default_path: str) -> str: +def init_temp_path(path: str | None, default_path: str) -> str: if args_manager.args.temp_path: path = args_manager.args.temp_path @@ -230,7 +230,7 @@ def get_temp_path(path: str | None, default_path: str) -> str: default_temp_path = os.path.join(tempfile.gettempdir(), 'fooocus') -temp_path = get_temp_path(get_config_item_or_set_default( +temp_path = init_temp_path(get_config_item_or_set_default( key='temp_path', default_value=default_temp_path, validator=lambda x: isinstance(x, str),