In this section, we will look at what Transformer models can do and use our first tool from the 🤗 Transformers library: the pipeline()
function.
Transformers are everywhere! Let’s look at a few examples of how they can be used to solve some interesting NLP problems.
The most basic object in the 🤗 Transformers library is the pipeline()
function. We can directly input any text into it and get an intelligible answer.
By default, this pipeline selects a particular pretrained model.
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
classifier(["Nice, I've been waiting for short HuggingFace course my whole life!", "I hate this so much"])
[{'label': 'POSITIVE', 'score': 0.9984038472175598},
{'label': 'NEGATIVE', 'score': 0.9995144605636597}]
There are three main steps involved when you pass some text to a pipeline()
:
- Text preprocessing,
- Model prediction,
- Output post-processing.
Some of the currently available pipelines are:
- feature-extraction
- fill-mask
- ner (named entity recognition)
- question-answering
- sentiment-analysis
- summarization
- text-generation
- translation
- zero-shot-classification
The zero-shot-classification
pipeline is very powerful for tasks where we need to classify texts that haven’t been labelled. It returns probability scores for any list of labels you want!
It's called zero-shot because you don’t need to fine-tune the model on your data to use it.
from transformers import pipeline
classifier = pipeline("zero-shot-classification")
classifier("This is a short course about the Transformers library",
candidate_labels=["education", "politics", "business"],)
{'sequence': 'This is a short course about the Transformers library',
'labels': ['education', 'business', 'politics'],
'scores': [0.7481650114059448, 0.17828474938869476, 0.07355023920536041]}
candidate_labels
The main idea here is that you provide a prompt and the model will auto-complete it by generating the remaining text.
from transformers import pipeline
generator = pipeline("text-generation")
generator("In this course, we will teach you how to")
[{'generated_text': 'In this course, we will teach you how to create the appropriate and easy-to-use, user-friendly website, without having to go far to change your password. We will also explain and explain the principles and the tools that developers use for'}]
max_length
: total length of the output text.num_return_sequences
: number of returning sequences.
The previous examples used the default model for the task at hand, but you can also choose a particular model from the Hub to use in a pipeline for a specific task — say, text generation.
Let’s try the distilgpt2 model!
from transformers import pipeline
generator = pipeline("text-generation", model="distilgpt2")
generator( "In this course, we will teach you how to",
max_length=30,
num_return_sequences=2,
)
[{'generated_text': 'In this course, we will teach you how to use your hands to manage your time of day by giving feedback and tips and teaching each day.\n'},
{'generated_text': 'In this course, we will teach you how to use the Java Virtual Machine for creating your Java application. We’ll teach you how to use'}]
The idea of this task is to fill in the blanks in a given text:
from transformers import pipeline
unmasker = pipeline("fill-mask")
unmasker("This course will teach you all about <mask> models.", top_k=2)
[{'score': 0.1961979866027832,
'token': 30412,
'token_str': ' mathematical',
'sequence': 'This course will teach you all about mathematical models.'},
{'score': 0.04052741825580597,
'token': 38163,
'token_str': ' computational',
'sequence': 'This course will teach you all about computational models.'}]
top_k argument
: controls how many possibilities you want to be displayed.<mask>
: mask token or special word the model must fills in. It depends on the used model.
NER is a task where the model has to find which parts of the input text correspond to entities such as persons, locations, or organizations.
from transformers import pipeline
ner = pipeline("ner", grouped_entities=True)
ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
[{'entity_group': 'PER',
'score': 0.9981694,
'word': 'Sylvain',
'start': 11,
'end': 18},
{'entity_group': 'ORG',
'score': 0.9796019,
'word': 'Hugging Face',
'start': 33,
'end': 45},
{'entity_group': 'LOC',
'score': 0.9932106,
'word': 'Brooklyn',
'start': 49,
'end': 57}]
Here the model correctly identified that Sylvain is a person (PER), Hugging Face an organization (ORG), and Brooklyn a location (LOC).
grouped_entities=True
: regroup together the parts of the sentence that correspond to the same entity (grouping “Hugging” and “Face” as a single organization)
The question-answering pipeline answers questions using information from a given context.
from transformers import pipeline
question_answerer = pipeline("question-answering")
question_answerer(
question="Where do I work?",
context="My name is Sylvain and I work at Hugging Face in Brooklyn",
)
{'score': 0.694976270198822, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}
Summarization is the task of reducing a text into a shorter text while keeping all (or most) of the important aspects referenced in the text.
from transformers import pipeline
summarizer = pipeline("summarization")
summarizer(
"""
America has changed dramatically during recent years. Not only has the number
of graduates in traditional engineering disciplines such as mechanical, civil,
electrical, chemical, and aeronautical engineering declined, but in most of
the premier American universities engineering curricula now concentrate on
and encourage largely the study of engineering science. As a result, there
are declining offerings in engineering subjects dealing with infrastructure,
the environment, and related issues, and greater concentration on high
technology subjects, largely supporting increasingly complex scientific
developments. While the latter is important, it should not be at the expense
of more traditional engineering.
Rapidly developing economies such as China and India, as well as other
industrial countries in Europe and Asia, continue to encourage and advance
the teaching of engineering. Both China and India, respectively, graduate
six and eight times as many traditional engineers as does the United States.
Other industrial countries at minimum maintain their output, while America
suffers an increasingly serious decline in the number of engineering
graduates and a lack of well-educated engineers.
"""
)
[{'summary_text': ' China and India graduate six and eight times as many traditional engineers as the U.S. does . Rapidly developing economies such as India and Europe continue to encourage and advance the teaching of engineering . America suffers an increasingly serious decline in the number of engineering graduates and a lack of well-educated engineers .'}]
Same as text generation:
max_length
min_length
For translation, you can use a default model if you provide a language pair in the task name (such as "translation_en_to_fr
"), but the easiest way is to pick the model you want to use on the Model Hub.
Translating from French to English:
from transformers import pipeline
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
translator("Ce cours est produit par Hugging Face.")
[{'translation_text': 'This course is produced by Hugging Face.'}]
Same as text generation & summarization:
max_length
min_length
The pipelines shown so far are mostly for demonstrative purposes. They were programmed for specific tasks and cannot perform variations of them. In the next chapter, you’ll learn what’s inside a pipeline()
function and how to customize its behavior.