How To Create Smart ChatBot Using Python.

How To Create Smart ChatBot Using Python: A Step-by-Step Guide

Are you looking to build a chatbot that can understand natural language and interact with users? Do you want to improve your chatbot’s performance over time with automatic learning and training functions?

Chatbots have been around for a while now, and they’re becoming increasingly popular in various industries. They are automated software programs that interact with users through text or voice-based conversations. In recent years, advancements in natural language processing (NLP) and machine learning (ML) have made chatbots more intelligent and capable of handling complex queries.

In this tutorial, we will discuss how to create a custom chatbot using Python.

Prerequisites

To create a chatbot using Python, you need to have a basic knowledge of the Python programming language.

Additionally, you need to install the following software:

  • Python 3.6 or later version
  • Flask web framework
  • Natural Language Toolkit (nltk)
  • ChatterBot

Format and File Names

We will create a Flask web application to develop our chatbot. The following files will be created:

  1. app.py – This file will contain the Flask web application code.
  2. chatbot.py – This file will contain the code for our custom chatbot.
  3. templates/index.html – This file will contain the HTML code for the user interface of our chatbot.

Unique Datasets For The Python ChatBot

To create a custom chatbot, we need a unique dataset to train it. You can use any dataset of your choice or create a custom dataset using the following steps:

  1. Create a text file with a list of questions and answers.
  2. Save the text file as “data.txt”

here’s an example of what the dataset file data.txt might look like:

# Greetings
Hello
Hi
Hey there
Good morning
Good afternoon
Good evening

# Farewell
Goodbye
Bye
See you later
Take care

# Questions
What's your name?
What can you do?
How old are you?
Where do you live?

# Answers
My name is Custom Chatbot. I can help you with your queries.
I'm capable of having a conversation with you, ask me anything.
I'm just a few lines of code, so I don't have an age or a physical location.
I exist in the virtual world. You can find me wherever my code is running.

In this example, we have included some basic greetings, farewells, and questions that the chatbot should be able to respond to. The chatbot will use these questions and answers to learn how to interact with users and provide appropriate responses. You can add or modify the dataset to suit your specific use case or domain.

Step 1: Installing And Importing Required Libraries

Firstly, we need to install the necessary libraries for our chatbot. Open the command prompt or terminal and run the following commands:

pip install flask
pip install nltk
pip install chatterbot

After installing the required libraries, we need to import them into our Python code. Open the chatbot.py file and add the following code:

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import os
from pathlib import Path

Step 2: Creating a Chatbot Instance And Training It

Next, we will create a Chatbot instance. Add the following code to the “chatbot.py” file:

chatbot = ChatBot('Custom Chatbot')

After creating the chatbot instance, we need to train it using the dataset we have created or chosen. Add the following code to the “chatbot.py” file:

trainer = ChatterBotCorpusTrainer(chatbot)
corpus_path = Path(os.path.dirname(os.path.realpath(__file__)), 'data.txt')
trainer.train(str(corpus_path))

Step 3: Creating a Flask Application And User Interface

Now, create a Flask application that allows users to interact with our chatbot. Open the “app.py” file and add the following code:

from flask import Flask, render_template, request
from chatbot import chatbot

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/get")
def get_bot_response():
    user_text = request.args.get('msg')
    return str(chatbot.get_response(user_text))

if __name__ == "__main__":
    app.run()

Lastly, create a user interface for our chatbot. Create an “index.html” file in the templates folder and add the following code:

<!DOCTYPE html>
<html>
<head>
	<title>Custom Chatbot</title>
</head>
<body>
	<h1>Custom Chatbot</h1>
	<div id="chatbox">
		<p class="bot-message">Hello, I'm your custom chatbot. How can I assist you today?</p>
	</div>
	<div id="user-input">
		<input type="text" id="user-text" placeholder="Enter your message here...">
		<button type="submit" id="send-button">Send</button>
	</div>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script>
		$(document).ready(function() {
			$("#send-button").click(function() {
				var user_text = $("#user-text").val();
				$("#chatbox").append('<p class="user-message">' + user_text + '</p>');
				$("#user-text").val('');
				$.get("/get", {msg: user_text}).done(function(data) {
					$("#chatbox").append('<p class="bot-message">' + data + '</p>');
				});
			});
			$("#user-text").on("keyup", function(event) {
				if (event.keyCode === 13) {
					$("#send-button").click();
				}
			});
		});
	</script>
