def post(self): self.__init() logger_info("Code @ " + self.name + " POST") prediction_result = self.bPrediction.disease_prediction( json.loads(self.request.body)) json_response = json.dumps({'result': str(prediction_result)}) self.write(json_response)
def product_prediction(self, prediction_params, model): # ----> Machine Learning Code goes here products_dict = { 'beans': 0, 'cake': 1, 'candy': 2, 'cereal': 3, 'chips': 4, 'chocolate': 5, 'coffee': 6, 'corn': 7, 'fish': 8, 'flour': 9, 'honey': 10, 'jam': 11, 'juice': 12, 'milk': 13, 'nuts': 14, 'oil': 15, 'pasta': 16, 'rice': 17, 'soda': 18, 'spices': 19, 'sugar': 20, 'tea': 21, 'tomatosauce': 22, 'vinegar': 23, 'water': 24 } reversed_prod_dict = {v: k for k, v in products_dict.items()} logger_info("Code block at Product Prediction fn.") with open("imageToSave.png", "wb") as fh: #fh.write(prediction_params["key_image"].decode('base64')) fh.write(base64.b64decode(prediction_params["key_image"])) fh.close() img = image.load_img('imageToSave.png', target_size=(227, 227)) img = image.img_to_array(img) # img = np.array(img) images = [] images.append(img) images = np.array(images) product_name = 'Oops, something went wrong' try: images = preprocess_input(images) predict = model.predict(images, batch_size=1) product_id = np.argmax(predict, axis=1) product_name = reversed_prod_dict[product_id[0]] except Exception as e: logger_error(str(e)) finally: return product_name
def Main(): tornado.options.parse_command_line() # ---> Load the app config data logger_info('Loading config file: ' + appcfg_filename) try: app_config = load_config(appcfg_filename) web_cfg = {} model = load_model('model_rsenet_aug_sqzn.txt') web_cfg['keras_model'] = model web_cfg['app_config'] = app_config web_cfg['template_path'] = os.path.join(os.path.dirname(__file__), 'templates') web_cfg['static_path'] = os.path.join(os.path.dirname(__file__), 'static') web_cfg['bind_host'] = app_config['server']['bind_host'] web_cfg['bind_port'] = options.port if web_cfg['bind_port'] == 8000: web_cfg['bind_port'] = app_config['server']['bind_port'] except Exception as e: logger_error('Exception raised during web_cfg building : ' + e.message) # --- Launch the web application --- logger_info('*** Initialize web listener (port: %s) ***' % web_cfg['bind_port']) webapp = WebApp(web_cfg) webapp.run()
def post(self): self.__init() logger_info("Code @ " + self.name + " POST") a = self.request body = tornado.escape.json_decode(self.request.body) prediction_result = self.bPrediction.sms_classifier(body, self.model) json_response = json.dumps({'result': str(prediction_result)}) self.write(json_response)
def post(self): self.__init() logger_info("Code @ " + self.name + " POST") body = tornado.escape.json_decode( self.request.body) #-----> For python 3 #body = json.loads(self.request.body) #----> For python 2 prediction_result = self.bPrediction.product_prediction( body, self.model) json_response = json.dumps({'result': str(prediction_result)}) self.write(json_response)
def __init__(self, config): self.config = config autoreload.start() logger_info('Application reloaded') rest_path = r'/api/001/' handlers = [('/?', DemoHandler), (rest_path + r'predict/?', PredictionHandler.make_api())] settings = { 'static_path': config['static_path'], 'template_path': config['template_path'] } tornado.web.Application.__init__(self, handlers, **settings)
def post(self): self.__init() logger_info("Code @ " + self.name + " POST") print(self.request.body) # json_data = tornado.escape.json_decode(self.request.body) # print("Json Data here:") # print(json_data) prediction_result = self.bPrediction.disease_prediction(json.loads(self.request.body)) json_response = json.dumps({'result': str(prediction_result)}) self.write(json_response)
def run(self, port=None, host=None): if host is None: host = self.config['bind_host'] if port is None: port = int(self.config['bind_port']) listening_port = int(os.environ.get("PORT", port)) else: listening_port = int(os.environ.get("PORT", port)) self.listen(listening_port) # --- Record actual server port number --- logger_info('*** Listening on Server (port: %s) ***' % listening_port) IOLoop.current().start()
def __init__(self, config): self.config = config autoreload.start() logger_info('Application reloaded') rest_path = r'/api/001/' handlers = [('/?', DemoHandler), (rest_path + r'predict/?', PredictionHandler.make_api()) ] settings = { 'static_path': config['static_path'] , 'template_path': config['template_path'] } tornado.web.Application.__init__(self, handlers, **settings)
def disease_prediction(self, prediction_params): # ----> Machine Learning Code goes here dataset = pd.Series(prediction_params) logger_info("Code block at Disease Prediction fn.") logger_info("Parameters received : " + str(prediction_params)) rf = pickle.load(open('ckd_prediction_model.sav', 'rb')) try: Y_pred = rf.predict(dataset) except ValueError as error: return error.message return Y_pred[0]
def post(self): self.__init() logger_info("Code @ " + self.name + " POST") print(self.request.body) # json_data = tornado.escape.json_decode(self.request.body) # print("Json Data here:") # print(json_data) prediction_result = self.bPrediction.disease_prediction( json.loads(self.request.body)) json_response = json.dumps({'result': str(prediction_result)}) self.write(json_response)
def Main(): tornado.options.parse_command_line() # ---> Load the app config data logger_info('Loading config file: ' + appcfg_filename) try: app_config = load_config(appcfg_filename) web_cfg = {} web_cfg['app_config'] = app_config web_cfg['template_path'] = os.path.join(os.path.dirname(__file__), 'templates') web_cfg['static_path'] = os.path.join(os.path.dirname(__file__), 'static') web_cfg['bind_host'] = app_config['server']['bind_host'] web_cfg['bind_port'] = options.port if web_cfg['bind_port'] == 8000: web_cfg['bind_port'] = app_config['server']['bind_port'] except Exception as e: logger_error('Exception raised during web_cfg building : '+e.message) # --- Launch the web application --- logger_info('*** Initialize web listener (port: %s) ***' % web_cfg['bind_port']) webapp = WebApp(web_cfg) webapp.run()
def sms_classifier(self, prediction_params, model): # ----> Machine Learning Code goes here logger_info("Code block at Product Prediction fn.") smsText = prediction_params["text"][0] models_dir = "tensorflow_model/" # model_name = "fasttext_keras.sav" class_mappings = {0: 'ham', 1: 'info', 2: 'spam'} # classifier = load_model(osp.join(models_dir, model_name)) sms_class = "Gibberish" try: sms_text = self.preprocess(smsText) with open(osp.join(models_dir, 'tokenizer.pickle'), 'rb') as handle: tokenizer = pickle.load(handle) sms_encoded = tokenizer.texts_to_sequences([sms_text]) sms_encoded = pad_sequences(sequences=sms_encoded, maxlen=140) predicted_probabilities = model.predict(sms_encoded) sms_class = class_mappings[np.argmax(predicted_probabilities[0])] except Exception as e: logger_error(str(e)) finally: return sms_class
def post(self): logger_info('POST method hit') self.write("Test write")
def get(self): self.write("Hello world! ML App here") logger_info("Demo Handler GET Request")
def get(self): self.__init() self.write("Prediction Handler") logger_info("Code @ " + self.name + " GET")
def get(self): self.write("Hello world! SMS predictor app here!") logger_info("Demo Handler GET Request")
def get(self): self.write("Hello! Ready to recognize product categories.") logger_info("Handling GET Request")