Skip to content

Commit

Permalink
feat: Load (Single) Prompts from File - add text_data_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
ltdrdata committed Jul 9, 2024
1 parent d84435e commit ccd472e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 48 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 = [0, 80, 5]
version_code = [0, 81]
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
104 changes: 58 additions & 46 deletions inspire/prompt_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def INPUT_TYPES(cls):

CATEGORY = "InspirePack/Prompt"

def doit(self, prompt_dir):
@staticmethod
def doit(prompt_dir):
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 @@ -104,7 +105,8 @@ def INPUT_TYPES(cls):
except Exception:
prompt_files = []

return {"required": {"prompt_file": (prompt_files,)}}
return {"required": {"prompt_file": (prompt_files,)},
"optional": {"text_data_opt": ("STRING", {"defaultInput": True})}}

RETURN_TYPES = ("ZIPPED_PROMPT",)
OUTPUT_IS_LIST = (True,)
Expand All @@ -113,27 +115,32 @@ def INPUT_TYPES(cls):

CATEGORY = "InspirePack/Prompt"

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

prompts = []
try:
with open(prompt_path, "r", encoding="utf-8") as file:
prompt_data = file.read()
prompt_list = re.split(r'\n\s*-+\s*\n', prompt_data)

pattern = r"positive:(.*?)(?:\n*|$)negative:(.*)"

for prompt in prompt_list:
matches = re.search(pattern, prompt, re.DOTALL)

if matches:
positive_text = matches.group(1).strip()
negative_text = matches.group(2).strip()
result_tuple = (positive_text, negative_text, prompt_file)
prompts.append(result_tuple)
else:
print(f"[WARN] LoadPromptsFromFile: invalid prompt format in '{prompt_file}'")
if text_data_opt is None:
with open(prompt_path, "r", encoding="utf-8") as file:
prompt_data = file.read()
else:
prompt_data = text_data_opt

prompt_list = re.split(r'\n\s*-+\s*\n', prompt_data)

pattern = r"positive:(.*?)(?:\n*|$)negative:(.*)"

for prompt in prompt_list:
matches = re.search(pattern, prompt, re.DOTALL)

if matches:
positive_text = matches.group(1).strip()
negative_text = matches.group(2).strip()
result_tuple = (positive_text, negative_text, prompt_file)
prompts.append(result_tuple)
else:
print(f"[WARN] LoadPromptsFromFile: invalid prompt format in '{prompt_file}'")
except Exception as e:
print(f"[ERROR] LoadPromptsFromFile: an error occurred while processing '{prompt_file}': {str(e)}")

Expand All @@ -156,10 +163,11 @@ def INPUT_TYPES(cls):
prompt_files = []

return {"required": {
"prompt_file": (prompt_files,),
"index": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
}
}
"prompt_file": (prompt_files,),
"index": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {"text_data_opt": ("STRING", {"defaultInput": True})}
}

RETURN_TYPES = ("ZIPPED_PROMPT",)
OUTPUT_IS_LIST = (True,)
Expand All @@ -168,31 +176,36 @@ def INPUT_TYPES(cls):

CATEGORY = "InspirePack/Prompt"

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

prompts = []
try:
with open(prompt_path, "r", encoding="utf-8") as file:
prompt_data = file.read()
prompt_list = re.split(r'\n\s*-+\s*\n', prompt_data)
try:
prompt = prompt_list[index]
except Exception:
prompt = prompt_list[-1]

pattern = r"positive:(.*?)(?:\n*|$)negative:(.*)"
matches = re.search(pattern, prompt, re.DOTALL)

if matches:
positive_text = matches.group(1).strip()
negative_text = matches.group(2).strip()
result_tuple = (positive_text, negative_text, prompt_file)
prompts.append(result_tuple)
else:
print(f"[WARN] LoadPromptsFromFile: invalid prompt format in '{prompt_file}'")
if text_data_opt is None:
with open(prompt_path, "r", encoding="utf-8") as file:
prompt_data = file.read()
else:
prompt_data = text_data_opt

prompt_list = re.split(r'\n\s*-+\s*\n', prompt_data)
try:
prompt = prompt_list[index]
except Exception:
prompt = prompt_list[-1]

pattern = r"positive:(.*?)(?:\n*|$)negative:(.*)"
matches = re.search(pattern, prompt, re.DOTALL)

if matches:
positive_text = matches.group(1).strip()
negative_text = matches.group(2).strip()
result_tuple = (positive_text, negative_text, prompt_file)
prompts.append(result_tuple)
else:
print(f"[WARN] LoadSinglePromptFromFile: invalid prompt format in '{prompt_file}'")
except Exception as e:
print(f"[ERROR] LoadPromptsFromFile: an error occurred while processing '{prompt_file}': {str(e)}")
print(f"[ERROR] LoadSinglePromptFromFile: an error occurred while processing '{prompt_file}': {str(e)}")

return (prompts, )

Expand Down Expand Up @@ -235,9 +248,8 @@ def doit(self, positive, negative, name_opt=""):
return ((positive, negative, name_opt), )


prompt_blacklist = set([
'filename_prefix'
])
prompt_blacklist = set(['filename_prefix'])


class PromptExtractor:
@classmethod
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 = "0.80.5"
version = "0.81"
license = "LICENSE"
dependencies = ["matplotlib", "cachetools"]

Expand Down

0 comments on commit ccd472e

Please sign in to comment.