Build a Chatbot using Python/Flask
Chatbots are being made to ease the pain that the industries are facing today. The purpose of chat bots is to support and scale business teams in their relations with customers. It could live in any major chat applications like Facebook Messenger, Slack, Telegram, Text Messages, etc.
This article shows how to create a simple chatbot in Python & Flask using the ChatterBot library. Our bot will be used for small talk, as well as to answer some math questions. Here, we’ll scratch the surface of what’s possible in building custom chatbots and NLP in general.
pip install chatterbot
pip install chatterbot_corpus
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
We are creating a Flask app, to get started with Flask, you can visit Here
app = Flask(__name__)
#bot = ChatBot("Pikachu")
We can create and train the bot by creating an instance of ListTrainer and supplying it with the lists of strings:
trainer = ListTrainer(bot)
Getting started with the training part, there are different ways how we can train the bot, by this,
trainer.train(['What is your name?', 'My name is Pikachu'])
trainer.train(['How are you?', 'I am good' ])
trainer.train(['Bye?', 'Bye, see you later' ])
or, we can also train by this,
conversation = [
"Hello",
"Hello!!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]
trainer.train(conversation)
You can use your own or an existing corpus of data to train a bot. For example, you can use some corpus provided by chatterbot (inbuilt features):
corpus_trainer = ChatterBotCorpusTrainer(bot)
corpus_trainer.train('chatterbot.corpus.english')
The run() method of Flask class runs the application on the local development server.
@app.route("/")
def home():
return render_template("home.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
return str(bot.get_response(userText))
if __name__ == "__main__":
app.run()
Yay, our first model is ready, let's test our bot.
The above given Python script is executed from Python shell.
python app.py
A message in Python shell informs you that
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)