Building a Real Time Chat Application with NLP Capabilities

A Chat App with Sentiment Analysis and Tone Detection using TensorFlow JS Deep Learning API, IBM Cloud, Node.JS, Web Sockets, and React

Deval Parikh
Towards Data Science

--

Photo by Volodymyr Hryshchenko on Unsplash

In a world where Artificial Intelligence (AI) and Machine Learning (ML) models are being leveraged to obtain real-time information to continuously improve customer experience, I wanted to discover how effective ML can prove to be when trying to understand human conversations, and even try building our own custom application that implements this technology.

Conversational Machine Learning in Practice

The world is quickly adapting to using AI-driven applications to assist humans on a day to day basis. Siri and Alexa are driven by Natural Language Processing (NLP) based ML models that are continuously training and iterating to sound more natural and offer more value by understanding complex human dialog. Not to mention, Google’s latest conversation technology, LaMDA. LaMDA is trained in a way to be able to interrupt larger blocks of human conversation and understand how multiple words or phrases are related to each other to gain more context about the inputed text, specifically targeting human dialog. It is then able to dynamically generate a response that is natural and human-like. The following is from Google’s AI blog post, LaMDA: our breakthrough conversation technology, which briefly describes the deep learning technology LaMDA is built on:

[1] LaMDA’s conversational skills have been years in the making. Like many recent language models, including BERT and GPT-3, it’s built on Transformer, a neural network architecture that Google Research invented and open-sourced in 2017

This technology is super impressive and is quickly proving how valuable it can be in our daily lives, from making reservations for us to eliminating the need for human powered call centers.

Challenges of Machine Learning

However, it’s not all rainbows and sunshines, in the process of training and integrating ML models into production applications, there comes many challenges. How do we test these models? How do we develop reliable models? How do we ensure biases in ML models are optimally mitigated? How easily can we build applications to scale that use these models? These are all questions to consider when developing ML solutions at scale.

Let’s Build Our Own App!

In this article, we are going to build a custom full stack application that utilizes Google’s TensorFlow and IBM’s Cloud Machine Learning as a Service (MLaaS) platform to discover how competently engineers can develop maintainable, reliable, and scalable full stack ML solutions using Cloud-based tools.

The application we will be building is a real-time chat application that is able to detect the tone of the users’ messages. As you can imagine the use cases for this can span greatly, from understanding customers’ interaction with customer service chats to understanding how well a production AI chatbot is performing.

Architecture

Real-time Chat App Architecture
At a high level, this is the architecture of the application.

The high level application architecture consists of utilizing React and TypeScript for building out our custom user interface. Using Node.JS and the Socket.IO library to enable real-time, bidirectional network communication between the end user and the application server. Since Socket.IO allows us to have event-based communication, we can make network calls to our ML services asynchronously upon a message that is being sent from an end user host.

Backend

As for as the ML services, we can make an HTTP request (that contains the content of the chat message being sent in the payload) to IBM’s Cloud based tone detection models. The response from IBM’s web service will be a JSON object containing an array of tones that the model classified that message having, such as

[“Joy”, “Positive”, “Happy”]

Asynchronously, our Node.JS web service can make a request to TensorFlow’s Sentiment API. TensorFlow’s ML model is a Convolutional Neural Network based Deep Learning architecture that has been trained on 50,000 labelled data points made up of IBMD movie reviews to be able to predict the sentiment of newly introduced inputted texts. We will send each new chat message through TensorFlow’s pre-trained model to get an average Sentiment score of the entire chat conversation.

The event-based function to run ML services upon a new chat message.

In the above gist, you can see upon a client sending a new message, the server will call 2 functions, getTone and updateSentiment, while passing in the text value of the chat message into those functions.

The getTone function created to make the API call to IBM Cloud Tone Analyzer API.
The updateSentiment function created to run the TensorFlow ML Model with the inputted chat message text to calculate the average sentiment.
The predict function that uses the TensorFlow JS framework.

The predict function above is called in updateSentiment. This function loads the TensorFlow pre-trained model by using a network fetch, preprocesses the inputted data, and uses the model to evaluate a sentiment score. This all happens in the background parallel to processing other backend tasks.

Frontend

User Interface using Socket IO Client Library for bidirectional connection with the server code.

In the code above, we are building a functional React component to handle client side interaction with the Chat Application. Since we are using a functional component, we have access to React hooks, such as useState and useEffect. The message state holds the value of the current user’s inputed text. The messages state holds the array of all sent messages. You can see the connection to the Socket server in useEffect, which will be called upon every re-render/on-load of the component. When a new message is emitted from the server, and event is triggered for the UI to receive and render that new message to all online user instances.

User Interface’s sendMessage function to build the messageObject to emit to the Socket server.

The messageObject emitted from the sendMessage function will reach the server which will parse out the messageObject.body (the actual value of the user sent chat message) and process the data through the ML web services built out previously. It will then build and return a new object containing the message, username, and the tone of the message acquired from the ML model’s output.

The client-side JSX code to render the messages and parse out the message sender’s username, as well as the tone of the message, and of course the message body.

and the final results… TADA!

Demo of Real-time Chat App
A demonstration of the application we built in this article. Each chat bubble contains the tag of the tone of that given message. (Source: Author)

Full Source Code

To checkout the full code visit the Github repository:

Conclusion

Through building this proof of concept project, I hope to demonstrate how seamless it is to develop a custom application that can integrate Machine Learning tools and techniques by utilizing Cloud-based Web Services to be scalable and maintainable. By using IBM’s Cloud Services and Google’s TensorFlow Pre-Trained Sentiment Model, we were able to build a chat application that can classify the tone of each chat message, as well as the overall sentiment of the conversation.

References

[1] E. Collins, Z. Ghahramani, LaMDA: our breakthrough conversation technology (2021), https://blog.google/technology/ai

--

--