
How to create a custom pipeline?
In this guide, we will see how to create a custom pipeline and share it on the Hub or add it to the
🤗 Transformers library.
First and foremost, you need to decide the raw entries the pipeline will be able to take. It can be strings, raw bytes,
dictionaries or whatever seems to be the most likely desired input. Try to keep these inputs as pure Python as possible
as it makes compatibility easier (even through other languages via JSON). Those will be the inputs of the
pipeline (preprocess).
Then define the outputs. Same policy as the inputs. The simpler, the better. Those will be the outputs of
postprocess method.
Start by inheriting the base class Pipeline with the 4 methods needed to implement preprocess,
_forward, postprocess, and _sanitize_parameters.
thon
from transformers import Pipeline
class MyPipeline(Pipeline):
    def _sanitize_parameters(self, **kwargs):
        preprocess_kwargs = {}
        if "maybe_arg" in kwargs:
            preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"]
        return preprocess_kwargs, {}, {}
def preprocess(self, inputs, maybe_arg=2):
    model_input = Tensor(inputs["input_ids"])
    return {"model_input": model_input}