Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google.colab import drive
|
| 2 |
+
drive.mount('/content/drive')
|
| 3 |
+
import nltk
|
| 4 |
+
|
| 5 |
+
nltk.download('punkt')
|
| 6 |
+
nltk.download('wordnet')
|
| 7 |
+
|
| 8 |
+
import json
|
| 9 |
+
import random
|
| 10 |
+
import numpy as np
|
| 11 |
+
import nltk
|
| 12 |
+
from tensorflow.keras.models import Sequential
|
| 13 |
+
from tensorflow.keras.layers import Dense, Dropout
|
| 14 |
+
from tensorflow.keras.optimizers import SGD
|
| 15 |
+
from sklearn.preprocessing import LabelEncoder
|
| 16 |
+
from nltk.stem import WordNetLemmatizer
|
| 17 |
+
|
| 18 |
+
file_path = '/content/drive/MyDrive/Colab_Notebooks/Dataset/intents.json'
|
| 19 |
+
with open(file_path,'r') as file:
|
| 20 |
+
data = json.load(file)
|
| 21 |
+
|
| 22 |
+
lemmatizer = WordNetLemmatizer()
|
| 23 |
+
words = []
|
| 24 |
+
classes = []
|
| 25 |
+
documents = []
|
| 26 |
+
ignore_words = ['?', '!', '.']
|
| 27 |
+
|
| 28 |
+
for intent in data['intents']:
|
| 29 |
+
for pattern in intent['patterns']:
|
| 30 |
+
# Tokenize each word
|
| 31 |
+
word_list = nltk.word_tokenize(pattern)
|
| 32 |
+
words.extend(word_list)
|
| 33 |
+
documents.append((word_list, intent['tag']))
|
| 34 |
+
if intent['tag'] not in classes:
|
| 35 |
+
classes.append(intent['tag'])
|
| 36 |
+
|
| 37 |
+
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
|
| 38 |
+
words = sorted(list(set(words)))
|
| 39 |
+
classes = sorted(list(set(classes)))
|
| 40 |
+
|
| 41 |
+
training = []
|
| 42 |
+
output_empty = [0] * len(classes)
|
| 43 |
+
|
| 44 |
+
for doc in documents:
|
| 45 |
+
bag = []
|
| 46 |
+
word_patterns = doc[0]
|
| 47 |
+
word_patterns = [lemmatizer.lemmatize(word.lower()) for word in word_patterns]
|
| 48 |
+
for w in words:
|
| 49 |
+
bag.append(1 if w in word_patterns else 0)
|
| 50 |
+
|
| 51 |
+
output_row = list(output_empty)
|
| 52 |
+
output_row[classes.index(doc[1])] = 1
|
| 53 |
+
training.append([bag, output_row])
|
| 54 |
+
|
| 55 |
+
random.shuffle(training)
|
| 56 |
+
training = np.array(training, dtype=object)
|
| 57 |
+
|
| 58 |
+
train_x = np.array(list(training[:, 0]))
|
| 59 |
+
train_y = np.array(list(training[:, 1]))
|
| 60 |
+
|
| 61 |
+
model = Sequential()
|
| 62 |
+
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
|
| 63 |
+
model.add(Dropout(0.5))
|
| 64 |
+
model.add(Dense(64, activation='relu'))
|
| 65 |
+
model.add(Dropout(0.5))
|
| 66 |
+
model.add(Dense(len(train_y[0]), activation='softmax'))
|
| 67 |
+
|
| 68 |
+
sgd = SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
| 69 |
+
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
|
| 70 |
+
|
| 71 |
+
hist = model.fit(train_x, train_y, epochs=200, batch_size=5, verbose=1)
|
| 72 |
+
model.save('chatbot_model.h5', hist)
|
| 73 |
+
|
| 74 |
+
print("Model created and saved successfully!")
|
| 75 |
+
|
| 76 |
+
import tensorflow as tf
|
| 77 |
+
model = tf.keras.models.load_model('chatbot_model.h5')
|
| 78 |
+
|
| 79 |
+
def clean_up_sentence(sentence):
|
| 80 |
+
sentence_words = nltk.word_tokenize(sentence)
|
| 81 |
+
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
|
| 82 |
+
return sentence_words
|
| 83 |
+
|
| 84 |
+
def bag_of_words(sentence, words):
|
| 85 |
+
sentence_words = clean_up_sentence(sentence)
|
| 86 |
+
bag = [0] * len(words)
|
| 87 |
+
for s in sentence_words:
|
| 88 |
+
for i, w in enumerate(words):
|
| 89 |
+
if w == s:
|
| 90 |
+
bag[i] = 1
|
| 91 |
+
return np.array(bag)
|
| 92 |
+
|
| 93 |
+
def predict_class(sentence, model):
|
| 94 |
+
bow = bag_of_words(sentence, words)
|
| 95 |
+
res = model.predict(np.array([bow]))[0]
|
| 96 |
+
ERROR_THRESHOLD = 0.25
|
| 97 |
+
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
|
| 98 |
+
|
| 99 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
| 100 |
+
return_list = [{"intent": classes[r[0]], "probability": str(r[1])} for r in results]
|
| 101 |
+
return return_list
|
| 102 |
+
|
| 103 |
+
def get_response(intents_list, intents_json):
|
| 104 |
+
tag = intents_list[0]['intent']
|
| 105 |
+
for i in intents_json['intents']:
|
| 106 |
+
if i['tag'] == tag:
|
| 107 |
+
return random.choice(i['responses'])
|
| 108 |
+
|
| 109 |
+
print("Bot is ready to chat! Type 'quit' to stop.")
|
| 110 |
+
while True:
|
| 111 |
+
message = input("You: ")
|
| 112 |
+
if message.lower() == "quit":
|
| 113 |
+
break
|
| 114 |
+
|
| 115 |
+
ints = predict_class(message, model)
|
| 116 |
+
if ints:
|
| 117 |
+
res = get_response(ints, data)
|
| 118 |
+
print("Bot:", res)
|
| 119 |
+
else:
|
| 120 |
+
print("Bot: Sorry, I don't understand that.")\
|