|
|
|
|
|
import torch, os, json, random, hashlib |
|
from urllib.request import urlopen |
|
import json |
|
|
|
class WAS_NSP_CLIPTextEncoder: |
|
def __init__(self): |
|
pass |
|
|
|
@classmethod |
|
def INPUT_TYPES(s): |
|
return { |
|
"required": { |
|
"noodle_key": ("STRING", {"default": '__', "multiline": False}), |
|
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), |
|
"text": ("STRING", {"multiline": True}), |
|
"clip": ("CLIP",), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("CONDITIONING",) |
|
FUNCTION = "nsp_encode" |
|
|
|
CATEGORY = "conditioning" |
|
|
|
def nsp_encode(self, clip, text, noodle_key = '__', seed = 0): |
|
|
|
|
|
local_pantry = 'ComfyUI/custom_nodes/nsp_pantry.json' |
|
if not os.path.exists(local_pantry): |
|
response = urlopen('https://raw.githubusercontent.com/WASasquatch/noodle-soup-prompts/main/nsp_pantry.json') |
|
tmp_pantry = json.loads(response.read()) |
|
|
|
pantry_serialized = json.dumps(tmp_pantry, indent=4) |
|
with open(local_pantry, "w") as f: |
|
f.write(pantry_serialized) |
|
del response, tmp_pantry |
|
|
|
|
|
with open(local_pantry, 'r') as f: |
|
nspterminology = json.load(f) |
|
|
|
if seed > 0 or seed < 1: |
|
random.seed(seed) |
|
|
|
|
|
new_text = text |
|
for term in nspterminology: |
|
|
|
tkey = f'{noodle_key}{term}{noodle_key}' |
|
|
|
tcount = new_text.count(tkey) |
|
|
|
for _ in range(tcount): |
|
new_text = new_text.replace(tkey, random.choice(nspterminology[term]), 1) |
|
seed = seed+1 |
|
random.seed(seed) |
|
|
|
print('Parsed Prompt:', new_text) |
|
|
|
return ([[clip.encode(new_text), {}]], ) |
|
|
|
NODE_CLASS_MAPPINGS = { |
|
"CLIPTextEncode (NSP)": WAS_NSP_CLIPTextEncoder |
|
} |
|
|