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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# # CHATBOTS - Using Natural Language Processing and Tensorflow
|
| 5 |
+
# In this Jupyter Notebook, We are going to Build a Chatbot that Understands the Context of Sentense and Respond accordingly.
|
| 6 |
+
These are the Things that we are going to do in this Project -
|
| 7 |
+
1. Transforming the Conversational Intents into Tensorflow model (Neural Network using TFLEARN) using NLP and Save it as Pickle also.
|
| 8 |
+
2. Load the Same Pickle and Model to Build the Framework to Process the Responses.
|
| 9 |
+
3. At Last, We Show How the Inputs are Processed and Give the Reponses.
|
| 10 |
+
-------------------------------------------------------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
##### TFLEARN - TFlearn is a modular and transparent deep learning library built on top of Tensorflow. It was designed to provide a higher-level API to TensorFlow in order to facilitate and speed-up experimentations, while remaining fully transparent and compatible with it. (http://tflearn.org/)
|
| 13 |
+
-------------------------------------------------------------------------------------------------------
|
| 14 |
+
##### TENSORFLOW - TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications.
|
| 15 |
+
|
| 16 |
+
# In[ ]:
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# In[5]:
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
#Used in Tensorflow Model
|
| 26 |
+
import numpy as np
|
| 27 |
+
import tensorflow.compat.v1 as tf
|
| 28 |
+
tf.disable_v2_behavior()
|
| 29 |
+
import tflearn
|
| 30 |
+
import random
|
| 31 |
+
|
| 32 |
+
#Usde to for Contextualisation and Other NLP Tasks.
|
| 33 |
+
import nltk
|
| 34 |
+
from nltk.stem.lancaster import LancasterStemmer
|
| 35 |
+
stemmer = LancasterStemmer()
|
| 36 |
+
|
| 37 |
+
#Other
|
| 38 |
+
import json
|
| 39 |
+
import pickle
|
| 40 |
+
import warnings
|
| 41 |
+
warnings.filterwarnings("ignore")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# In[6]:
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
print("Processing the Intents.....")
|
| 48 |
+
with open('intents.json') as json_data:
|
| 49 |
+
intents = json.load(json_data)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# In[7]:
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
words = []
|
| 57 |
+
classes = []
|
| 58 |
+
documents = []
|
| 59 |
+
ignore_words = ['?']
|
| 60 |
+
print("Looping through the Intents to Convert them to words, classes, documents and ignore_words.......")
|
| 61 |
+
for intent in intents['intents']:
|
| 62 |
+
for pattern in intent['patterns']:
|
| 63 |
+
# tokenize each word in the sentence
|
| 64 |
+
w = nltk.word_tokenize(pattern)
|
| 65 |
+
# add to our words list
|
| 66 |
+
words.extend(w)
|
| 67 |
+
# add to documents in our corpus
|
| 68 |
+
documents.append((w, intent['tag']))
|
| 69 |
+
# add to our classes list
|
| 70 |
+
if intent['tag'] not in classes:
|
| 71 |
+
classes.append(intent['tag'])
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# In[8]:
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
print("Stemming, Lowering and Removing Duplicates.......")
|
| 78 |
+
words = [stemmer.stem(w.lower()) for w in words if w not in ignore_words]
|
| 79 |
+
words = sorted(list(set(words)))
|
| 80 |
+
|
| 81 |
+
# remove duplicates
|
| 82 |
+
classes = sorted(list(set(classes)))
|
| 83 |
+
|
| 84 |
+
print (len(documents), "documents")
|
| 85 |
+
print (len(classes), "classes", classes)
|
| 86 |
+
print (len(words), "unique stemmed words", words)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# In[9]:
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
print("Creating the Data for our Model.....")
|
| 93 |
+
training = []
|
| 94 |
+
output = []
|
| 95 |
+
print("Creating an List (Empty) for Output.....")
|
| 96 |
+
output_empty = [0] * len(classes)
|
| 97 |
+
|
| 98 |
+
print("Creating Training Set, Bag of Words for our Model....")
|
| 99 |
+
for doc in documents:
|
| 100 |
+
# Initialize our bag of words
|
| 101 |
+
bag = []
|
| 102 |
+
# List of tokenized words for the pattern
|
| 103 |
+
pattern_words = doc[0]
|
| 104 |
+
# Stem each word
|
| 105 |
+
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
|
| 106 |
+
|
| 107 |
+
# Create our bag of words array
|
| 108 |
+
for w in words:
|
| 109 |
+
bag.append(1) if w in pattern_words else bag.append(0)
|
| 110 |
+
|
| 111 |
+
# Output is a '0' for each tag and '1' for current tag
|
| 112 |
+
output_row = list(output_empty)
|
| 113 |
+
output_row[classes.index(doc[1])] = 1
|
| 114 |
+
|
| 115 |
+
# Append the feature vector and output row as a tuple
|
| 116 |
+
training.append((bag, output_row))
|
| 117 |
+
|
| 118 |
+
print("Shuffling Randomly and Converting into Numpy Array for Faster Processing......")
|
| 119 |
+
random.shuffle(training)
|
| 120 |
+
|
| 121 |
+
# Separate feature vectors and output rows into separate lists
|
| 122 |
+
train_x = np.array([x[0] for x in training])
|
| 123 |
+
train_y = np.array([x[1] for x in training])
|
| 124 |
+
|
| 125 |
+
print("Creating Train and Test Lists.....")
|
| 126 |
+
|
| 127 |
+
print("Building Neural Network for Our Chatbot to be Contextual....")
|
| 128 |
+
print("Resetting graph data....")
|
| 129 |
+
tf.reset_default_graph()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# In[ ]:
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# In[10]:
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
net = tflearn.input_data(shape=[None, len(train_x[0])])
|
| 142 |
+
net = tflearn.fully_connected(net, 8)
|
| 143 |
+
net = tflearn.fully_connected(net, 8)
|
| 144 |
+
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
|
| 145 |
+
net = tflearn.regression(net)
|
| 146 |
+
print("Training....")
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# In[11]:
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# In[12]:
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
print("Training the Model.......")
|
| 159 |
+
model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)
|
| 160 |
+
print("Saving the Model.......")
|
| 161 |
+
model.save('model.tflearn')
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# In[13]:
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
print("Pickle is also Saved..........")
|
| 168 |
+
#pickling
|
| 169 |
+
pickle.dump( {'words':words, 'classes':classes, 'train_x':train_x, 'train_y':train_y}, open( "training_data", "wb" ) )
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# In[14]:
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
print("Loading Pickle.....")
|
| 176 |
+
data = pickle.load( open( "training_data", "rb" ) )#serializes the dta (convert in byte stream)
|
| 177 |
+
words = data['words']
|
| 178 |
+
classes = data['classes']
|
| 179 |
+
train_x = data['train_x']
|
| 180 |
+
train_y = data['train_y']
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
with open('intents.json') as json_data:
|
| 184 |
+
intents = json.load(json_data)
|
| 185 |
+
|
| 186 |
+
print("Loading the Model......")
|
| 187 |
+
# load our saved model
|
| 188 |
+
model.load('./model.tflearn')
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# In[30]:
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def clean_up_sentence(sentence):
|
| 195 |
+
# It Tokenize or Break it into the constituents parts of Sentense.
|
| 196 |
+
sentence_words = nltk.word_tokenize(sentence)
|
| 197 |
+
# Stemming means to find the root of the word.
|
| 198 |
+
sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]
|
| 199 |
+
return sentence_words
|
| 200 |
+
|
| 201 |
+
# Return the Array of Bag of Words: True or False and 0 or 1 for each word of bag that exists in the Sentence
|
| 202 |
+
def bow(sentence, words, show_details=False):
|
| 203 |
+
sentence_words = clean_up_sentence(sentence)
|
| 204 |
+
bag = [0]*len(words)
|
| 205 |
+
for s in sentence_words:
|
| 206 |
+
for i,w in enumerate(words):
|
| 207 |
+
if w == s:
|
| 208 |
+
bag[i] = 1
|
| 209 |
+
if show_details:
|
| 210 |
+
print ("found in bag: %s" % w)
|
| 211 |
+
return(np.array(bag))
|
| 212 |
+
|
| 213 |
+
ERROR_THRESHOLD = 0.25
|
| 214 |
+
print("ERROR_THRESHOLD = 0.25")
|
| 215 |
+
|
| 216 |
+
def classify(sentence):
|
| 217 |
+
# Prediction or To Get the Posibility or Probability from the Model
|
| 218 |
+
results = model.predict([bow(sentence, words)])[0]
|
| 219 |
+
# Exclude those results which are Below Threshold
|
| 220 |
+
results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
|
| 221 |
+
# Sorting is Done because heigher Confidence Answer comes first.
|
| 222 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
| 223 |
+
return_list = []
|
| 224 |
+
for r in results:
|
| 225 |
+
return_list.append((classes[r[0]], r[1])) #Tuppl -> Intent and Probability
|
| 226 |
+
return return_list
|
| 227 |
+
|
| 228 |
+
def response(sentence, userID='123', show_details=False):
|
| 229 |
+
results = classify(sentence)
|
| 230 |
+
if results:
|
| 231 |
+
while results:
|
| 232 |
+
for i in intents['intents']:
|
| 233 |
+
if i['tag'] == results[0][0]:
|
| 234 |
+
# Return a random response from the list of responses for the matching intent
|
| 235 |
+
return random.choice(i['responses'])
|
| 236 |
+
results.pop(0)
|
| 237 |
+
# If no matching intent was found, return a default response
|
| 238 |
+
return "Sorry, I didn't understand that."
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
# In[ ]:
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
# In[31]:
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
import gradio as gr
|
| 251 |
+
|
| 252 |
+
def chat_response(message):
|
| 253 |
+
return response(message) # Return the response from the chatbot
|
| 254 |
+
|
| 255 |
+
gr.Interface(fn=chat_response, inputs="text", outputs="text").launch()
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
# In[ ]:
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
# In[54]:
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# In[44]:
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
# In[ ]:
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
|