DEV Community

Cover image for Sentiment Analysis with PubNub Functions and HuggingFace
PubNub Developer Relations for PubNub

Posted on

Sentiment Analysis with PubNub Functions and HuggingFace

With AI constantly on the rise in terms of the way you can use it within your own application or business, it is essential to cover how it can be used when creating real-time systems. Many use cases fall under this category, whether you're making an in-app chat, IoT, or live events solution; there is almost always a way AI can be applied to your solution to make it that much better.

This blog will teach you how to monitor your live chat for “positive” or “negative” behaviors using sentiment analysis. This application has a couple of use cases, such as monitoring a support chat for an inappropriate customer or, on the other hand, monitoring a live events chat to see if your customers are enjoying the event they are watching. In any case, many applications can be considered using AI, especially when creating real-time systems.

This is where PubNub fits in. PubNub provides a highly available, real-time communication platform. You can use it to build chat, IoT device control systems, geolocation apps, and many other communication systems. To learn more about what PubNub is capable of visit our docs or pricing page to get started.

What are PubNub Integrations?

First, for those unaware of PubNub Functions, PubNub Functions allow you to capture events happening within your PubNub instance. This enables you to write code to leverage existing integrations or transform, re-route, augment, filter, and even aggregate your data. So what exactly does that mean? Essentially, suppose you are sending a message, signal, file, etc. (data) across PubNub. In that case, you are able to write code either before it is received by the client or after to block the data being sent across, modify the data or even route the data to another third-party system.

PubNub Integrations is an extension of PubNub Functions that allows you to quickly set up a PubNub Function and integrate it with some third-party services such as AWS, Giphy, Google Cloud, etc. It is essentially pre-written code to connect your real-time system to any third-party systems in our Functions catalog. This catalog is found on the PubNub portal or the “Home” tab.

Refer to the image below and click “Discover & Integrate” to browse PubNub’s pre-made Functions.

Getting Started with HuggingFace

At this point, probably everyone has heard about OpenAI, GPT-4, Claude or any of the popular Large Language Models (LLMs). However, using these LLMs in a production environment can be expensive or nondeterministic regarding its results. I guess that is the downside of being good at everything; you could be better at performing one specific task. This is where HuggingFace can utilized. HuggingFace provides open-source AI and machine learning models that can easily be deployed on HuggingFace itself or third-party systems such as Amazon SageMaker or Azure ML.  You can interface with these deployments through an API and control the scaling of these models, which makes them perfectly suited for production environments. These models range in size but are generally small AI models that are good at doing one specific task. With capabilities to fine-tune these models, or use the pre-trained model for specific tasks, embedding them into various applications becomes more efficient, enhancing automation and performance. Combining these models can create new and intricate AI applications. In this case, by utilizing HuggingFace models, you wouldn’t have to depend on a production application on a third-party provider such as OpenAI or Google, ensuring a more targeted and customizable approach to deploying deep learning solutions in your operations.

Refer to the screenshot below to see how many different AI models HuggingFace provides.

What is Sentiment Analysis

Sentiment analysis (also referred to as opinion mining or emotion AI) is the use of natural language processing and text analysis to determine if the emotion or tone of the message is either positive, negative or neutral. Today, companies have huge volumes of text-based data such as emails, customer support messages, social media comments, reviews, etc. Sentiment analysis can scan these texts to determine the author's attitude towards a topic. Companies can then use this data to increase customer service or brand reputation. 

Sentiment Analysis Use Cases

Moderation: You can use sentiment analysis to monitor your Live Events, Support, or In-App Chat to see if any customers are misbehaving and quickly ban and delete their messages. This would allow for a healthy environment on your platform.

Data Analytics: Companies sometimes want to scan through thousands of emails or texts to see how customers respond to the event or company brand. They can use this data to target what they are doing right or wrong depending on their actions and make better decisions on what to do next.

Implementing Sentiment Analysis in a PubNub Function

Using the HuggingFace PubNub Integration, let’s implement Sentiment Analysis right into PubNub Functions. First, head to the PubNub portal and open the Function Catalog on the Home page. Once in the Functions Catalog, click on the “HuggingFace Serverless API.”

After selecting the PubNub integration, follow the steps by choosing the keyset and app for which you want to configure your PubNub Function. After you get to the variables step, we will need to grab your API key from HuggingFace. Head over to your profile settings within Huggingface and select Access Tokens, as we will need to add this to your PubNub Functions secrets. Then hit create, and we will officially integrate your PubNub instance with HuggingFace. It’s that easy. You should now see a PubNub Function with the following code sample.

