Let’s build a Chatbot for Mental Health.

Zaid Shaikh
6 min readMay 29, 2021
Photo by Blake Connally on Unsplash

We all have came across some Pop-ups right at the below end of the Visiting Pages of any Website. The Idea of a Software communicating with Humans with that Human Emotional tone Sounds Great isn’t it? Yes this is exactly why a term “CHATBOT" has emerged in recent times.

Today we’ll be building one such Chatbot, & not just any Chatbot for some E-Commerce, Retail or Business. We will be dealing with a Serious Issue that is Growing not just in India but in fact all around the World i.e. Mental Health. Before diving onto how we’ll accomplish it we’ll 1st look at the Reason why we are even building one such Chatbot.

The Problem :-

Today with Technology enhancements with an ever fast Growing rate, people all around the World have accustomed to high stressful work Environments. This situation makes people prone to many Issues like Stress, Depression, Sleep Disorders, Obsessive Compulsive Disorders etc. With an emergence of Social Media, there has been a rise in many Inferior Issues amongst people which leads to Mental Health Problems like Generalized Anxiety, Insecurity, Inferiority Complex which makes an Individual Prone to Mental Health Illness where he/she is not even aware of.

You see, the list might go on but the Underlying cause remains the same i.e. Mental Health Issues. These very little Mental Health Issues may lead to serious Life threatening problems like Cardiac Arrest, High Blood Pressure, High levels of Cholesterol which is a leading cause of unwanted Weight gain and Brain Strokes.

Won’t a Convenience of a Software, let’s not assume to solve the problem but at least to ease the problem will be a great Task? This is where availability of a Chatbot makes it possible (making best use of Tech alas!!!💬😶).

Let’s Code out the Psychiatrist 👨‍💻.

To accomplish that level of Human Intelligence as a Doctor, we will be using Multiple Tools in our Project.

  • Keras :- A High Level API based on Tensorflow. This will power that Human Intelligence in your Chatbot. This library is written in Python.
  • NLTK :- A Natural Language Processing library written in Python. Drives out Conversation between the user & the Chatbot.
  • JSON :- Used for building the Dataset which will be fed to the Neural Network. Data written in JSON can be parsed well with most of the Programming Languages, Python again being one of them.

Note :- I’ve used Google Colaboratory Notebook to build this Project since it reduces multiple Headache Tasks like Installation of Tensorflow & Keras which are large packages in Size to Download. Everything comes pre Installed and the only need is to import them into your Project.

Code :-

Before diving into the Code, lets understand some few Basics building Blocks of a Deep Learning based Chatbot.

There’s usually 2 types of Chatbots that are really common, one being Rule based Chatbots where the User input should be Pre defined. These Chatbots have Pre defined set of options to select as the answer. The other type being Deep Learning based where we make use of NLP & build a Neural Network to train our Data. These type of Chatbots don’t necessarily need one fixed pattern of input but can find out the best match of the user’s input & give a suitable Output Accordingly.

Here’s a look at our Sample dataset that we will be using. Note that the data used here is way too less for a Real World Project & we will discuss why more people should contribute to the dataset. The dataset contains a single dictionary called intents which contains all our different tags. Each tag represents a different class where each class is going to be a different Mental Issue except greetings & goodbye.

This is our Sample Dataset.

First things first, we will import all our necessary Libraries. We will also make use of pickle module for saving our Classes in a separate file to use it for later.

Importing Libraries

I’ve used the Google Colab files module to load the Dataset file. This data will be parsed using JSON module into Python Dictionary.

The most basic task to perform before processing data into Dataset is Tokenization. Tokenizing means to break a Component into smaller chunks/parts & in context of processing English Paragraphs, sentences & Words we will break Paras into individual sentences & sentences into individual words. Following this, we perform a process called Lemmatization to find a Root meaning of a Word. Lets assume 3 words like Playing, Played & Player. All these 3 words will refer to the root word Play. This will ensure that multiple Words with Same root meaning will be Clubbed together.

Performing NLP concepts like Tokenizing & Lemmatization

Until now, we have performed Natural Language Processing onto our Data. Before feeding our processed data to the Neural Network, lets clear one Problem that we might face.

Neural Networks never work with Textual Data. Its easy when you have Images which are nothing but essentially 2D planes of values ranging from 0 to 255 (Single plane incase of B&W & 3 planes incase of RGB). But with Textual data in our case, we have to map our Data in some numerical form.

To handle this, we will perform a Technique named One Hot Encoding. Here we perform mapping of Text data into Binary Values named FROM 0’s & 1’s. This Binary Encoded data will be Mapped into a 2D array with x-labels & y-labels. You can learn more about it here.

Now lets create a Model with 3 layers. Note that again count of Layers & Neurons may vary for every other Project as per the needs. For our case there will be 128, 64 Layers for first 2 Layers. The 3rd Layer will have the no of Neurons equal to the total no of Mental Health Issues. This way we can map the Probability of finding each Mental Health Issue to each of its Neuron.

The Sequential() function tells our Network that it will be a Linear Network. We have also made use of Activation functions, one of which is ReLU. The rectified linear activation function or ReLU for short is a piecewise linear function that will output the input directly if it is positive, otherwise, it will output zero.

Following this we will use Stochastic Descendent Gradient which Compiles the data into 2D space.

Stochastic Descendant Gradient.

Now we will run our Model & save it.

Train our Model

Here’s how the Model looks while running. We can see the loss of Data & Accuracy of the Model with each successive iteration.

Up to this point we have Successfully Trained our Neural Network. Now lets work on the Response to be Delivered to the user.

We will again be Performing Natural Language Processing but this time on the Mental Health Problem a user tells to the Bot during Conversation. All the steps performed above will be Done in similar way. Unlike last time, now we will Encode the Data & Check its most suitable match with the user. This data will be Matched with the output Layer & the Neuron with the highest Probability will indicate us which Mental Issue the User is Facing.

Following this, the Chatbot after Identifying the Issue will then respond back to the User with a Response from the Dataset. This is exactly how the Conversation will take place just like one between a Patient & a Doctor.

Sample Conversation

The above pic shows a Sample Conversion between both the Chatbot & User.

Note :- One thing that I’d like to add is that I’ll be sharing the Repository Link below. I am pretty much welcoming other People from Medical Background to work on the Sample Dataset & add data to other Issues or existing ones. Also welcoming my fellow Developers to add more features to this project from their Scope. My main aim is to Solve Increasing Mental Health Problems amongst the General population by means of latest Tech. We are all in need of one & the Covid-19 Pandemic has proven us why Mental Health is also crucial alongside Physical Health.

Link to GitHub Repository :- https://github.com/Zaid-Shaikh333/Chatbot-for-Mental-health

Also if your enjoyed reading this Story do give it a Clap down below.👏

Follow me for more such Tech Blogs. 💻👨‍💻💬

--

--

Zaid Shaikh

Converting my thoughts into Words 📖💻🖋️📝