├── README.md ├── words.pkl ├── classes.pkl ├── chatbot_model.h5 ├── train_chatbot.py ├── intents.json └── gui_chatbot.py /README.md: -------------------------------------------------------------------------------- 1 | # ChatBot_FinalYear_Project -------------------------------------------------------------------------------- /words.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SubrataNandy2020/ChatBot_FinalYear_Project/HEAD/words.pkl -------------------------------------------------------------------------------- /classes.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SubrataNandy2020/ChatBot_FinalYear_Project/HEAD/classes.pkl -------------------------------------------------------------------------------- /chatbot_model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SubrataNandy2020/ChatBot_FinalYear_Project/HEAD/chatbot_model.h5 -------------------------------------------------------------------------------- /train_chatbot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras.models import Sequential 3 | from keras.layers import Dense, Activation, Dropout 4 | from keras.optimizers import SGD 5 | import random 6 | 7 | import nltk 8 | from nltk.stem import WordNetLemmatizer 9 | lemmatizer = WordNetLemmatizer() 10 | import json 11 | import pickle 12 | 13 | words=[] 14 | classes = [] 15 | documents = [] 16 | ignore_letters = ['!', '?', ',', '.'] 17 | intents_file = open('intents.json').read() 18 | intents = json.loads(intents_file) 19 | 20 | for intent in intents['intents']: 21 | for pattern in intent['patterns']: 22 | #tokenize each word 23 | word = nltk.word_tokenize(pattern) 24 | words.extend(word) 25 | #add documents in the corpus 26 | documents.append((word, intent['tag'])) 27 | # add to our classes list 28 | if intent['tag'] not in classes: 29 | classes.append(intent['tag']) 30 | print(documents) 31 | # lemmaztize and lower each word and remove duplicates 32 | words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_letters] 33 | words = sorted(list(set(words))) 34 | # sort classes 35 | classes = sorted(list(set(classes))) 36 | # documents = combination between patterns and intents 37 | print (len(documents), "documents") 38 | # classes = intents 39 | print (len(classes), "classes", classes) 40 | # words = all words, vocabulary 41 | print (len(words), "unique lemmatized words", words) 42 | 43 | pickle.dump(words,open('words.pkl','wb')) 44 | pickle.dump(classes,open('classes.pkl','wb')) 45 | 46 | # create our training data 47 | training = [] 48 | # create an empty array for our output 49 | output_empty = [0] * len(classes) 50 | # training set, bag of words for each sentence 51 | for doc in documents: 52 | # initialize our bag of words 53 | bag = [] 54 | # list of tokenized words for the pattern 55 | pattern_words = doc[0] 56 | # lemmatize each word - create base word, in attempt to represent related words 57 | pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words] 58 | # create our bag of words array with 1, if word match found in current pattern 59 | for word in words: 60 | bag.append(1) if word in pattern_words else bag.append(0) 61 | 62 | # output is a '0' for each tag and '1' for current tag (for each pattern) 63 | output_row = list(output_empty) 64 | output_row[classes.index(doc[1])] = 1 65 | 66 | training.append([bag, output_row]) 67 | # shuffle our features and turn into np.array 68 | random.shuffle(training) 69 | training = np.array(training) 70 | # create train and test lists. X - patterns, Y - intents 71 | train_x = list(training[:,0]) 72 | train_y = list(training[:,1]) 73 | print("Training data created") 74 | 75 | # Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons 76 | # equal to number of intents to predict output intent with softmax 77 | model = Sequential() 78 | model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu')) 79 | model.add(Dropout(0.5)) 80 | model.add(Dense(64, activation='relu')) 81 | model.add(Dropout(0.5)) 82 | model.add(Dense(len(train_y[0]), activation='softmax')) 83 | 84 | # Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this model 85 | sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 86 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) 87 | 88 | #fitting and saving the model 89 | hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1) 90 | model.save('chatbot_model.h5', hist) 91 | 92 | print("model created") 93 | -------------------------------------------------------------------------------- /intents.json: -------------------------------------------------------------------------------- 1 | {"intents": [ 2 | {"tag": "greeting", 3 | "patterns": ["Hi there", "How are you", "Is anyone there?","Hey","Hola", "Hello", "Good day"], 4 | "responses": ["Hello, thanks for asking", "Good to see you again", "Hi there, how can I help?"], 5 | "context": [""] 6 | }, 7 | {"tag": "goodbye", 8 | "patterns": ["Bye", "See you later", "Goodbye", "Nice chatting to you, bye", "Till next time"], 9 | "responses": ["See you!", "Have a nice day", "Bye! Come back again soon."], 10 | "context": [""] 11 | }, 12 | {"tag": "thanks", 13 | "patterns": ["Thanks", "Thank you", "That's helpful", "Awesome, thanks", "Thanks for helping me"], 14 | "responses": ["Happy to help!", "Any time!", "My pleasure"], 15 | "context": [""] 16 | }, 17 | {"tag": "noanswer", 18 | "patterns": [], 19 | "responses": ["Sorry, can't understand you", "Please give me more info", "Not sure I understand"], 20 | "context": [""] 21 | }, 22 | {"tag": "options", 23 | "patterns": ["How you could help me?", "What you can do?", "What help you provide?", "How you can be helpful?", "What support is offered"], 24 | "responses": ["I can guide you through Adverse drug reaction list, Blood pressure tracking, Hospitals and Pharmacies", "Offering support for Adverse drug reaction, Blood pressure, Hospitals and Pharmacies"], 25 | "context": [""] 26 | }, 27 | {"tag": "adverse_drug", 28 | "patterns": ["How to check Adverse drug reaction?", "Open adverse drugs module", "Give me a list of drugs causing adverse behavior", "List all drugs suitable for patient with adverse reaction", "Which drugs dont have adverse reaction?" ], 29 | "responses": ["Navigating to Adverse drug reaction module"], 30 | "context": [""] 31 | }, 32 | {"tag": "blood_pressure", 33 | "patterns": ["Open blood pressure module", "Task related to blood pressure", "Blood pressure data entry", "I want to log blood pressure results", "Blood pressure data management" ], 34 | "responses": ["Navigating to Blood Pressure module"], 35 | "context": [""] 36 | }, 37 | {"tag": "blood_pressure_search", 38 | "patterns": ["I want to search for blood pressure result history", "Blood pressure for patient", "Load patient blood pressure result", "Show blood pressure results for patient", "Find blood pressure results by ID" ], 39 | "responses": ["Please provide Patient ID", "Patient ID?"], 40 | "context": ["search_blood_pressure_by_patient_id"] 41 | }, 42 | {"tag": "search_blood_pressure_by_patient_id", 43 | "patterns": [], 44 | "responses": ["Loading Blood pressure result for Patient"], 45 | "context": [""] 46 | }, 47 | {"tag": "pharmacy_search", 48 | "patterns": ["Find me a pharmacy", "Find pharmacy", "List of pharmacies nearby", "Locate pharmacy", "Search pharmacy" ], 49 | "responses": ["Please provide pharmacy name"], 50 | "context": ["search_pharmacy_by_name"] 51 | }, 52 | {"tag": "search_pharmacy_by_name", 53 | "patterns": [], 54 | "responses": ["Loading pharmacy details"], 55 | "context": [""] 56 | }, 57 | {"tag": "hospital_search", 58 | "patterns": ["Lookup for hospital", "Searching for hospital to transfer patient", "I want to search hospital data", "Hospital lookup for patient", "Looking up hospital details" ], 59 | "responses": ["Please provide hospital name or location"], 60 | "context": ["search_hospital_by_params"] 61 | }, 62 | {"tag": "search_hospital_by_params", 63 | "patterns": [], 64 | "responses": ["Please provide hospital type"], 65 | "context": ["search_hospital_by_type"] 66 | }, 67 | {"tag": "search_hospital_by_type", 68 | "patterns": [], 69 | "responses": ["Loading hospital details"], 70 | "context": [""] 71 | } 72 | ] 73 | } 74 | -------------------------------------------------------------------------------- /gui_chatbot.py: -------------------------------------------------------------------------------- 1 | import nltk 2 | from nltk.stem import WordNetLemmatizer 3 | lemmatizer = WordNetLemmatizer() 4 | import pickle 5 | import numpy as np 6 | 7 | from keras.models import load_model 8 | model = load_model('chatbot_model.h5') 9 | import json 10 | import random 11 | intents = json.loads(open('intents.json').read()) 12 | words = pickle.load(open('words.pkl','rb')) 13 | classes = pickle.load(open('classes.pkl','rb')) 14 | 15 | 16 | def clean_up_sentence(sentence): 17 | # tokenize the pattern - splitting words into array 18 | sentence_words = nltk.word_tokenize(sentence) 19 | # stemming every word - reducing to base form 20 | sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words] 21 | return sentence_words 22 | 23 | 24 | # return bag of words array: 0 or 1 for words that exist in sentence 25 | def bag_of_words(sentence, words, show_details=True): 26 | # tokenizing patterns 27 | sentence_words = clean_up_sentence(sentence) 28 | # bag of words - vocabulary matrix 29 | bag = [0]*len(words) 30 | for s in sentence_words: 31 | for i,word in enumerate(words): 32 | if word == s: 33 | # assign 1 if current word is in the vocabulary position 34 | bag[i] = 1 35 | if show_details: 36 | print ("found in bag: %s" % word) 37 | return(np.array(bag)) 38 | 39 | def predict_class(sentence): 40 | # filter below threshold predictions 41 | p = bag_of_words(sentence, words,show_details=False) 42 | res = model.predict(np.array([p]))[0] 43 | ERROR_THRESHOLD = 0.25 44 | results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD] 45 | # sorting strength probability 46 | results.sort(key=lambda x: x[1], reverse=True) 47 | return_list = [] 48 | for r in results: 49 | return_list.append({"intent": classes[r[0]], "probability": str(r[1])}) 50 | return return_list 51 | 52 | def getResponse(ints, intents_json): 53 | tag = ints[0]['intent'] 54 | list_of_intents = intents_json['intents'] 55 | for i in list_of_intents: 56 | if(i['tag']== tag): 57 | result = random.choice(i['responses']) 58 | break 59 | return result 60 | 61 | 62 | #Creating tkinter GUI 63 | import tkinter 64 | from tkinter import * 65 | 66 | def send(): 67 | msg = EntryBox.get("1.0",'end-1c').strip() 68 | EntryBox.delete("0.0",END) 69 | 70 | if msg != '': 71 | ChatBox.config(state=NORMAL) 72 | ChatBox.insert(END, "You: " + msg + '\n\n') 73 | ChatBox.config(foreground="#446665", font=("Verdana", 12 )) 74 | 75 | ints = predict_class(msg) 76 | res = getResponse(ints, intents) 77 | 78 | ChatBox.insert(END, "Bot: " + res + '\n\n') 79 | 80 | ChatBox.config(state=DISABLED) 81 | ChatBox.yview(END) 82 | 83 | 84 | root = Tk() 85 | root.title("Chatbot") 86 | root.geometry("400x500") 87 | root.resizable(width=FALSE, height=FALSE) 88 | 89 | #Create Chat window 90 | ChatBox = Text(root, bd=0, bg="white", height="8", width="50", font="Arial",) 91 | 92 | ChatBox.config(state=DISABLED) 93 | 94 | #Bind scrollbar to Chat window 95 | scrollbar = Scrollbar(root, command=ChatBox.yview, cursor="heart") 96 | ChatBox['yscrollcommand'] = scrollbar.set 97 | 98 | #Create Button to send message 99 | SendButton = Button(root, font=("Verdana",12,'bold'), text="Send", width="12", height=5, 100 | bd=0, bg="#f9a602", activebackground="#3c9d9b",fg='#000000', 101 | command= send ) 102 | 103 | #Create the box to enter message 104 | EntryBox = Text(root, bd=0, bg="white",width="29", height="5", font="Arial") 105 | #EntryBox.bind("", send) 106 | 107 | 108 | #Place all components on the screen 109 | scrollbar.place(x=376,y=6, height=386) 110 | ChatBox.place(x=6,y=6, height=386, width=370) 111 | EntryBox.place(x=128, y=401, height=90, width=265) 112 | SendButton.place(x=6, y=401, height=90) 113 | 114 | root.mainloop() 115 | --------------------------------------------------------------------------------