|
| 1 | +# Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from diffusers.modular_pipelines import ( |
| 17 | + AutoPipelineBlocks, |
| 18 | + ConditionalPipelineBlocks, |
| 19 | + InputParam, |
| 20 | + ModularPipelineBlocks, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +class TextToImageBlock(ModularPipelineBlocks): |
| 25 | + model_name = "text2img" |
| 26 | + |
| 27 | + @property |
| 28 | + def inputs(self): |
| 29 | + return [InputParam(name="prompt")] |
| 30 | + |
| 31 | + @property |
| 32 | + def intermediate_outputs(self): |
| 33 | + return [] |
| 34 | + |
| 35 | + @property |
| 36 | + def description(self): |
| 37 | + return "text-to-image workflow" |
| 38 | + |
| 39 | + def __call__(self, components, state): |
| 40 | + block_state = self.get_block_state(state) |
| 41 | + block_state.workflow = "text2img" |
| 42 | + self.set_block_state(state, block_state) |
| 43 | + return components, state |
| 44 | + |
| 45 | + |
| 46 | +class ImageToImageBlock(ModularPipelineBlocks): |
| 47 | + model_name = "img2img" |
| 48 | + |
| 49 | + @property |
| 50 | + def inputs(self): |
| 51 | + return [InputParam(name="prompt"), InputParam(name="image")] |
| 52 | + |
| 53 | + @property |
| 54 | + def intermediate_outputs(self): |
| 55 | + return [] |
| 56 | + |
| 57 | + @property |
| 58 | + def description(self): |
| 59 | + return "image-to-image workflow" |
| 60 | + |
| 61 | + def __call__(self, components, state): |
| 62 | + block_state = self.get_block_state(state) |
| 63 | + block_state.workflow = "img2img" |
| 64 | + self.set_block_state(state, block_state) |
| 65 | + return components, state |
| 66 | + |
| 67 | + |
| 68 | +class InpaintBlock(ModularPipelineBlocks): |
| 69 | + model_name = "inpaint" |
| 70 | + |
| 71 | + @property |
| 72 | + def inputs(self): |
| 73 | + return [InputParam(name="prompt"), InputParam(name="image"), InputParam(name="mask")] |
| 74 | + |
| 75 | + @property |
| 76 | + def intermediate_outputs(self): |
| 77 | + return [] |
| 78 | + |
| 79 | + @property |
| 80 | + def description(self): |
| 81 | + return "inpaint workflow" |
| 82 | + |
| 83 | + def __call__(self, components, state): |
| 84 | + block_state = self.get_block_state(state) |
| 85 | + block_state.workflow = "inpaint" |
| 86 | + self.set_block_state(state, block_state) |
| 87 | + return components, state |
| 88 | + |
| 89 | + |
| 90 | +class ConditionalImageBlocks(ConditionalPipelineBlocks): |
| 91 | + block_classes = [InpaintBlock, ImageToImageBlock, TextToImageBlock] |
| 92 | + block_names = ["inpaint", "img2img", "text2img"] |
| 93 | + block_trigger_inputs = ["mask", "image"] |
| 94 | + default_block_name = "text2img" |
| 95 | + |
| 96 | + @property |
| 97 | + def description(self): |
| 98 | + return "Conditional image blocks for testing" |
| 99 | + |
| 100 | + def select_block(self, mask=None, image=None) -> str | None: |
| 101 | + if mask is not None: |
| 102 | + return "inpaint" |
| 103 | + if image is not None: |
| 104 | + return "img2img" |
| 105 | + return None # falls back to default_block_name |
| 106 | + |
| 107 | + |
| 108 | +class OptionalConditionalBlocks(ConditionalPipelineBlocks): |
| 109 | + block_classes = [InpaintBlock, ImageToImageBlock] |
| 110 | + block_names = ["inpaint", "img2img"] |
| 111 | + block_trigger_inputs = ["mask", "image"] |
| 112 | + default_block_name = None # no default; block can be skipped |
| 113 | + |
| 114 | + @property |
| 115 | + def description(self): |
| 116 | + return "Optional conditional blocks (skippable)" |
| 117 | + |
| 118 | + def select_block(self, mask=None, image=None) -> str | None: |
| 119 | + if mask is not None: |
| 120 | + return "inpaint" |
| 121 | + if image is not None: |
| 122 | + return "img2img" |
| 123 | + return None |
| 124 | + |
| 125 | + |
| 126 | +class AutoImageBlocks(AutoPipelineBlocks): |
| 127 | + block_classes = [InpaintBlock, ImageToImageBlock, TextToImageBlock] |
| 128 | + block_names = ["inpaint", "img2img", "text2img"] |
| 129 | + block_trigger_inputs = ["mask", "image", None] |
| 130 | + |
| 131 | + @property |
| 132 | + def description(self): |
| 133 | + return "Auto image blocks for testing" |
| 134 | + |
| 135 | + |
| 136 | +class TestConditionalPipelineBlocksSelectBlock: |
| 137 | + def test_select_block_with_mask(self): |
| 138 | + blocks = ConditionalImageBlocks() |
| 139 | + assert blocks.select_block(mask="something") == "inpaint" |
| 140 | + |
| 141 | + def test_select_block_with_image(self): |
| 142 | + blocks = ConditionalImageBlocks() |
| 143 | + assert blocks.select_block(image="something") == "img2img" |
| 144 | + |
| 145 | + def test_select_block_with_mask_and_image(self): |
| 146 | + blocks = ConditionalImageBlocks() |
| 147 | + assert blocks.select_block(mask="m", image="i") == "inpaint" |
| 148 | + |
| 149 | + def test_select_block_no_triggers_returns_none(self): |
| 150 | + blocks = ConditionalImageBlocks() |
| 151 | + assert blocks.select_block() is None |
| 152 | + |
| 153 | + def test_select_block_explicit_none_values(self): |
| 154 | + blocks = ConditionalImageBlocks() |
| 155 | + assert blocks.select_block(mask=None, image=None) is None |
| 156 | + |
| 157 | + |
| 158 | +class TestConditionalPipelineBlocksWorkflowSelection: |
| 159 | + def test_default_workflow_when_no_triggers(self): |
| 160 | + blocks = ConditionalImageBlocks() |
| 161 | + execution = blocks.get_execution_blocks() |
| 162 | + assert execution is not None |
| 163 | + assert isinstance(execution, TextToImageBlock) |
| 164 | + |
| 165 | + def test_mask_trigger_selects_inpaint(self): |
| 166 | + blocks = ConditionalImageBlocks() |
| 167 | + execution = blocks.get_execution_blocks(mask=True) |
| 168 | + assert isinstance(execution, InpaintBlock) |
| 169 | + |
| 170 | + def test_image_trigger_selects_img2img(self): |
| 171 | + blocks = ConditionalImageBlocks() |
| 172 | + execution = blocks.get_execution_blocks(image=True) |
| 173 | + assert isinstance(execution, ImageToImageBlock) |
| 174 | + |
| 175 | + def test_mask_and_image_selects_inpaint(self): |
| 176 | + blocks = ConditionalImageBlocks() |
| 177 | + execution = blocks.get_execution_blocks(mask=True, image=True) |
| 178 | + assert isinstance(execution, InpaintBlock) |
| 179 | + |
| 180 | + def test_skippable_block_returns_none(self): |
| 181 | + blocks = OptionalConditionalBlocks() |
| 182 | + execution = blocks.get_execution_blocks() |
| 183 | + assert execution is None |
| 184 | + |
| 185 | + def test_skippable_block_still_selects_when_triggered(self): |
| 186 | + blocks = OptionalConditionalBlocks() |
| 187 | + execution = blocks.get_execution_blocks(image=True) |
| 188 | + assert isinstance(execution, ImageToImageBlock) |
| 189 | + |
| 190 | + |
| 191 | +class TestAutoPipelineBlocksSelectBlock: |
| 192 | + def test_auto_select_mask(self): |
| 193 | + blocks = AutoImageBlocks() |
| 194 | + assert blocks.select_block(mask="m") == "inpaint" |
| 195 | + |
| 196 | + def test_auto_select_image(self): |
| 197 | + blocks = AutoImageBlocks() |
| 198 | + assert blocks.select_block(image="i") == "img2img" |
| 199 | + |
| 200 | + def test_auto_select_default(self): |
| 201 | + blocks = AutoImageBlocks() |
| 202 | + # No trigger -> returns None -> falls back to default (text2img) |
| 203 | + assert blocks.select_block() is None |
| 204 | + |
| 205 | + def test_auto_select_priority_order(self): |
| 206 | + blocks = AutoImageBlocks() |
| 207 | + assert blocks.select_block(mask="m", image="i") == "inpaint" |
| 208 | + |
| 209 | + |
| 210 | +class TestAutoPipelineBlocksWorkflowSelection: |
| 211 | + def test_auto_default_workflow(self): |
| 212 | + blocks = AutoImageBlocks() |
| 213 | + execution = blocks.get_execution_blocks() |
| 214 | + assert isinstance(execution, TextToImageBlock) |
| 215 | + |
| 216 | + def test_auto_mask_workflow(self): |
| 217 | + blocks = AutoImageBlocks() |
| 218 | + execution = blocks.get_execution_blocks(mask=True) |
| 219 | + assert isinstance(execution, InpaintBlock) |
| 220 | + |
| 221 | + def test_auto_image_workflow(self): |
| 222 | + blocks = AutoImageBlocks() |
| 223 | + execution = blocks.get_execution_blocks(image=True) |
| 224 | + assert isinstance(execution, ImageToImageBlock) |
| 225 | + |
| 226 | + |
| 227 | +class TestConditionalPipelineBlocksStructure: |
| 228 | + def test_block_names_accessible(self): |
| 229 | + blocks = ConditionalImageBlocks() |
| 230 | + sub = dict(blocks.sub_blocks) |
| 231 | + assert set(sub.keys()) == {"inpaint", "img2img", "text2img"} |
| 232 | + |
| 233 | + def test_sub_block_types(self): |
| 234 | + blocks = ConditionalImageBlocks() |
| 235 | + sub = dict(blocks.sub_blocks) |
| 236 | + assert isinstance(sub["inpaint"], InpaintBlock) |
| 237 | + assert isinstance(sub["img2img"], ImageToImageBlock) |
| 238 | + assert isinstance(sub["text2img"], TextToImageBlock) |
| 239 | + |
| 240 | + def test_description(self): |
| 241 | + blocks = ConditionalImageBlocks() |
| 242 | + assert "Conditional" in blocks.description |
0 commit comments