Deploy a Chatbot using TensorFlow in Python
Last Updated :
23 Jul, 2025
In this article, you'll learn how to deploy a Chatbot using Tensorflow. A Chatbot is basically a bot (a program) that talks and responds to various questions just like a human would. We'll be using a number of Python modules to do this.
This article is divided into two sections:
First, we'll train the Chatbot model, and then in section two, we'll learn how to make it work and respond to various inputs by the user.
Modules required:
- random - This module is used to generate random responses from the Chatbot
- json - To read from json file
- pickle - To save data into files
- tensorflow - To train neural networks. It is an open source machine learning library.
- numpy - It is a Python library used for working with arrays
- nltk - It is a leading platform for building Python programs to work with human language data.
Training the Chatbot model
The first thing we're going to do is to train the Chatbot model. In order to do that, create a file named 'intense.json' in which we'll write all the intents, tags and words or phrases our Chatbot would be responding to.
Step 1: Create a "intense.json":
Here we've added just three tags just to show how it works. You can add a lot of them!
Step 2: Create a "training.py":
The next step is training our model. We'll use a class called WordNetLemmatizer() which will give the root words of the words that the Chatbot can recognize. For example, for hunting, hunter, hunts and hunted, the lemmatize function of the WordNetLemmatizer() class will give "hunt" because it is the root word.
- Create a WordNetLemmatizer() class object.
- Read the contents from the "intense.json" file and store it to a variable "intents". Next, initialize empty lists to store the contents.
- Next up, we have a function called word_tokenize(para). It takes a sentence as a parameter and then returns a list containing all the words of the sentence as strings. Here we're tokenizing the patterns and then appending them to a list 'words'. So, at last, this list 'words' would have all the words that are in the 'patterns' list.
- In documents, we have all the patterns with their tags in the form of a tuple.
- Now, using a list comprehension, we'll modify the list 'words' we created above and store the words' 'lemma' or simply put, the root words.
- Dump the data of the 'words' and 'classes' to binary files of the same name, using the pickle module's dump() function.
Python3
# importing the required modules.
import random
import json
import pickle
import numpy as np
import nltk
from keras.models import Sequential
from nltk.stem import WordNetLemmatizer
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
lemmatizer = WordNetLemmatizer()
# reading the json.intense file
intents = json.loads(open("intense.json").read())
# creating empty lists to store data
words = []
classes = []
documents = []
ignore_letters = ["?", "!", ".", ","]
for intent in intents['intents']:
for pattern in intent['patterns']:
# separating words from patterns
word_list = nltk.word_tokenize(pattern)
words.extend(word_list) # and adding them to words list
# associating patterns with respective tags
documents.append(((word_list), intent['tag']))
# appending the tags to the class list
if intent['tag'] not in classes:
classes.append(intent['tag'])
# storing the root words or lemma
words = [lemmatizer.lemmatize(word)
for word in words if word not in ignore_letters]
words = sorted(set(words))
# saving the words and classes list to binary files
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))
Step 3: Now we need to classify our data into 0's and 1's because neural networks works with numerical values, not strings or anything else.
- Create an empty list called training, in which we'll store the data used for training. Also create an output_empty list that will store as many 0's as there are classes in the intense.json.
- Next up we'll create a bag that will store the 0's and 1's. (0, if the word isn't in the pattern and 1 if the word is in the pattern). To do that, we'll iterate through the documents list and append 1 to the 'bag' if it is not in the patterns, 0 otherwise.
- Now shuffle this training set and make it a numpy array.
- Split the training set consisting of 1's and 0's into two parts, that is train_x and train_y.
Python3
# we need numerical values of the
# words because a neural network
# needs numerical values to work with
training = []
output_empty = [0]*len(classes)
for document in documents:
bag = []
word_patterns = document[0]
word_patterns = [lemmatizer.lemmatize(
word.lower()) for word in word_patterns]
for word in words:
bag.append(1) if word in word_patterns else bag.append(0)
# making a copy of the output_empty
output_row = list(output_empty)
output_row[classes.index(document[1])] = 1
training.append([bag, output_row])
random.shuffle(training)
training = np.array(training)
# splitting the data
train_x = list(training[:, 0])
train_y = list(training[:, 1])
Step 4: We've come to the model-building part of our Chatbot model. Here, we're going to deploy a Sequential model, that we'll train on the dataset we prepared above.
- Add(): This function is used to add layers in a neural network.
- Dropout(): This function is used to avoid overfitting
Python3
# creating a Sequential machine learning model
model = Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]), ),
activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]),
activation='softmax'))
# compiling the model
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd, metrics=['accuracy'])
hist = model.fit(np.array(train_x), np.array(train_y),
epochs=200, batch_size=5, verbose=1)
# saving the model
model.save("chatbotmodel.h5", hist)
# print statement to show the
# successful training of the Chatbot model
print("Yay!")
Output:
Creating a main.py to run the Chatbot
We're done training the model, now we need to create the main file that will make the Chatbot model work and respond to our inputs.
Step 1: To get started, import the following modules.
Python3
# required modules
import random
import json
import pickle
import numpy as np
import nltk
from keras.models import load_model
from nltk.stem import WordNetLemmatizer
Step 2: Initialize the following classes and file contents.
Python3
lemmatizer = WordNetLemmatizer()
# loading the files we made previously
intents = json.loads(open("intense.json").read())
words = pickle.load(open('words.pkl', 'rb'))
classes = pickle.load(open('classes.pkl', 'rb'))
model = load_model('chatbotmodel.h5')
Step 3: We'll define 3 functions here.
clean_up_sentences(sentence) - This function will separate words from the sentences we'll give as input.
Python3
def clean_up_sentences(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word)
for word in sentence_words]
return sentence_words
bagw(sentence): This function will append 1 to a list variable 'bag' if the word is contained inside our input and is also present in the list of words created earlier.
- First we'll use the function defined above to separate out 'root' words from the input, then in the next line, initialise a list variable called bag that will contain as many 0's as the length of the words list.
- Using nested for loops, we'll check whether the word in the input is also in the words list. If it is, we'll append 1 to the bag, otherwise it'll remain 0.
- Return a numpy array of the list variable bag that now contains 1's and 0's.
Python3
def bagw(sentence):
# separate out words from the input sentence
sentence_words = clean_up_sentences(sentence)
bag = [0]*len(words)
for w in sentence_words:
for i, word in enumerate(words):
# check whether the word
# is present in the input as well
if word == w:
# as the list of words
# created earlier.
bag[i] = 1
# return a numpy array
return np.array(bag)
predict_class(sentence): This function will predict the class of the sentence input by the user.
- Initialize a variable bow that will contain a NumPy array of 0's and 1's, using the function defined above. Using the predict() function, we'll predict the result based on the user's input.
- Initialize a variable ERROR_THRESHOLD and append from 'res' if the value is greater than the ERROR_THRESHOLD, then sort it using the sort function.
- Using a list variable return_list, store the tag or classes that was in the intense.json file.
Python3
def predict_class(sentence):
bow = bagw(sentence)
res = model.predict(np.array([bow]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res)
if r > ERROR_THRESHOLD]
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({'intent': classes[r[0]],
'probability': str(r[1])})
return return_list
get_response(intents_list, intents_json): This function will print a random response from whichever class the sentence/words input by the user belongs to.
- First we initialize some required variables, such as tags, list_of_intents and results.
- If the tag matches the tags in the list_of_intents, store a random response in a variable called result, using the choice() method of the random module.
- Return result.
Python3
def get_response(intents_list, intents_json):
tag = intents_list[0]['intent']
list_of_intents = intents_json['intents']
result = ""
for i in list_of_intents:
if i['tag'] == tag:
# prints a random response
result = random.choice(i['responses'])
break
return result
print("Chatbot is up!")
Step 4: Finally, we'll initialize an infinite while loop that will prompt the user for an input and print the Chatbot's response.
Python3
while True:
message = input("")
ints = predict_class(message)
res = get_response(ints, intents)
print(res)
main.py is as follows:
Python3
import random
import json
import pickle
import numpy as np
import nltk
from keras.models import load_model
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
intents = json.loads(open("intense.json").read())
words = pickle.load(open('words.pkl', 'rb'))
classes = pickle.load(open('classes.pkl', 'rb'))
model = load_model('chatbotmodel.h5')
def clean_up_sentences(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word)
for word in sentence_words]
return sentence_words
def bagw(sentence):
sentence_words = clean_up_sentences(sentence)
bag = [0]*len(words)
for w in sentence_words:
for i, word in enumerate(words):
if word == w:
bag[i] = 1
return np.array(bag)
def predict_class(sentence):
bow = bagw(sentence)
res = model.predict(np.array([bow]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res)
if r > ERROR_THRESHOLD]
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({'intent': classes[r[0]],
'probability': str(r[1])})
return return_list
def get_response(intents_list, intents_json):
tag = intents_list[0]['intent']
list_of_intents = intents_json['intents']
result = ""
for i in list_of_intents:
if i['tag'] == tag:
result = random.choice(i['responses'])
break
return result
print("Chatbot is up!")
while True:
message = input("")
ints = predict_class(message)
res = get_response(ints, intents)
print(res)
Output:
Similar Reads
Deep Learning Tutorial Deep Learning is a subset of Artificial Intelligence (AI) that helps machines to learn from large datasets using multi-layered neural networks. It automatically finds patterns and makes predictions and eliminates the need for manual feature extraction. Deep Learning tutorial covers the basics to adv
5 min read
Deep Learning Basics
Introduction to Deep LearningDeep Learning is transforming the way machines understand, learn and interact with complex data. Deep learning mimics neural networks of the human brain, it enables computers to autonomously uncover patterns and make informed decisions from vast amounts of unstructured data. How Deep Learning Works?
7 min read
Artificial intelligence vs Machine Learning vs Deep LearningNowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are
4 min read
Deep Learning Examples: Practical Applications in Real LifeDeep learning is a branch of artificial intelligence (AI) that uses algorithms inspired by how the human brain works. It helps computers learn from large amounts of data and make smart decisions. Deep learning is behind many technologies we use every day like voice assistants and medical tools.This
3 min read
Challenges in Deep LearningDeep learning, a branch of artificial intelligence, uses neural networks to analyze and learn from large datasets. It powers advancements in image recognition, natural language processing, and autonomous systems. Despite its impressive capabilities, deep learning is not without its challenges. It in
7 min read
Why Deep Learning is ImportantDeep learning has emerged as one of the most transformative technologies of our time, revolutionizing numerous fields from computer vision to natural language processing. Its significance extends far beyond just improving predictive accuracy; it has reshaped entire industries and opened up new possi
5 min read
Neural Networks Basics
What is a Neural Network?Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
11 min read
Types of Neural NetworksNeural networks are computational models that mimic the way biological neural networks in the human brain process information. They consist of layers of neurons that transform the input data into meaningful outputs through a series of mathematical operations. In this article, we are going to explore
7 min read
Layers in Artificial Neural Networks (ANN)In Artificial Neural Networks (ANNs), data flows from the input layer to the output layer through one or more hidden layers. Each layer consists of neurons that receive input, process it, and pass the output to the next layer. The layers work together to extract features, transform data, and make pr
4 min read
Activation functions in Neural NetworksWhile building a neural network, one key decision is selecting the Activation Function for both the hidden layer and the output layer. It is a mathematical function applied to the output of a neuron. It introduces non-linearity into the model, allowing the network to learn and represent complex patt
8 min read
Feedforward Neural NetworkFeedforward Neural Network (FNN) is a type of artificial neural network in which information flows in a single direction i.e from the input layer through hidden layers to the output layer without loops or feedback. It is mainly used for pattern recognition tasks like image and speech classification.
6 min read
Backpropagation in Neural NetworkBack Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Deep Learning Models
Deep Learning Frameworks
TensorFlow TutorialTensorFlow is an open-source machine-learning framework developed by Google. It is written in Python, making it accessible and easy to understand. It is designed to build and train machine learning (ML) and deep learning models. It is highly scalable for both research and production.It supports CPUs
2 min read
Keras TutorialKeras high-level neural networks APIs that provide easy and efficient design and training of deep learning models. It is built on top of powerful frameworks like TensorFlow, making it both highly flexible and accessible. Keras has a simple and user-friendly interface, making it ideal for both beginn
3 min read
PyTorch TutorialPyTorch is an open-source deep learning framework designed to simplify the process of building neural networks and machine learning models. With its dynamic computation graph, PyTorch allows developers to modify the networkâs behavior in real-time, making it an excellent choice for both beginners an
7 min read
Caffe : Deep Learning FrameworkCaffe (Convolutional Architecture for Fast Feature Embedding) is an open-source deep learning framework developed by the Berkeley Vision and Learning Center (BVLC) to assist developers in creating, training, testing, and deploying deep neural networks. It provides a valuable medium for enhancing com
8 min read
Apache MXNet: The Scalable and Flexible Deep Learning FrameworkIn the ever-evolving landscape of artificial intelligence and deep learning, selecting the right framework for building and deploying models is crucial for performance, scalability, and ease of development. Apache MXNet, an open-source deep learning framework, stands out by offering flexibility, sca
6 min read
Theano in PythonTheano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays efficiently. It is mostly used in building Deep Learning Projects. Theano works way faster on the Graphics Processing Unit (GPU) rather than on the CPU. This article will help you to unde
4 min read
Model Evaluation
Deep Learning Projects