Skip to main content

How can I make a simple ChatBot?


Another post starts with you beautiful people!
It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem.
So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot?
Quite interesting right!

Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this?
And like always Python has helped me this time also. There is a Python library available with name as ChatterBot which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another sources.

For understanding process flow of a chat bot please see this link process flow of chatterbot

I have installed the chatterbot library from the official PyPi as pip install chatterbot
Let's start our journey by importing this library in our jupyter notebook first-

Next, we need to create a new instance of the ChatBot class as below-
Here you can give any name to your bot as for example I have given name 'Moto' to my chat bot.

Let's starts with greetings to check if our chatbot understands the basics greetings from the user input or not. For this we don't need to write a code from scratch. ChatterBot includes tools that help simplify the process of training a chat bot instance.We just need to import required built-in trainer class like below-

ListTrainer Allows a chat bot to be trained using a list of strings where the list represents a conversation. So let's define our conversation list which will establish each item in the list as a possible response to it’s predecessor in the list as below-

Now to use 'ListTrainer' training class we must pass it to the set_trainer() method before calling train() as below-
Once you run the above lines you will see a progress bar showing training progress of the bot. Make sure if you make any changes to the conversation list then you need to rerun these lines otherwise your changes will not reflect.
Let's check how our bot responses on some of our greeting inputs-

If you notice the first input I have given; this input is not listed in our conversation list yet our bot is able to give a right response. This is the beauty of a real machine learning conversational dialog engine.

Now what about if you don't want to use a conversation list; instead you want to use a corpus data. No any hack for this; ChatterBot comes with a corpus data and utility module that makes it easy to quickly train our bot to communicate. To do so, simply specify the corpus data modules you want to use as below-

Let's create a new chat bot for this example, set the trainer as Corpus trainer instead of List trainer and train our bot for English corpus as below-

See here the long training list of the corpus English language having by the corpus trainer class. It contains most of the basics conversations we see in computers, emotions, gossips, greetings, humor etc. Since we have trained the all corpus of English; it is training our bot for all. If you want to train some specific just add that one in the train() like below-

Let's check our new bot responses on some of inputs:-

Quite amazing right :) No any coding and your bot is able to give proper response.

Now our bot is ready to handle greetings, conversations, humor, gossip etc. Let's move forward and make our bot ready to handle real world conversations. What about Twitter data or Whatsapp data or Facebook data? Sounds interesting and complex too? No worry ChatterBot is already there to make our way smooth.

Let's see how can we train our bot with data from the Twitter API. Remember whenever you are going to work with any third party api you will need api credentials to interact with those apis. And in this example also we need access key, token, consumer key etc from the Twitter API.

Our first step will be to get api key,token,access key and token and for getting this you need to create an account in twitter first then you need to create an app using the same account in twitter developer account

I have also created a test twitter account and an app for this tutorial and now I have the api key, api token, access key and access token. Now see how we can use twitter data to train our bot as below-

Let's understand new inputs we are giving here:

  1. input_adapterto allow a chat bot to have a versatile method of receiving or retrieving input from a given source. In our example the output terminal adapter allows a user to type into their terminal to communicate with the chat bot.
  2. output_adapter: a generic class that is delivering a response to an API endpoint.
  3. logic_adapter: determine the logic for how ChatterBot selects a response to a given input statement.In our example adapter returns a specified default response if a response can not be determined with a high amount of confidence.
Once you run the above code, you will see following training process:-

In the above response, our bot is trained with twitter data. In the same way you can train your bot with any third party api. Training is an important part which make your bot more accurate and reliable.

Behind the seen chatterbot library uses Naive Bayes Classifier algorithm to determine if an input statement meets a particular set of criteria that warrant a response to be generated from that logic adapter.

Let's move ahead and see how can I use my bot in web? For deploying your bot to any web app Python provides us two web frameworks- Django and Flask. In this post I am going to share you integration of your bot with Django.

If you are new to Django as I was, please refer this great link build-your-first-python-and-django-application . Believe me a Data Scientist with Django skill add-on will definitely attract the recruiters. So just have a look and learn the basics of Django framework before deploying your chat bot to this web framework.