//
// **                                             **
// ** Add your API Key to MY SECRETS (Left Panel) **
// **                                             **
// **             HUGGINGFACE_API_KEY             **
// **                                             **
//
const FunctionConfig = {
    "HUGGINGFACE_API_KEY": "HuggingFace"
};
//
// Import Modules
//
const http = require('xhr');
const vault = require('vault');
//
// Main
//
export default async (request) => {
    let message = request.message.text;
    console.log('Sentiment:');
    let model = 'distilbert-base-uncased-finetuned-sst-2-english';
    let response = await query(message, model);
    console.log(response);
    console.log('GPT2:');
    model = 'gpt2';
    response = await query(message, model);
    console.log(response);
    console.log('Google\'s Gemma:');
    model = 'google/gemma-7b-it';
    response = await query(`<start_of_turn>user\n${message}\n<start_of_turn>model`, model);
    console.log(response);
    console.log('Capture:');
    model = 'dbmdz/bert-large-cased-finetuned-conll03-english';
    response = await query(message, model);
    console.log(response);
    return request.ok()
};
//
// HuggingFace API Call
//
async function query(text, model='distilbert-base-uncased-finetuned-sst-2-english') {
    const apikey = await vault.get(FunctionConfig.HUGGINGFACE_API_KEY);
    const response = await http.fetch( `https://api-inference.huggingface.co/models/${model}`, {
        headers: { Authorization: `Bearer ${apikey}`, 'Content-Type': 'application/json' },
        method: "POST",
        body: JSON.stringify({inputs:text}),
    });
    return await response.json();
}
Enter fullscreen mode Exit fullscreen mode

We will focus on the first model used within this PubNub Function, which is “distilbert-base-uncased-finetuned-sst-2-english”. To learn more about this function, navigate to distilbert-base-uncased-finetuned-sst-2-english or the model it is based on, which is distilbert/distilbert-base-uncased. You can also check out the model on github. This model will do all the heavy lifting for us and determine the sentiment analysis of a PubNub message when sent through the network. 

I refactored my current function to look like this:

//
// **                                             **
// ** Add your API Key to MY SECRETS (Left Panel) **
// **                                             **
// **             HUGGINGFACE_API_KEY             **
// **                                             **
//
const FunctionConfig = {
    "HUGGINGFACE_API_KEY": "HuggingFace"
};
//
// Import Modules
//
const http = require('xhr');
const vault = require('vault');
//
// Main
//
export default async (request) => {
    let message = request.message.text;
    console.log('Sentiment:');
    let model = 'distilbert-base-uncased-finetuned-sst-2-english';
    let response = await query(message, model);
    // Get the negative score
    if(response[0][0].label == "NEGATIVE"){
        request.message.score = response[0][0].score;
    }
    else{
        request.message.score = response[0][1].score;
    }
    console.log(request.message.score);
    return request.ok()
};
//
// HuggingFace API Call
//
async function query(text, model='distilbert-base-uncased-finetuned-sst-2-english') {
    const apikey = await vault.get(FunctionConfig.HUGGINGFACE_API_KEY);
    const response = await http.fetch( `https://api-inference.huggingface.co/models/${model}`, {
        headers: { Authorization: `Bearer ${apikey}`, 'Content-Type': 'application/json' },
        method: "POST",
        body: JSON.stringify({inputs:text}),
    });
    return await response.json();
}
Enter fullscreen mode Exit fullscreen mode

This code is now calling the Sentiment Analysis HuggingFace model and attaches it to the message payload under score. Through testing, if I upload a message such as:

{
    "text": "That was not cool"
}
Enter fullscreen mode Exit fullscreen mode

We will get a negative score of 0.9997703433036804, which is a high negative score, and that message can be flagged. It is important to note that the range of this models output is from 0 - 1, 0 being a very low score and 1 being the highest.  On the other hand, if we send a message such as:

{
    "text": "Thats so Cool!"
}
Enter fullscreen mode Exit fullscreen mode

We will receive a negative score of 0.00014850986190140247, which is a really low negative score, meaning the message has a positive tone.

Get Started with PubNub

With only a few steps, we have implemented sentiment analysis into the PubNub network utilizing PubNub integrations. If you are interested in more you can do with AI and PubNub, check out some of our other blogs, such as:

Build a Chatbot with PubNub and ChatGPT / OpenAI: We will walk you through setting up your own Chatbot using an OpenAI integration and PubNub.

Build an LLM Chatbot with a Custom Knowledge Base: Here, we go through how to build your own custom knowledgebase LLM Chatbot utilizing PubNub Functions. Meaning you can answer questions about your own company data.

A Developers Guide to Prompt Engineering and LLMs: Take a guide on prompt engineering and how to get the answers you want from these Large Language Models.

Geolocation Tutorial integrated with ChatGPT: In this tutorial, you’ll discover how to build a geolocation tracking app that allows users to share locations and send messages in real-time, as well as integrate with ChatGPT to provide information about your location.

How can PubNub help you?

This article was originally published on PubNub.com

Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.

The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.

Experience PubNub

Check out Live Tour to understand the essential concepts behind every PubNub-powered app in less than 5 minutes

Get Setup

Sign up for a PubNub account for immediate access to PubNub keys for free

Get Started

The PubNub docs will get you up and running, regardless of your use case or SDK

Top comments (0)