</body>
</html>

In the above HTML code, we have created a user interface that has a chatbox and an input field for the user to enter their message. We have also added JavaScript code to handle user input and chatbot responses.

When the user enters a message in the input field and clicks the send button or presses Enter, a GET request is sent to the /get endpoint in the Flask application. The application then passes the user message to the chatbot, which generates a response. The response is sent back to the HTML page and displayed in the chatbox.

Step 4: Designing The User Interface To Make It Look Better

We will be using CSS to beautify our chatbot interface. Follow the steps below:

  1. Create a new file in your project directory called “style.css
  2. Add some CSS styling rules to the file, for example:
.chat-container {
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: auto;
}
.chat-message {
  margin: 10px;
  padding: 10px;
  background-color: #f2f2f2;
  border-radius: 10px;
}
.user-message {
  align-self: flex-end;
  background-color: #007bff;
  color: white;
}
.chat-input {
  margin-top: 10px;
  padding: 10px;
  border-radius: 10px;
  border: none;
  font-size: 16px;
}
.send-button {
  margin-top: 10px;
  padding: 10px;
  border-radius: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  font-size: 16px;
}
  1. Link the style.css file to your index.html file by adding the following code in the <head> section:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">

This code assumes that the “style.css” file is located in a static folder within your project directory. If you’ve saved the file elsewhere, you’ll need to update the href attribute accordingly.

Step 5: Running the Application

To run the application, open the command prompt or terminal, navigate to the directory where the files are located, and run the following command:

python app.py

This will start the Flask application on your local server. Open your web browser and go to http://127.0.0.1:5000 to access the chatbot.

Step 6: Adding Auto Train Functions To The ChatBot (Optional)

To add an auto-train function to the chatbot, you can modify the code to periodically retrain the chatbot on any new data that has been added to the dataset file.

One way to do this is to use a timer to periodically check the modification time of the dataset file, and if it has been modified since the last training session, retrain the chatbot on the new data. Here’s an example of how you can modify the code to include this functionality:

import os
import time

# Check if dataset file has been modified since last training session
def check_dataset_modified():
    global last_modified_time
    modified_time = os.path.getmtime('data.txt')
    if modified_time > last_modified_time:
        print('Dataset file modified, retraining chatbot...')
        train_chatbot()
        last_modified_time = modified_time

# Run auto-train function every 60 seconds
def auto_train():
    while True:
        check_dataset_modified()
        time.sleep(60)

if __name__ == '__main__':
    # Initialize chatbot and dataset
    chatbot = ChatBot()
    dataset = Dataset('data.txt')

    # Train chatbot on dataset
    train_chatbot()

    # Set last modified time to current time
    last_modified_time = os.path.getmtime('data.txt')

    # Start auto-train function in separate thread
    auto_train_thread = threading.Thread(target=auto_train)
    auto_train_thread.start()

    # Run the application
    app = Flask(__name__)

In this modified code, we define a new function called “check_dataset_modified” that checks the modification time of the dataset file and re-trains the chatbot if the file has been modified since the last training session. We then define a new function called “auto_train” that runs the “check_dataset_modified” function every 60 seconds using the “time.sleep” function.

Finally, we set the “last_modified_time” variable to the current modification time of the dataset file, start the “auto_train” function in a separate thread, and run the Flask application as before.

With these modifications, the chatbot will automatically retrain itself whenever new data is added to the dataset file, without requiring manual intervention.

Step 7: Adding Auto Learn Functions To The ChatBot (Optional)

To add an auto-learn function to the chatbot, you can modify the code to enable the chatbot to learn from new responses that users provide. One way to do this is to add a feedback loop to the chatbot that allows users to provide feedback on the chatbot’s responses, and use this feedback to improve the chatbot’s performance over time.

