pipelines.rst 8.84 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
.. 
    Copyright 2020 The HuggingFace Team. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
    specific language governing permissions and limitations under the License.

Lysandre Debut's avatar
Lysandre Debut committed
13
Pipelines
Sylvain Gugger's avatar
Sylvain Gugger committed
14
-----------------------------------------------------------------------------------------------------------------------
Lysandre Debut's avatar
Lysandre Debut committed
15

Sylvain Gugger's avatar
Sylvain Gugger committed
16
17
The pipelines are a great and easy way to use models for inference. These pipelines are objects that abstract most of
the complex code from the library, offering a simple API dedicated to several tasks, including Named Entity
Sylvain Gugger's avatar
Sylvain Gugger committed
18
19
Recognition, Masked Language Modeling, Sentiment Analysis, Feature Extraction and Question Answering. See the
:doc:`task summary <../task_summary>` for examples of use.
Lysandre Debut's avatar
Lysandre Debut committed
20
21
22

There are two categories of pipeline abstractions to be aware about:

Sylvain Gugger's avatar
Sylvain Gugger committed
23
24
25
- The :func:`~transformers.pipeline` which is the most powerful object encapsulating all other pipelines.
- The other task-specific pipelines:

26
    - :class:`~transformers.AudioClassificationPipeline`
27
    - :class:`~transformers.AutomaticSpeechRecognitionPipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
28
29
30
    - :class:`~transformers.ConversationalPipeline`
    - :class:`~transformers.FeatureExtractionPipeline`
    - :class:`~transformers.FillMaskPipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
31
    - :class:`~transformers.ImageClassificationPipeline`
32
    - :class:`~transformers.ImageSegmentationPipeline`
33
    - :class:`~transformers.ObjectDetectionPipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
34
35
    - :class:`~transformers.QuestionAnsweringPipeline`
    - :class:`~transformers.SummarizationPipeline`
36
    - :class:`~transformers.TableQuestionAnsweringPipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
37
38
    - :class:`~transformers.TextClassificationPipeline`
    - :class:`~transformers.TextGenerationPipeline`
