Chatbots are very amazing. Isn't it. Today almost every website uses a chatbot to interact with their users instead of hiring people. Chatbot is a kind of AI (Artificail Intelligence), which use various algorithms like NLP (Natural Language processing), Speech Recognition, Text to Speech to understand human languages, speak and enhance user experience. Making a chatbot in present time is not a big deal. So, follow this tutorial carefully to create your own chatbot with python and tensorflow.


What Is a Chatbot ?

If you don't know about chatbot, Chatbot is a AI, that is trained to interact with humans and to assist them.  It is very easy to make a chatbot, the thing we need is dataset for chatbot, don't worry I'll provide that dataset to you. Now let's talk about some features of chatbot :

  • It can understand natural human language.
  • It can use both speech recognition and text to speech.
  • It is easy to make a chatbot with Tensorflow.
  • Chatbot reduce human efforts because it can help like a human.
Even virtual assistant that we use today like Google assistant, Alexa or Siri are also chatbots.
Read more about chatbout Here .

Let' start creating Chatbot 

* NOTE: Follow all steps carefully * 

Prerequisites

  1. Python must Installed in your computer (I recommend version 3.6) .
  2. Basic knowledge of python is must.
  3. Internet connection.
  4. A code editor. I recommend VS Code.
  5. Some hardwork and time...
Run python in your terminal to see that python is installed properly and also check pip command is working to install the required modules.
Now if you have installed python and VS code in your computer let's start building our Chatbot.

STEP 1: Create a Virtual Environment and install the required dependencies.

Go to terminal and install the virtualenv package to create virtual environment.
$ pip install --user virtualenv
Now go to your project folder and create virtual environment.
$ python -m venv env
Here replace env with your project name in my case I am naming the project as "chatbot"

Awesome! Your virtual environment is created successfully.

Now open your virtual environment in VS code. You will see this. Note that I am using windows here.


In the file menu you can see this


The Scripts folder here contains the executables.

Now you need to activate your "chatbot" virtual  environment. 

Run this command to activate virtual environment.
You can also use your VS code terminal for running any command.
To activate in macos or linux:
$ source env/bin/activate

On windows:

$ .\env\Scripts\activate
Now to simply deactivate this virtual environment you can run this command.
$ deactivate

If you have deactivated your virtual environment activate it again. Its's time install the required modules

For this project you will need these following modules :-
  1. Numpy
    $ pip install numpy
  2. Tensorflow
    $ pip install tensorflow
  3.  NLTK 
    $ pip install nltk

Thats it, now move ahead to Step 2.

STEP 2: Get your dataset for Chatbot.



The most important thing that you need for making your AI is a dataset.
You can use any dataset to prepare and train your model according to your need, but for learning purposes you should use a smaller data set like this: 

Jarvis is the name of chatbot, driven from Iron man movie 😅

Understand these terms in dataset: 
  • Tags : This tag is kind of a unique name for every intention. The work of our Deep Learning model is to identify this inention tag on the basis of question asked by the user.  
  • Patterns : These are a set of questions that a user might ask from the chatbot which will help our model to identify the intention of user.
  • Responses : A set of replies that the the bot will answer for any kind of question. 
Note that this dataset is in JSON format. And I have added random things to it 😅 you can change it and add more intents if you want. But for now focus on learning.
Save this in your project folder with a name "intents.json".


STEP 3: Preprocess Data

Now before creating our deep learning model with tensoflow, preprocessing of data is important.

For data preprocessing for our chatbot model you must understand the following terms:

1.Tokenization

In tokenization we split a sentence, phrase or a whole paragraph into smaller units like a word or term and each of these unit is a token.

For example - "how are you ?" get tokenized into ['how", "are", "you", "?"]

For tokenization we will use nltk.word_tokenize() function of nltk:


2. Word stemming

Stemming is a process of reducing a word to its word stem or root form by removing its suffix or prefix.

Fox example - "walking" is the word and its suffix is "ing" and by removing this "ing" we will get the root word which is "walk".

For word stemming we will use LancasterStemmer by nltk.stem:

Hope you understand this.
Now create main.py in your folder that you have opened in VS code.
Your VS code file menu should look like this



Put the following code in your main.py

By running this we are tokenizing patterns of each intents of our data and also stemming them. Try running this to see the output.

STEP 4: Data splitting

Data splitting is one of the main work while you are making any deep learning or machine learning model. Here we split the available data into two parts:

Now to split we will do this:


Note that here we converted text data into number.

STEP 5: Creating Our Model

To create our model we will use the Sequential model from keras and add layers to it, Note that we are training model for 200 epochs and get 100% accuracy
Now to code the model just do this:

Here we are saving model in a "my_model" folder. We can use this model later by loading it.

STEP 6: Complete Training

Now if you have performed all the steps above correctly your main.py will be like this:



If you put this code in main.py and run it you will see that it will take some time, but after that it will create "data.pickle" and "my_model" folder in your project. Your VS code file menu will be like this



Congratulation your training process is done now. 
Now if you have done that lets move ahead.

STEP 6: Getting Responses

For getting any responses we must take a input from user and process that input text and use our saved model to get the answer.

First create chatbot.py file in your project folder and put the following code.

Understand the code above, It is looking complex but it is very simple. Try Reading comments in the code above to understand work of each function.


Finally, after too much hardwork we created our chatbot let's run chatbot.py to see the results.
A small chat with our bot :


Amazing our bot is working and answering our questions. It is a simple and basic chatbot. I know it is too boring to chat with our bot in this dull and ugly terminal, but you can create a GUI for your bot easily with libraries like tkinter or PyQt5. 
 
If you try to ask some wierd or out of the data questions to the bot you will notice that it will not answer you as it only answers the questions for which it is trained for, it don't answer most of your question as it is trained on a smaller and random data. Try to update data and add more intents to make your bot better.

You can also do many other customization  in the bot. Like you can add functions for - telling time, making reminders and much more.

I hope this will help you. If you like this share it with your friends more and more. If you face any problem or want more such posts please comment below.
Be healthy and keep learning.

Thanks