Emotion detection from strings in Python

Emotion detection from strings in Python

1. Install and Import Libraries

Following installs the transformers library from Hugging Face, imports the pipeline function from the transformers library, imports the login function from the Hugging Face Hub, imports the pandas library for data manipulation, imports the seaborn library for data visualization.

pip install transformers -q
from transformers import pipeline
from huggingface_hub import login
import pandas as pd
import seaborn as sns

2. Authenticate with Hugging Face Hub

Logs into the Hugging Face Hub using the provided token.

login(token="your_token_here")

3. Load the Sentiment Analysis Model

Loads the sentiment analysis pipeline using the ‘EmoRoBERTa’ model for emotion detection.

emotion = pipeline('sentiment-analysis', model='arpanghoshal/EmoRoBERTa')

4. Test the Model with a Sample Text

Uses the emotion detection pipeline to analyze the given text and stores the result of the emotion analysis.

emotion_label = emotion("If your task is similar to the task the model of the checkpoint was trained on, you can use this")
emotion_label

5. Load and Preprocess the Data

Loads the dataset from the given URL into a pandas DataFrame, prints the shape (rows and columns) of the DataFrame, limits the DataFrame to the first 100 rows for easier processing, prints the new shape of the DataFrame.

large_text = pd.read_csv('https://github.com/abishekarun/Text-Emotion-Classification/blob/master/text_emotion.csv?raw=true')
large_text.shape

large_text = large_text[:100]
large_text.shape

6. Apply the Emotion Detection Model

The code snippet first applies the emotion detection model to the content of the first 10 rows of the ‘content’ column using large_text['content'][1:10].apply(emotion). Then, a function get_emotion_label(text) is defined to extract and return the emotion label from the model’s output, specifically return(emotion(text)[0]['label']). This function is tested with a sample text by calling get_emotion_label("your_text_here"). Subsequently, the get_emotion_label function is applied to the first 10 rows of the ‘content’ column using large_text['content'][1:10].apply(get_emotion_label), ensuring that the emotion labels are correctly extracted and applied.

large_text['content'][1:10].apply(emotion)

def get_emotion_label(text):
  return(emotion(text)[0]['label'])

get_emotion_label("If your task is similar to the task the model of the checkpoint was trained on, you can use this")
large_text['content'][1:10].apply(get_emotion_label)

7. Add Emotion Labels to DataFrame

The line large_text['emotion'] = large_text['content'].apply(get_emotion_label) applies the get_emotion_label function to every entry in the content column of the DataFrame, extracting the emotion label for each piece of text and storing the results in a new column called emotion. This step effectively enriches the DataFrame by adding a new dimension of information. Finally, displaying large_text       shows the updated DataFrame, now with an additional ’emotion’ column that contains the detected emotions for each text entry.

large_text['emotion'] = large_text['content'].apply(get_emotion_label)
large_text

8. Visualize the Emotion Distribution

Creates a count plot of the ’emotion’ column using seaborn, sets the title of the plot to “Emotion Distribution”.

sns.countplot(data = large_text, y = 'emotion').set_title("Emotion Distribution")

Summary

This code:

  1. Installs necessary libraries and imports required functions.
  2. Logs into the Hugging Face Hub using a token.
  3. Loads an emotion detection model.
  4. Tests the model with a sample text.
  5. Loads a dataset of text and limits it to the first 100 rows.
  6. Applies the emotion detection model to the dataset and extracts emotion labels.
  7. Adds these emotion labels as a new column in the DataFrame.
  8. Visualizes the distribution of emotions in the dataset using a seaborn count plot.

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top