Once you learn about the Django basics, create a Django project and import that in your favourite IDE. I have created and imported a sample application in my Django 2.0.6 + MySql 5.7 and it looks like as below-

If you don't want to create the app by your own and just want a working example of this then don't worry. Just copy the example project from this link working example and import in pycharm ide.The structure will look like same as above screen shot.

Now once you setup the project open the settings,py file and search for the INSTALLED_APP. Here you need to add your app name like below-

Next, you need to add your chatbot related settings (trainer, adapter etc) in the same file as below-

Next, define the root url and wsgi parameters of your app as below-

After this you need to define your database related information like below-

In this example we are using sqlite3 db, you can change it according to your database.

Once you done all above settings, it's time to build and deploy your Django app with your bot. For this open your Anaconda Prompt and go to the path where your django app project is. In my setup It is like below-

Next, run following command to install required libraries-

After this you need to run following two commands one by one-


Now it's time to train our bot with simple greetings as below-

Next start your server by running this command-

Once server is tarted, hit the url http://127.0.0.1:8000/ and you will see following screen-

It means you have successfully setup, build and deploy Django app with your chat bot :)

That's it my friends! In this post you have learnt What is a chatbot? How can you create your first chatbot with inbuilt Python library? How can you integrate your bot with a web framework?
As a data scientist if you have a knowledge of such integrations then you are going to make your own way in this IT world. There are lots of pending items waiting for you to do some hands on.
So import the project example, try to extend the built in functionalities and make your bot stronger day by day.
In my next post I will share you how can you use Google's DialogFlow to built a bot.

With that said my friends- Go chase your dreams, have an awesome day, make every second count and see you later in my next post.







Comments

  1. Pinwords is a really basic tool, but it’s great if you want to quickly make an image from a quote and add it to your Pinterest account. Pinstamatic actually lets you

    create a whole bunch of items to add to your Pinterest boards including website snapshots, pins of Twitter profiles and Spotify tracks that you can pin.

    ReplyDelete
  2. We have to thank and share such a nice blog post and interesting information. It is helpful for develop my knowledge, people always searching for this type of posts.
    Chatbot Developers in Dubai
    Facebook Chatbot in Dubai
    AI Chatbot in Dubai
    Artificial intelligence Company in Dubai

    ReplyDelete
  3. Thanks! to Chatlayer for sharing this great information with us.
    https://www.sinch.com/en-in/products/customer-engagement/chatlayer/

    ReplyDelete
  4. Really Great post sharing.useful to this articles.
    User Interaction: Chatbots are designed to engage with users in a conversational manner. Python provides a robust platform for creating interactive conversational experiences. it's a
    useful to user interaction.

    ReplyDelete
  5. Data Science Expert
    Welcome to Analytic Square, where we tailor courses to meet industry demands. Our focus? Short, targeted programs to boost your skills. From data analysis to digital marketing, we've got you covered. Let's elevate your expertise together!

    ReplyDelete
  6. Greps AI excels in delivering Greps AI Innovative AI Solutions, focusing on digital marketing, UI/UX design, and chatbot development. Our expertise in leveraging advanced AI technologies ensures businesses achieve enhanced customer engagement and operational efficiency. With a commitment to innovation and quality, Greps AI is dedicated to driving transformative outcomes through tailored AI solutions tailored to meet diverse business needs.

    ReplyDelete
  7. Excellent post which I'm sharing.beneficial to this article.
    Australian AI Solutions Company

    ReplyDelete

Post a Comment

Popular posts from this blog

How to install and compile YOLO v4 with GPU enable settings in Windows 10?

Another post starts with you beautiful people! Last year I had shared a post about  installing and compiling Darknet YOLOv3   in your Windows machine and also how to detect an object using  YOLOv3 with Keras . This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my  previous post  by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce GTX 1660 Ti Version 445.87

How to use opencv-python with Darknet's YOLOv4?

Another post starts with you beautiful people 😊 Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one- Setup Darknet's YOLOv4 Train custom dataset with YOLOv4 Create production-ready API of YOLOv4 model Create a web app for your YOLOv4 model Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome computer vision and machine learning software library-  OpenCV  and of course with Python 🐍. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command- pip install opencv-python --up