Sentiment and Emotion Analysis API

What is Sentiment Analysis?

Sentiment analysis is the process of extracting a general sentiment from a block of text. Basically it's about determining whether the text is positive or negative.

For example, let's imagine our program finds the following Twit:

Look what's just come on the market in #ValThorens! A recently renovated, charming 6 bed duplex apartment in the heart of the resort with superb views!

This is a commercial Twit that clearly shows a positive sentiment.

The Natural Language Processing model in charge of sentiment analysis would return the main sentiment and its likelihood. Here we would get a positive sentiment with a high likelihood.

What is Emotion Analysis?

Emotion analysis is about detecting one or several emotions from a block of text: sadness, joy, love, anger, fear, surprise...

The Natural Language Processing model in charge of emotion analysis would return each emotion together with its likelihood.

Sentiment analysis

Why Use Sentiment/Emotion Analysis?

Sentiment and emotion analyses can be interesting in many situations. Let's give you a couple of examples.

Social Network Analysis

Imagine you're working in a marketing department that is regularly posting new content on social networks. You might want to automatically monitor the user reactions in order to quickly intervene in case of negative feedback.


Some support requests might be more urgent than others, depending on how angry users are. Detecting the user's sentiment automatically can help support address critical tickets more quickly.

Public Relations

Gauging the sentiment of a couple of persons on the internet is easy, but understanding the global sentiment of thousands of persons is another thing. Automated sentiment analysis is the key solution here.

Product Launch

Right after launching a new product, it can be critical to react quickly in case of poor reception by customers, bloggers, journalists... Sentiment analysis can help in such situations.

Sentiment and Emotion Analyses with Hugging Face Transformers.

Hugging Face transformers is an amazing library that has been recently released. It is based on either PyTorch or TensorFlow, depending on the model you're using. Transformers have clearly helped deep learning Natural Language Processing make great progress in terms of accuracy. However this accuracy improvement comes at a cost: transformers are extremely demanding in terms of resources.

Hugging Face is a central repository regrouping all the newest open-source Natural Language Processing transformer-based models. This model are perfect for sentiment analysis: DistilBERT Base Uncased Finetuned SST-2.
The following is for emotion analysis: Distilbert Base Uncased Emotion
There is also a model dedicated to financial sentiment analysis: Prosus AI's Finbert

Sentiment/Emotion Analysis Inference API

Building an inference API for sentiment and emotion analyses is a necessary step as soon a you want to use sentiment/emotion analysis in production. But keep in mind that building such an API is not necessarily easy. First because you need to code the API (easy part) but also because you need to build a highly available, fast, and scalable infrastructure to serve your models behind the hood (hardest part). Machine learning models consume a lot of resources (memory, disk space, CPU, GPU...) which makes it hard to achieve high-availability and low latency at the same time.

Leveraging such an API is very interesting because it is completely decoupled from the rest of your stack (microservice architecture), so you can easily scale it independently and ensure high-availability of your models through redundancy. But an API is also the way to go in terms of language interoperability. Most machine learning frameworks are developed in Python, but it's likely that you want to access them from other languages like Javascript, Go, Ruby... In such situation, an API is a great solution.

NLP Cloud's Sentiment/Emotion Analysis API

NLP Cloud proposes a sentiment analysis API that gives you the opportunity to perform sentiment and emotion analyses out of the box, based on one of these 7 Hugging Face transformers models, with excellent performances:
- DistilBERT Base Uncased Finetuned SST-2
- Distilbert Base Uncased Emotion
- Prosus AI's Finbert
The response time (latency) is very good for these models. You can either use the pre-trained models, train your own model, or upload your own custom models!

For more details, see our documentation about sentiment and emotion analyses here.

Testing sentiment/emotion analysis locally is one thing, but using it reliably in production is another thing. With NLP Cloud you can just do both!