Coders Packet

Prototype of a Banking Chatbot using ChatterBot library - Python

By C Koushik

A prototype of banking chatbot implemented in python using ChatterBot library, flask web framework, and SQLStorageAdapter.

Chatbots are computer programmed conversational agents that try to mimic written or spoken words for interacting with the user via a chat box interface. They find wide applications in today's software applications, sometimes powered by AI to deliver great customer services by providing support in various domains to satisfy consumers. Especially in banking sectors, powerful chatbots can facilitate two-way communications by reducing manual human interactions to offer financial services and help.

This project is a prototype or a concept of such a chatbot. I built this banking chatbot using the ChatterBot python library which is specifically designed to generate chatbots. ChatterBot uses inbuilt ML algorithms to respond to different user queries. ChatterBot provides simple APIs to create chatbots that can automate conversations with users.

I trained the chatbot using pre-existing data for some known queries and I finally run the same on the web using flask with HTML and CSS frontend. The conversations with the bot are saved in a separate folder for future iterations when the chatbot can eventually learn to respond in certain situations.

A stepwise implementation of the chatbot prototype is as follows:

1) Installing necessary packages

pip install Flask==0.12.3
pip install chatterbot==0.8.4
pip install SQLAlchemy==1.1.11

2) Importing libraries

from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

3) Training the chatbot

banking_bot = ChatBot('BankBot')
banking_bot.set_trainer(ListTrainer)
for file in os.listdir('data'):
        print('Training using '+file)
        convData = open('data/' + file).readlines()
        banking_bot.train(convData)
        print("Training completed for "+file)

The ChatterBot class comes with an inbuilt train() instance that is initialized with my chatbot name. The train() accepts a list of strings to be trained which is fed from the data folder containing known user queries as lists using the ListTrainer object of the training class.

4) Setting Storage and Logic Adapters

Storage Adapters allow the chatbot to connect to the specified type of storage.

The SQLStorageAdapter by default uses sqlite database file to store conversation data supported by SQLAlchemy.

Logic Adapters determine how the chatbot responds to an input string.

The BestMatch adapter responds based on known responses that matches closest to the input string.

banking_bot = ChatBot('BankBot',
             storage_adapter='chatterbot.storage.SQLStorageAdapter',
             logic_adapters=[
   {
       'import_path': 'chatterbot.logic.BestMatch'
   },
   
],
trainer='chatterbot.trainers.ListTrainer')
 
 

5) Getting Response

get_response() method is used to get responses based on the input string and they are finally saved in a separate folder.

def get_bot_response():
    userText = request.args.get('msg')
    response = str(banking_bot.get_response(userText))

    appendfile=os.listdir('saved_conversations')[-1]
    appendfile= open('saved_conversations/'+str(filenumber),"a")
    appendfile.write('user : '+userText+'\n')
    appendfile.write('bot : '+response+'\n')
    appendfile.close()

    return response

6) Implementation

To run the project, open command prompt, install the necessary packages as listed in step 1. Run train.py followed by run.py

 

Download Complete Code

Comments

No comments yet