def unload_model(): if not request.json: abort(ConfigManager.error_code_400) json_data = request.json response_json = dict() status = dict() response_code = 200 try: if not LMS.LoadModelService().check_if_model_load(json_data): raise CE.ModelNotInMemory("No such model loaded", 403) response_json["success"] = LMS.LoadModelService().unload_model( json_data) status["loaded"] = False mp.ModelProcessor().update_key_mongo(json_data["appId"], json_data["modelName"], status) except CE.ModelNotInMemory as e: ErrorLogger.exception('EXCEPTION %s: NO such model "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except Exception as e: ErrorLogger.exception('EXCEPTION %s: Damm! Something Blew up', 500) response_json["success"] = False response_json["message"] = "EXCEPTION 500: Damm! Something Blew up" response_code = 500 return jsonify(response_json), response_code
def load_model(): if not request.json: abort(ConfigManager.error_code_400) json_data = request.json response_json = dict() response_json["success"] = True response_json["message"] = "InProgress" response_code = 200 try: if mp.ModelProcessor().get_model_mongo_byAppIdandmodelName_count( json_data) == 0: raise CE.ModelDoesNotExist("Model name does not exist", 403) elif LMS.LoadModelService().check_if_model_load(json_data): raise CE.ModelAlreadyInMemory("Model already deployed", 403) thread = Thread(target=LMS.LoadModelService().load_model, args=(json_data, )) thread.start() InfoLogger.info( "New thread started to load model for Appid: %s and Modelname: %s", str(json_data["appId"]), str(json_data["modelName"])) except CE.ModelDoesNotExist as e: ErrorLogger.exception('EXCEPTION %s: NO such model "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except CE.ModelAlreadyInMemory as e: ErrorLogger.exception('EXCEPTION %s: NO such model "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except Exception as e: ErrorLogger.exception('EXCEPTION %s: Damm! Something Blew up', 500) response_json["success"] = False response_json["message"] = "EXCEPTION 500: Damm! Something Blew up" response_code = 500 return jsonify(response_json), response_code
def batchPredict(): if not request.json: abort(ConfigManager.error_code_400) json_data = request.json response_json = dict() response_json["success"] = True response_code = 200 try: if mp.ModelProcessor().get_model_mongo_byAppIdandmodelName_count( json_data) == 0: raise CE.ModelDoesNotExist("Model name does not exist", 403) elif not LMS.LoadModelService().check_if_model_load(json_data): raise CE.ModelNotInMemory( "Model not deployed, Please deploy model", 403) response_json["prediction"] = str( PR.PredictService().predict_batch_service(json_data)) except CE.ModelDoesNotExist as e: ErrorLogger.exception('EXCEPTION %s: NO such model "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except CE.ModelNotInMemory as e: ErrorLogger.exception('EXCEPTION %s: NO such model "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except CE.InvalidPredictionParams as e: ErrorLogger.exception('EXCEPTION %s:Invalid Prediction Params "%s"', str(e.errors), str(e)) response_json["success"] = False response_json["message"] = str(e) response_code = int(e.errors) except Exception as e: ErrorLogger.exception('EXCEPTION %s: Damm! Something Blew up', 500) response_json["success"] = False response_json["message"] = "EXCEPTION 500: Damm! Something Blew up" response_code = 500 return jsonify(response_json), response_code
def delete_model(self, model_params): status = False Directory = ConfigManager.ROOT_DIR + "/TrainedModelsDirectory" filename = str(model_params["modelName"]) + "_" + str(model_params["algorithm"]["name"]) + "_" \ + str(model_params["appId"]) + ".sav" filepath = os.path.join(Directory, filename) try: result = mp.ModelProcessor().delete_model_mongo(model_params) if result["n"] >= 1: status = True if LMS.LoadModelService().check_if_model_load(model_params): status = LMS.LoadModelService().unload_model(model_params) # remove trained model file if os.path.isfile(filepath): os.remove(filepath) except Exception as e: ErrorLogger.exception('EXCEPTION %s: Damm! Something Blew up', 500) raise Exception("Failed to delete model: %s" % e.message, 500) return status
def testGetModelDetails(self): test_data = { "appId": "1", "modelName": "newdetnew", "features": [ "{\"value\":\"S038\",\"columnName\":\"Center code\"}", "{\"value\":\"P006\",\"columnName\":\"TestCode\"}" ], "algorithm": { "name": "Naive Bayes", "type": "gaussianNB", "isDefault": True, "priors": "None" } } test_data = json.dumps(test_data) test_data = json.loads(test_data) LMS.LoadModelService().load_model(test_data) GMS.ModelService().delete_model(test_data) print(str(PR.PredictService().predict_service(test_data)))
def predict_service(self, model_params): feature_dict = dict() feature_list = list() prediction_dict = dict() model_collection_data = mp.ModelProcessor( ).get_model_mongo_byAppIdandmodelName(model_params) if not len(model_params['features']) == len( model_collection_data["featureList"]): raise CE.InvalidPredictionParams( "Invalid number of features, expected: " + str(len(model_collection_data["featureList"])) + " got: " + str(len(model_params['features'])), 403) # Add feature value to list and check if any categorical feature for feature in model_params["features"]: feature = json.loads(feature) if feature["value"]: if feature["columnName"] in model_collection_data[ "featureList"]: if 'categoricalData' in model_collection_data: if feature["columnName"] in model_collection_data[ "categoricalData"]: if feature["value"] in model_collection_data[ "categoricalData"][ feature["columnName"]]["encoding"]: feature_dict[str( feature["columnName"] )] = model_collection_data["categoricalData"][ feature["columnName"]]["encoding"][ feature["value"]] else: raise CE.InvalidPredictionParams( "Model not trained for feature:" + feature["columnName"] + " with value:" + str(feature["value"]), 403) else: if type(feature["value"]) == int or type( feature["value"]) == float: feature_dict[str( feature["columnName"])] = feature["value"] else: raise CE.InvalidPredictionParams( "Model not trained for this feature:" + feature["columnName"] + " to be categorical. Enter Int value", 403) else: if type(feature["value"]) == int or type( feature["value"]) == float: feature_dict[str( feature["columnName"])] = feature["value"] else: raise CE.InvalidPredictionParams( "Model not trained for this feature:" + feature["columnName"] + " to be categorical. Enter Int value", 403) else: raise CE.InvalidPredictionParams( "Model not trained with feature: " + feature["columnName"], 403) else: raise CE.InvalidPredictionParams( "Value can not be null for feature: " + feature["columnName"], 403) for feature in model_collection_data["featureList"]: feature_list.append(feature_dict[str(feature)]) # scale data Directory = ConfigManager.ROOT_DIR + "/TrainedModelsDirectory" filename = str(model_params["modelName"]) + "_" + "minmaxScaler_" \ + str(model_params["appId"]) + ".sav" filepath = os.path.join(Directory, filename) min_max_scaler = pickle.load(open(filepath, 'rb')) feature_list = min_max_scaler.transform([feature_list])[0] # model file name if "Neural Network" in model_params["algorithm"]["name"]: filename = str(model_params["modelName"]) + "_" + str(model_params["algorithm"]["name"]) \ + "_" + str(model_params["appId"]) + ".hdf5" else: filename = str(model_params["modelName"]) + "_" + str(model_params["algorithm"]["name"]) \ + "_" + str(model_params["appId"]) + ".sav" # load the model from memory loaded_model = LMS.LoadModelService().load_model_dict[filename] # Make prediction if model_params["algorithm"]["name"] == 'Neural Network': feature_list = np.array([feature_list]) from keras import backend with backend.get_session().graph.as_default() as g: prediction_list = loaded_model.predict_classes(feature_list) prediction_list = prediction_list[0] else: print("Something else") prediction_list = loaded_model.predict([feature_list]) target_counter = 0 for target in model_collection_data["targetList"]: if 'categoricalData' in model_collection_data: if target in model_collection_data["categoricalData"]: cat_dict = dict(model_collection_data["categoricalData"] [target]["encoding"]) cat_decoded_value = list(cat_dict.keys())[list( cat_dict.values()).index( prediction_list[target_counter])] prediction_dict[str(target)] = str(cat_decoded_value) target_counter = target_counter + 1 else: prediction_dict[str( target)] = prediction_list[target_counter] target_counter = target_counter + 1 else: prediction_dict[str(target)] = prediction_list[target_counter] target_counter = target_counter + 1 return prediction_dict