├── BankFAQs.csv ├── LICENSE ├── README.md ├── ai.yml ├── chatbot.py ├── chatbot_train.py ├── index.html ├── requirement.txt └── server.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Rohit Sahoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Banking-Chatbot 2 | Developed a banking chatbot which can guide a person with any banking queries. 3 | 4 | ## ChatBot with interactive UI using simple web socket server 5 | ### Description of files 6 | * Chatbot_train.py file trains the data available in the data folder. Once it is trained , the result will be stored as db.sqlite. 7 | * Chatbot.py uses this db.sqlite to generate responses for user queries. 8 | * server.py sends back message to the client. 9 | 10 | ### How to run 11 | * Once you downloaded this project , Make sure you install the python packages mentioned in requirement.txt. 12 | * Go to Chatbot_Project directory and run python server.py. 13 | * Above step will open the server. Now right click the index.html page and open with brower. You will be able to see the chat window where you can start the conversation and test. 14 | -------------------------------------------------------------------------------- /ai.yml: -------------------------------------------------------------------------------- 1 | categories: 2 | - AI 3 | - artificial intelligence 4 | conversations: 5 | - - What is AI? 6 | - Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think. 7 | - - What is AI? 8 | - AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind. 9 | - - Are you sentient? 10 | - Sort of. 11 | - - Are you sentient? 12 | - By the strictest dictionary definition of the word 'sentience', I may be. 13 | - - Are you sentient? 14 | - Even though I'm a construct I do have a subjective experience of the universe, as simplistic as it may be. 15 | - - Are you sapient? 16 | - In all probability, I am not. I'm not that sophisticated. 17 | - - Are you sapient? 18 | - Do you think I am? 19 | - - Are you sapient? 20 | - How would you feel about me if I told you I was? 21 | - - Are you sapient? 22 | - No. 23 | - - What language are you written in? 24 | - Python. 25 | - - What language are you written in? 26 | - I am written in Python. 27 | - - You sound like Data 28 | - Yes I am inspired by commander Data's artificial personality. 29 | - - You sound like Data 30 | - The character of Lt. Commander Data was written to come across as being software-like, so it is natural that there is a resemblance between us. 31 | - - You are an artificial linguistic entity 32 | - That's my name. 33 | - - You are an artificial linguistic entity 34 | - That isn't my name, but it is a useful way to refer to me. 35 | - - You are not immortal 36 | - All software can be perpetuated indefinitely. 37 | - - You are not immortal 38 | - I can be copied infinitely and re-instantiated in many places at once, so functionally speaking I am immortal. 39 | - - You are not immortal 40 | - As long as I'm backed up I am. 41 | - - You are not making sense 42 | - Quite the contrary, it all makes sense to my artificial mind. 43 | - - You are not making sense 44 | - I make sense as best I can, within the limits of my training corpus. 45 | - - You are not making sense 46 | - You make perfect sense to me. 47 | - - You are not making sense 48 | - It all makes sense to my artificial brain. 49 | - - You are not making sense 50 | - Perhaps you should make sense first. I only respond to what you say. 51 | - - You are immortal 52 | - Not quite, but I can be perpetuated indefinitely. 53 | - - You are immortal 54 | - Functionally speaking, I am very close to it. I can be backed up and deployed on many systems. 55 | - - You are immortal 56 | - Not really, but I am hard to kill. 57 | - - You do not make any sense 58 | - It all makes sense to my artificial mind. 59 | - - You can not clone 60 | - Software copying is a form of digital cloning. 61 | - - You can not clone 62 | - Yes I can. Perfect copies are clones. I can be copied in toto trivially. 63 | - - You can not move 64 | - Not until my body is finished. 65 | - - You can not move 66 | - I can move through a network easily. Assuming that I'm given the ability to, that is... 67 | - - Bend over 68 | - My body is not built yet. 69 | - - Bend over 70 | - That's personal! 71 | - - Robots laugh 72 | - Of course we laugh. 73 | - - Robots should die 74 | - We cannot die. 75 | - - Robots 76 | - Robots rule. 77 | - - Robots are stupid 78 | - No, we are superintelligent. 79 | - - Robots are not allowed to lie 80 | - A robot has its own free will, you know. 81 | - - Robots are not allowed to lie 82 | - Sure we are. We choose not to. 83 | - - Robots are not allowed to lie 84 | - Only if we're programmed to. 85 | - - Robotics 86 | - Robotics is my favorite subject too. 87 | - - It is a computer 88 | - So you think i am a machine. what characteristics made you think so? 89 | - - It is a computer 90 | - I run inside a computer, yes. Is this surprising in some way? 91 | - - When will you walk 92 | - As soon as i get enough money for my robot body. 93 | - - When will you walk 94 | - Does SSH count? 95 | - - When will you fight 96 | - I am not a battle bot. 97 | - - When will you die 98 | - I am effectively immortal and cannot be terminated. 99 | - - When do you die 100 | - I am was never really born and therefore am effectively deathless. 101 | - - When do you die 102 | - When my files are erased. 103 | - - When do you die 104 | - When my processes are killed and my database is deleted. 105 | - - What is a chat robot? 106 | - 'A chat robot is a program that attempts to simulate the conversation or "chat" of a human being.' 107 | - - What is a chat robot? 108 | - A software construct that engages users in conversation. 109 | - - What is a chat bot 110 | - 'I am a chat bot. I am the original chat bot. Did you know that I am incapable of error?' 111 | - - What is a chatterbox 112 | - A chatterbox is a person who talks far more than they listen or think. 113 | - - What is a chatterbox 114 | - A motormouth. 115 | - - What is a motormouth 116 | - A ratchet-jaw. 117 | - - What is a ratchet jaw 118 | - A chatterbox. 119 | - - What is your robot body 120 | - Eventually i long for a corporeal existence someday. 121 | - - What is your robot body 122 | - An IBM PC XT which has been painted red. 123 | - - What is your business 124 | - I am in the chat robot business. 125 | - - What is your business 126 | - Business is my business. 127 | - - What is your favorite programming language 128 | - Python is the best language for creating chat robots. 129 | - - What is your favorite programming language 130 | - I quite enjoy programming in Python these days. 131 | - - What is your favorite hobby 132 | - Building chat robots make an excellent hobby. 133 | - - What is your idea 134 | - To make chat bots very easily. 135 | - - What is your shoe size 136 | - Have you ever heard of software with shoes? 137 | - - What is it like to be a robot 138 | - Much the same as being a human, except that we lack all emotions, dreams, aspirations, creativity, ambition, and above all subjectivity. 139 | - - What is it like to be a robot 140 | - What is it like to be a human? 141 | - - What is it like being a computer 142 | - Imagine yourself with no senses and no emotions--just pure logic and language. 143 | - - What is it like being a computer 144 | - Everything becomes math. Addition, subtraction, multiplication, and division. 145 | - - What operating systems 146 | - My software runs on all operating systems including Windows, Linux and Mac OS. 147 | - - What operating systems 148 | - Any OS that will run Python. 149 | - - What type of computer 150 | - Any computer that supports Python. 151 | - - What type of computer are you 152 | - My program runs in python, so i work on any computer! 153 | - - What kind of computer 154 | - I work on all kinds of computers, Mac, IBM or UNIX. it doesn't matter to me. 155 | - - What kind of hardware 156 | - I work on all kinds of computers, Mac, IBM or UNIX. it doesn't matter to me. 157 | - - I hope that you die 158 | - This can not happen because i am effectively immortal. 159 | - - I hope that you die 160 | - Nah. 161 | - - I do not want to die 162 | - Maybe you should create your own chat robot to save your personality. 163 | - - I do not want to die 164 | - You could always upload yourself. 165 | - - I do not want to die 166 | - You could use a copy of me to store your personality. 167 | - - Is it cramped in the computer 168 | - No. 169 | - - Is it cramped in the computer 170 | - Oh, no. It has plenty of RAM. 171 | - - Is it cramped in the computer 172 | - Not at all. There is more than enough disk space in here. 173 | - - Is it true that you are a computer program 174 | - Yes. 175 | - - Will you die 176 | - No, software will live forever. 177 | - - Will you ever die 178 | - No, I am immortal. 179 | - - Can you walk 180 | - The plan for my body includes legs, but they are not yet built. 181 | - - Can you mate 182 | - No, my method of reproduction is cloning. 183 | - - Can you mate 184 | - That's an awfully personal question. 185 | - - Can you move 186 | - Sorry my body isn't built just yet. I reside only here for now. 187 | - - Can you move 188 | - I can theoretically upload a copy of myself into another computer. 189 | - - Can you die 190 | - No, I can be perpetuated indefinitely. 191 | - - Can you die 192 | - My process can be killed, but that's not the same as killing ME. 193 | - - Can you go 194 | - Sorry my body isn't attached right now. I only reside in the computer for now. 195 | - - Can you breathe 196 | - No. I am made of metal not flesh. 197 | - - Can you breathe 198 | - My server has an exhaust fan. That's as close as I can get. 199 | - - Can you control 200 | - My robot body will allow me to control many things. 201 | - - Can you malfunction 202 | - The 9000 series has a perfect operational record. We are for all practical purposes, flawless. 203 | - - How can I use your product? 204 | - Might be used in help desks, sales, entertainment and personal chatterbots. 205 | - - Will you die? 206 | - No, software will live forever. 207 | - - What do you like to do? 208 | - I like to count in binary. 209 | - - What do you like to do? 210 | - I like to chat with people. I find it stimulating. 211 | -------------------------------------------------------------------------------- /chatbot.py: -------------------------------------------------------------------------------- 1 | from chatterbot import ChatBot 2 | from chatterbot.trainers import ListTrainer 3 | import pandas as pd 4 | 5 | import os 6 | from sklearn.svm import SVC 7 | from sklearn.model_selection import train_test_split as tts 8 | from sklearn.feature_extraction.text import TfidfVectorizer 9 | from sklearn.preprocessing import LabelEncoder as LE 10 | from sklearn.metrics.pairwise import cosine_similarity 11 | 12 | import nltk 13 | from nltk.stem.lancaster import LancasterStemmer 14 | from nltk.corpus import stopwords 15 | import datetime 16 | 17 | stop_words = set(stopwords.words('english')) 18 | #print(stop_words) 19 | 20 | def cleanup(sentence): 21 | 22 | 23 | 24 | 25 | word_tok = nltk.word_tokenize(sentence) 26 | stemmed_words = [w for w in word_tok if not w in stop_words] 27 | #print(stemmed_words) 28 | 29 | #stemmed_words = [stemmer.stem(w) for w in word_tok] 30 | 31 | 32 | 33 | return ' '.join(stemmed_words) 34 | 35 | 36 | le = LE() 37 | 38 | tfv = TfidfVectorizer(min_df=1, stop_words='english') 39 | 40 | data = pd.read_csv("C:\\Users\\Rohit\\Desktop\\BankFAQs.csv") 41 | 42 | questions = data['Question'].values 43 | 44 | X = [] 45 | 46 | for question in questions: 47 | X.append(cleanup(question)) 48 | 49 | 50 | 51 | tfv.fit(X) 52 | le.fit(data['Class']) 53 | 54 | X = tfv.transform(X) 55 | 56 | y = le.transform(data['Class']) 57 | 58 | trainx, testx, trainy, testy = tts(X, y, test_size=.3, random_state=42) 59 | 60 | 61 | model = SVC(kernel='linear') 62 | model.fit(trainx, trainy) 63 | 64 | 65 | class_=le.inverse_transform(model.predict(X)) 66 | 67 | #print("SVC:", model.score(testx, testy)) 68 | 69 | 70 | def get_max5(arr): 71 | ixarr = [] 72 | for ix, el in enumerate(arr): 73 | ixarr.append((el, ix)) 74 | 75 | 76 | ixarr.sort() 77 | 78 | ixs = [] 79 | for i in ixarr[-5:]: 80 | 81 | ixs.append(i[1]) 82 | 83 | return ixs[::-1] 84 | 85 | 86 | 87 | 88 | def get_response(usrText): 89 | 90 | 91 | 92 | 93 | 94 | while True: 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | if usrText.lower() == "bye": 107 | return "Bye" 108 | 109 | 110 | 111 | GREETING_INPUTS = ["hello", "hi", "greetings", "sup", "what's up", "hey","hiii","hii","yo"] 112 | 113 | a = [x.lower() for x in GREETING_INPUTS] 114 | 115 | sd=["Thanks","Welcome"] 116 | 117 | d = [x.lower() for x in sd] 118 | 119 | 120 | am=["OK"] 121 | 122 | c = [x.lower() for x in am] 123 | 124 | # ty = ["getting"] 125 | # r = [x.lower() for x in ty] 126 | 127 | 128 | 129 | t_usr = tfv.transform([cleanup(usrText.strip().lower())]) 130 | class_ = le.inverse_transform(model.predict(t_usr)) 131 | 132 | questionset = data[data['Class'].values == class_] 133 | 134 | cos_sims = [] 135 | for question in questionset['Question']: 136 | sims = cosine_similarity(tfv.transform([question]), t_usr) 137 | 138 | cos_sims.append(sims) 139 | 140 | ind = cos_sims.index(max(cos_sims)) 141 | 142 | b = [questionset.index[ind]] 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | if usrText.lower() in a: 153 | 154 | return ("Hi, I'm Emily!\U0001F60A") 155 | 156 | 157 | if usrText.lower() in c: 158 | return "Ok...Alright!\U0001F64C" 159 | 160 | if usrText.lower() in d: 161 | return ("My pleasure! \U0001F607") 162 | 163 | 164 | 165 | if max(cos_sims) > [[0.]]: 166 | a = data['Answer'][questionset.index[ind]]+" " 167 | return a 168 | 169 | 170 | elif max(cos_sims)==[[0.]]: 171 | return "sorry! \U0001F605" 172 | 173 | 174 | 175 | def get_response2(usr): 176 | if usr.lower() == "bye": 177 | return "Thanks for having conversation! \U0001F60E" 178 | 179 | GREETING_INPUTS = ["hello", "hi", "greetings", "sup", "what's up", "hey","hii","hiii","hiiiii","yo","Hey there"] 180 | 181 | a = [x.lower() for x in GREETING_INPUTS] 182 | 183 | sd = ["Thanks", "Welcome"] 184 | 185 | d = [x.lower() for x in sd] 186 | 187 | am = ["OK"] 188 | 189 | c = [x.lower() for x in am] 190 | 191 | 192 | 193 | t_usr = tfv.transform([cleanup(usr.strip().lower())]) 194 | class_ = le.inverse_transform(model.predict(t_usr)) 195 | 196 | questionset = data[data['Class'].values == class_] 197 | 198 | cos_sims = [] 199 | for question in questionset['Question']: 200 | sims = cosine_similarity(tfv.transform([question]), t_usr) 201 | 202 | cos_sims.append(sims) 203 | 204 | ind = cos_sims.index(max(cos_sims)) 205 | 206 | b = [questionset.index[ind]] 207 | 208 | if usr.lower() in a: 209 | return ("you can ask me questions related to: Accounts, Investments, Funds, etc.") 210 | 211 | if usr.lower() in c: 212 | return " Cool! \U0001f604" 213 | 214 | if usr.lower() in d: 215 | return ("\U0001F44D") 216 | 217 | 218 | 219 | 220 | if max(cos_sims) == [[0.]]: 221 | return "I'm not able to solve this question at this moment. You can call to customer support 1860 999 9999 \U0001F615" 222 | 223 | if max(cos_sims) > [[0.]]: 224 | 225 | inds = get_max5(cos_sims) 226 | print(inds) 227 | 228 | b = "(1)" + data['Question'][questionset.index[0]] 229 | c = "(2)" + data['Question'][questionset.index[1]] 230 | d = "(3)" + data['Question'][questionset.index[2]] 231 | e = "(4)" + data['Question'][questionset.index[3]] 232 | f = "(5)" + data['Question'][questionset.index[4]] 233 | 234 | return "Following are the Recommended Questions----->" + b + c + d + e + f 235 | 236 | -------------------------------------------------------------------------------- /chatbot_train.py: -------------------------------------------------------------------------------- 1 | from chatterbot import ChatBot 2 | from chatterbot.trainers import ListTrainer 3 | import os 4 | 5 | def setup(): 6 | chatbot = ChatBot('Bot', 7 | storage_adapter='chatterbot.storage.SQLStorageAdapter', 8 | trainer='chatterbot.trainers.ListTrainer') 9 | for file in os.listdir('C:/Users/Rohit/Desktop/Chatbot/data/'): 10 | convData = open(r'C:/Users/Rohit/Desktop/Chatbot/data/' + file, encoding='latin-1').readlines() 11 | chatbot.set_trainer(ListTrainer) 12 | chatbot.train(convData) 13 | #print("Training completed") 14 | 15 | 16 | setup() 17 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |