Pipeline#
transformers.Pipeline
provides the most straightforward way to use models available in Hugging Face, combining all procedures into a single class. Learn more on the dedicated documentation page.
This page considers features of using this tool.
import transformers
from math import exp
Pipeline task#
The behavior of the pipeline strongly depends on its task
. Each task returns a different object. For a full list of tasks and links to the corresponding objects, check this link.
You can specify different options for different tasks, both when you’re creating the pipeline and when you’re working on it.
The following cell shows an example of using task="text-classification"
. For this, you can specify function_to_apply
, which is exclusive to text-classification
. In the example, 'none'
is specified, indicating that the result shouldn’t be transformed.
ans = transformers.pipeline(
task="text-classification",
function_to_apply="none",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)("wow")[0]
ans
Device set to use cpu
{'label': 'POSITIVE', 'score': 4.055374622344971}
The following cell applies a sigmoid function to the result of the previous cell:
1 / (1 + exp(-ans["score"]))
0.9829661915068255
The same result using pipeline with function_to_apply="sigmoid"
on the same phrase:
ans = transformers.pipeline(
task="text-classification",
function_to_apply="sigmoid",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)("wow")[0]
ans
Device set to use cpu
{'label': 'POSITIVE', 'score': 0.9829661846160889}
Consider another example: using the question-answering
pipeline. For it, the argument function_to_apply
doesn’t make sense at all. Moreover, you need to pass data to it in a different manner.
The following cell defines the pipeline and shows errors if you don’t pass data to it in the right way.
qa_pipeline = transformers.pipeline(
task="question-answering",
model="distilbert-base-cased-distilled-squad",
)
try:
qa_pipeline("wow")
except Exception as e:
print(e)
Device set to use cpu
w argument needs to be of type (SquadExample, dict)
/home/fedor/Documents/knowledge/venv/lib/python3.12/site-packages/transformers/pipelines/question_answering.py:391: FutureWarning: Passing a list of SQuAD examples to the pipeline is deprecated and will be removed in v5. Inputs should be passed using the `question` and `context` keyword arguments instead.
warnings.warn(
This type of pipeline needs question
and context
arguments to work correctly, the following cell shows the case of using it.
qa_pipeline(
question="How tall is John",
context=(
"John was a really tall men. "
"His height was around 2m. "
"But it was nothing special for him."
)
)
{'score': 0.6897236108779907, 'start': 50, 'end': 52, 'answer': '2m'}