The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
[Partner Nodes] feat(Krea): add Krea 3 Image node (#17067)
Alexander Piskuna5e95e675a6c
diff --git a/comfy_api_nodes/apis/krea.py b/comfy_api_nodes/apis/krea.pyindex 6e294a3b7..5176ab940 100644--- a/comfy_api_nodes/apis/krea.py+++ b/comfy_api_nodes/apis/krea.py@@ -23,6 +23,16 @@ class KreaGenerateImageRequest(BaseModel):image_style_references: list[KreaImageStyleReference] | None = Field(default=None)+class Krea3GenerateImageRequest(BaseModel):+ prompt: str = Field(...)+ thinking_mode: str = Field(...)+ resolution: str = Field(...)+ aspect_ratio: str | None = Field(default=None)+ seed: int = Field(...)+ image_urls: list[str] | None = Field(default=None)+ regional_edit: bool = Field(...)++class KreaJobResult(BaseModel):urls: list[str] | None = Field(default=None)style_id: str | None = Field(default=None)diff --git a/comfy_api_nodes/nodes_krea.py b/comfy_api_nodes/nodes_krea.pyindex fc2ed434b..735fcc343 100644--- a/comfy_api_nodes/nodes_krea.py+++ b/comfy_api_nodes/nodes_krea.py@@ -6,6 +6,7 @@ from typing_extensions import overridefrom comfy_api.latest import IO, ComfyExtension, Inputfrom comfy_api_nodes.apis.krea import (+ Krea3GenerateImageRequest,KreaAssetResponse,KreaGenerateImageRequest,KreaImageStyleReference,@@ -26,9 +27,9 @@ class KreaIO:STYLE_REF = "KREA_STYLE_REF"-async def _upload_image_to_krea_assets(cls: type[IO.ComfyNode], image: Input.Image) -> str:+async def _upload_image_to_krea_assets(cls: type[IO.ComfyNode], image: Input.Image, total_pixels: int) -> str:"""Upload an image to Krea's /assets endpoint and return the Krea-hosted image URL."""- img_io = tensor_to_bytesio(image, total_pixels=2048 * 2048, mime_type="image/png")+ img_io = tensor_to_bytesio(image, total_pixels=total_pixels, mime_type="image/png")response = await sync_op(cls,endpoint=ApiEndpoint(path="/proxy/krea/assets", method="POST"),@@ -56,6 +57,28 @@ _KREA_QUEUED_STATUSES = ["backlogged", "queued", "scheduled"]_UUID_RE = re.compile(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")+_KREA3_ASPECT_RATIOS = ["auto", "1:1", "4:5", "3:4", "2:3", "9:16", "5:4", "4:3", "3:2", "16:9", "21:9"]+_IMAGE_REF_RE = re.compile(r"@image(?P<idx>\d*)(?!\w)", re.IGNORECASE | re.ASCII)+++def _resolve_image_refs(prompt: str, total_images: int) -> str:+ parts = []+ pos = 0+ prev_end = -1+ for match in _IMAGE_REF_RE.finditer(prompt):+ start = match.start()+ if start > 0 and start != prev_end and prompt[start - 1].isascii() and (prompt[start - 1].isalnum() or prompt[start - 1] == "_"):+ continue+ idx = int(match.group("idx") or 1)+ if not 1 <= idx <= total_images:+ raise ValueError(f"The prompt references @Image{idx}, but only {total_images} images are connected.")+ parts.append(prompt[pos:start])+ parts.append(f"image {idx}")+ pos = match.end()+ prev_end = match.end()+ parts.append(prompt[pos:])+ return "".join(parts)+def _krea_model_inputs() -> list:"""Nested inputs shared by Krea 2 Medium, Medium Turbo and Large under the DynamicCombo."""@@ -275,17 +298,150 @@ class Krea2StyleReferenceNode(IO.ComfyNode):chain: list[dict] = list(style_reference) if style_reference else []if len(chain) >= 10:raise ValueError("Krea 2 accepts at most 10 image_style_references in one generation.")- url = await _upload_image_to_krea_assets(cls, image)+ url = await _upload_image_to_krea_assets(cls, image, 2048 * 2048)chain.append({"url": url, "strength": float(strength)})return IO.NodeOutput(chain)+class Krea3ImageNode(IO.ComfyNode):++ @classmethod+ def define_schema(cls) -> IO.Schema:+ return IO.Schema(+ node_id="Krea3ImageNode",+ display_name="Krea 3 Image",+ category="partner/image/Krea",+ description="Edit images with Krea 3, or generate a new image from text when no image is connected.",+ inputs=[+ IO.DynamicCombo.Input(+ "model",+ options=[+ IO.DynamicCombo.Option(+ "Krea 3",+ [+ IO.Autogrow.Input(+ "images",+ template=IO.Autogrow.TemplatePrefix(+ IO.Image.Input("image"),+ prefix="image_",+ min=0,+ max=8,+ ),+ tooltip="Up to 8 images, numbered in connection order. "+ "Leave empty for text-to-image.",+ ),+ IO.String.Input(+ "prompt",+ multiline=True,+ default="",+ tooltip="What to change and what to keep. "+ "Refer to the images as @Image1, @Image2, ... or image 1, image 2, ...",+ ),+ IO.Combo.Input(+ "aspect_ratio",+ options=_KREA3_ASPECT_RATIOS,+ default="auto",+ tooltip="auto follows the first image, or the prompt when no image is connected.",+ ),+ IO.Combo.Input(+ "resolution",+ options=["1K", "2K"],+ default="2K",+ ),+ IO.Combo.Input(+ "thinking_mode",+ options=["fast", "high"],+ default="fast",+ tooltip="fast is recommended. "+ "high plans complex edits in more detail and takes longer.",+ ),+ IO.Boolean.Input(+ "precise_edit",+ default=False,+ tooltip="Keep everything outside the edited area unchanged. "+ "Needs at least one image.",+ ),+ IO.Int.Input(+ "seed",+ default=42,+ min=0,+ max=2147483647,+ control_after_generate=True,+ tooltip="Krea rewrites the prompt before generating, "+ "so the same seed does not repeat an image.",+ ),+ ],+ ),+ ],+ ),+ ],+ outputs=[IO.Image.Output()],+ hidden=[+ IO.Hidden.auth_token_comfy_org,+ IO.Hidden.api_key_comfy_org,+ IO.Hidden.unique_id,+ ],+ is_api_node=True,+ price_badge=IO.PriceBadge(+ depends_on=IO.PriceBadgeDepends(+ widgets=["model.resolution", "model.thinking_mode"],+ input_groups=["model.images"],+ ),+ expr="""+ (+ $res := $lookup(widgets, "model.resolution");+ $high := $lookup(widgets, "model.thinking_mode") = "high";+ $refs := $lookup(inputGroups, "model.images");+ $base := $res = "1k" ? ($high ? 0.0715 : 0.0429) : ($high ? 0.1144 : 0.0858);+ $min := $base + 0.0286 * $refs;+ ($res = "1k" or $refs = 0)+ ? {"type":"usd","usd": $min}+ : {"type":"range_usd","min_usd": $min,"max_usd": $base + 0.1001 * $refs}+ )+ """,+ ),+ )++ @classmethod+ async def execute(cls, model: dict) -> IO.NodeOutput:+ validate_string(model["prompt"], min_length=1)+ prompt = _resolve_image_refs(model["prompt"], len(model["images"]))+ reference_pixels = 1024 * 1024 if model["resolution"] == "1K" else 2048 * 2048+ image_urls = [+ await _upload_image_to_krea_assets(cls, image, reference_pixels) for image in model["images"].values()+ ]+ initial = await sync_op(+ cls,+ ApiEndpoint(path="/proxy/krea/generate/image/krea/krea-3", method="POST"),+ response_model=KreaJob,+ data=Krea3GenerateImageRequest(+ prompt=prompt,+ thinking_mode=model["thinking_mode"],+ resolution=model["resolution"].lower(),+ aspect_ratio=None if model["aspect_ratio"] == "auto" else model["aspect_ratio"],+ seed=model["seed"],+ image_urls=image_urls or None,+ regional_edit=model["precise_edit"],+ ),+ )+ job = await poll_op(+ cls,+ ApiEndpoint(path=f"/proxy/krea/jobs/{initial.job_id}", method="GET"),+ response_model=KreaJob,+ status_extractor=lambda r: r.status,+ queued_statuses=_KREA_QUEUED_STATUSES,+ poll_interval=1.0,+ )+ return IO.NodeOutput(await download_url_to_image_tensor(job.result.urls[0]))++class KreaExtension(ComfyExtension):@overrideasync def get_node_list(self) -> list[type[IO.ComfyNode]]:return [Krea2ImageNode,Krea2StyleReferenceNode,+ Krea3ImageNode,]