Chat-bot using deep learning — By Retrieval based

Prashant Sagar
4 min readMay 27, 2021

What is Chat-bot?

A chatbot is a conversational piece of software powered by pre-programmed responses or artificial intelligence to answer questions without the need of a human operator. Chat-bots are used a lot in customer interaction, marketing on social network sites and instantly messaging the client. There are two basic types of chat-bot models are Retrieval based and Generative based models.here we made using Retrieval based(it just pick the responses form your datasets and response).

About this project

we built this chat-bot using deep learning, for that we need a datasets which contain some pattern and responses(intents). for that we use use a special recurrent neural network (LSTM) to classify which category the user’s message belongs to and then we will give a random response from the list of responses.

Prerequisites

for built you must familiar with Tensorflow, keras, NLTK

files & model

  • Intents.json — Contain predefined patterns and responses(Datasets).
  • train_chatbot.py — Python code for model training.
  • Words.pkl — contains a list of our vocabulary of our words
  • Classes.pkl — contains the list of categories.
  • Chatbot_model.h5 — Trained model that contains information about the model.
  • Chatgui.py — GUI for our chat-bot. Users can easily interact with the bot.

source code: https://github.com/prashantsagar73/chatbot_ITMU

Import and load the files

clone the git repo and after go to the that directory where you clone the files and install the requirement.txt

pip install -r requirements.txt

or you can install it manually following packages: make sure you use virtual env for this process.

pip install tensorflow 
pip install keras
pip install pickle5
pip install nltk

file intents.json

contain the data sets in form of pattern and response , you can edit this as well as you requirements after successfully run the whole code.

trian_chatbot.py

First we import the packages that are required in this chat-bot.

1. Process data

Tokenizing is the process of breaking the whole text into small parts like words.Here we iterate through the patterns and tokenize the sentence using nltk.word_tokenize() function and append each word in the words list. We also create a list of classes for our tags.

word tokenize

2. Now we will lemmatize each word and remove duplicate words from the list.

3. Create training and testing data

we will create the training data in which we will provide the input and the output. Our input will be the pattern and output will be the class our input pattern belongs to. for computer we convert in into the numbers.

4. Build the model

building the model

5. Predict the response

After creating the model load the trained model and then use a graphical user interface that will predict the response from the bot. The model will only tell us the class it belongs to, so we will implement some functions which will identify the class and then retrieve us a random response from the list of responses.

Again we import the necessary packages and load the ‘words.pkl’ and ‘classes.pkl’ pickle files which we have created when we trained our model.To predict the class, we will need to provide input in the same way as we did while training.

After predicting the class, we will get a random response from the list of intents.

Now our model is ready so we come to chat-bot gui

we use Tkinter library which is shipped with tons of useful libraries for GUI. that take the input message from the user and then use the helper functions we have created to get the response from the bot and display it on the GUI. Here is the full source code for the GUI.

7. Run the chat-bot

First, we train the model using the command in the terminal or any text editor as you like.

python train_chatbot.py

If model build successfully then you got a message

model is created

Now run the GUI get intact with cht-bot

python chatgui.py

Now enjoy with you chat-bot for more fun i recommend to use your personal data,

Have fun!

--

--