To run or modify the example code yourself, you can open and download the Jupyter notebook underlying this page via the sidebar.

No problem! Explanations for the individual steps of the code are hidden behind the numbers on the right. Simply hover your mouse over the number to see the explanation. Where the respective code is executed (in Python, R, or the terminal) is shown in the header of the code block.

Python
print("Hello! 👋")
1
This is a simple Python command that prints the text “Hello” to the console. The output appears below the code block.
Hello! 👋

Load data

Regardless of the model used, we first load the already-installed packages as well as our text data, and store it in a list object:

Python
import pandas as pd
from transformers import pipeline

data = pd.read_csv("data/example_dataset.csv")
textlist = list(data["text"])
1
Importing the required libraries
2
Loading the text data from a CSV file and storing it in a list

Load model

Here we use a category-specific encoder model for emotion classification, fine-tuned on the GoEmotions dataset (Demszky et al., 2020). This model is freely available on Hugging Face and can be loaded directly via the pipeline() function. As needed, further parameters can be specified in this function, which can be found in the Hugging Face documentation (e.g., long texts may need to be shortened, depending on the model’s maximum input length; truncation=True). Also, please note that running the pipeline() command can sometimes take a while, especially the first time, since the LLM needs to be downloaded and loaded.

TipGood to know

Instead of the model used here, you can also use other category-specific encoder models (usually labeled for “Text Classification”) for different languages and use cases. An overview of available models can be found on Hugging Face. Make sure that the chosen model is suitable for your specific task (e.g. sentiment analysis, topic classification, etc.) and supports the language of your texts.

Since this is a category-specific model, it was trained once on one fixed set of labels — here, the 28 GoEmotions categories. Conveniently, fear is already one of those 28 categories. We request scores for all labels (top_k=None) so we can pick out the fear entry for each text.

Python
emotion_classifier = pipeline("text-classification",
                               model="SamLowe/roberta-base-go_emotions",
                               top_k=None,
                               truncation=True)
3
Initializing the pipeline for text classification
4
Specifying the model, here a category-specific encoder model fine-tuned on GoEmotions
5
Requesting scores for all 28 labels instead of only the single most likely one, so we can look up fear specifically
6
Enabling truncation of long texts

Perform classification

Now we apply the model to our list of texts and pick out each text’s fear score.

Python
result = emotion_classifier(textlist)
fear_scores = [next(item["score"] for item in scores if item["label"] == "fear")
               for scores in result]

result_df = pd.DataFrame({
    "text": data["text"],
    "fear_score": fear_scores,
})

print(result_df)
7
Applying the emotion classifier to the list of texts; for each text, this returns a score for every one of the 28 GoEmotions labels
8
Picking out just the fear score for each text from that list of 28 scores
9
Assembling the original texts and their fear scores into a DataFrame for a clear overview
10
Printing the result DataFrame
                                                text  fear_score
0       I feel helpless and afraid like never before    0.860037
1  No commute; sunny day; my kids laughing; being...    0.000854
2  Feeling scared and lonely, can't wait for this...    0.861225
3  It's gonna get tougher but we will get through...    0.008892
4  I am terrified by what is happening. I’m scare...    0.903767
5  Stay inside, stay safe, and this madness will ...    0.005280
6  I am scared that I will get sick and die, and ...    0.896429
7  Feeling relaxed and happy but I understand tha...    0.002808
8  I am very scared and worried about contracting...    0.804676
9     I'm confident we will get through this crisis.    0.007578
Source: category-specific-encoder.ipynb

References

Demszky, D., Movshovitz-Attias, D., Ko, J., Cowen, A., Nemade, G., & Ravi, S. (2020, June 2). GoEmotions: A dataset of fine-grained emotions. https://doi.org/10.48550/arXiv.2005.00547