diff --git a/.gitignore b/.gitignore index f262ebf..430337a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ custom_characters.json config.json src/img/ src/testing.py +state.json diff --git a/__init__.py b/__init__.py index d6f3979..861e6ec 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ from .src.dynamic_lora_loader import generate_lora_node_class -from .sn0w import ConfigReader, Logger +from .sn0w import ConfigReader, Logger, Utility from .src.find_resolution import FindResolutionNode from .src.lora_selector import LoraSelectorNode @@ -52,6 +52,8 @@ NODE_DISPLAY_NAME_MAPPINGS = { WEB_DIRECTORY = "./web" +Utility.initialize_state() + current_unique_id = 0 # Global variable to track the unique ID logger = Logger() @@ -67,8 +69,6 @@ def parse_custom_lora_loaders(custom_lora_loaders): required_folders_with_names.append((name, value.split(','))) # Assuming values are comma-separated return required_folders_with_names - - def generate_and_register_lora_node(lora_type, setting): global current_unique_id # Use the global variable to keep track of IDs diff --git a/sn0w.py b/sn0w.py index e27494d..91c9049 100644 --- a/sn0w.py +++ b/sn0w.py @@ -70,6 +70,9 @@ class Logger: self.print_sn0w(message, color) class Utility: + logger = Logger() + ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) + @staticmethod def levenshtein_distance(s1, s2): if len(s1) < len(s2): @@ -108,4 +111,58 @@ class Utility: Utility._check_image_dimensions(batched_tensors, image_names) batched_tensors = torch.cat(batched_tensors, dim=0) - return batched_tensors \ No newline at end of file + return batched_tensors + + @staticmethod + def initialize_state(): + # Default values + default_state = { + 'random_character_chosen': False + } + + state_path = os.path.join(Utility.ROOT_DIR, 'state.json') + + # Check if state.json exists; if not, create it with default values + if not os.path.exists(state_path): + with open(state_path, 'w') as f: + json.dump(default_state, f) + Utility.logger.log("Initialized state file with default values at the root.", "DEBUG") + else: + # Ensure all default keys exist in the file, if file exists + with open(state_path, 'r') as f: + state = json.load(f) + # Update the file with any missing default values + state_updated = False + for key, value in default_state.items(): + if key not in state: + state[key] = value + state_updated = True + if state_updated: + with open(state_path, 'w') as f: + json.dump(state, f) + Utility.logger.log("State file checked and updated with any missing default values at the root.", "DEBUG") + + @staticmethod + def save_state(name, value): + state_path = os.path.join(Utility.ROOT_DIR, 'state.json') + state = {} + if os.path.exists(state_path): + with open(state_path, 'r') as f: + state = json.load(f) + state[name] = value + with open(state_path, 'w') as f: + json.dump(state, f) + + @staticmethod + def load_state(name): + state_path = os.path.join(Utility.ROOT_DIR, 'state.json') + try: + with open(state_path, 'r') as f: + state = json.load(f) + return state.get(name, None) + except FileNotFoundError: + Utility.logger.log("State file not found at the root. Creating new state file.", "ERROR") + # Create a new state.json with default values or empty + with open(state_path, 'w') as f: + json.dump({}, f) + return None \ No newline at end of file diff --git a/src/character_select.py b/src/character_select.py index d7bfafe..3618a91 100644 --- a/src/character_select.py +++ b/src/character_select.py @@ -1,7 +1,8 @@ import os import json import random -from ..sn0w import ConfigReader, Logger +import hashlib +from ..sn0w import ConfigReader, Logger, Utility class CharacterSelectNode: # Initialize class variables @@ -76,24 +77,26 @@ class CharacterSelectNode: @classmethod def IS_CHANGED(cls, **kwargs): - if cls.random_character_chosen: + if Utility.load_state("random_character_chosen"): return float("NaN") + else: + return hashlib.sha256(b'false').hexdigest() RETURN_TYPES = ("STRING", "STRING", "BOOLEAN") RETURN_NAMES = ("CHARACTER STRING", "CHARACTER PROMPT", "XL") - FUNCTION = "find_associated_string" + FUNCTION = "find_character" CATEGORY = "sn0w" - def find_associated_string(self, character, character_strength, character_prompt, xl, random_character): + def find_character(self, character, character_strength, character_prompt, xl, random_character): if character == "None": return ("", "", "") if random_character: - self.random_character_chosen = True + Utility.save_state("random_character_chosen", True) char_item = self.final_character_dict.get(random.choice(list(self.final_character_dict.keys()))) self.logger.log("Random Character: " + str(char_item["name"]), "INFORMATIONAL") else: - self.random_character_chosen = False + Utility.save_state("random_character_chosen", False) char_item = self.final_character_dict.get(character) if char_item: