Build your own Chatbot in Python with AJAX.

Hemanthhari2000
featurepreneur
Published in
3 min readMar 8, 2021

Build an end-to-end Chatbot with Flask and AJAX.

chat image
Photo by Alexander Shatov on Unsplash

A chatbot is an application that automates a conversation and interacts with people. They are mainly used in online businesses which include interactions between customers. This provides an efficient way of handling frequently asked questions and a better user experience. Chatbots have grown tremendously in past years and it is evaluated at a whopping 17.17 BILLION USD in 2020 and it is estimated to reach 102.29 BILLION USD by the year 2026. So, it is proved that chatbot has a great future ahead, and learning to make one on your own is the best way to get started. Now, let’s build a chatbot from scratch that responds to our question.

So, Let’s Get Started.

Prerequisites

For this project, you’ll need

  1. Tensorflow (2.x)
  2. Flask (latest)
  3. nltk (3.5)

That’s it, Yep, only these 3 packages. Now, Let’s see the directory structure for this project.

CHATBOT
├── app.py
├── chatbot.py
├── train.py
├── requirements.txt
├── resources
│ ├── data
│ │ └── intents.json
│ ├── models
│ └── pickles
├── static
│ ├── index.js
│ └── style.css
└── templates
└── index.html

Now let’s add something called intents which is the data that our chatbot will be trained on. So, in the intents.json file, the data looks somewhat like this.

{"intents": [{     "tag": "greeting",     "patterns": [
"Hi","How are you",
"Is anyone there?","Hello","Good day","Whats up"
],
"responses": [
"Hello!","Good to see you again!",
"Hi there, how can I help?"
],
"context_set": "" }
]
}

This is the JSON format for the intents. You can be creative and add as many intents as you want. Be creative as it is your own chatbot. Check out my GitHub repo for full code here.

Enough of data collection, Let’s get our hands dirty and begin to code. Open your train.py file and import all the packages we need.

from tensorflow.keras.optimizers import SGD 
from tensorflow.keras.layers import Dense, Activation, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.models import load_model
import random
import json
import numpy as np
import pickle
import nltk
nltk.download('punkt')
nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()

Now, we will build a machine learning model that is able to learn on a set of datasets that we previously built.

In chatbot.py

Phew… Finally, all the chatbot code is done and now we will start our Flask app. In app.py

Now, we need to add the index.html file to show our chatbot on our browser and to get the responses using AJAX calls. So, in index.html

That’s it We are done. We made a Flask app that has our trained chatbot which gets the response using AJAX. Hence, the output looks something like this.

For full implementation of the code with styling and containerization with docker is available at my GitHub repo.

Conclusion

In this article, we went through every single step of creating a chatbot from scratch that is from building the dataset to training it and then connecting it with a web service like Flask with asynchronous calls (AJAX). We really went from the ground up. This type of Chatbots can be used by many online businesses to support and improve customer service and to make communication between users seamless. Will see you in my next article until then code learn repeat …….

--

--