Thursday, September 23Digital Marketing Journals

Building a Chatbot for Mental Health Awareness and Education | by Kevin Liu | Apr, 2021


Let’s leverage technology for good.

Kevin Liu
Photo by Andy Kelly on Unsplash

In recent years, chatbots have impacted several industries from retail, banking, finance, healthcare, to energy, investment has skyrocketed.

Which got me thinking, to broaden my knowledge within the realm of AI and chatbots, what problems can I solve using the power of tech?

Lo and behold, a mental health awareness chatbot.

Before we get started with the technical breakdown of the chatbot, I actually want to first provide some context on mental health specifically.

Photo by Nik Shuliahin on Unsplash

For starters, mental health disorders and problems affect an estimated 792 million people worldwide.

That’s basically 1 in 10 people globally.

In Canada, where I’m from, the problem is even worse, as 1 in 5 Canadians experience a mental illness or addiction problem every single year, with 1 in 2 experiencing one by the time that they reach the age of 40.

70% of the mental health problems also begun during childhood or adolescence, and youth experiencing the highest rates than any other age group. The reason that this is such a big problem is that mental illness can reduce life expectancy by 10–20 years.

Furthermore, the negative economic effect in Canada alone is estimated to be around $51 billion CAD/year attributed to the “healthcare costs, lost productivity, and reductions in health-related quality of life.” [3]

At the end of the day, based on my analysis and research, some of the root causes of the problems that exist can be attributed to:

  • the lack of access to services (due to the lack of funding, lack of accessibility w/ not enough adequate care)
  • the stigma associated with mental health, creating an inability to voice concerns and preventing people from getting the help that they need
  • the lack of systems to support those that need help and assistance

For my project, I decided to tackle the last 2 out of the 3 root causes and attempt to develop a solution to fill the gaps.

Here’s how I did it.

👋 Step 0: Preparation and Learning Resources

These were absolutely golden and extremely helpful along the way.

After I had a decent understanding of the theoretical standpoints, I started to break down the project into a few key steps, which are broken down in detail down below.

Step 1: Transforming Conversational Intents and Definitions to a Pytorch Model
Step 2: Building a Chatbot Framework for Response Processing
Step 3: Creating a Graphical Interface

With the theoretical understanding in place, I got started by opening Visual Studio Code and created a few core files that I knew that I’d need.

  • app.py (for the graphical user interface)
  • chat.py (for building a chatbot framework)
  • intents.json (helping the computer understand the intents)
  • model.py (a few lines of code integrating the deep learning components)
  • nltk_utils.py (for the natural language processing with nltk and other libraries)
  • train.py (for the training of the program itself!)

If you’d like to check out the code, the full repository can be found on Github by following this link!

1. How Conversational AI can Automate Customer Service

2. Automated vs Live Chats: What will the Future of Customer Service Look Like?

3. Chatbots As Medical Assistants In COVID-19 Pandemic

4. Chatbot Vs. Intelligent Virtual Assistant — What’s the difference & Why Care?

🚇 Step 1: Transforming Conversational Intents and Definitions to a Pytorch Model

Every intent contains:

  • a tag (a label/name that you choose)
  • patterns (sentence patterns for the text classifier in the neural network)
  • responses (the answer that you would like the machine to give when done)

Now that we’ve handled all of the conversational intents through the intents.json file, we can move towards the next step, where we handle the initial NLP pre-processing.

The first thing to do is import the essentials.

Once that’s taken care of, the next task is organizing the documents, words and classification classes.

Throughout that process, I created a list of sentences that could then be broken down further into a list of the stemmed words, with each sentence associated with an intent (a class).

After the natural language processing is handled, I followed the tutorial and proceeded with building out the deep learning model, with Pytorch. For more information on how Pytorch works and the Deep Learning concepts, it’s able to apply, check out this article series.

After the model was built, I continued the tutorial to proceed and build out the training code as shown down below.

The purpose of this code down below is to load the intents.json file, apply the natural language processing code, create the training data, and begin training the model.

Now that the hard part’s done, let’s move towards the next section where I break down the chatbot response framework.

🏗 Step 2: Building a Chatbot Framework for Response Processing

Based on the tutorial and other resources I consulted, I then structured the code and wrote my model the way I did to solve the classification problem of gathering the intent from the user intent, through classification (of the tag that the statement falls under, and thus picks a response from that).

💻 Step 3: Creating a Graphical Interface

Lastly, to end off, I wanted to provide a few quick shoutouts to Darien Schettler, Victor Sami, Vitor Falleiros, Bryan Horowitz, and Afshin Nensi, who were all extremely helpful throughout the process, by providing feedback on my work, and/or sharing some of their thoughts surrounding the mental health chatbot space.

The full video is coming really soon, so keep your eyes peeled for that!

Leave a Reply