Skip to content

ControlInput

Bases: BaseModel

A Pydantic model representing the input control parameters for an operation, typically involving image processing.

This model includes parameters such as the operation mode, the image to be processed, an alternative raw image, and a strength parameter. It validates these parameters to ensure they meet specific criteria, such as the mode being one of the predefined valid modes and ensuring that both 'image' and 'image_raw' are not provided simultaneously.

Attributes:

Name Type Description
mode str

The operation mode, which must be one of the predefined valid modes.

image LazyLoadingImage

An instance of LazyLoadingImage to be processed. Defaults to None.

image_raw LazyLoadingImage

An alternative raw image instance of LazyLoadingImage. Defaults to None.

strength float

A float value representing the strength of the operation, must be between 0 and 1000 (inclusive). Defaults to 1.

Methods:

Name Description
image_raw_validate

Validates that either 'image' or 'image_raw' is provided, but not both.

mode_validate

Validates that the 'mode' attribute is one of the predefined valid modes in the configuration.

Raises:

Type Description
ValueError

Raised if both 'image' and 'image_raw' are specified, or if the 'mode' is not a valid mode.

Source code in imaginairy/schema.py
class ControlInput(BaseModel):
    """
    A Pydantic model representing the input control parameters for an operation,
    typically involving image processing.

    This model includes parameters such as the operation mode, the image to be processed,
    an alternative raw image, and a strength parameter. It validates these parameters to
    ensure they meet specific criteria, such as the mode being one of the predefined valid modes
    and ensuring that both 'image' and 'image_raw' are not provided simultaneously.

    Attributes:
        mode (str): The operation mode, which must be one of the predefined valid modes.
        image (LazyLoadingImage, optional): An instance of LazyLoadingImage to be processed.
                                            Defaults to None.
        image_raw (LazyLoadingImage, optional): An alternative raw image instance of
                                                LazyLoadingImage. Defaults to None.
        strength (float): A float value representing the strength of the operation, must be
                          between 0 and 1000 (inclusive). Defaults to 1.

    Methods:
        image_raw_validate: Validates that either 'image' or 'image_raw' is provided,
                            but not both.
        mode_validate: Validates that the 'mode' attribute is one of the predefined valid
                       modes in the configuration.

    Raises:
        ValueError: Raised if both 'image' and 'image_raw' are specified, or if the
                    'mode' is not a valid mode.
    """

    mode: str
    image: LazyLoadingImage | None = None
    image_raw: LazyLoadingImage | None = None
    strength: float = Field(1, ge=0, le=1000)

    # @field_validator("image", "image_raw", mode="before")
    # def validate_images(cls, v):
    #     if isinstance(v, str):
    #         return LazyLoadingImage(filepath=v)
    #
    #     return v

    @field_validator("image_raw")
    def image_raw_validate(cls, v, info: core_schema.FieldValidationInfo):
        if info.data.get("image") is not None and v is not None:
            raise ValueError("You cannot specify both image and image_raw")

        # if v is None and values.get("image") is None:
        #     raise ValueError("You must specify either image or image_raw")

        return v

    @field_validator("mode")
    def mode_validate(cls, v):
        if v not in config.CONTROL_CONFIG_SHORTCUTS:
            valid_modes = list(config.CONTROL_CONFIG_SHORTCUTS.keys())
            valid_modes = ", ".join(valid_modes)
            msg = f"Invalid controlnet mode: '{v}'. Valid modes are: {valid_modes}"
            raise ValueError(msg)
        return v