implemented api and progress updates via websocket

This commit is contained in:
donny committed 2026-01-03 16:49:42 -07:00
1 parent 5441b0d8ff
commit 7c8768ac1f
8 files changed
+325 -50

No files matched your search

+48 -39
View File
@@ -2,51 +2,60 @@ from comfy_script.runtime import *
load('http://127.0.0.1:8188/')
from comfy_script.runtime.nodes import *
from dataclasses import dataclass
from typing import List, Optional
import random
from pydantic import BaseModel
from typing import List
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from base import BaseWorkflow
from parameters import Lora
# Workflow-specific parameters
class WorkflowParams(BaseModel):
prompt: str
negativePrompt: str = "text, watermark"
checkpoints: List[str]
loras: List[Lora] = []
steps: int = 20
seed: int = None
width: int = 512
height: int = 512
# Workflow definition
class TestWorkflow(BaseWorkflow):
def run(self):
params: WorkflowParams = self.params
with Workflow(wait=False):
model, clip, vae = self.load_checkpoints()
model, clip = self.apply_loras(model, clip)
conditioning = CLIPTextEncode(params.prompt, clip)
conditioning2 = CLIPTextEncode(params.negativePrompt, clip)
latent = EmptyLatentImage(params.width, params.height, 1)
seed = params.seed
steps = params.steps
latent = KSampler(model, seed, steps, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1)
image = VAEDecode(latent, vae)
preview = PreviewImage(image)
return {
'prompt_id': preview.task.prompt_id,
'status': 'queued',
'parameters': self.params.model_dump()
}
# Module-level entry point
def run_workflow(**kwargs):
"""Entry point for the workflow."""
required = ['prompt', 'checkpoints', 'seed', 'steps', 'negativePrompt', 'width', 'height']
missing = [f for f in required if f not in kwargs]
if missing:
raise ValueError(f"Missing required fields: {', '.join(missing)}")
params = WorkflowParams(**kwargs)
with Workflow(wait=False):
if len(params.checkpoints) > 1:
model1, clip1, vae = CheckpointLoaderSimple(params.checkpoints[0])
model2, clip2, vae = CheckpointLoaderSimple(params.checkpoints[1])
model = ModelMergeSimple(model1, model2, 0.5)
clip = CLIPMergeSimple(clip1, clip2, 0.5)
else:
model, clip, vae = CheckpointLoaderSimple(params.checkpoints[0])
conditioning = CLIPTextEncode(params.prompt, clip)
conditioning2 = CLIPTextEncode(params.negativePrompt, clip)
latent = EmptyLatentImage(params.width, params.height, 1)
seed = params.seed
steps = params.steps
latent = KSampler(model, seed, steps, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1)
image = VAEDecode(latent, vae)
preview = PreviewImage(image)
return {
'prompt_id': preview.task.prompt_id,
'status': 'queued',
'parameters': kwargs
}
# Parameters typing
@dataclass
class WorkflowParams:
prompt: str
checkpoints: List[str]
seed: Optional[int] = random.randint(0, 15663308700286)
steps: int = 20
negativePrompt: str = "text, watermark"
width: int = 512
height: int = 512
# Create and run workflow
workflow = TestWorkflow(params)
return workflow.run()