Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # QuenWildcards — A1111-style __token__ resolver for ComfyUI
- # Usage: Put text files in a folder (one option per line), then write __token__ in your prompt.
- # The node replaces each __token__ with a random line from <folder>/<token>.txt
- import os, re, random, hashlib
- class QuenWildcardPrompt:
- @classmethod
- def INPUT_TYPES(cls):
- return {
- "required": {
- "text": ("STRING", {"multiline": True}),
- "folder": ("STRING", {"default": "A:/Comfy/wildcards/"}),
- "seed": ("INT", {"default": 0, "min": -1, "max": 2**31-1}),
- "deterministic": ("BOOLEAN", {"default": True}),
- "ext": ("STRING", {"default": ".txt"}),
- }
- }
- RETURN_TYPES = ("STRING",)
- FUNCTION = "generate"
- CATEGORY = "Quen/Text"
- TOKEN_RE = re.compile(r"__([A-Za-z0-9_\-]+)__")
- def _pick(self, choices, token, seed, deterministic):
- if not choices:
- return f"__{token}__"
- if deterministic:
- h = hashlib.sha256(f"{token}:{seed}".encode("utf-8")).digest()
- rnd = random.Random(int.from_bytes(h[:8], "little"))
- return rnd.choice(choices)
- else:
- return random.choice(choices)
- def generate(self, text, folder, seed, deterministic, ext):
- folder = os.path.expandvars(os.path.expanduser(folder))
- def replace(m):
- token = m.group(1)
- path = os.path.join(folder, f"{token}{ext}")
- try:
- with open(path, "r", encoding="utf-8") as f:
- lines = [ln.strip() for ln in f.readlines() if ln.strip()]
- except Exception:
- lines = []
- return self._pick(lines, token, seed, deterministic)
- return (self.TOKEN_RE.sub(replace, text),)
- NODE_CLASS_MAPPINGS = {
- "QuenWildcardPrompt": QuenWildcardPrompt
- }
- NODE_DISPLAY_NAME_MAPPINGS = {
- "QuenWildcardPrompt": "Quen Wildcard Prompt"
- }
Advertisement
Add Comment
Please, Sign In to add comment