| import json |
| import os |
| import logging |
| import random |
|
|
| logger = logging.getLogger(__name__) |
|
|
| class WorkflowManager: |
| def __init__(self, workflows_dir="templates/workflows"): |
| self.workflows_dir = workflows_dir |
|
|
| def load_workflow(self, workflow_filename): |
| """ |
| templates/workflows/ ν΄λ λ΄μ JSON νμΌμ λ‘λνλ ν¨μ. |
| """ |
| file_path = os.path.join(self.workflows_dir, workflow_filename) |
| if not file_path.endswith('.json'): |
| file_path += '.json' |
| |
| try: |
| with open(file_path, 'r', encoding='utf-8') as f: |
| return json.load(f) |
| except Exception as e: |
| logger.error(f"Error loading workflow {workflow_filename}: {e}") |
| return None |
|
|
| def build_workflow(self, workflow_json, prompt_text=None, negative_prompt=None, seed=None, |
| image_path=None, checkpoint_name=None, width=None, height=None): |
| """ |
| μν¬νλ‘μ° λ΄μ νΉμ λ
Έλλ€μ μ°Ύμ κ°μ μΉννλ ν¨μ. |
| |
| 1. CLIP Text Encode (Positive/Negative) |
| 2. KSampler (Seed) |
| 3. Load Image (Image path) |
| 4. Checkpoint Loader (Model name) |
| 5. Empty Latent Image (Width/Height) |
| """ |
| |
| wf = json.loads(json.dumps(workflow_json)) |
| |
| for node_id, node in wf.items(): |
| class_type = node.get("class_type") |
| inputs = node.get("inputs", {}) |
|
|
| |
| if class_type == "CLIPTextEncode": |
| if prompt_text and "text" in inputs and "positive" in str(node.get("_meta", {}).get("title", "")).lower(): |
| inputs["text"] = prompt_text |
| elif negative_prompt and "text" in inputs and "negative" in str(node.get("_meta", {}).get("title", "")).lower(): |
| inputs["text"] = negative_prompt |
| |
| elif prompt_text and "text" in inputs: |
| inputs["text"] = prompt_text |
|
|
| |
| elif class_type == "KSampler": |
| if seed is not None: |
| inputs["seed"] = seed |
| else: |
| |
| inputs["seed"] = random.randint(0, 0xffffffffffffffff) |
|
|
| |
| elif class_type == "LoadImage": |
| if image_path: |
| inputs["image"] = image_path |
|
|
| |
| elif class_type in ["CheckpointLoaderSimple", "CheckpointLoader"]: |
| if checkpoint_name: |
| inputs["ckpt_name"] = checkpoint_name |
|
|
| |
| elif class_type == "EmptyLatentImage": |
| if width: |
| inputs["width"] = width |
| if height: |
| inputs["height"] = height |
|
|
| return wf |
|
|
| def get_available_workflows(self): |
| """ |
| μ¬μ© κ°λ₯ν μν¬νλ‘μ° λͺ©λ‘μ λ°νν©λλ€. |
| """ |
| try: |
| return [f for f in os.listdir(self.workflows_dir) if f.endswith('.json')] |
| except Exception as e: |
| logger.error(f"Error listing workflows: {e}") |
| return [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|