Classifying a transcription using Hugging Face transformers

Now, let’s use Hugging Face transformers to classify the output text from the previous customer call audio transcription and perform sentiment analysis to label it.

The following code snippet utilizes Hugging Face’s transformers library to perform sentiment analysis on a given text. It begins by importing the necessary module, and then it loads a pre-trained sentiment analysis pipeline from Hugging Face’s transformers. The code defines an example text that expresses dissatisfaction with a product not yet received. Subsequently, the sentiment analysis pipeline is applied to classify the sentiment of the text, and the result is displayed by printing it to the console. The sentiment analysis model outputs a label indicating the sentiment, such as positive or negative, along with a confidence score.

Let’s import the Python library:
from transformers import pipeline
 Load the sentiment analysis pipeline
sentiment_classifier = pipeline(‘sentiment-analysis’)
 text for sentiment analysis
text=”Hello, I have not received the product yet.
I am very disappointed.are you going to replace if my product is damaged or missed.I will be happy if you replace with new product incase i missed the product die to incorrect shipping address”
 Perform sentiment analysis
result = sentiment_classifier(text)
 Display the result
print(result)

Here is the output:
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
[{‘label’: ‘NEGATIVE’, ‘score’: 0.9992625117301941}]

Hands-on – labeling audio data using a CNN

In this section, we will see how to train the CNN network on audio data and use it to label the audio data.

The following code demonstrates the process of labeling audio data using a CNN. The code outlines how to employ a CNN to label audio data, specifically training the model on a dataset of cat and dog audio samples. The goal is to classify new, unseen audio data as either a cat or a dog. Let’s take the cat and dog sample audio data and train the CNN model. Then, we will send new unseen data to the model to predict whether it is a cat or a dog:

  1. Load and pre-process the data: The audio data for cats and dogs is loaded from the specified folder structure using the load_and_preprocess_data function. The load_and_preprocess_data function processes the audio data, converting it into mel spectrograms and resizing them for model compatibility.
  2. Split data into training and testing sets: The loaded and pre-processed data is split into training and testing sets using train_test_split, and labels are converted to one-hot encoding.
  3. Create a neural network model: A CNN model is created using TensorFlow and Keras, comprising convolutional layers, pooling layers, and fully connected layers.
  4. Compile the model: The model is compiled with an Adam optimizer, categorical cross-entropy loss, and accuracy as the evaluation metric.
  5. Train the model: The CNN model is trained on the training data for a specified number of epochs.
  6. Evaluate the accuracy of the model: The accuracy of the trained model is evaluated on the testing set.
  7. Save the trained model: The trained model is saved for future use.
  8. Test the new audio file: Finally, the saved model is loaded, and a new audio file (in this case, a cat meow) is processed and classified, with class probabilities and accuracy displayed for each class.

Leave a Reply

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