Skip to content

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(Bria): add FIBO 2.0 Image Generate node and move Image Edit to FIBO 2.0 (#17229)

Alexander Piskun7ef81780f814
diff --git a/comfy_api_nodes/apis/bria.py b/comfy_api_nodes/apis/bria.py
index a48eea567..230ec4258 100644
--- a/comfy_api_nodes/apis/bria.py
+++ b/comfy_api_nodes/apis/bria.py
@@ -45,6 +45,22 @@ class BriaEditImageRequest(BaseModel):
)
+class BriaGenerateImageRequest(BaseModel):
+ prompt: str | None = Field(None)
+ structured_prompt: str | None = Field(None)
+ images: list[str] | None = Field(None, description="A single reference image.")
+ aspect_ratio: str = Field(...)
+ resolution: str = Field(...)
+ seed: int = Field(...)
+ prompt_content_moderation: bool = Field(False, description="If true, returns 422 on prompt moderation failure.")
+ visual_input_content_moderation: bool = Field(
+ False, description="If true, returns 422 on reference image moderation failure."
+ )
+ visual_output_content_moderation: bool = Field(
+ False, description="If true, returns 422 on visual output moderation failure."
+ )
+
+
class BriaRemoveBackgroundRequest(BaseModel):
image: str = Field(...)
sync: bool = Field(False)
diff --git a/comfy_api_nodes/nodes_bria.py b/comfy_api_nodes/nodes_bria.py
index 3c2866e2a..6673bdcef 100644
--- a/comfy_api_nodes/nodes_bria.py
+++ b/comfy_api_nodes/nodes_bria.py
@@ -14,6 +14,7 @@ from comfy_api_nodes.apis.bria import (
BriaExpandRequest,
BriaExpandResponse,
BriaFiboEditResponse,
+ BriaGenerateImageRequest,
BriaGenFillRequest,
BriaImageEditResponse,
BriaImageResultResponse,
@@ -51,9 +52,12 @@ from comfy_api_nodes.util import (
)
BRIA_MAX_OUTPUT_SIDE = 8192
+BRIA_MAX_CANVAS_AREA = 25_000_000
BRIA_MIN_RATIO = 0.5
BRIA_MAX_RATIO = 3.0
BRIA_MIN_SHORT_SIDE = 224
+BRIA_ASPECT_RATIOS = ["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9"]
+BRIA_FIBO_2_ASPECT_RATIOS = ["16:10", "10:16", "2:1", "1:2", "21:9", "9:21", "3:1", "1:3", "4:1", "1:4", "5:1", "1:5"]
def _upscaled_output_side(height: int, width: int, multiplier: int) -> int:
@@ -71,9 +75,9 @@ class BriaImageEditNode(IO.ComfyNode):
def define_schema(cls):
return IO.Schema(
node_id="BriaImageEditNode",
- display_name="Bria FIBO Image Edit",
+ display_name="Bria FIBO 2.0 Image Edit",
category="partner/image/Bria",
- description="Edit images using Bria latest model",
+ description="Edit images using Bria FIBO 2.0.",
inputs=[
IO.Combo.Input("model", options=["FIBO"]),
IO.Image.Input("image"),
@@ -149,7 +153,7 @@ class BriaImageEditNode(IO.ComfyNode):
],
is_api_node=True,
price_badge=IO.PriceBadge(
- expr="""{"type":"usd","usd":0.04}""",
+ expr="""{"type":"usd","usd":0.0572}""",
),
)
@@ -169,9 +173,16 @@ class BriaImageEditNode(IO.ComfyNode):
) -> IO.NodeOutput:
if not prompt and not structured_prompt:
raise ValueError("One of prompt or structured_prompt is required to be non-empty.")
+ if prompt and structured_prompt:
+ raise ValueError("Use either prompt or structured_prompt, not both.")
mask_url = None
if mask is not None:
- mask_url = await upload_image_to_comfyapi(cls, convert_mask_to_image(mask), wait_label="Uploading mask")
+ if mask.shape[-2:] != image.shape[1:3]:
+ raise ValueError(
+ f"Mask must be the same size as the image: image is {image.shape[2]}x{image.shape[1]}, "
+ f"mask is {mask.shape[-1]}x{mask.shape[-2]}."
+ )
+ mask_url = await upload_image_to_comfyapi(cls, _mask_to_binary_image(mask, "edit"), wait_label="Uploading mask")
response = await sync_op(
cls,
ApiEndpoint(path="proxy/bria/v2/image/edit", method="POST"),
@@ -503,8 +514,7 @@ class BriaExpandImage(IO.ComfyNode):
IO.DynamicCombo.Input(
"expand_mode",
options=[
- *[IO.DynamicCombo.Option(ratio, []) for ratio in
- ["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9"]],
+ *[IO.DynamicCombo.Option(ratio, []) for ratio in BRIA_ASPECT_RATIOS],
IO.DynamicCombo.Option(
"custom_ratio",
[
@@ -633,17 +643,32 @@ class BriaExpandImage(IO.ComfyNode):
canvas_size = [expand_mode["canvas_width"], expand_mode["canvas_height"]]
original_image_size = [expand_mode["image_width"], expand_mode["image_height"]]
original_image_location = [expand_mode["image_x"], expand_mode["image_y"]]
- elif mode == "custom_ratio":
- ratio_width, ratio_height = expand_mode["ratio_width"], expand_mode["ratio_height"]
- aspect_ratio = ratio_width / ratio_height
- if not BRIA_MIN_RATIO <= aspect_ratio <= BRIA_MAX_RATIO:
+ x, y = original_image_location
+ if x <= 0 and y <= 0 and x + original_image_size[0] >= canvas_size[0] and y + original_image_size[1] >= canvas_size[1]:
+ raise ValueError("The image covers the whole canvas, so there is nothing to expand.")
+ else:
+ if mode == "custom_ratio":
+ ratio_width, ratio_height = expand_mode["ratio_width"], expand_mode["ratio_height"]
+ aspect_ratio = ratio_width / ratio_height
+ if not BRIA_MIN_RATIO <= aspect_ratio <= BRIA_MAX_RATIO:
+ raise ValueError(
+ f"Bria accepts a width-to-height ratio between {BRIA_MIN_RATIO} and {BRIA_MAX_RATIO}: "
+ f"{ratio_width}:{ratio_height} is {aspect_ratio:.4f}. "
+ f"Use the manual expand mode to reach a canvas of any shape."
+ )
+ else:
+ ratio_width, ratio_height = map(int, mode.split(":"))
+ aspect_ratio = mode
+ ratio = ratio_width / ratio_height
+ height, width = image.shape[1], image.shape[2]
+ canvas_width, canvas_height = max(width, int(height * ratio)), max(height, int(width / ratio))
+ if (canvas_width, canvas_height) == (width, height):
+ return IO.NodeOutput(_drop_alpha(image[:1]), "")
+ if canvas_width * canvas_height > BRIA_MAX_CANVAS_AREA:
raise ValueError(
- f"Bria accepts a width-to-height ratio between {BRIA_MIN_RATIO} and {BRIA_MAX_RATIO}: "
- f"{ratio_width}:{ratio_height} is {aspect_ratio:.4f}. "
- f"Use the manual expand mode to reach a canvas of any shape."
+ f"Expanding {width}x{height} to {ratio_width}:{ratio_height} needs a {canvas_width}x{canvas_height} "
+ f"canvas, but Bria allows at most {BRIA_MAX_CANVAS_AREA} pixels. Downscale the image first."
)
- else:
- aspect_ratio = mode
response = await sync_op(
cls,
ApiEndpoint(path="/proxy/bria/v2/image/edit/expand", method="POST"),
@@ -1154,7 +1179,7 @@ def _structured_prompt_output() -> IO.String.Output:
return IO.String.Output(
id="structured_prompt",
display_name="structured_prompt",
- tooltip="Structured description of the edited image, for a follow-up edit with Bria FIBO Image Edit.",
+ tooltip="Structured description of the edited image, for a follow-up edit with Bria FIBO 2.0 Image Edit.",
)
@@ -1182,6 +1207,105 @@ async def _run_fibo_edit(cls: type[IO.ComfyNode], path: str, data: BaseModel) ->
)
+class BriaImageGenerate(IO.ComfyNode):
+
+ @classmethod
+ def define_schema(cls):
+ return IO.Schema(
+ node_id="BriaImageGenerate",
+ display_name="Bria FIBO 2.0 Image Generate",
+ category="partner/image/Bria",
+ description="Generate an image from a text prompt, a reference image or a structured prompt "
+ "using Bria FIBO 2.0.",
+ inputs=[
+ IO.String.Input("prompt", multiline=True, default=""),
+ IO.String.Input(
+ "structured_prompt",
+ multiline=True,
+ default="",
+ tooltip="JSON structured prompt from a previous Bria FIBO result. With the same seed it "
+ "recreates that image; change the JSON or add a prompt to refine it.",
+ ),
+ IO.Combo.Input(
+ "aspect_ratio",
+ options=BRIA_ASPECT_RATIOS + BRIA_FIBO_2_ASPECT_RATIOS,
+ default="1:1",
+ tooltip="16:10 and the ratios after it need 1MP and exactly one of prompt or structured_prompt.",
+ ),
+ IO.Combo.Input("resolution", options=["1MP", "4MP"]),
+ IO.Int.Input(
+ "seed",
+ default=42,
+ min=1,
+ max=2147483647,
+ display_mode=IO.NumberDisplay.number,
+ control_after_generate=True,
+ ),
+ _moderation_combo("prompt_content_moderation", "visual_input_moderation", "visual_output_moderation"),
+ IO.Image.Input(
+ "image",
+ optional=True,
+ tooltip="Reference image to draw on. The result is a new image at the selected aspect ratio, "
+ "not an edit of this one.",
+ ),
+ ],
+ outputs=[
+ IO.Image.Output(),
+ IO.String.Output(
+ id="structured_prompt",
+ display_name="structured_prompt",
+ tooltip="Structured description of the generated image, to recreate or refine it with this node.",
+ ),
+ ],
+ 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(
+ expr='{"type":"usd","usd":0.0572}',
+ ),
+ )
+
+ @classmethod
+ async def execute(
+ cls,
+ prompt: str,
+ structured_prompt: str,
+ aspect_ratio: str,
+ resolution: str,
+ seed: int,
+ moderation: InputModerationSettings,
+ image: Input.Image | None = None,
+ ) -> IO.NodeOutput:
+ if not prompt and not structured_prompt and image is None:
+ raise ValueError("Provide a prompt, a structured_prompt or a reference image.")
+ if image is not None and structured_prompt:
+ raise ValueError("Use either a reference image or a structured_prompt, not both.")
+ if aspect_ratio in BRIA_FIBO_2_ASPECT_RATIOS and (resolution == "4MP" or bool(prompt) == bool(structured_prompt)):
+ raise ValueError(f"Aspect ratio {aspect_ratio} needs 1MP and exactly one of prompt or structured_prompt.")
+ images = None
+ if image is not None:
+ images = [await upload_image_to_comfyapi(cls, _drop_alpha(image), wait_label="Uploading image")]
+ visual_input, visual_output = _visual_moderation(moderation)
+ return await _run_fibo_edit(
+ cls,
+ "/proxy/bria/v2/image/generate",
+ BriaGenerateImageRequest(
+ prompt=prompt or None,
+ structured_prompt=structured_prompt or None,
+ images=images,
+ aspect_ratio=aspect_ratio,
+ resolution=resolution,
+ seed=seed,
+ prompt_content_moderation=moderation.get("prompt_content_moderation", False),
+ visual_input_content_moderation=visual_input,
+ visual_output_content_moderation=visual_output,
+ ),
+ )
+
+
class BriaEraseByText(IO.ComfyNode):
@classmethod
@@ -1834,6 +1958,7 @@ class BriaExtension(ComfyExtension):
async def get_node_list(self) -> list[type[IO.ComfyNode]]:
return [
BriaImageEditNode,
+ BriaImageGenerate,
BriaRemoveImageBackground,
BriaGenFill,
BriaEraser,