From 4ffd42385f62ddd64525fe67e764e85a1c8431ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20=20Sk=C3=B6ld?= Date: Tue, 25 Jun 2024 20:10:33 +0200 Subject: [PATCH] Only show needed custom lora settings --- __init__.py | 5 ++++- src/check_folder_paths.py | 33 +++++++++++++++++++++++++++++++++ web/js/customLoraLoaders.js | 27 ++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/check_folder_paths.py diff --git a/__init__.py b/__init__.py index 794fb71..607a48d 100644 --- a/__init__.py +++ b/__init__.py @@ -1,8 +1,9 @@ import importlib -import json import os +import folder_paths from .src.dynamic_lora_loader import generate_lora_node_class from .src.dynamic_scheduler_loader import generate_scheduler_node_class +from .src.check_folder_paths import check_lora_folders from .sn0w import ConfigReader, Logger from .src.lora_selector import LoraSelectorNode @@ -52,6 +53,8 @@ NODE_DISPLAY_NAME_MAPPINGS = { WEB_DIRECTORY = "./web" +check_lora_folders() + current_unique_id = 0 # Global variable to track the unique ID logger = Logger() diff --git a/src/check_folder_paths.py b/src/check_folder_paths.py new file mode 100644 index 0000000..576cde1 --- /dev/null +++ b/src/check_folder_paths.py @@ -0,0 +1,33 @@ +import folder_paths +import json +import os + +from ..sn0w import Logger + +def check_lora_folders(): + logger = Logger() + custom_paths = ["loras_15", "loras_xl", "loras_3"] + existing_paths = [] + + dir_path = os.path.dirname(os.path.realpath(__file__)) + if os.path.basename(dir_path) == "src": + dir_path = os.path.dirname(dir_path) + json_path = os.path.join(dir_path, 'web/settings/sn0w_settings.json') + # Ensure the directory exists + os.makedirs(os.path.dirname(json_path), exist_ok=True) + + for path in custom_paths: + try: + lora_path = folder_paths.get_filename_list(path) + if lora_path: + existing_paths.append(path) + except Exception as e: + logger.log(f"{path} doesn't exist: {e}", "WARNING") + + print(existing_paths) + # Write the result to a JSON file in /web/settings + data = {"loaders_enabled": existing_paths} + with open(json_path, "w") as json_file: + json.dump(data, json_file, indent=4) + + return existing_paths \ No newline at end of file diff --git a/web/js/customLoraLoaders.js b/web/js/customLoraLoaders.js index 5f77297..534a3e1 100644 --- a/web/js/customLoraLoaders.js +++ b/web/js/customLoraLoaders.js @@ -1,9 +1,30 @@ import { SettingUtils } from './sn0w.js'; import { app } from "../../../scripts/app.js"; +import { api } from "../../../scripts/api.js"; +async function fetchExtensionSettings() { + try { + const response = await api.fetchApi('/extensions/ComfyUI-Sn0w-Scripts/settings/sn0w_settings.json'); + if (!response.ok) { + throw new Error('Network response was not ok ' + response.statusText); + } + const data = await response.json(); + return data; + } catch (error) { + console.error('There was a problem with the fetch operation:', error); + } +} + +const sn0wSettings = await fetchExtensionSettings(); const defaultValue = "ExampleName1:Value1\nExampleName2:Value2"; const tooltip = "Enter each name-value pair on a new line, separated by a colon (:)."; +const settingsMap = { + "sn0w.CustomLoraLoaders1.5": "loras_15", + "sn0w.CustomLoraLoadersXL": "loras_xl", + "sn0w.CustomLoraLoaders3": "loras_3" +} + const settingsDefinitions = [ { id: "sn0w.CustomLoraLoadersXL", @@ -42,4 +63,8 @@ const registerSetting = (settingDefinition) => { }; // Register settings -settingsDefinitions.forEach(registerSetting); +settingsDefinitions.forEach(setting => { + if (sn0wSettings["loaders_enabled"].includes(settingsMap[setting.id])) { + registerSetting(setting); + } +});