Create bot.py
Browse files
bot.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from abc import ABC, abstractmethod
|
2 |
+
import nltk
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
import json
|
6 |
+
import pickle
|
7 |
+
from nltk.stem import WordNetLemmatizer
|
8 |
+
from tensorflow.keras.models import Sequential, load_model
|
9 |
+
from tensorflow.keras.layers import Dense, Activation, Dropout
|
10 |
+
from tensorflow.keras.optimizers import SGD
|
11 |
+
|
12 |
+
class ChatbotModel(ABC):
|
13 |
+
@abstractmethod
|
14 |
+
def load_data(self, file_path): pass
|
15 |
+
@abstractmethod
|
16 |
+
def load_model(self, model_path): pass
|
17 |
+
@abstractmethod
|
18 |
+
def clean_up_sentence(self, sentence): pass
|
19 |
+
@abstractmethod
|
20 |
+
def bag_of_words(self, sentence): pass
|
21 |
+
@abstractmethod
|
22 |
+
def predict_class(self, sentence): pass
|
23 |
+
@abstractmethod
|
24 |
+
def get_response(self, intents_list): pass
|
25 |
+
@abstractmethod
|
26 |
+
def chat(self, text): pass
|
27 |
+
@abstractmethod
|
28 |
+
def preprocess_data(self): pass
|
29 |
+
@abstractmethod
|
30 |
+
def create_training_data(self, words, classes, documents): pass
|
31 |
+
@abstractmethod
|
32 |
+
def build_model(self, train_x, train_y): pass
|
33 |
+
@abstractmethod
|
34 |
+
def train_model(self, model, train_x, train_y, epochs, batch_size): pass
|
35 |
+
|
36 |
+
class GenAIGlobalImpactElectionsbot(ChatbotModel):
|
37 |
+
def __init__(self, intents_file, model_file, words_file, classes_file):
|
38 |
+
self.intents = self.load_data(intents_file)
|
39 |
+
self.words = pickle.load(open(words_file, 'rb'))
|
40 |
+
self.classes = pickle.load(open(classes_file, 'rb'))
|
41 |
+
self.model = self.load_model(model_file)
|
42 |
+
self.lemmatizer = WordNetLemmatizer()
|
43 |
+
|
44 |
+
def load_data(self, file_path):
|
45 |
+
with open(file_path) as json_file:
|
46 |
+
return json.load(json_file)
|
47 |
+
|
48 |
+
def load_model(self, model_path):
|
49 |
+
return load_model(model_path)
|
50 |
+
|
51 |
+
def clean_up_sentence(self, sentence):
|
52 |
+
sentence_words = nltk.word_tokenize(sentence)
|
53 |
+
sentence_words = [self.lemmatizer.lemmatize(word.lower()) for word in sentence_words]
|
54 |
+
return sentence_words
|
55 |
+
|
56 |
+
def bag_of_words(self, sentence):
|
57 |
+
sentence_words = self.clean_up_sentence(sentence)
|
58 |
+
bag = [0] * len(self.words)
|
59 |
+
for w in sentence_words:
|
60 |
+
for i, word in enumerate(self.words):
|
61 |
+
if word == w:
|
62 |
+
bag[i] = 1
|
63 |
+
return np.array(bag)
|
64 |
+
|
65 |
+
def predict_class(self, sentence):
|
66 |
+
bow = self.bag_of_words(sentence)
|
67 |
+
res = self.model.predict(np.array([bow]))[0]
|
68 |
+
ERROR_THRESHOLD = 0.25
|
69 |
+
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
|
70 |
+
|
71 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
72 |
+
return_list = []
|
73 |
+
for r in results:
|
74 |
+
return_list.append({'intent': self.classes[r[0]], 'probability': str(r[1])})
|
75 |
+
return return_list
|
76 |
+
|
77 |
+
def get_response(self, intents_list):
|
78 |
+
tag = intents_list[0]['intent']
|
79 |
+
list_of_intents = self.intents['intents']
|
80 |
+
for i in list_of_intents:
|
81 |
+
if i['tag'] == tag:
|
82 |
+
result = random.choice(i['responses'])
|
83 |
+
break
|
84 |
+
return result
|
85 |
+
|
86 |
+
def chat(self, text):
|
87 |
+
ints = self.predict_class(text)
|
88 |
+
res = self.get_response(ints)
|
89 |
+
return res
|
90 |
+
|
91 |
+
def preprocess_data(self):
|
92 |
+
words = []
|
93 |
+
classes = []
|
94 |
+
documents = []
|
95 |
+
ignore_words = ['?', '!']
|
96 |
+
for intent in self.intents['intents']:
|
97 |
+
for pattern in intent['patterns']:
|
98 |
+
word_list = nltk.word_tokenize(pattern)
|
99 |
+
words.extend(word_list)
|
100 |
+
documents.append((word_list, intent['tag']))
|
101 |
+
if intent['tag'] not in classes:
|
102 |
+
classes.append(intent['tag'])
|
103 |
+
words = [self.lemmatizer.lemmatize(word.lower()) for word in words if word not in ignore_words]
|
104 |
+
words = sorted(list(set(words)))
|
105 |
+
classes = sorted(list(set(classes)))
|
106 |
+
pickle.dump(words, open('words.pkl', 'wb'))
|
107 |
+
pickle.dump(classes, open('classes.pkl', 'wb'))
|
108 |
+
return words, classes, documents
|
109 |
+
|
110 |
+
def create_training_data(self, words, classes, documents):
|
111 |
+
training = []
|
112 |
+
output_empty = [0] * len(classes)
|
113 |
+
for doc in documents:
|
114 |
+
bag = []
|
115 |
+
word_patterns = doc[0]
|
116 |
+
word_patterns = [self.lemmatizer.lemmatize(word.lower()) for word in word_patterns]
|
117 |
+
for word in words:
|
118 |
+
bag.append(1) if word in word_patterns else bag.append(0)
|
119 |
+
output_row = list(output_empty)
|
120 |
+
output_row[classes.index(doc[1])] = 1
|
121 |
+
training.append([bag, output_row])
|
122 |
+
training = np.array(training, dtype=object)
|
123 |
+
train_x = list(training[:, 0])
|
124 |
+
train_y = list(training[:, 1])
|
125 |
+
return train_x, train_y
|
126 |
+
|
127 |
+
def build_model(self, train_x, train_y):
|
128 |
+
model = Sequential()
|
129 |
+
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
|
130 |
+
model.add(Dropout(0.5))
|
131 |
+
model.add(Dense(64, activation='relu'))
|
132 |
+
model.add(Dropout(0.5))
|
133 |
+
model.add(Dense(len(train_y[0]), activation='softmax'))
|
134 |
+
sgd = SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
135 |
+
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
|
136 |
+
return model
|
137 |
+
|
138 |
+
def train_model(self, model, train_x, train_y, epochs=200, batch_size=5):
|
139 |
+
model.fit(np.array(train_x), np.array(train_y), epochs=epochs, batch_size=batch_size, verbose=1)
|
140 |
+
model.save('Gen-E1_chatbot.h5')
|
141 |
+
return model
|
142 |
+
|
143 |
+
# Example usage
|
144 |
+
if __name__ == "__main__":
|
145 |
+
# Initialize the GenAIGlobalImpactElectionsbot
|
146 |
+
Gen_E1 = GenAIGlobalImpactElectionsbot('intents.json', 'Gen-E1_chatbot.h5', 'words.pkl', 'classes.pkl')
|
147 |
+
|
148 |
+
# Preprocess data and create training data
|
149 |
+
words, classes, documents = Gen_E1.preprocess_data()
|
150 |
+
train_x, train_y = Gen_E1.create_training_data(words, classes, documents)
|
151 |
+
|
152 |
+
# Build and train the model
|
153 |
+
model = Gen_E1.build_model(train_x, train_y)
|
154 |
+
Gen_E1.train_model(model, train_x, train_y)
|
155 |
+
|
156 |
+
# Chat with the Gen-E1
|
157 |
+
while True:
|
158 |
+
user_input = input("You: ")
|
159 |
+
if user_input.lower() in ['exit', 'quit']:
|
160 |
+
break
|
161 |
+
response = Gen_E1.chat(user_input)
|
162 |
+
print(f"Gen-E1: {response}")
|