File size: 1,854 Bytes
e185079
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
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.