class Client(): def __init__(self, user): self.Helpers = Helpers() self.Logging = Logging() self._confs = self.Helpers.loadConfigs() self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"] + "Client/") self.apiUrl = self._confs["AI"]["FQDN"] + "/communicate/infer/" + user self.headers = {"content-type": 'application/json'} self.Logging.logMessage(self.LogFile, "CLIENT", "INFO", "GeniSys AI Client Ready")
class JumpWayREST(): def __init__(self): self.Helpers = Helpers() self.Logging = Logging() self._confs = self.Helpers.loadConfigs() self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"]+"Client/") def createHashMac(self, secret, data): return hmac.new(bytearray(secret.encode("utf-8")), data.encode("utf-8"), digestmod=hashlib.sha256).hexdigest() def apiCall(self, apiUrl, data, headers): self.Logging.logMessage( self.LogFile, "JUMPWAY", "INFO", "Sending JumpWay REST Request") response = requests.post( apiUrl, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth( self._confs["iotJumpWay"]["App"], self.createHashMac( self._confs["iotJumpWay"]["API"]["Secret"], self._confs["iotJumpWay"]["API"]["Secret"]))) output = json.loads(response.content) self.Logging.logMessage( self.LogFile, "JUMPWAY", "INFO", "JumpWay REST Response Received: " + str(output)) return output
class Humans(): def __init__(self): self.Helpers = Helpers() self.Logging = Logging() self.JumpWayREST = JumpWayREST() self._confs = self.Helpers.loadConfigs() self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"]+"Client/") self.Logging.logMessage( self.LogFile, "CLIENT", "INFO", "GeniSys AI JumpWay REST Client Ready") def getHumanByFace(self, response): data = {} headers = {'content-type': 'application/json'} cameraEnpoint = self._confs["iotJumpWay"]["API"]["REST"] + "/TASS/0_1_0/checkCamera" self.Logging.logMessage( self.LogFile, "HUMANS", "INFO", "Checking Camera...") response = self.JumpWayREST.apiCall( cameraEnpoint, data, headers) self.Logging.logMessage( self.LogFile, "CLIENT", "OK", "Response: "+str(response)) if response["Response"] == "OK": responseLength = len(response["ResponseData"]) if responseLength == 1: message = "I detected " + str(responseLength) + " human, " + response["ResponseData"][0]["userid"] else: message = "I detected " + str(responseLength) + " humans, " return message else: return response["ResponseMessage"]
class NLU(): def __init__(self): self.Helpers = Helpers() self.Logging = Logging() self._confs = self.Helpers.loadConfigs() self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"] + "NLU/") self.ChatLogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"] + "Chat/") self.Logging.logMessage(self.LogFile, "NLU", "INFO", "NLU Classifier LogFile Set") self.startMQTT() def commandsCallback(self, topic, payload): self.Logging.logMessage( self.LogFile, "iotJumpWay", "INFO", "Recieved iotJumpWay Command Data : " + str(payload)) commandData = json.loads(payload.decode("utf-8")) def startMQTT(self): try: self.jumpwayClient = jumpWayDevice.DeviceConnection({ "locationID": self._confs["iotJumpWay"]["Location"], "zoneID": self._confs["iotJumpWay"]["Zone"], "deviceId": self._confs["iotJumpWay"]["Device"], "deviceName": self._confs["iotJumpWay"]["DeviceName"], "username": self._confs["iotJumpWay"]["MQTT"]["Username"], "password": self._confs["iotJumpWay"]["MQTT"]["Password"] }) self.jumpwayClient.connectToDevice() self.jumpwayClient.subscribeToDeviceChannel("Commands") self.jumpwayClient.deviceCommandsCallback = self.commandsCallback self.Logging.logMessage(self.LogFile, "iotJumpWay", "INFO", "iotJumpWay Client Ready") except Exception as e: self.Logging.logMessage(self.LogFile, "iotJumpWay", "INFO", "iotJumpWay Client Initiation Failed") print(str(e)) sys.exit() def setup(self): self.Logging.logMessage(self.LogFile, "NLU", "INFO", "NLU Classifier Initiating") self.Data = Data(self.Logging, self.LogFile) self.Model = Model() self.Context = Context() self.user = {} self.ner = None self.trainingData = self.Data.loadTrainingData() self.trainedData = self.Data.loadTrainedData() self.trainedWords = self.trainedData["words"] self.trainedClasses = self.trainedData["classes"] self.x = self.trainedData["x"] self.y = self.trainedData["y"] self.intentMap = self.trainedData["iMap"][0] self.restoreEntitiesModel() self.restoreModel() self.Logging.logMessage(self.LogFile, "NLU", "INFO", "NLU Ready") def restoreEntitiesModel(self): if os.path.exists(self._confs["ClassifierSettings"]["EntitiesDat"]): self.ner = named_entity_extractor( self._confs["ClassifierSettings"]["EntitiesDat"]) self.Logging.logMessage(self.LogFile, "NER", "OK", "Restored NLU NER Model") def restoreModel(self): self.tmodel = self.Model.buildDNN(self.x, self.y) self.Logging.logMessage(self.LogFile, "NLU", "INFO", "Restored NLU Model") def setupEntities(self): if self._confs["ClassifierSettings"]["Entities"] == "Mitie": self.entityExtractor = Entities() self.Logging.logMessage(self.LogFile, "NER", "INFO", "NLU Entity Extractor Initiated") def initiateSession(self, userID): self.userID = userID if not self.userID in self.user: self.user[self.userID] = {} self.user[self.userID]["history"] = {} self.Logging.logMessage(self.LogFile, "Session", "INFO", "NLU Session Ready For User #" + self.userID) def setThresholds(self, threshold): self.threshold = float(threshold) self.entityThrshld = self._confs["ClassifierSettings"]["Mitie"][ "Threshold"] def predict(self, parsedSentence): predictions = [[index, confidence] for index, confidence in enumerate( self.tmodel.predict([ self.Data.makeInferenceBag(parsedSentence, self.trainedWords) ])[0]) if confidence > self.threshold] predictions.sort(key=lambda x: x[1], reverse=True) classification = [] for prediction in predictions: classification.append( (self.trainedClasses[prediction[0]], prediction[1])) return classification def talk(self, sentence, debug=False): self.Logging.logMessage(self.LogFile, "GeniSys", "STATUS", "Processing") parsed, fallback, entityHolder, parsedSentence = self.entityExtractor.parseEntities( sentence, self.ner, self.trainingData) classification = self.predict(parsedSentence) 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 fallback and "fallbacks" in theIntent and len( theIntent["fallbacks"]): response = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["fallbacks"]), entityHolder) action, actionResponses = self.Helpers.setAction(theIntent) elif "entityType" in theIntent and theIntent[ "entityType"] == "Numbers": response = random.choice(theIntent["responses"]) action, actionResponses = self.Helpers.setAction(theIntent) elif not len(entityHolder) and len(theIntent["entities"]): response = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["fallbacks"]), entityHolder) action, actionResponses = self.Helpers.setAction(theIntent) elif clearEntities: entityHolder = [] response = random.choice(theIntent["responses"]) action, actionResponses = self.Helpers.setAction(theIntent) else: response = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["responses"]), entityHolder) action, actionResponses = self.Helpers.setAction(theIntent) if action != None: classParts = action.split(".") classFolder = classParts[0] className = classParts[1] module = __import__(classParts[0] + "." + classParts[1], globals(), locals(), [className]) actionClass = getattr(module, className)() response = getattr(actionClass, classParts[2])( random.choice(actionResponses)) return { "Response": "OK", "ResponseData": [{ "Received": sentence, "Intent": classification[0][0], "Confidence": str(classification[0][1]), "Response": response, "ContextIn": contextIn, "ContextOut": contextOut, "Context": contextCurrent, "Action": action, "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 = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["fallbacks"]), entityHolder) action, actionResponses = None, [] else: response = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["responses"]), entityHolder) action, actionResponses = self.Helpers.setAction(theIntent) if action != None: classParts = action.split(".") classFolder = classParts[0] className = classParts[1] module = __import__(classParts[0] + "." + classParts[1], globals(), locals(), [className]) actionClass = getattr(module, className)() response = getattr(actionClass, classParts[2])( random.choice(actionResponses)) else: response = self.entityExtractor.replaceResponseEntities( random.choice(theIntent["responses"]), entityHolder) return { "Response": "OK", "ResponseData": [{ "Received": sentence, "Intent": classification[0][0], "Confidence": str(classification[0][1]), "Response": response, "ContextIn": contextIn, "ContextOut": contextOut, "ContextCurrent": contextCurrent, "Action": action, "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._confs["ClassifierSettings"]["defaultResponses"]), "ContextIn": "NA", "ContextOut": "NA", "ContextCurrent": contextCurrent, "Action": "NA", "Entities": entityHolder }] }
class Trainer(): def __init__(self, jumpWay): self.Helpers = Helpers() self.Logging = Logging() self.jumpwayCl = jumpWay self._confs = self.Helpers.loadConfigs() self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"] + "Train/") self.Logging.logMessage(self.LogFile, "LogFile", "INFO", "NLU Trainer LogFile Set") self.Model = Model() self.Data = Data(self.Logging, self.LogFile) self.intentMap = {} self.words = [] self.classes = [] self.dataCorpus = [] self.setupData() self.setupEntities() def setupData(self): self.trainingData = self.Data.loadTrainingData() self.Logging.logMessage(self.LogFile, "Trainer", "INFO", "Loaded NLU Training Data") self.words, self.classes, self.dataCorpus, self.intentMap = self.Data.prepareData( self.trainingData) self.x, self.y = self.Data.finaliseData(self.classes, self.dataCorpus, self.words) self.Logging.logMessage(self.LogFile, "TRAIN", "INFO", "NLU Trainer Data Ready") def setupEntities(self): if self._confs["ClassifierSettings"]["Entities"] == "Mitie": self.entityExtractor = Entities() self.Logging.logMessage(self.LogFile, "TRAIN", "OK", "NLU Trainer Entity Extractor Ready") self.entityExtractor.trainEntities( self._confs["ClassifierSettings"]["Mitie"]["ModelLocation"], self.trainingData) def trainModel(self): while True: self.Logging.logMessage(self.LogFile, "TRAIN", "ACTION", "Ready To Begin Training ? (Yes/No)") userInput = input(">") if userInput == 'Yes': break if userInput == 'No': exit() humanStart, trainingStart = self.Helpers.timerStart() self.Logging.logMessage(self.LogFile, "TRAIN", "INFO", "NLU Model Training At " + humanStart) self.jumpwayCl.publishToDeviceChannel( "Training", { "NeuralNet": "NLU", "Start": trainingStart, "End": "In Progress", "Total": "In Progress", "Message": "NLU Model Training At " + humanStart }) self.Model.trainDNN(self.x, self.y, self.words, self.classes, self.intentMap) trainingEnd, trainingTime, humanEnd = self.Helpers.timerEnd( trainingStart) self.Logging.logMessage( self.LogFile, "TRAIN", "OK", "NLU Model Trained At " + humanEnd + " In " + str(trainingEnd) + " Seconds") self.jumpwayCl.publishToDeviceChannel( "Training", { "NeuralNet": "NLU", "Start": trainingStart, "End": trainingEnd, "Total": trainingTime, "Message": "NLU Model Trained At " + humanEnd + " In " + str(trainingEnd) + " Seconds" })