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|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# CHATBOTS - Using Natural Language Processing and Tensorflow"
|
| 8 |
+
]
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"cell_type": "raw",
|
| 12 |
+
"metadata": {},
|
| 13 |
+
"source": [
|
| 14 |
+
"# In this Jupyter Notebook, We are going to Build a Chatbot that Understands the Context of Sentense and Respond accordingly.\n",
|
| 15 |
+
"These are the Things that we are going to do in this Project -\n",
|
| 16 |
+
"1. Transforming the Conversational Intents into Tensorflow model (Neural Network using TFLEARN) using NLP and Save it as Pickle also.\n",
|
| 17 |
+
"2. Load the Same Pickle and Model to Build the Framework to Process the Responses.\n",
|
| 18 |
+
"3. At Last, We Show How the Inputs are Processed and Give the Reponses.\n",
|
| 19 |
+
"-------------------------------------------------------------------------------------------------------\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"##### 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/)\n",
|
| 22 |
+
"-------------------------------------------------------------------------------------------------------\n",
|
| 23 |
+
"##### 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.\n"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"cell_type": "code",
|
| 28 |
+
"execution_count": null,
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [],
|
| 31 |
+
"source": []
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": 5,
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [
|
| 38 |
+
{
|
| 39 |
+
"name": "stdout",
|
| 40 |
+
"output_type": "stream",
|
| 41 |
+
"text": [
|
| 42 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\anaconda3\\Lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\AppData\\Local\\Temp\\ipykernel_29544\\870218512.py:4: The name tf.disable_v2_behavior is deprecated. Please use tf.compat.v1.disable_v2_behavior instead.\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\anaconda3\\Lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:108: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
|
| 47 |
+
"Instructions for updating:\n",
|
| 48 |
+
"non-resource variables are not supported in the long term\n",
|
| 49 |
+
"curses is not supported on this machine (please install/reinstall curses for an optimal experience)\n",
|
| 50 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\anaconda3\\Lib\\site-packages\\tflearn\\helpers\\summarizer.py:9: The name tf.summary.merge is deprecated. Please use tf.compat.v1.summary.merge instead.\n",
|
| 51 |
+
"\n"
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
],
|
| 55 |
+
"source": [
|
| 56 |
+
"#Used in Tensorflow Model\n",
|
| 57 |
+
"import numpy as np\n",
|
| 58 |
+
"import tensorflow.compat.v1 as tf\n",
|
| 59 |
+
"tf.disable_v2_behavior()\n",
|
| 60 |
+
"import tflearn\n",
|
| 61 |
+
"import random\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"#Usde to for Contextualisation and Other NLP Tasks.\n",
|
| 64 |
+
"import nltk\n",
|
| 65 |
+
"from nltk.stem.lancaster import LancasterStemmer\n",
|
| 66 |
+
"stemmer = LancasterStemmer()\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"#Other\n",
|
| 69 |
+
"import json\n",
|
| 70 |
+
"import pickle\n",
|
| 71 |
+
"import warnings\n",
|
| 72 |
+
"warnings.filterwarnings(\"ignore\")\n"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 6,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [
|
| 80 |
+
{
|
| 81 |
+
"name": "stdout",
|
| 82 |
+
"output_type": "stream",
|
| 83 |
+
"text": [
|
| 84 |
+
"Processing the Intents.....\n"
|
| 85 |
+
]
|
| 86 |
+
}
|
| 87 |
+
],
|
| 88 |
+
"source": [
|
| 89 |
+
"print(\"Processing the Intents.....\")\n",
|
| 90 |
+
"with open('intents.json') as json_data:\n",
|
| 91 |
+
" intents = json.load(json_data)\n",
|
| 92 |
+
"\n"
|
| 93 |
+
]
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"cell_type": "code",
|
| 97 |
+
"execution_count": 7,
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"outputs": [
|
| 100 |
+
{
|
| 101 |
+
"name": "stdout",
|
| 102 |
+
"output_type": "stream",
|
| 103 |
+
"text": [
|
| 104 |
+
"Looping through the Intents to Convert them to words, classes, documents and ignore_words.......\n"
|
| 105 |
+
]
|
| 106 |
+
}
|
| 107 |
+
],
|
| 108 |
+
"source": [
|
| 109 |
+
"words = []\n",
|
| 110 |
+
"classes = []\n",
|
| 111 |
+
"documents = []\n",
|
| 112 |
+
"ignore_words = ['?']\n",
|
| 113 |
+
"print(\"Looping through the Intents to Convert them to words, classes, documents and ignore_words.......\")\n",
|
| 114 |
+
"for intent in intents['intents']:\n",
|
| 115 |
+
" for pattern in intent['patterns']:\n",
|
| 116 |
+
" # tokenize each word in the sentence\n",
|
| 117 |
+
" w = nltk.word_tokenize(pattern)\n",
|
| 118 |
+
" # add to our words list\n",
|
| 119 |
+
" words.extend(w)\n",
|
| 120 |
+
" # add to documents in our corpus\n",
|
| 121 |
+
" documents.append((w, intent['tag']))\n",
|
| 122 |
+
" # add to our classes list\n",
|
| 123 |
+
" if intent['tag'] not in classes:\n",
|
| 124 |
+
" classes.append(intent['tag'])\n"
|
| 125 |
+
]
|
| 126 |
+
},
|
| 127 |
+
{
|
| 128 |
+
"cell_type": "code",
|
| 129 |
+
"execution_count": 8,
|
| 130 |
+
"metadata": {},
|
| 131 |
+
"outputs": [
|
| 132 |
+
{
|
| 133 |
+
"name": "stdout",
|
| 134 |
+
"output_type": "stream",
|
| 135 |
+
"text": [
|
| 136 |
+
"Stemming, Lowering and Removing Duplicates.......\n",
|
| 137 |
+
"98 documents\n",
|
| 138 |
+
"30 classes ['application_dates', 'ba', 'baallb', 'bba', 'bdesign', 'blu_embers_timings', 'bsc', 'btech', 'cafes', 'cost_of_study', 'courses', 'cup_of_joe_timings', 'doctors', 'eligibility_criteria', 'emergency', 'exchange_program', 'faculty', 'goodbye', 'greeting', 'hours', 'mba', 'meal_menu', 'meal_timings', 'new_embers_timings', 'online_payments', 'other_requirements', 'phd', 'restaurants', 'rise_timings', 'thanks']\n",
|
| 139 |
+
"125 unique stemmed words [\"'\", \"'s\", '.', 'a', 'about', 'abroad', 'addit', 'am', 'anyon', 'apply', 'ar', 'assocy', 'at', 'avail', 'ba', 'bba', 'bdesign', 'beyond', 'blu', 'breakfast', 'bsc', 'btech', 'bye', 'caf', 'can', 'cas', 'coff', 'contact', 'cost', 'cours', 'criter', 'cup', 'dat', 'day', 'deadlin', 'degr', 'detail', 'din', 'do', 'doct', 'doe', 'eat', 'elig', 'els', 'emb', 'emerg', 'entail', 'exchang', 'expens', 'facul', 'for', 'get', 'giv', 'good', 'goodby', 'hello', 'help', 'hi', 'hono', 'hospit', 'hour', 'how', 'i', 'in', 'inform', 'is', 'it', 'joe', 'lat', 'lik', 'list', 'llb', 'lunch', 'mba', 'me', 'meal', 'memb', 'menu', 'method', 'mor', 'much', 'nee', 'new', 'numb', 'of', 'off', 'on', 'onlin', 'op', 'opt', 'oth', 'particip', 'pay', 'phd', 'plac', 'profess', 'program', 'requir', 'resta', 'ris', 'see', 'shop', 'should', 'stud', 'study', 'sunday', 'tel', 'thank', 'that', 'the', 'ther', 'tim', 'timelin', 'to', 'today', 'univers', 'what', 'when', 'wher', 'who', 'with', 'work', 'woxs', 'yo', 'you']\n"
|
| 140 |
+
]
|
| 141 |
+
}
|
| 142 |
+
],
|
| 143 |
+
"source": [
|
| 144 |
+
"print(\"Stemming, Lowering and Removing Duplicates.......\")\n",
|
| 145 |
+
"words = [stemmer.stem(w.lower()) for w in words if w not in ignore_words]\n",
|
| 146 |
+
"words = sorted(list(set(words)))\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"# remove duplicates\n",
|
| 149 |
+
"classes = sorted(list(set(classes)))\n",
|
| 150 |
+
"\n",
|
| 151 |
+
"print (len(documents), \"documents\")\n",
|
| 152 |
+
"print (len(classes), \"classes\", classes)\n",
|
| 153 |
+
"print (len(words), \"unique stemmed words\", words)"
|
| 154 |
+
]
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"cell_type": "code",
|
| 158 |
+
"execution_count": 9,
|
| 159 |
+
"metadata": {},
|
| 160 |
+
"outputs": [
|
| 161 |
+
{
|
| 162 |
+
"name": "stdout",
|
| 163 |
+
"output_type": "stream",
|
| 164 |
+
"text": [
|
| 165 |
+
"Creating the Data for our Model.....\n",
|
| 166 |
+
"Creating an List (Empty) for Output.....\n",
|
| 167 |
+
"Creating Training Set, Bag of Words for our Model....\n",
|
| 168 |
+
"Shuffling Randomly and Converting into Numpy Array for Faster Processing......\n",
|
| 169 |
+
"Creating Train and Test Lists.....\n",
|
| 170 |
+
"Building Neural Network for Our Chatbot to be Contextual....\n",
|
| 171 |
+
"Resetting graph data....\n"
|
| 172 |
+
]
|
| 173 |
+
}
|
| 174 |
+
],
|
| 175 |
+
"source": [
|
| 176 |
+
"print(\"Creating the Data for our Model.....\")\n",
|
| 177 |
+
"training = []\n",
|
| 178 |
+
"output = []\n",
|
| 179 |
+
"print(\"Creating an List (Empty) for Output.....\")\n",
|
| 180 |
+
"output_empty = [0] * len(classes)\n",
|
| 181 |
+
"\n",
|
| 182 |
+
"print(\"Creating Training Set, Bag of Words for our Model....\")\n",
|
| 183 |
+
"for doc in documents:\n",
|
| 184 |
+
" # Initialize our bag of words\n",
|
| 185 |
+
" bag = []\n",
|
| 186 |
+
" # List of tokenized words for the pattern\n",
|
| 187 |
+
" pattern_words = doc[0]\n",
|
| 188 |
+
" # Stem each word\n",
|
| 189 |
+
" pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]\n",
|
| 190 |
+
" \n",
|
| 191 |
+
" # Create our bag of words array\n",
|
| 192 |
+
" for w in words:\n",
|
| 193 |
+
" bag.append(1) if w in pattern_words else bag.append(0)\n",
|
| 194 |
+
"\n",
|
| 195 |
+
" # Output is a '0' for each tag and '1' for current tag\n",
|
| 196 |
+
" output_row = list(output_empty)\n",
|
| 197 |
+
" output_row[classes.index(doc[1])] = 1\n",
|
| 198 |
+
"\n",
|
| 199 |
+
" # Append the feature vector and output row as a tuple\n",
|
| 200 |
+
" training.append((bag, output_row))\n",
|
| 201 |
+
"\n",
|
| 202 |
+
"print(\"Shuffling Randomly and Converting into Numpy Array for Faster Processing......\")\n",
|
| 203 |
+
"random.shuffle(training)\n",
|
| 204 |
+
"\n",
|
| 205 |
+
"# Separate feature vectors and output rows into separate lists\n",
|
| 206 |
+
"train_x = np.array([x[0] for x in training])\n",
|
| 207 |
+
"train_y = np.array([x[1] for x in training])\n",
|
| 208 |
+
"\n",
|
| 209 |
+
"print(\"Creating Train and Test Lists.....\")\n",
|
| 210 |
+
"\n",
|
| 211 |
+
"print(\"Building Neural Network for Our Chatbot to be Contextual....\")\n",
|
| 212 |
+
"print(\"Resetting graph data....\")\n",
|
| 213 |
+
"tf.reset_default_graph()\n"
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
"cell_type": "code",
|
| 218 |
+
"execution_count": null,
|
| 219 |
+
"metadata": {},
|
| 220 |
+
"outputs": [],
|
| 221 |
+
"source": []
|
| 222 |
+
},
|
| 223 |
+
{
|
| 224 |
+
"cell_type": "code",
|
| 225 |
+
"execution_count": 10,
|
| 226 |
+
"metadata": {},
|
| 227 |
+
"outputs": [
|
| 228 |
+
{
|
| 229 |
+
"name": "stdout",
|
| 230 |
+
"output_type": "stream",
|
| 231 |
+
"text": [
|
| 232 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\anaconda3\\Lib\\site-packages\\tflearn\\initializations.py:164: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
| 233 |
+
"Instructions for updating:\n",
|
| 234 |
+
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
| 235 |
+
"WARNING:tensorflow:From C:\\Users\\meghn\\anaconda3\\Lib\\site-packages\\tflearn\\optimizers.py:238: The name tf.train.AdamOptimizer is deprecated. Please use tf.compat.v1.train.AdamOptimizer instead.\n",
|
| 236 |
+
"\n",
|
| 237 |
+
"Training....\n"
|
| 238 |
+
]
|
| 239 |
+
}
|
| 240 |
+
],
|
| 241 |
+
"source": [
|
| 242 |
+
"net = tflearn.input_data(shape=[None, len(train_x[0])])\n",
|
| 243 |
+
"net = tflearn.fully_connected(net, 8)\n",
|
| 244 |
+
"net = tflearn.fully_connected(net, 8)\n",
|
| 245 |
+
"net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')\n",
|
| 246 |
+
"net = tflearn.regression(net)\n",
|
| 247 |
+
"print(\"Training....\")"
|
| 248 |
+
]
|
| 249 |
+
},
|
| 250 |
+
{
|
| 251 |
+
"cell_type": "code",
|
| 252 |
+
"execution_count": 11,
|
| 253 |
+
"metadata": {},
|
| 254 |
+
"outputs": [],
|
| 255 |
+
"source": [
|
| 256 |
+
"model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')"
|
| 257 |
+
]
|
| 258 |
+
},
|
| 259 |
+
{
|
| 260 |
+
"cell_type": "code",
|
| 261 |
+
"execution_count": 12,
|
| 262 |
+
"metadata": {},
|
| 263 |
+
"outputs": [
|
| 264 |
+
{
|
| 265 |
+
"name": "stdout",
|
| 266 |
+
"output_type": "stream",
|
| 267 |
+
"text": [
|
| 268 |
+
"Training Step: 12999 | total loss: \u001b[1m\u001b[32m0.00966\u001b[0m\u001b[0m | time: 0.050s\n",
|
| 269 |
+
"| Adam | epoch: 1000 | loss: 0.00966 - acc: 0.9996 -- iter: 96/98\n",
|
| 270 |
+
"Training Step: 13000 | total loss: \u001b[1m\u001b[32m0.00887\u001b[0m\u001b[0m | time: 0.054s\n",
|
| 271 |
+
"| Adam | epoch: 1000 | loss: 0.00887 - acc: 0.9997 -- iter: 98/98\n",
|
| 272 |
+
"--\n",
|
| 273 |
+
"Saving the Model.......\n",
|
| 274 |
+
"INFO:tensorflow:C:\\Users\\meghn\\model.tflearn is not in all_model_checkpoint_paths. Manually adding it.\n"
|
| 275 |
+
]
|
| 276 |
+
}
|
| 277 |
+
],
|
| 278 |
+
"source": [
|
| 279 |
+
"print(\"Training the Model.......\")\n",
|
| 280 |
+
"model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)\n",
|
| 281 |
+
"print(\"Saving the Model.......\")\n",
|
| 282 |
+
"model.save('model.tflearn')\n"
|
| 283 |
+
]
|
| 284 |
+
},
|
| 285 |
+
{
|
| 286 |
+
"cell_type": "code",
|
| 287 |
+
"execution_count": 13,
|
| 288 |
+
"metadata": {},
|
| 289 |
+
"outputs": [
|
| 290 |
+
{
|
| 291 |
+
"name": "stdout",
|
| 292 |
+
"output_type": "stream",
|
| 293 |
+
"text": [
|
| 294 |
+
"Pickle is also Saved..........\n"
|
| 295 |
+
]
|
| 296 |
+
}
|
| 297 |
+
],
|
| 298 |
+
"source": [
|
| 299 |
+
"print(\"Pickle is also Saved..........\")\n",
|
| 300 |
+
"#pickling \n",
|
| 301 |
+
"pickle.dump( {'words':words, 'classes':classes, 'train_x':train_x, 'train_y':train_y}, open( \"training_data\", \"wb\" ) )"
|
| 302 |
+
]
|
| 303 |
+
},
|
| 304 |
+
{
|
| 305 |
+
"cell_type": "code",
|
| 306 |
+
"execution_count": 14,
|
| 307 |
+
"metadata": {},
|
| 308 |
+
"outputs": [
|
| 309 |
+
{
|
| 310 |
+
"name": "stdout",
|
| 311 |
+
"output_type": "stream",
|
| 312 |
+
"text": [
|
| 313 |
+
"Loading Pickle.....\n",
|
| 314 |
+
"Loading the Model......\n",
|
| 315 |
+
"INFO:tensorflow:Restoring parameters from C:\\Users\\meghn\\model.tflearn\n"
|
| 316 |
+
]
|
| 317 |
+
}
|
| 318 |
+
],
|
| 319 |
+
"source": [
|
| 320 |
+
"print(\"Loading Pickle.....\")\n",
|
| 321 |
+
"data = pickle.load( open( \"training_data\", \"rb\" ) )#serializes the dta (convert in byte stream)\n",
|
| 322 |
+
"words = data['words']\n",
|
| 323 |
+
"classes = data['classes']\n",
|
| 324 |
+
"train_x = data['train_x']\n",
|
| 325 |
+
"train_y = data['train_y']\n",
|
| 326 |
+
"\n",
|
| 327 |
+
"\n",
|
| 328 |
+
"with open('intents.json') as json_data:\n",
|
| 329 |
+
" intents = json.load(json_data)\n",
|
| 330 |
+
" \n",
|
| 331 |
+
"print(\"Loading the Model......\")\n",
|
| 332 |
+
"# load our saved model\n",
|
| 333 |
+
"model.load('./model.tflearn')"
|
| 334 |
+
]
|
| 335 |
+
},
|
| 336 |
+
{
|
| 337 |
+
"cell_type": "code",
|
| 338 |
+
"execution_count": 30,
|
| 339 |
+
"metadata": {},
|
| 340 |
+
"outputs": [
|
| 341 |
+
{
|
| 342 |
+
"name": "stdout",
|
| 343 |
+
"output_type": "stream",
|
| 344 |
+
"text": [
|
| 345 |
+
"ERROR_THRESHOLD = 0.25\n"
|
| 346 |
+
]
|
| 347 |
+
}
|
| 348 |
+
],
|
| 349 |
+
"source": [
|
| 350 |
+
"def clean_up_sentence(sentence):\n",
|
| 351 |
+
" # It Tokenize or Break it into the constituents parts of Sentense.\n",
|
| 352 |
+
" sentence_words = nltk.word_tokenize(sentence)\n",
|
| 353 |
+
" # Stemming means to find the root of the word.\n",
|
| 354 |
+
" sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]\n",
|
| 355 |
+
" return sentence_words\n",
|
| 356 |
+
"\n",
|
| 357 |
+
"# Return the Array of Bag of Words: True or False and 0 or 1 for each word of bag that exists in the Sentence\n",
|
| 358 |
+
"def bow(sentence, words, show_details=False):\n",
|
| 359 |
+
" sentence_words = clean_up_sentence(sentence)\n",
|
| 360 |
+
" bag = [0]*len(words)\n",
|
| 361 |
+
" for s in sentence_words:\n",
|
| 362 |
+
" for i,w in enumerate(words):\n",
|
| 363 |
+
" if w == s:\n",
|
| 364 |
+
" bag[i] = 1\n",
|
| 365 |
+
" if show_details:\n",
|
| 366 |
+
" print (\"found in bag: %s\" % w)\n",
|
| 367 |
+
" return(np.array(bag))\n",
|
| 368 |
+
"\n",
|
| 369 |
+
"ERROR_THRESHOLD = 0.25\n",
|
| 370 |
+
"print(\"ERROR_THRESHOLD = 0.25\")\n",
|
| 371 |
+
"\n",
|
| 372 |
+
"def classify(sentence):\n",
|
| 373 |
+
" # Prediction or To Get the Posibility or Probability from the Model\n",
|
| 374 |
+
" results = model.predict([bow(sentence, words)])[0]\n",
|
| 375 |
+
" # Exclude those results which are Below Threshold\n",
|
| 376 |
+
" results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]\n",
|
| 377 |
+
" # Sorting is Done because heigher Confidence Answer comes first.\n",
|
| 378 |
+
" results.sort(key=lambda x: x[1], reverse=True)\n",
|
| 379 |
+
" return_list = []\n",
|
| 380 |
+
" for r in results:\n",
|
| 381 |
+
" return_list.append((classes[r[0]], r[1])) #Tuppl -> Intent and Probability\n",
|
| 382 |
+
" return return_list\n",
|
| 383 |
+
"\n",
|
| 384 |
+
"def response(sentence, userID='123', show_details=False):\n",
|
| 385 |
+
" results = classify(sentence)\n",
|
| 386 |
+
" if results:\n",
|
| 387 |
+
" while results:\n",
|
| 388 |
+
" for i in intents['intents']:\n",
|
| 389 |
+
" if i['tag'] == results[0][0]:\n",
|
| 390 |
+
" # Return a random response from the list of responses for the matching intent\n",
|
| 391 |
+
" return random.choice(i['responses'])\n",
|
| 392 |
+
" results.pop(0)\n",
|
| 393 |
+
" # If no matching intent was found, return a default response\n",
|
| 394 |
+
" return \"Sorry, I didn't understand that.\"\n"
|
| 395 |
+
]
|
| 396 |
+
},
|
| 397 |
+
{
|
| 398 |
+
"cell_type": "code",
|
| 399 |
+
"execution_count": null,
|
| 400 |
+
"metadata": {
|
| 401 |
+
"scrolled": true
|
| 402 |
+
},
|
| 403 |
+
"outputs": [],
|
| 404 |
+
"source": []
|
| 405 |
+
},
|
| 406 |
+
{
|
| 407 |
+
"cell_type": "code",
|
| 408 |
+
"execution_count": 31,
|
| 409 |
+
"metadata": {
|
| 410 |
+
"scrolled": true
|
| 411 |
+
},
|
| 412 |
+
"outputs": [
|
| 413 |
+
{
|
| 414 |
+
"name": "stdout",
|
| 415 |
+
"output_type": "stream",
|
| 416 |
+
"text": [
|
| 417 |
+
"Running on local URL: http://127.0.0.1:7871\n",
|
| 418 |
+
"\n",
|
| 419 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
| 420 |
+
]
|
| 421 |
+
},
|
| 422 |
+
{
|
| 423 |
+
"data": {
|
| 424 |
+
"text/html": [
|
| 425 |
+
"<div><iframe src=\"http://127.0.0.1:7871/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 426 |
+
],
|
| 427 |
+
"text/plain": [
|
| 428 |
+
"<IPython.core.display.HTML object>"
|
| 429 |
+
]
|
| 430 |
+
},
|
| 431 |
+
"metadata": {},
|
| 432 |
+
"output_type": "display_data"
|
| 433 |
+
},
|
| 434 |
+
{
|
| 435 |
+
"data": {
|
| 436 |
+
"text/plain": []
|
| 437 |
+
},
|
| 438 |
+
"execution_count": 31,
|
| 439 |
+
"metadata": {},
|
| 440 |
+
"output_type": "execute_result"
|
| 441 |
+
}
|
| 442 |
+
],
|
| 443 |
+
"source": [
|
| 444 |
+
"import gradio as gr\n",
|
| 445 |
+
"\n",
|
| 446 |
+
"def chat_response(message):\n",
|
| 447 |
+
" return response(message) # Return the response from the chatbot\n",
|
| 448 |
+
"\n",
|
| 449 |
+
"gr.Interface(fn=chat_response, inputs=\"text\", outputs=\"text\").launch()\n"
|
| 450 |
+
]
|
| 451 |
+
},
|
| 452 |
+
{
|
| 453 |
+
"cell_type": "code",
|
| 454 |
+
"execution_count": null,
|
| 455 |
+
"metadata": {},
|
| 456 |
+
"outputs": [],
|
| 457 |
+
"source": []
|
| 458 |
+
},
|
| 459 |
+
{
|
| 460 |
+
"cell_type": "code",
|
| 461 |
+
"execution_count": 54,
|
| 462 |
+
"metadata": {},
|
| 463 |
+
"outputs": [],
|
| 464 |
+
"source": []
|
| 465 |
+
},
|
| 466 |
+
{
|
| 467 |
+
"cell_type": "code",
|
| 468 |
+
"execution_count": 44,
|
| 469 |
+
"metadata": {},
|
| 470 |
+
"outputs": [],
|
| 471 |
+
"source": []
|
| 472 |
+
},
|
| 473 |
+
{
|
| 474 |
+
"cell_type": "code",
|
| 475 |
+
"execution_count": null,
|
| 476 |
+
"metadata": {},
|
| 477 |
+
"outputs": [],
|
| 478 |
+
"source": []
|
| 479 |
+
}
|
| 480 |
+
],
|
| 481 |
+
"metadata": {
|
| 482 |
+
"kernelspec": {
|
| 483 |
+
"display_name": "Python 3 (ipykernel)",
|
| 484 |
+
"language": "python",
|
| 485 |
+
"name": "python3"
|
| 486 |
+
},
|
| 487 |
+
"language_info": {
|
| 488 |
+
"codemirror_mode": {
|
| 489 |
+
"name": "ipython",
|
| 490 |
+
"version": 3
|
| 491 |
+
},
|
| 492 |
+
"file_extension": ".py",
|
| 493 |
+
"mimetype": "text/x-python",
|
| 494 |
+
"name": "python",
|
| 495 |
+
"nbconvert_exporter": "python",
|
| 496 |
+
"pygments_lexer": "ipython3",
|
| 497 |
+
"version": "3.11.5"
|
| 498 |
+
}
|
| 499 |
+
},
|
| 500 |
+
"nbformat": 4,
|
| 501 |
+
"nbformat_minor": 2
|
| 502 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
tensorflow==1.15.0 # Specify the version you are using
|
| 3 |
+
tflearn
|
| 4 |
+
nltk
|