|
|
--- |
|
|
license: apache-2.0 |
|
|
--- |
|
|
# Custom Block Template |
|
|
|
|
|
A template for creating custom ModularPipelineBlocks. Use this as a starting point for building your own blocks. |
|
|
|
|
|
## Getting Started |
|
|
|
|
|
### 1. Download the template |
|
|
```python |
|
|
from diffusers import ModularPipelineBlocks |
|
|
|
|
|
model_id = "diffusers/custom-block-template" |
|
|
local_dir = model_id.split("/")[-1] |
|
|
|
|
|
blocks = ModularPipelineBlocks.from_pretrained( |
|
|
model_id, |
|
|
trust_remote_code=True, |
|
|
local_dir=local_dir |
|
|
) |
|
|
``` |
|
|
|
|
|
This saves the template files to `custom-block-template/` locally. Feel free to use a custom `local_dir` |
|
|
|
|
|
### 2. Edit locally |
|
|
|
|
|
Open `block.py` and implement your custom block: |
|
|
- Update the class name and `description` |
|
|
- Define `expected_components` if your block needs models |
|
|
- Define `inputs` with your input parameters |
|
|
- Define `intermediate_outputs` with your output parameters |
|
|
- Implement your logic in `__call__` |
|
|
|
|
|
|
|
|
### 3. Test your block |
|
|
```python |
|
|
from diffusers import ModularPipelineBlocks |
|
|
|
|
|
blocks = ModularPipelineBlocks.from_pretrained(local_dir, trust_remote_code=True) |
|
|
pipeline = blocks.init_pipeline() |
|
|
output = pipeline(...) # your inputs here |
|
|
``` |
|
|
|
|
|
### 4. Upload to the Hub |
|
|
```python |
|
|
pipeline.save_pretrained(local_dir, repo_id="your-username/your-block-name", push_to_hub=True) |
|
|
``` |
|
|
|
|
|
## Adding Mellon Support |
|
|
|
|
|
To use your block in [Mellon](https://github.com/cubiq/Mellon), add `metadata={"mellon": "<type>"}` to your InputParam/OutputParam definitions, then generate the config: |
|
|
```python |
|
|
from diffusers.modular_pipelines.mellon_node_utils import MellonPipelineConfig |
|
|
|
|
|
mellon_config = MellonPipelineConfig.from_custom_block(blocks) |
|
|
mellon_config.save(local_dir, repo_id="your-username/your-block-name", push_to_hub=True) |
|
|
``` |
|
|
|
|
|
See the [Mellon integration guide](https://huggingface.co/docs/diffusers/main/en/modular_pipelines/mellon_custom_blocks) for more details. |