|
| 1 | +<!--版权所有 2025 The HuggingFace Team。保留所有权利。 |
| 2 | +
|
| 3 | +根据Apache许可证2.0版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在 |
| 4 | +
|
| 5 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +获取许可证的副本。 |
| 8 | +
|
| 9 | +除非适用法律要求或书面同意,根据许可证分发的软件按"原样"分发,无任何明示或暗示的担保或条件。有关许可证的特定语言管理权限和限制,请参阅许可证。 |
| 10 | +--> |
| 11 | + |
| 12 | +# AutoPipelineBlocks |
| 13 | + |
| 14 | +[`~modular_pipelines.AutoPipelineBlocks`] 是一种包含支持不同工作流程的块的多块类型。它根据运行时提供的输入自动选择要运行的子块。这通常用于将多个工作流程(文本到图像、图像到图像、修复)打包到一个管道中以便利。 |
| 15 | + |
| 16 | +本指南展示如何创建 [`~modular_pipelines.AutoPipelineBlocks`]。 |
| 17 | + |
| 18 | +创建三个 [`~modular_pipelines.ModularPipelineBlocks`] 用于文本到图像、图像到图像和修复。这些代表了管道中可用的不同工作流程。 |
| 19 | + |
| 20 | +<hfoptions id="auto"> |
| 21 | +<hfoption id="text-to-image"> |
| 22 | + |
| 23 | +```py |
| 24 | +import torch |
| 25 | +from diffusers.modular_pipelines import ModularPipelineBlocks, InputParam, OutputParam |
| 26 | + |
| 27 | +class TextToImageBlock(ModularPipelineBlocks): |
| 28 | + model_name = "text2img" |
| 29 | + |
| 30 | + @property |
| 31 | + def inputs(self): |
| 32 | + return [InputParam(name="prompt")] |
| 33 | + |
| 34 | + @property |
| 35 | + def intermediate_outputs(self): |
| 36 | + return [] |
| 37 | + |
| 38 | + @property |
| 39 | + def description(self): |
| 40 | + return "我是一个文本到图像的工作流程!" |
| 41 | + |
| 42 | + def __call__(self, components, state): |
| 43 | + block_state = self.get_block_state(state) |
| 44 | + print("运行文本到图像工作流程") |
| 45 | + # 在这里添加你的文本到图像逻辑 |
| 46 | + # 例如:根据提示生成图像 |
| 47 | + self.set_block_state(state, block_state) |
| 48 | + return components, state |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +</hfoption> |
| 53 | +<hfoption id="image-to-image"> |
| 54 | + |
| 55 | +```py |
| 56 | +class ImageToImageBlock(ModularPipelineBlocks): |
| 57 | + model_name = "img2img" |
| 58 | + |
| 59 | + @property |
| 60 | + def inputs(self): |
| 61 | + return [InputParam(name="prompt"), InputParam(name="image")] |
| 62 | + |
| 63 | + @property |
| 64 | + def intermediate_outputs(self): |
| 65 | + return [] |
| 66 | + |
| 67 | + @property |
| 68 | + def description(self): |
| 69 | + return "我是一个图像到图像的工作流程!" |
| 70 | + |
| 71 | + def __call__(self, components, state): |
| 72 | + block_state = self.get_block_state(state) |
| 73 | + print("运行图像到图像工作流程") |
| 74 | + # 在这里添加你的图像到图像逻辑 |
| 75 | + # 例如:根据提示转换输入图像 |
| 76 | + self.set_block_state(state, block_state) |
| 77 | + return components, state |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +</hfoption> |
| 82 | +<hfoption id="inpaint"> |
| 83 | + |
| 84 | +```py |
| 85 | +class InpaintBlock(ModularPipelineBlocks): |
| 86 | + model_name = "inpaint" |
| 87 | + |
| 88 | + @property |
| 89 | + def inputs(self): |
| 90 | + return [InputParam(name="prompt"), InputParam(name="image"), InputParam(name="mask")] |
| 91 | + |
| 92 | + @property |
| 93 | + |
| 94 | + def intermediate_outputs(self): |
| 95 | + return [] |
| 96 | + |
| 97 | + @property |
| 98 | + def description(self): |
| 99 | + return "我是一个修复工作流!" |
| 100 | + |
| 101 | + def __call__(self, components, state): |
| 102 | + block_state = self.get_block_state(state) |
| 103 | + print("运行修复工作流") |
| 104 | + # 在这里添加你的修复逻辑 |
| 105 | + # 例如:根据提示填充被遮罩的区域 |
| 106 | + self.set_block_state(state, block_state) |
| 107 | + return components, state |
| 108 | +``` |
| 109 | + |
| 110 | +</hfoption> |
| 111 | +</hfoptions> |
| 112 | + |
| 113 | +创建一个包含子块类及其对应块名称列表的[`~modular_pipelines.AutoPipelineBlocks`]类。 |
| 114 | + |
| 115 | +你还需要包括`block_trigger_inputs`,一个触发相应块的输入名称列表。如果在运行时提供了触发输入,则选择该块运行。使用`None`来指定如果未检测到触发输入时运行的默认块。 |
| 116 | + |
| 117 | +最后,重要的是包括一个`description`,清楚地解释哪些输入触发哪些工作流。这有助于用户理解如何运行特定的工作流。 |
| 118 | + |
| 119 | +```py |
| 120 | +from diffusers.modular_pipelines import AutoPipelineBlocks |
| 121 | + |
| 122 | +class AutoImageBlocks(AutoPipelineBlocks): |
| 123 | + # 选择子块类的列表 |
| 124 | + block_classes = [block_inpaint_cls, block_i2i_cls, block_t2i_cls] |
| 125 | + # 每个块的名称,顺序相同 |
| 126 | + block_names = ["inpaint", "img2img", "text2img"] |
| 127 | + # 决定运行哪个块的触发输入 |
| 128 | + # - "mask" 触发修复工作流 |
| 129 | + # - "image" 触发img2img工作流(但仅在未提供mask时) |
| 130 | + # - 如果以上都没有,运行text2img工作流(默认) |
| 131 | + block_trigger_inputs = ["mask", "image", None] |
| 132 | + # 对于AutoPipelineBlocks来说,描述极其重要 |
| 133 | + |
| 134 | + def description(self): |
| 135 | + return ( |
| 136 | + "Pipeline generates images given different types of conditions!\n" |
| 137 | + + "This is an auto pipeline block that works for text2img, img2img and inpainting tasks.\n" |
| 138 | + + " - inpaint workflow is run when `mask` is provided.\n" |
| 139 | + + " - img2img workflow is run when `image` is provided (but only when `mask` is not provided).\n" |
| 140 | + + " - text2img workflow is run when neither `image` nor `mask` is provided.\n" |
| 141 | + ) |
| 142 | +``` |
| 143 | + |
| 144 | +包含`description`以避免任何关于如何运行块和需要什么输入的混淆**非常**重要。虽然[`~modular_pipelines.AutoPipelineBlocks`]很方便,但如果它没有正确解释,其条件逻辑可能难以理解。 |
| 145 | + |
| 146 | +创建`AutoImageBlocks`的一个实例。 |
| 147 | + |
| 148 | +```py |
| 149 | +auto_blocks = AutoImageBlocks() |
| 150 | +``` |
| 151 | + |
| 152 | +对于更复杂的组合,例如在更大的管道中作为子块使用的嵌套[`~modular_pipelines.AutoPipelineBlocks`]块,使用[`~modular_pipelines.SequentialPipelineBlocks.get_execution_blocks`]方法根据你的输入提取实际运行的块。 |
| 153 | + |
| 154 | +```py |
| 155 | +auto_blocks.get_execution_blocks("mask") |
| 156 | +``` |
0 commit comments