-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChatbot.py
363 lines (274 loc) · 13.2 KB
/
Chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
############################################################################################
#
# Project: Peter Moss Acute Myeloid & Lymphoblastic Leukemia AI Research Project
# Repository: ALL Detection System 2019
# Project: Chatbot
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
# Contributors:
# Title: Training Class
# Description: Trains the ALL Detection System 2019 Chatbot.
# License: MIT License
# Last Modified: 2020-07-15
#
# Example Usage:
#
# python3 Chatbot.py TRAIN
# python3 Chatbot.py INPUT
# python3 Chatbot.py LOCAL "Hi how are you?"
# python3 Chatbot.py SERVER
#
############################################################################################
import sys, os, random, json, string, warnings
from flask import Flask, Response, request
from Train import Trainer
from Classes.Helpers import Helpers
from Classes.Data import Data
from Classes.Mitie import Entities
from Classes.Model import Model
from Classes.Context import Context
from Classes.Extensions import Extensions
app = Flask(__name__)
class Chatbot():
""" ALL Detection System 2019 Chatbot Class
The ALL Detection System 2019 Chatbot.
"""
def __init__(self):
""" Initializes the Chatbot class. """
self.isTraining = False
self.ner = None
self.Helpers = Helpers()
self.user = {}
self.LogFile = self.Helpers.setLogFile(self.Helpers.confs["System"]["Logs"]+"NLU/")
self.ChatLogFile = self.Helpers.setLogFile(self.Helpers.confs["System"]["Logs"]+"Chat/")
def initiateSession(self):
""" Initializes a Chatbot sesiion.
Initiates empty guest user session, GeniSys will ask the user
verify their GeniSys user by speaking or typing if it does
not know who it is speaking to.
"""
self.userID = 0
if not self.userID in self.user:
self.user[self.userID] = {}
self.user[self.userID]["history"] = {}
def initNLU(self):
""" Initializes a Chatbot sesiion.
Initiates the NLU setting up the data, NLU / entities models
and required modules such as context and extensions.
"""
self.Data = Data()
self.trainingData = self.Data.loadTrainingData()
self.trainedData = self.Data.loadTrainedData()
self.Model = Model()
self.Context = Context()
self.Extensions = Extensions()
self.restoreData()
self.restoreNER()
self.restoreNLU()
self.initiateSession()
self.setThresholds()
def commandsCallback(self,topic,payload):
""" iotJumpWay callback function.
The callback function that is triggerend in the event of a
command communication from the iotJumpWay.
"""
self.Helpers.logMessage(self.LogFile, "iotJumpWay", "INFO",
"Recieved iotJumpWay Command Data : " + str(payload))
commandData = json.loads(payload.decode("utf-8"))
def restoreData(self):
""" Restores the training data.
Sets the local trained data using data retrieved above
"""
self.trainedWords = self.trainedData["words"]
self.trainedClasses = self.trainedData["classes"]
self.x = self.trainedData["x"]
self.y = self.trainedData["y"]
self.intentMap = self.trainedData["intentMap"][0]
def loadEntityController(self):
""" Initiates the entity extractor class """
self.entityController = Entities()
def restoreNER(self):
""" Loads entity controller and restores the NER model """
self.loadEntityController()
self.ner = self.entityController.restoreNER()
def restoreNLU(self):
""" Restores the NLU model """
self.tmodel = self.Model.buildDNN(self.x, self.y)
def setThresholds(self):
""" Sets thresholds
Sets the threshold for the NLU engine, this can be changed
using arguments to commandline programs or paramters for
API calls.
"""
self.threshold = self.Helpers.confs["NLU"]["Threshold"]
self.entityThrshld = self.Helpers.confs["NLU"]["Mitie"]["Threshold"]
def communicate(self, sentence):
""" Responds to the user
First checks to ensure that the program is not training,
then parses any entities that may be in the intent, then
checks context and extensions before providing a response.
"""
if self.isTraining == False:
parsed, fallback, entityHolder, parsedSentence = self.entityController.parseEntities(
sentence,
self.ner,
self.trainingData
)
classification = self.Model.predict(self.tmodel, parsedSentence,
self.trainedWords, self.trainedClasses)
if len(classification) > 0:
clearEntities = False
theIntent = self.trainingData["intents"][self.intentMap[classification[0][0]]]
if len(entityHolder) and not len(theIntent["entities"]):
clearEntities = True
if(self.Context.checkSessionContext(self.user[self.userID], theIntent)):
if self.Context.checkClearContext(theIntent, 0):
self.user[self.userID]["context"] = ""
contextIn, contextOut, contextCurrent = self.Context.setContexts(theIntent, self.user[self.userID])
if not len(entityHolder) and len(theIntent["entities"]):
response, entities = self.entityController.replaceResponseEntities(random.choice(theIntent["fallbacks"]), entityHolder)
extension, extensionResponses, exEntities = self.Extensions.setExtension(theIntent)
elif clearEntities:
entityHolder = []
response = random.choice(theIntent["responses"])
extension, extensionResponses, exEntities = self.Extensions.setExtension(theIntent)
else:
response, entities = self.entityController.replaceResponseEntities(random.choice(theIntent["responses"]), entityHolder)
extension, extensionResponses, exEntities = self.Extensions.setExtension(theIntent)
if extension != None:
classParts = extension.split(".")
classFolder = classParts[0]
className = classParts[1]
theEntities = None
if exEntities != False:
theEntities = entities
module = __import__(classParts[0]+"."+classParts[1], globals(), locals(), [className])
extensionClass = getattr(module, className)()
response = getattr(extensionClass, classParts[2])(extensionResponses, theEntities)
return {
"Response": "OK",
"ResponseData": [{
"Received": sentence,
"Intent": classification[0][0],
"Confidence": str(classification[0][1]),
"Response": response,
"Context": [{
"In": contextIn,
"Out": contextOut,
"Current": contextCurrent
}],
"Extension": extension,
"Entities": entityHolder
}]
}
else:
self.user[self.userID]["context"] = ""
contextIn, contextOut, contextCurrent = self.Context.setContexts(theIntent, self.user[self.userID])
if fallback and fallback in theIntent and len(theIntent["fallbacks"]):
response, entities = self.entityController.replaceResponseEntities(random.choice(theIntent["fallbacks"]), entityHolder)
extension, extensionResponses = None, []
else:
response, entities = self.entityController.replaceResponseEntities(random.choice(theIntent["responses"]), entityHolder)
extension, extensionResponses, exEntities = self.Extensions.setExtension(theIntent)
if extension != None:
classParts = extension.split(".")
classFolder = classParts[0]
className = classParts[1]
theEntities = None
if exEntities != False:
theEntities = entities
module = __import__(classParts[0]+"."+classParts[1], globals(), locals(), [className])
extensionClass = getattr(module, className)()
response = getattr(extensionClass, classParts[2])(extensionResponses, theEntities)
else:
response = self.entityController.replaceResponseEntities(random.choice(theIntent["responses"]), entityHolder)
if(type(response)==tuple):
response = response[0]
return {
"Response": "OK",
"ResponseData": [{
"Received": sentence,
"Intent": classification[0][0],
"Confidence": str(classification[0][1]),
"Response": response,
"Context": [{
"In": contextIn,
"Out": contextOut,
"Current": contextCurrent
}],
"Extension": extension,
"Entities": entityHolder
}]
}
else:
contextCurrent = self.Context.getCurrentContext(self.user[self.userID])
return {
"Response": "FAILED",
"ResponseData": [{
"Received": sentence,
"Intent": "UNKNOWN",
"Confidence": "NA",
"Responses": [],
"Response": random.choice(self.Helpers.confs["NLU"]["defaultResponses"]),
"Context": [{
"In": "NA",
"Out": "NA",
"Current": contextCurrent
}],
"Extension":"NA",
"Entities": entityHolder
}]
}
else:
return {
"Response": "FAILED",
"ResponseData": [{
"Status": "Training",
"Message": "NLU Engine is currently training"
}]
}
Chatbot = Chatbot()
@app.route("/infer", methods = ["POST"])
def infer():
""" Inference endpoint
Is triggered when an authorized request is made to the infer
endpoint.
"""
Chatbot.initiateSession()
if request.headers["Content-Type"] == "application/json":
query = request.json
response = Chatbot.communicate(query["query"])
return Response(response=json.dumps(response, indent=4, sort_keys=True),
status=200, mimetype="application/json")
if __name__ == "__main__":
if sys.argv[1] == "TRAIN":
""" Training mode
Is triggered when the 1st commandline line argument is TRAIN.
"""
Train = Trainer()
Train.trainModel()
elif sys.argv[1] == "SERVER":
""" Server mode
Is triggered when the 1st commandline line argument is SERVER.
"""
Chatbot.initNLU()
Chatbot.Helpers.logMessage(
Chatbot.LogFile,
"Inference",
"INFO",
"Inference Started In SERVER Mode")
app.run(host=Chatbot.Helpers.confs["System"]["IP"], port=Chatbot.Helpers.confs["System"]["Port"])
elif sys.argv[1] == "INPUT":
""" Input mode
Is triggered when the 1st commandline line argument is INPUT.
"""
Chatbot.initNLU()
while True:
intent = input(">")
Chatbot.Helpers.logMessage(Chatbot.ChatLogFile,
"Human", "Intent", intent)
response = Chatbot.communicate(intent)
Chatbot.Helpers.logMessage(Chatbot.ChatLogFile, "GeniSys",
"Response", str(response["ResponseData"][0]["Response"]))
Chatbot.Helpers.logMessage(Chatbot.ChatLogFile, "GeniSys",
"Raw Response", str(response["ResponseData"]), True)