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
  8. "Unlock the power of AI with chatbots! From boosting customer support to driving engagement, chatbots are transforming the way businesses connect. 🚀 Explore how they streamline tasks and create seamless user experiences.

    ReplyDelete
  9. Looking for reliable garage door repair in Austin? FixingOTX offers expert service, fast response times, and affordable solutions for all your garage door needs. fixngotx garage door repair Austin Whether it’s a broken spring, malfunctioning opener, or door off track, our experienced team ensures your garage door works smoothly and safely. Call today!

    ReplyDelete

Post a Comment

Popular posts from this blog

Generative AI with LangChain: Basics

  Wishing everyone a Happy New Year '24😇 I trust that you've found valuable insights in my previous blog posts. Embarking on a new learning adventure with this latest post, we'll delve into the realm of Generative AI applications using LangChain💪. This article will initially cover the basics of Language Models and LangChain. Subsequent posts will guide you through hands-on experiences with various Generative AI use cases using LangChain. Let's kick off by exploring the essential fundamentals💁 What is a Large Language Model (LLM)? A large language model denotes a category of artificial intelligence (AI) models that undergo extensive training with extensive textual data to comprehend and produce language resembling human expression🙇. Such a large language model constitutes a scaled-up transformer model, often too extensive for execution on a single computer. Consequently, it is commonly deployed as a service accessible through an API or web interface. These models are...

How can I become a TPU expert?

Another post starts with you beautiful people! I have two good news for all of you! First good news is that Tensorflow has released it's new version (TF 2.1) which is focused on TPUs and the most interesting thing about this release is that it now also supports Keras high level API. And second wonderful news is to help us get started Kaggle has launched a TPU Playground Challenge . This means there is no any way to stop you learning & using TPUs. In this post I am going to share you how to configure and use TPUs while solving a image classification problem. What are TPUs? You must have heard about TPU while using  Google Colab . Now Kaggle also supports this hardware accelerator. TPUs or Tensor Processing Units are hardware accelerators specialized in deep learning tasks. They were created by Google and have been behind many cutting edge results in machine learning research. Kaggle Notebooks are configured with TPU v3-8s, which is a specialized hardware with...

LightGBM and Kaggle's Mercari Price Suggestion Challenge

Another post starts with you beautiful people! I hope you have enjoyed and must learnt something from previous two posts about real world machine learning problems in Kaggle. As I said earlier Kaggle is a great platform to apply your machine learning skills and enhance your knowledge; today I will share again my learning from there with all of you! In this post we will work upon an online machine learning competition where we need to predict the the price of products for Japan’s biggest community-powered shopping app. The main attraction of this challenge is that this is a Kernels-only competition; it means the datasets are given for downloading only in stage 1.In next final stage it will be available only in Kernels. What kind of problem is this? Since our goal is to predict the price (which is a number), it will be a regression problem. Data: You can see the datasets  here Exploring the datasets: The datasets provided are in the zip format of 'tsv'. So how can ...