Archived
117 lines
4.5 KiB
Python
117 lines
4.5 KiB
Python
"""
|
|
Basic SDXL Text to Image workflow.
|
|
Schema is defined in basic_sdxl.yaml (auto-generated from WorkflowParams).
|
|
"""
|
|
from comfy_script.runtime import *
|
|
load('http://127.0.0.1:8188/')
|
|
from comfy_script.runtime.nodes import *
|
|
|
|
from pydantic import BaseModel, Field
|
|
from typing import List, Optional
|
|
import sys
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
from base import BaseWorkflow
|
|
from parameters import (
|
|
Prompt, NegativePrompt, Checkpoint, Seed, Sampler, Scheduler,
|
|
Loras, Dimensions, Loopback, InputImage
|
|
)
|
|
|
|
# ============================================
|
|
# Workflow Metadata
|
|
# ============================================
|
|
|
|
WORKFLOW_METADATA = {
|
|
"name": "Basic SDXL Text to Image",
|
|
"description": "Workflow with SDXL presets for quick iteration.",
|
|
"model_types": ["SDXL"],
|
|
"category": "Image Generation",
|
|
}
|
|
|
|
# ============================================
|
|
# Workflow Parameters Model
|
|
# ============================================
|
|
|
|
class WorkflowParams(BaseModel):
|
|
"""
|
|
Parameters for SDXL text-to-image generation.
|
|
Schema is auto-generated to basic_sdxl.yaml from these type annotations.
|
|
"""
|
|
# Use annotated types for automatic UI schema
|
|
prompt: Prompt
|
|
negativePrompt: NegativePrompt = "text, watermark"
|
|
checkpoints: Checkpoint
|
|
loras: Loras # Uses shared type with category: models
|
|
sampler: Sampler = "euler"
|
|
scheduler: Scheduler = "normal"
|
|
seed: Seed # Required, no default
|
|
inputImage: InputImage = "" # Optional input image for img2img
|
|
|
|
# Nested objects using shared types
|
|
dimensions: Dimensions
|
|
loopback: Loopback
|
|
|
|
# Inline Field() for one-off constraints
|
|
steps: int = Field(default=20, ge=1, le=150)
|
|
cfg: float = Field(default=8.0, ge=1.0, le=20.0, json_schema_extra={"step": 0.5})
|
|
denoise: float = Field(default=1.0, ge=0.0, le=1.0, json_schema_extra={"step": 0.01, "category": "composition", "depends_on": {"inputImage": True}})
|
|
|
|
# Workflow definition
|
|
class SDXLWorkflow(BaseWorkflow):
|
|
def run(self):
|
|
params: WorkflowParams = self.params
|
|
|
|
with Workflow(wait=False) as wf:
|
|
model, clip, vae = self.load_checkpoints()
|
|
model, clip = self.apply_loras(model, clip)
|
|
vae = VAELoader(VAEs.sdxl_vae)
|
|
|
|
if params.inputImage != "" and params.inputImage is not None:
|
|
image, mask = LoadImage(params.inputImage)
|
|
image = ResizeImagesByLongerEdge(image, max(params.dimensions.width, params.dimensions.height))
|
|
|
|
latent = VAEEncode(image, vae)
|
|
else:
|
|
latent = EmptyLatentImage(params.dimensions.width, params.dimensions.height, 1)
|
|
|
|
conditioning = CLIPTextEncode(params.prompt, clip)
|
|
conditioning2 = CLIPTextEncode(params.negativePrompt, clip)
|
|
|
|
latent = KSampler(model, params.seed, params.steps, params.cfg, params.sampler, params.scheduler, conditioning, conditioning2, latent, params.denoise)
|
|
|
|
if params.loopback.enabled:
|
|
for _ in range(params.loopback.iterations):
|
|
latent = LatentUpscaleBy(latent, 'nearest-exact', params.loopback.upscale)
|
|
latent = KSampler(model, params.seed + 1, params.loopback.steps, params.loopback.cfg, params.sampler, params.scheduler, conditioning, conditioning2, latent, params.loopback.denoise)
|
|
|
|
image = VAEDecode(latent, vae)
|
|
|
|
# We can have some base class method here to handle saving based on a flag so we can control if an image is saved on the server
|
|
# or if we return it to the client via websocket, etc.
|
|
|
|
PreviewImage(image, extra_pnginfo={"imagelab_parameters": params.model_dump(mode='json')})
|
|
|
|
# TODO: pass paramsRaw here to get the actual imagelab parameters in the metadata
|
|
# SaveImage(image, extra_pnginfo={"imagelab_parameters": params.model_dump(mode='json')})
|
|
|
|
return {
|
|
'prompt_id': wf.task.prompt_id,
|
|
'status': 'queued',
|
|
'parameters': self.params.model_dump(mode='json')
|
|
}
|
|
|
|
# Module-level entry point
|
|
def run_workflow(**kwargs):
|
|
"""Entry point for the workflow."""
|
|
required = ['prompt', 'checkpoints', 'seed', 'steps', 'negativePrompt', 'width', 'height', 'cfg']
|
|
missing = [f for f in required if f not in kwargs]
|
|
if missing:
|
|
raise ValueError(f"Missing required fields: {', '.join(missing)}")
|
|
|
|
params = WorkflowParams(**kwargs)
|
|
|
|
# Create and run workflow
|
|
workflow = SDXLWorkflow(params)
|
|
return workflow.run()
|