Image of juicy_fish on Freepik
Hugging Face hosts a variety of transformer-based language models (LMs) specialized in addressing language comprehension and generation tasks, including but not limited to:
- Classification of texts
- Named Entity Recognition (NER)
- Text generation
- Questions and Answers
- Summary
- Translation
A particular – and quite common – case of a text classification task is sentiment analysis, where the goal is to identify the sentiment of a given text. The “simplest” type of sentiment analysis: LMs are trained to determine the polarity of an input text, such as a customer review about a product, into positive vs. negative, or positive vs. negative vs. neutral. These two specific problems are formulated as binary or multi-class classification tasks, respectively.
There are also LMs that, while still identifiable as sentiment analysis models, are trained to categorize texts into various emotions such as anger, happiness, sadness, etc.
This Python-based tutorial focuses on loading and illustrating the use of a pre-trained Hugging Face model to classify the main emotion associated with an input text. We will use the emotion dataset Publicly available at the Hugging Face Hub. This dataset contains thousands of twitter messages written in English.
Loading the dataset
We will start by loading the training data into the emotions dataset by running the following instructions:
!pip install datasets
from datasets import load_dataset
all_data = load_dataset("jeffnyman/emotions")
train_data = all_data("train")
Below is a summary of what the training subset includes in the train data The variable contains:
Dataset({
features: ('text', 'label'),
num_rows: 16000
})
The emotion training dataset contains 16,000 instances associated with twitter messages. For each instance, there are two features: an input feature containing the actual message text and an output feature or label containing its associated emotion as a numeric identifier:
- 0: sadness
- 1: joy
- 2: love
- 3: anger
- 4: fear
- 5: surprise
For example, the first labeled instance in the training fold has been classified with the emotion “sadness”:
Production:
{'text': 'i didnt feel humiliated', 'label': 0}
Loading the language model
Once we have loaded the data, the next step is to load a suitable pre-trained LM of Hugging Face for our target emotion detection task. There are two main approaches to loading and using LM using Hugging Face Transformers Library:
- Pipes They offer a very high level of abstraction to prepare to load an LM and perform inferences on them almost instantly with very few lines of code, at the cost of having little configurability.
- Car classes Provide a lower level of abstraction, which requires more coding skills but offers more flexibility to tune model parameters and customize text preprocessing steps like tokenization.
This tutorial gives you a simple start, focusing on loading models as pipelines. Pipelines require specifying at least the language task type and optionally a model name to load. Since emotion detection is a very specific form of text classification problem, the task argument to use when loading the model should be “text-classification”:
from transformers import pipeline
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
On the other hand, it is highly recommended to specify with the 'model' argument the name of a specific model in Hugging Face Hub capable of addressing our specific emotion detection task. Otherwise, by default, we can load a text classification model that has not been trained on data for this particular 6-class classification problem.
You might be wondering, “How do I know which model name to use?” The answer is simple: explore the Hugging Face website a bit to find suitable models or models trained on a specific dataset, such as emotion data.
The next step is to start making predictions. Pipelines make this inference process incredibly easy, but it suffices to call our newly instantiated pipeline variable and pass an input text to classify as an argument:
example_tweet = "I love hugging face transformers!"
prediction = classifier(example_tweet)
print(prediction)
As a result, we obtain a predicted label and a confidence score: the closer this score is to 1, the more “confident” the prediction made is.
({'label': 'joy', 'score': 0.9825918674468994})
So our example entry “I love hugging Transformers!” certainly conveys a feeling of joy.
You can pass multiple input texts to the pipeline to perform multiple predictions simultaneously, as follows:
example_tweets = ("I love hugging face transformers!", "I really like coffee but it's too bitter...")
prediction = classifier(example_tweets)
print(prediction)
The second input in this example seemed much more challenging for the model to perform a reliable classification on:
({'label': 'joy', 'score': 0.9825918674468994}, {'label': 'sadness', 'score': 0.38266682624816895})
Finally, we can also pass a batch of instances from a dataset such as the “emotions” data we loaded earlier. This example passes the first 10 training inputs to our LM sequence to classify their sentiments and then prints a list containing each predicted label, leaving out their confidence scores:
train_batch = train_data(:10)("text")
predictions = classifier(train_batch)
labels = (x('label') for x in predictions)
print(labels)
Production:
('sadness', 'sadness', 'anger', 'joy', 'anger', 'sadness', 'surprise', 'fear', 'joy', 'joy')
For comparison, here are the original labels given to these 10 training instances:
print(train_data(:10)("label"))
Production:
(0, 0, 3, 2, 3, 0, 5, 4, 1, 2)
Looking at the emotions that each numerical identifier is associated with, we can see that roughly 7 out of 10 predictions match the actual labels given to those 10 instances.
Now that you know how to use Hugging Face Transformer models to detect emotions from text, why not explore other use cases and language tasks where pre-trained LMs can help?
Ivan Palomares Carrascosa is a leader, writer, speaker, and advisor on ai, machine learning, deep learning, and law. He trains and guides others to leverage ai in the real world.