Skip to content

Commit

Permalink
feat: LoadPromptFromFile/Dir - reload
Browse files Browse the repository at this point in the history
  • Loading branch information
ltdrdata committed Sep 8, 2024
1 parent 25ae8d5 commit 74b2856
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import importlib

version_code = [1, 1]
version_code = [1, 2]
version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '')
print(f"### Loading: ComfyUI-Inspire-Pack ({version_str})")

Expand Down
67 changes: 62 additions & 5 deletions inspire/prompt_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import comfy
import traceback
import random
import hashlib

from server import PromptServer
from .libs import utils, common
Expand Down Expand Up @@ -49,7 +50,13 @@ def INPUT_TYPES(cls):
except Exception:
prompt_dirs = []

return {"required": {"prompt_dir": (prompt_dirs,)}}
return {"required": {
"prompt_dir": (prompt_dirs,)
},
"optional": {
"reload": ("BOOLEAN", { "default": False, "label_on": "if file changed", "label_off": "if value changed"}),
}
}

RETURN_TYPES = ("ZIPPED_PROMPT",)
OUTPUT_IS_LIST = (True,)
Expand All @@ -59,7 +66,30 @@ def INPUT_TYPES(cls):
CATEGORY = "InspirePack/Prompt"

@staticmethod
def doit(prompt_dir):
def IS_CHANGED(prompt_dir, reload=False):
if not reload:
return prompt_dir
else:
global prompts_path
prompt_dir = os.path.join(prompts_path, prompt_dir)
files = [f for f in os.listdir(prompt_dir) if f.endswith(".txt")]

md5 = hashlib.md5()
files.sort()

for file in files:
md5.update(file.encode('utf-8'))
with open(os.path.join(prompt_dir, file), 'rb') as f:
while True:
chunk = f.read(4096)
if not chunk:
break
md5.update(chunk)

return md5.hexdigest()

@staticmethod
def doit(prompt_dir, reload=False):
global prompts_path
prompt_dir = os.path.join(prompts_path, prompt_dir)
files = [f for f in os.listdir(prompt_dir) if f.endswith(".txt")]
Expand Down Expand Up @@ -105,8 +135,14 @@ def INPUT_TYPES(cls):
except Exception:
prompt_files = []

return {"required": {"prompt_file": (prompt_files,)},
"optional": {"text_data_opt": ("STRING", {"defaultInput": True})}}
return {"required": {
"prompt_file": (prompt_files,)
},
"optional": {
"text_data_opt": ("STRING", {"defaultInput": True}),
"reload": ("BOOLEAN", {"default": False, "label_on": "if file changed", "label_off": "if value changed"}),
}
}

RETURN_TYPES = ("ZIPPED_PROMPT",)
OUTPUT_IS_LIST = (True,)
Expand All @@ -116,7 +152,28 @@ def INPUT_TYPES(cls):
CATEGORY = "InspirePack/Prompt"

@staticmethod
def doit(prompt_file, text_data_opt=None):
def IS_CHANGED(prompt_file, text_data_opt=None, reload=False):
md5 = hashlib.md5()

if text_data_opt is not None:
md5.update(text_data_opt)
return md5.hexdigest()
elif not reload:
return prompt_file
else:
prompt_path = os.path.join(prompts_path, prompt_file)

with open(prompt_path, 'rb') as f:
while True:
chunk = f.read(4096)
if not chunk:
break
md5.update(chunk)

return md5.hexdigest()

@staticmethod
def doit(prompt_file, text_data_opt=None, reload=False):
prompt_path = os.path.join(prompts_path, prompt_file)

prompts = []
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui-inspire-pack"
description = "This extension provides various nodes to support Lora Block Weight and the Impact Pack. Provides many easily applicable regional features and applications for Variation Seed."
version = "1.1"
version = "1.2"
license = { file = "LICENSE" }
dependencies = ["matplotlib", "cachetools"]

Expand Down

0 comments on commit 74b2856

Please sign in to comment.