Skip to content

Commit

Permalink
v0.8.0-beta
Browse files Browse the repository at this point in the history
Revamped the img2img workflow, added soft inapinting, added fooocus inpainting, added IPA integration with masking and added CN union integration
  • Loading branch information
ramyma committed Aug 15, 2024
1 parent 81350bf commit c9adf31
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 460 deletions.
42 changes: 42 additions & 0 deletions assets/js/src/LayersControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ const LayersControl = () => {
controlnetDetect,
ip_adapter_models,
ip_adapter_weight_types,
unionControlnetTypes,
} = useControlnet();

// const { register, handleSubmit, setValue } = useForm();
Expand Down Expand Up @@ -905,6 +906,22 @@ const LayersControl = () => {
</Checkbox>
)}

{backend === "comfy" && !activeControlnetLayer?.isIpAdapter && (
<Checkbox
checked={activeControlnetLayer?.is_union}
value={activeControlnetLayer?.is_union}
onChange={(value) =>
handleControlnetAttrsChange(
"is_union",
value,
activeControlnetLayer.id
)
}
>
Union Controlnet
</Checkbox>
)}

{backend === "comfy" && activeControlnetLayer?.isIpAdapter ? (
<>
<div className="flex gap-2 h-full flex-col">
Expand Down Expand Up @@ -993,6 +1010,31 @@ const LayersControl = () => {
</>
)}

{backend === "comfy" &&
!activeControlnetLayer?.isIpAdapter &&
activeControlnetLayer?.is_union && (
<div className="flex gap-2 flex-col">
<Label
htmlFor={`module${activeControlnetLayer?.union_type}`}
>
Union Type
</Label>
<Select
name="union_type"
items={unionControlnetTypes}
value={activeControlnetLayer?.union_type}
onChange={(value) =>
handleControlnetSelectChange({
name: "union_type",
type: "text",
value,
layerId: activeControlnetLayer.id,
})
}
/>
</div>
)}

{backend === "auto" &&
!/ip\S*adapter/gi.test(activeControlnetLayer.module) && (
<div className="flex gap-2 flex-col">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Control, Controller } from "react-hook-form";
import { comfySoftPaintingFields } from "./constants";
import Slider from "../../components/Slider";
import { MainFormValues } from "../MainForm";
import ExpandCollapseCheckbox from "../../components/ExpandCollapseCheckbox";

export type ComfySoftInpaintingArgs = {
isEnabled: boolean;
maskBlur: number;
};

const SoftInpaintingFields = ({
control,
}: {
control: Control<MainFormValues>;
}) => {
return (
<div className="flex flex-col gap-3">
<Controller
name={"comfySoftInpainting.isEnabled"}
control={control}
render={({ field }) => (
<ExpandCollapseCheckbox {...field} label="Soft Inpainting">
<div className="h-auto flex relative flex-col gap-8 bg-neutral-100/5 p-4 rounded-md overflow-hidden">
{comfySoftPaintingFields?.map(
({ value: defaultValue, ...rest }) => (
<div key={rest.name}>
<Controller
name={"comfySoftInpainting." + rest.name}
control={control}
render={({ field }) => <Slider {...rest} {...field} />}
defaultValue={defaultValue}
/>
</div>
)
)}
</div>
</ExpandCollapseCheckbox>
)}
defaultValue={false}
/>
</div>
);
};

export default SoftInpaintingFields;
24 changes: 24 additions & 0 deletions assets/js/src/MainForm/ComfySoftInpaintingFields/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ComfySoftInpaintingArgs } from "./ComfySoftInpaintingFields";

export const defaultComfySoftPaintingArgs: ComfySoftInpaintingArgs = {
isEnabled: false,
maskBlur: 6,
};

export const comfySoftPaintingFields: {
label: string;
name: keyof ComfySoftInpaintingArgs;
value: number;
min: number;
max: number;
step: number;
}[] = [
{
label: "Mask Blur",
name: "maskBlur",
value: 6,
min: 0,
max: 30,
step: 1,
},
];
1 change: 1 addition & 0 deletions assets/js/src/MainForm/ComfySoftInpaintingFields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./ComfySoftInpaintingFields";
20 changes: 14 additions & 6 deletions assets/js/src/MainForm/MainForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ import { REGIONAL_PROMPTS_SEPARATOR, weightTypesByName } from "./constants";
import { selectPromptRegionLayers } from "../state/promptRegionsSlice";
import { showNotification } from "../Notifications/utils";
import Button from "../components/Button";
import Modal from "../components/Modal";
import ModelsModal from "../ModelsModal";
import LorasModal from "../LorasModal";
import { useCustomEventListener } from "react-custom-events";
import ComfySoftInpaintingFields from "./ComfySoftInpaintingFields";
import { ComfySoftInpaintingArgs } from "./ComfySoftInpaintingFields/ComfySoftInpaintingFields";

export type MainFormValues = Record<string, any> & {
softInpainting: SoftInpaintingArgs;
comfySoftInpainting: ComfySoftInpaintingArgs;
regionalPrompts?: Record<string, { prompt: EditorState; weight }>;
globalPromptWeight?: number;
};
Expand Down Expand Up @@ -284,6 +284,7 @@ const MainForm = () => {
negative_prompt,
scheduler,
softInpainting,
comfySoftInpainting,
globalPromptWeight,
fooocus_inpaint,
...rest
Expand Down Expand Up @@ -609,12 +610,17 @@ const MainForm = () => {
...(backend === "comfy" && iPAdapters?.length
? { ip_adapters: iPAdapters }
: {}),
...(backend === "comfy" && !txt2img ? { fooocus_inpaint } : {}),
...(backend === "comfy" && !txt2img && model?.isSdXl
? { fooocus_inpaint }
: {}),
...(backend === "comfy" && comfySoftInpainting.isEnabled
? { mask_blur: comfySoftInpainting.maskBlur }
: {}),
ultimate_upscale: isUltimateUpscaleEnabled,
clip_skip: clipSkip,
};

console.log(image, { attrs });
// console.log(image, { attrs });

batchImageResults?.length && dispatch(setBatchImageResults([]));

Expand Down Expand Up @@ -918,7 +924,9 @@ const MainForm = () => {

{showSoftInpainting && <SoftInpaintingFields control={control} />}

{backend === "comfy" && !txt2img && (
{backend === "comfy" && <ComfySoftInpaintingFields control={control} />}

{backend === "comfy" && model?.isSdXl && !txt2img && (
<Controller
name="fooocus_inpaint"
control={control}
Expand Down
13 changes: 12 additions & 1 deletion assets/js/src/hooks/useControlnet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,19 @@ const useControlnet = ({ fetchPolicy }: Props = {}) => {
fetchPolicy,
});

const { fetchData: fetchUnionControlnetTypes, data: unionControlnetTypes } =
useData<string[]>({
name: "union_controlnet_types",
fetchPolicy,
});

const {
fetchData: fetchIpApapterModels,
data: { ip_adapter_models, ip_adapter_weight_types } = {},
} = useData<string[]>({
} = useData<{
ip_adapter_models: string[];
ip_adapter_weight_types: string[];
}>({
name: "ip_adapter_models",
fetchPolicy,
condition: backend === "comfy",
Expand Down Expand Up @@ -186,10 +195,12 @@ const useControlnet = ({ fetchPolicy }: Props = {}) => {
return {
controlnet_models,
controlnet_preprocessors,
unionControlnetTypes,
ip_adapter_models,
ip_adapter_weight_types,
fetchControlnetModels,
fetchControlnetModules,
fetchUnionControlnetTypes,
fetchIpApapterModels,
controlnetDetect,
};
Expand Down
4 changes: 4 additions & 0 deletions assets/js/src/state/controlnetSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ type ControlnetUi = {
weight_type?: keyof typeof weightTypesByName;
composition_weight?: number;
isIpAdapter: boolean;
is_union: boolean;
iPAdapterModel?: string;
iPAdapterWeightType?: string;
union_type?: string;
};
export type ControlnetLayer = {
id?: string;
Expand Down Expand Up @@ -94,6 +96,8 @@ const controlnetLayerInitialState: ControlnetLayer = {
control_mode: "Balanced",
maskColor: "#FFFFFF",
isIpAdapter: false,
is_union: false,
union_type: "auto",
};
const initialState: ControlnetState = {
layers: [
Expand Down
17 changes: 17 additions & 0 deletions lib/ex_sd/comfy_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ defmodule ExSd.ComfyClient do
end
end

@spec get_union_controlnet_types() :: {:error, any()} | {:ok, list(binary())}
def get_union_controlnet_types() do
with response <- get("/object_info/SetUnionControlNetType"),
{:ok, body} <- handle_response(response) do
types =
body
|> get_in(["SetUnionControlNetType", "input", "required", "type"])
|> List.first()
|> Enum.sort()

{:ok, types}
else
{:error, _error} = res ->
res
end
end

@spec get_ip_adapter_models() :: {:error, any()} | {:ok, list(binary())}
def get_ip_adapter_models() do
with response <- get("/object_info/IPAdapterUnifiedLoader"),
Expand Down
3 changes: 3 additions & 0 deletions lib/ex_sd/sd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ defmodule ExSd.Sd do
@spec get_controlnet_preprocessors :: {:ok, list}
defdelegate get_controlnet_preprocessors(), to: SdServer

@spec get_union_controlnet_types :: {:ok, list}
defdelegate get_union_controlnet_types(), to: SdServer

@spec get_ip_adapter_models :: {:ok, map()}
defdelegate get_ip_adapter_models(), to: SdServer

Expand Down
45 changes: 40 additions & 5 deletions lib/ex_sd/sd/comfy_prompt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ defmodule ExSd.Sd.ComfyPrompt do
controlnet_args: controlnet_args
)

File.write!("./prompt.json", Jason.encode!(prompt, pretty: true))
# File.write!("./prompt.json", Jason.encode!(prompt, pretty: true))
prompt
end

Expand Down Expand Up @@ -698,7 +698,7 @@ defmodule ExSd.Sd.ComfyPrompt do
add_condition: has_ultimate_upscale
)

File.write!("./prompt.json", Jason.encode!(prompt, pretty: true))
# File.write!("./prompt.json", Jason.encode!(prompt, pretty: true))
prompt
end

Expand Down Expand Up @@ -971,6 +971,31 @@ defmodule ExSd.Sd.ComfyPrompt do
add_node(prompt, node)
end

@spec maybe_add_set_union_controlnet_type(
prompt(),
binary(),
boolean(),
[
{:control_net, ref_node_value()},
{:type, binary()}
]
) :: prompt()
def maybe_add_set_union_controlnet_type(prompt, name, condition, options \\ [])

def maybe_add_set_union_controlnet_type(prompt, _name, false, _options) do
prompt
end

def maybe_add_set_union_controlnet_type(prompt, name, _condition, options) do
node =
node(name, "SetUnionControlNetType", %{
control_net: Keyword.get(options, :control_net),
type: Keyword.get(options, :type)
})

add_node(prompt, node)
end

@spec add_image_loader(
prompt(),
[
Expand Down Expand Up @@ -1129,6 +1154,12 @@ defmodule ExSd.Sd.ComfyPrompt do
|> add_controlnet_loader("cn#{entry.model}_controlnet_loader",
control_net_name: entry.model
)
|> maybe_add_set_union_controlnet_type(
"cn#{index}_union_controlnet_type",
entry.is_union,
control_net: node_ref("cn#{entry.model}_controlnet_loader", 0),
type: entry.union_type
)
|> add_controlnet_apply_advanced(
name: "cn#{index}_apply_controlnet",
strength: entry.weight,
Expand All @@ -1155,7 +1186,10 @@ defmodule ExSd.Sd.ComfyPrompt do
# TODO: reuse loaded models to avoid loading a model more than once for different layers
control_net:
node_ref(
"cn#{entry.model}_controlnet_loader",
if(entry.is_union,
do: "cn#{index}_union_controlnet_type",
else: "cn#{entry.model}_controlnet_loader"
),
0
),
image:
Expand Down Expand Up @@ -1371,7 +1405,8 @@ defmodule ExSd.Sd.ComfyPrompt do
node_ref(
"attention_couple_regions_#{max(0, ceil(length(attention_couple_regions) / 10) - 1)}",
0
)
),
ip_adapter_active: not Enum.empty?(ip_adapters)
}
)
)
Expand Down Expand Up @@ -1798,7 +1833,7 @@ defmodule ExSd.Sd.ComfyPrompt do
image:
if(Map.get(ip_adapter, "image"),
do: node_ref("ip_adapter_#{index}_image_loader", 0),
else: nil
else: node_ref("image_input", 0)
),
attn_mask:
if(Map.get(ip_adapter, "mask"),
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_sd/sd/control_net_args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ defmodule ExSd.Sd.ControlNetArgs do
field :pixel_perfect, :boolean, default: true
field :control_mode, :string, default: "Balanced"
field :advanced_weighting, {:array, :float}, default: nil
field :is_union, :boolean, default: false
field :union_type, :binary
end

def changeset(%__MODULE__{} = control_net, attrs) do
Expand All @@ -52,7 +54,9 @@ defmodule ExSd.Sd.ControlNetArgs do
:guidance_end,
:pixel_perfect,
:control_mode,
:advanced_weighting
:advanced_weighting,
:is_union,
:union_type
]
)
end
Expand Down
Loading

0 comments on commit c9adf31

Please sign in to comment.