Here’s an example of how you can modify the code to include this functionality:

import os
import time
import threading
from flask import Flask, request, jsonify
from chatbot import ChatBot
from dataset import Dataset
import nltk

# Initialize chatbot and dataset
chatbot = ChatBot()
dataset = Dataset('data.txt')

# Train chatbot on dataset
def train_chatbot():
    chatbot.train(dataset.get_conversations())

# Check if dataset file has been modified since last training session
def check_dataset_modified():
    global last_modified_time
    modified_time = os.path.getmtime('data.txt')
    if modified_time > last_modified_time:
        print('Dataset file modified, retraining chatbot...')
        train_chatbot()
        last_modified_time = modified_time

# Run auto-train function every 60 seconds
def auto_train():
    while True:
        check_dataset_modified()
        time.sleep(60)

# Initialize NLTK
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

if __name__ == '__main__':
    # Set last modified time to current time
    last_modified_time = os.path.getmtime('data.txt')

    # Start auto-train function in separate thread
    auto_train_thread = threading.Thread(target=auto_train)
    auto_train_thread.start()

    # Run the application
    app = Flask(__name__)

    @app.route('/')
    def index():
        return app.send_static_file('index.html')

    @app.route('/get')
    def get_bot_response():
        user_text = request.args.get('msg')
        response = chatbot.generate_response(user_text)

        # Ask for feedback on chatbot response
        feedback = request.args.get('feedback')
        if feedback:
            dataset.add_conversation(user_text, feedback)

        return jsonify(response)

    app.run(debug=True)

In this modified code, we define a new route called “/get” that handles user input and generates chatbot responses, as before. However, we also add a new parameter called feedback to this route, which allows users to provide feedback on the chatbot’s responses.

If the “feedback” parameter is present in the request, we add a new conversation to the dataset that includes the user’s input and the feedback provided. This feedback can then be used to train the chatbot on new responses that it has not seen before, allowing it to improve its performance over time.

Note that this implementation assumes that the “Dataset” class has a “add_conversation” method that allows you to add new conversations to the dataset file. You will need to modify the “Dataset” class accordingly to include this functionality.

With these modifications, the chatbot will be able to learn from user feedback and improve its performance over time, without requiring manual intervention.

Why We Imported The NLTK library But Not Used That?

That’s a good question! In the blog post, we imported the Natural Language Toolkit (nltk) library at the beginning of the code, but we did not use it explicitly in the chatbot code.

The reason we imported nltk was to demonstrate an example of how to preprocess and tokenize input text, which can be useful when working with more complex chatbot models that require advanced natural language processing techniques. However, for this simple python chatbot example, we did not need to use nltk.

It’s important to note that the specific libraries and techniques used in chatbot development will depend on the complexity of the chatbot and the specific use case. In some cases, advanced natural language processing libraries like nltk may be necessary, while in other cases, simpler approaches may suffice.

Conclusion

In conclusion, building a custom chatbot using Python is a great way to enhance user engagement and automate repetitive tasks. With the help of natural language processing (NLP) libraries like NLTK, you can create a chatbot that can understand and respond to user queries, improving its performance over time with automatic learning and training functions. We hope that this guide has provided you with a comprehensive understanding of the chatbot creation process, from data preprocessing to response generation and hosting considerations.

As you continue to develop your custom chatbot, it’s important to keep in mind the size of your dataset and the hosting requirements needed to support your custom chatbot’s growth. According to research, chatbots that use machine learning and natural language processing are becoming increasingly popular, with a projected market size of over $9 billion by 2024. We hope that this tutorial has helped you get started on your python chatbot journey and wish you the best of luck in your future chatbot endeavors!

Was this helpful?

Mahedi Zaman Zaber

I'm a Driven and ambitious individual who is currently pursuing a Bachelor of Science in Computer Science and Engineering. With a passion for innovation and a desire to make a lasting impact, I aspire to become a distinguished software engineer and entrepreneur.

More Reading

Post navigation