BonusLockSMith's picture
Upload 148 files
5d7f5d2 verified
import csv, json, urllib.request
COMFY_URL = "http://127.0.0.1:8188/prompt"
WORKFLOW_PATH = r"Flux_Char_Raven_Snow\workflow.json"
CSV_PATH = r"Flux_Char_Raven_Snow\prompts\normalized\variations.csv"
# API workflow node IDs (update ONLY if your workflow graph changes)
PROMPT_NODE = "45_1" # CLIPTextEncode
KSAMPLER_NODE = "31_1" # KSampler
SAVE_NODE = "9_1" # SaveImage
SAVE_PREFIX = "Flux_Char_Raven_Snow/images/variations/raven"
BASE_PROMPT = (
"female warrior, snowy forest edge, cold mist, wind, wet-black surfaces, muted daylight, raven black hair, long braid, wet shine, dark leather armor top with cloak drape, strap harness, multiple bracelets on arms, muscular athletic build, broad shoulders, strong arms, strong jawline, intense eyes, slightly furrowed brow, wet skin sheen, rain-slick surface highlights, realistic detail, cinematic depth, hyper-detailed textures, natural light filtering through trees, side-lighting with shadow falloff, lush background depth, shallow depth of field, photorealistic, detailed fabric wrinkles, detailed leather grain, moisture beads, surface continuity"
)
def sanitize_graph(wf: dict) -> dict:
return {k: v for k, v in wf.items() if isinstance(v, dict) and "class_type" in v}
def post_prompt(graph: dict) -> dict:
body = json.dumps({"prompt": graph}).encode("utf-8")
req = urllib.request.Request(
COMFY_URL,
data=body,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read().decode("utf-8"))
def norm_key(s: str) -> str:
return (s or "").replace("\ufeff", "").strip().lower()
with open(WORKFLOW_PATH, "r", encoding="utf-8") as f:
workflow = sanitize_graph(json.load(f))
for nid in (PROMPT_NODE, KSAMPLER_NODE, SAVE_NODE):
if nid not in workflow:
raise RuntimeError(f"Missing node: {nid}")
rows = []
with open(CSV_PATH, newline="", encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
if not reader.fieldnames:
raise RuntimeError("CSV missing header. Expected: index,seed,suffix (index optional)")
header_map = {norm_key(h): h for h in reader.fieldnames}
if "seed" not in header_map or "suffix" not in header_map:
raise RuntimeError(f"CSV headers invalid. Found: {reader.fieldnames} (need seed,suffix; index optional)")
for r in reader:
rows.append(r)
print(f"Loaded {len(rows)} prompt variations.")
print("Queueing jobs…")
seed_key = header_map["seed"]
suffix_key = header_map["suffix"]
index_key = header_map.get("index")
for i, r in enumerate(rows, start=1):
seed = int(str(r[seed_key]).strip())
suffix = str(r[suffix_key]).strip()
idx = str(r[index_key]).strip() if index_key and r.get(index_key) is not None else f"{i:02d}"
workflow[SAVE_NODE]["inputs"]["filename_prefix"] = SAVE_PREFIX
workflow[KSAMPLER_NODE]["inputs"]["seed"] = seed
workflow[PROMPT_NODE]["inputs"]["text"] = f"{BASE_PROMPT}, {suffix}"
result = post_prompt(workflow)
print(f"[OK] {i}/{len(rows)} seed={seed} index={idx} -> {result}")
print("All CSV-driven jobs queued successfully.")