2023-09-06 17:36:00 +00:00
|
|
|
from typing import Literal
|
|
|
|
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
from invokeai.app.invocations.baseinvocation import (
|
|
|
|
BaseInvocation,
|
|
|
|
BaseInvocationOutput,
|
|
|
|
FieldDescriptions,
|
|
|
|
InputField,
|
|
|
|
InvocationContext,
|
|
|
|
OutputField,
|
|
|
|
UIType,
|
|
|
|
invocation,
|
|
|
|
invocation_output,
|
|
|
|
)
|
|
|
|
from invokeai.app.invocations.primitives import ImageField
|
|
|
|
|
|
|
|
IP_ADAPTER_MODELS = Literal[
|
2023-09-12 23:09:10 +00:00
|
|
|
"ip-adapter_sd15",
|
|
|
|
"ip-adapter-plus_sd15",
|
|
|
|
"ip-adapter-plus-face_sd15",
|
|
|
|
"ip-adapter_sdxl",
|
2023-09-06 17:36:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
IP_ADAPTER_IMAGE_ENCODER_MODELS = Literal[
|
|
|
|
"models/core/ip_adapters/sd-1/image_encoder/", "models/core/ip_adapters/sdxl/image_encoder"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class IPAdapterField(BaseModel):
|
|
|
|
image: ImageField = Field(description="The IP-Adapter image prompt.")
|
|
|
|
|
|
|
|
# TODO(ryand): Create and use a custom `IpAdapterModelField`.
|
|
|
|
ip_adapter_model: str = Field(description="The name of the IP-Adapter model.")
|
|
|
|
|
|
|
|
# TODO(ryand): Create and use a `CLIPImageEncoderField` instead that is analogous to the `ClipField` used elsewhere.
|
|
|
|
image_encoder_model: str = Field(description="The name of the CLIP image encoder model.")
|
|
|
|
|
|
|
|
weight: float = Field(default=1.0, ge=0, description="The weight of the IP-Adapter.")
|
|
|
|
|
|
|
|
|
|
|
|
@invocation_output("ip_adapter_output")
|
|
|
|
class IPAdapterOutput(BaseInvocationOutput):
|
|
|
|
# Outputs
|
2023-09-08 20:14:17 +00:00
|
|
|
ip_adapter: IPAdapterField = OutputField(description=FieldDescriptions.ip_adapter, title="IP-Adapter")
|
2023-09-06 17:36:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invocation("ip_adapter", title="IP-Adapter", tags=["ip_adapter", "control"], category="ip_adapter", version="1.0.0")
|
|
|
|
class IPAdapterInvocation(BaseInvocation):
|
|
|
|
"""Collects IP-Adapter info to pass to other nodes."""
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
image: ImageField = InputField(description="The IP-Adapter image prompt.")
|
|
|
|
ip_adapter_model: IP_ADAPTER_MODELS = InputField(
|
2023-09-12 23:09:10 +00:00
|
|
|
default="ip-adapter_sd15.bin",
|
2023-09-08 20:14:17 +00:00
|
|
|
description="The name of the IP-Adapter model.",
|
|
|
|
title="IP-Adapter Model",
|
2023-09-06 17:36:00 +00:00
|
|
|
)
|
|
|
|
image_encoder_model: IP_ADAPTER_IMAGE_ENCODER_MODELS = InputField(
|
|
|
|
default="models/core/ip_adapters/sd-1/image_encoder/", description="The name of the CLIP image encoder model."
|
|
|
|
)
|
|
|
|
weight: float = InputField(default=1.0, description="The weight of the IP-Adapter.", ui_type=UIType.Float)
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IPAdapterOutput:
|
|
|
|
return IPAdapterOutput(
|
|
|
|
ip_adapter=IPAdapterField(
|
|
|
|
image=self.image,
|
2023-09-12 23:09:10 +00:00
|
|
|
ip_adapter_model=self.ip_adapter_model,
|
|
|
|
image_encoder_model=self.image_encoder_model,
|
2023-09-06 17:36:00 +00:00
|
|
|
weight=self.weight,
|
|
|
|
),
|
|
|
|
)
|