From 27b58a79f27eaee52510a600bfe1c2e9d8fcdf0a Mon Sep 17 00:00:00 2001 From: bmaltais Date: Thu, 9 May 2024 13:30:27 -0400 Subject: [PATCH] Fix creation of output folders --- kohya_gui/common_gui.py | 16 ++++++++++------ kohya_gui/dreambooth_gui.py | 4 ++-- kohya_gui/finetune_gui.py | 4 ++-- kohya_gui/lora_gui.py | 4 ++-- kohya_gui/textual_inversion_gui.py | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/kohya_gui/common_gui.py b/kohya_gui/common_gui.py index 8fb9b180c..4f2b80477 100644 --- a/kohya_gui/common_gui.py +++ b/kohya_gui/common_gui.py @@ -1368,17 +1368,21 @@ def validate_file_path(file_path: str) -> bool: return True -def validate_folder_path(folder_path: str, can_be_written_to: bool = False) -> bool: +def validate_folder_path(folder_path: str, can_be_written_to: bool = False, create_if_not_exists: bool = False) -> bool: if folder_path == "": return True msg = f"Validating {folder_path} existence{' and writability' if can_be_written_to else ''}..." if not os.path.isdir(folder_path): - log.error(f"{msg} FAILED: does not exist") - return False - if can_be_written_to: - if not os.access(folder_path, os.W_OK): - log.error(f"{msg} FAILED: is not writable.") + if create_if_not_exists: + os.makedirs(folder_path) + log.info(f"{msg} SUCCESS") + return True + else: + log.error(f"{msg} FAILED: does not exist") return False + if can_be_written_to and not os.access(folder_path, os.W_OK): + log.error(f"{msg} FAILED: is not writable.") + return False log.info(f"{msg} SUCCESS") return True diff --git a/kohya_gui/dreambooth_gui.py b/kohya_gui/dreambooth_gui.py index 62cab8bd1..bcb563d9d 100644 --- a/kohya_gui/dreambooth_gui.py +++ b/kohya_gui/dreambooth_gui.py @@ -524,10 +524,10 @@ def train_model( if not validate_file_path(log_tracker_config): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(logging_dir, can_be_written_to=True): + if not validate_folder_path(logging_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(output_dir, can_be_written_to=True): + if not validate_folder_path(output_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE if not validate_model_path(pretrained_model_name_or_path): diff --git a/kohya_gui/finetune_gui.py b/kohya_gui/finetune_gui.py index 7bc95f9aa..b8b6a1eea 100644 --- a/kohya_gui/finetune_gui.py +++ b/kohya_gui/finetune_gui.py @@ -569,10 +569,10 @@ def train_model( if not validate_file_path(log_tracker_config): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(logging_dir, can_be_written_to=True): + if not validate_folder_path(logging_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(output_dir, can_be_written_to=True): + if not validate_folder_path(output_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE if not validate_model_path(pretrained_model_name_or_path): diff --git a/kohya_gui/lora_gui.py b/kohya_gui/lora_gui.py index 06653f668..4d03fcd9f 100644 --- a/kohya_gui/lora_gui.py +++ b/kohya_gui/lora_gui.py @@ -707,7 +707,7 @@ def train_model( if not validate_file_path(log_tracker_config): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(logging_dir, can_be_written_to=True): + if not validate_folder_path(logging_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE if LyCORIS_preset not in LYCORIS_PRESETS_CHOICES: @@ -717,7 +717,7 @@ def train_model( if not validate_file_path(network_weights): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(output_dir, can_be_written_to=True): + if not validate_folder_path(output_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE if not validate_model_path(pretrained_model_name_or_path): diff --git a/kohya_gui/textual_inversion_gui.py b/kohya_gui/textual_inversion_gui.py index 0740fa5ab..440d017c3 100644 --- a/kohya_gui/textual_inversion_gui.py +++ b/kohya_gui/textual_inversion_gui.py @@ -524,10 +524,10 @@ def train_model( if not validate_file_path(log_tracker_config): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(logging_dir, can_be_written_to=True): + if not validate_folder_path(logging_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE - if not validate_folder_path(output_dir, can_be_written_to=True): + if not validate_folder_path(output_dir, can_be_written_to=True, create_if_not_exists=True): return TRAIN_BUTTON_VISIBLE if not validate_model_path(pretrained_model_name_or_path):