39
    - :class:`~transformers.Text2TextGenerationPipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
40
41
    - :class:`~transformers.TokenClassificationPipeline`
    - :class:`~transformers.TranslationPipeline`
42
    - :class:`~transformers.ZeroShotClassificationPipeline`
Lysandre Debut's avatar
Lysandre Debut committed
43
44

The pipeline abstraction
Sylvain Gugger's avatar
Sylvain Gugger committed
45
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Lysandre Debut's avatar
Lysandre Debut committed
46

Sylvain Gugger's avatar
Sylvain Gugger committed
47
48
The `pipeline` abstraction is a wrapper around all the other available pipelines. It is instantiated as any other
pipeline but requires an additional argument which is the `task`.
Lysandre Debut's avatar
Lysandre Debut committed
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Simple call on one item:

.. code-block::

    >>> pipe = pipeline("text-classification")
    >>> pipe("This restaurant is awesome")
    [{'label': 'POSITIVE', 'score': 0.9998743534088135}]

To call a pipeline on many items, you can either call with a `list`.

.. code-block::

    >>> pipe = pipeline("text-classification")
    >>> pipe(["This restaurant is awesome", "This restaurant is aweful"])
    [{'label': 'POSITIVE', 'score': 0.9998743534088135},
     {'label': 'NEGATIVE', 'score': 0.9996669292449951}]


To iterate of full datasets it is recommended to use a :obj:`dataset` directly. This means you don't need to allocate
the whole dataset at once, nor do you need to do batching yourself. This should work just as fast as custom loops on
GPU. If it doesn't don't hesitate to create an issue.

.. code-block::

    pipe = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h", device=0)
    dataset = datasets.load_dataset("superb", name="asr", split="test")

    # KeyDataset (only `pt`) will simply return the item in the dict returned by the dataset item
    # as we're not interested in the `target` part of the dataset.
    for out in tqdm.tqdm(pipe(KeyDataset(dataset, "file"))):
        print(out)
        # {"text": "NUMBER TEN FRESH NELLY IS WAITING ON YOU GOOD NIGHT HUSBAND"}
        # {"text": ....}
        # ....


Sylvain Gugger's avatar
Sylvain Gugger committed
86
.. autofunction:: transformers.pipeline
Lysandre Debut's avatar
Lysandre Debut committed
87

88
89
90
91
Implementing a pipeline
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:doc:`Implementing a new pipeline <../add_new_pipeline>`
Lysandre Debut's avatar
Lysandre Debut committed
92
93

The task specific pipelines
Sylvain Gugger's avatar
Sylvain Gugger committed
94
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Lysandre Debut's avatar
Lysandre Debut committed
95

96

97
98
99
100
101
102
103
AudioClassificationPipeline
=======================================================================================================================

.. autoclass:: transformers.AudioClassificationPipeline
    :special-members: __call__
    :members:

104
105
106
107
108
109
110
AutomaticSpeechRecognitionPipeline
=======================================================================================================================

.. autoclass:: transformers.AutomaticSpeechRecognitionPipeline
    :special-members: __call__
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
111
ConversationalPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
112
=======================================================================================================================
Lysandre Debut's avatar
Lysandre Debut committed
113

Sylvain Gugger's avatar
Sylvain Gugger committed
114
.. autoclass:: transformers.Conversation
Lysandre Debut's avatar
Lysandre Debut committed
115

Sylvain Gugger's avatar
Sylvain Gugger committed
116
117
118
119
120
.. autoclass:: transformers.ConversationalPipeline
    :special-members: __call__
    :members:

FeatureExtractionPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
121
=======================================================================================================================
Lysandre Debut's avatar
Lysandre Debut committed
122

Sylvain Gugger's avatar
Sylvain Gugger committed
123
124
125
.. autoclass:: transformers.FeatureExtractionPipeline
    :special-members: __call__
    :members:
Lysandre Debut's avatar
Lysandre Debut committed
126
127

FillMaskPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
128
=======================================================================================================================
Lysandre Debut's avatar
Lysandre Debut committed
129
130

.. autoclass:: transformers.FillMaskPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
131
132
    :special-members: __call__
    :members:
Lysandre Debut's avatar
Lysandre Debut committed
133

134
135
136
137
138
139
140
ImageClassificationPipeline
=======================================================================================================================

.. autoclass:: transformers.ImageClassificationPipeline
    :special-members: __call__
    :members:

141
142
143
144
145
146
147
ImageSegmentationPipeline
=======================================================================================================================

.. autoclass:: transformers.ImageSegmentationPipeline
    :special-members: __call__
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
148
NerPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
149
=======================================================================================================================
Lysandre Debut's avatar
Lysandre Debut committed
150

151
152
153
.. autoclass:: transformers.NerPipeline

See :class:`~transformers.TokenClassificationPipeline` for all details.
Lysandre Debut's avatar
Lysandre Debut committed
154

155
156
157
158
159
160
161
ObjectDetectionPipeline
=======================================================================================================================

.. autoclass:: transformers.ObjectDetectionPipeline
    :special-members: __call__
    :members:

Lysandre Debut's avatar
Lysandre Debut committed
162
QuestionAnsweringPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
163
=======================================================================================================================
Lysandre Debut's avatar
Lysandre Debut committed
164
165

.. autoclass:: transformers.QuestionAnsweringPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
166
167
    :special-members: __call__
    :members:
168
169

SummarizationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
170
=======================================================================================================================
171
172

.. autoclass:: transformers.SummarizationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
173
174
    :special-members: __call__
    :members:
175

176
177
178
179
180
181
182
TableQuestionAnsweringPipeline
=======================================================================================================================

.. autoclass:: transformers.TableQuestionAnsweringPipeline
    :special-members: __call__


Sylvain Gugger's avatar
Sylvain Gugger committed
183
TextClassificationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
184
=======================================================================================================================
Sylvain Gugger's avatar
Sylvain Gugger committed
185
186
187
188

.. autoclass:: transformers.TextClassificationPipeline
    :special-members: __call__
    :members:
189
190

TextGenerationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
191
=======================================================================================================================
192
193

.. autoclass:: transformers.TextGenerationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
194
195
    :special-members: __call__
    :members:
196

197
Text2TextGenerationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
198
=======================================================================================================================
199
200
201
202
203

.. autoclass:: transformers.Text2TextGenerationPipeline
    :special-members: __call__
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
204
TokenClassificationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
205
=======================================================================================================================
206

Sylvain Gugger's avatar
Sylvain Gugger committed
207
208
209
210
.. autoclass:: transformers.TokenClassificationPipeline
    :special-members: __call__
    :members:

211
212
213
214
215
216
217
TranslationPipeline
=======================================================================================================================

.. autoclass:: transformers.TranslationPipeline
    :special-members: __call__
    :members:

218
ZeroShotClassificationPipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
219
=======================================================================================================================
220
221
222
223
224

.. autoclass:: transformers.ZeroShotClassificationPipeline
    :special-members: __call__
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
225
Parent class: :obj:`Pipeline`
Sylvain Gugger's avatar
Sylvain Gugger committed
226
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sylvain Gugger's avatar
Sylvain Gugger committed
227
228
229

.. autoclass:: transformers.Pipeline
    :members: