def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print( "ERROR API (predict): received request, but no 'query' found within" ) return jsonify([]) if 'type' not in request.json: print( "WARNING API (predict): received request, but no 'type' was found assuming 'numpy'" ) query_type = 'numpy' query = request.json['query'] idx = query['idx'] query = np.array(query['data']) ## load model model = model_load() if not model: print("ERROR: model is not navailable") return jsonify([]) _result = {} if idx == 1: _result[idx] = (model_predict(query, model)) else: for i, j in list(zip(query, idx)): _result[j] = (model_predict(i, model)) result = {} result['y_pred'] = {} ## convert numpy objects so ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result['y_pred'][key] = item.tolist() else: result['y_pred'][key] = item return (jsonify(result))
def predict(): if request.method == 'POST': max_price = float(request.form["max_price"]) fat_content = request.form["fat_content"] visibility = float(request.form["visibility"]) category = request.form["category"] store_size = request.form["store_size"] store_location_type = request.form["store_location_type"] store_type = request.form["store_type"] user_item_details = { "FatContent": [fat_content], "Visibility": [visibility], "Category": [category], "Max_Price": [max_price], "Store_Size": [store_size], "Store_Location_Type": [store_location_type], "Store_Type": [store_type] } # user_item_details.extend([visibility, max_price, fat_content, category, store_size, store_location_type, store_type]) encoded_item_details = bundas.preprocess_input(user_item_details) print(encoded_item_details) sales_prediction = bundas.model_predict(encoded_item_details) pred = sales_prediction[0].astype(str) print(sales_prediction[0]) return "<h1>Sales Prediction is " + pred + "</h1>" form = PredictionForm() return render_template('custom.html', form=form)
def transliterate(): print("Transliterating...") post_data = request.form return jsonify({ "input": post_data['input'], 'transliteration': model_predict(post_data['input']) })
def predict(): response = { 'result': -1 } try: country = request.args.get('country') year = request.args.get('year') month = request.args.get('month') day = request.args.get('day') env = request.args.get('env') res = model_predict(country, year, month, day, test=(env == 'test')) # text = '<h1 style="color:blue;">result'+str(res)+'</h1>' # print('type(res: ', type(res)) if isinstance(res, dict): if 'y_pred' in res.keys(): response['prediction'] = res['y_pred'].tolist() response['result'] = 0 else: response['error'] = str(res) print('response: \n', response) except: response['error'] = 'system error:' + str(sys.exc_info()[1]) json_object = json.dumps(response, indent=4) return Response(str(json_object), mimetype="application/json")
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'country' not in request.json: print( "ERROR API (predict): received request, but no 'country' found within" ) return jsonify([]) if 'date' not in request.json: print( "WARNING API (predict): received request, but no 'date' was found within" ) ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the country country_input = request.json['country'] date_input = request.json['date'] ## load model model = model_load(test=test) if not model: print("ERROR: model is not available") return jsonify([]) _result = model_predict(date=date_input, country=country_input, df=None, model=model, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item indexes = pd.Series([]) for i in range(len(_result["predicted"].index)): indexes[i] = str(_result["predicted"].index[i]) _result["predicted"].index = indexes _result["predicted"] = _result["predicted"].to_json() return (jsonify(_result))
def predict(): if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'country' not in request.json: print( "ERROR API (predict): received request, but no 'country' found within" ) return jsonify([]) if 'year' not in request.json: print( "ERROR API (predict): received request, but no 'year' found within" ) return jsonify([]) if 'month' not in request.json: print( "ERROR API (predict): received request, but no 'month' found within" ) return jsonify([]) if 'day' not in request.json: print( "ERROR API (predict): received request, but no 'day' found within") return jsonify([]) test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query country = request.json['country'] year = request.json['year'] month = request.json['month'] day = request.json['day'] ## load model (all_data, all_models) = model_load() if not all_models: print("ERROR: models are not available") return jsonify([]) _result = model_predict(country, year, month, day, all_models, all_data, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def test_log(self): today = date.today() logfile = "predict-{}-{}.log".format(today.year, today.month) beforecount = len(pd.read_csv(logfile)) out = model_predict("all", "2018", "06", "09") aftercount = len(pd.read_csv(logfile)) print(beforecount, aftercount) self.assertEqual(beforecount < aftercount, True)
def predict(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: file = request.files['file'] prediction_text = model_predict(file) render_template('predict', prediction_text=prediction_text)
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print( "ERROR API (predict): received request, but no 'query' found within" ) return jsonify([]) if 'type' not in request.json: print( "WARNING API (predict): received request, but no 'type' was found assuming 'numpy'" ) query_type = 'numpy' ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query query = request.json['query'] print(query) if request.json['type'] == 'dict': pass else: print( "ERROR API (predict): only dict data types have been implemented") return jsonify([]) ## load model model = model_load() if not model: print("ERROR: model is not available") return jsonify([]) _result = model_predict(query, model, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def build_model( X, y, X_train, y_train, X_test, y_test, seed, method, ): model = model.Model() evc_meta = model.model_ensemble(X, y, method=method) model.model_predict(evc_meta, X_train, y_train, X_test, y_test, seed=seed) model.cross_validate(evc_meta, X, y, seed) print("Start dumping Meta classifier...") joblib.dump(evc_meta, "meta_clf.pkl") print("Done dumping Meta classifier ! \n") return evc_meta
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'country' not in request.json: print( "ERROR API (predict): received request, but no 'country' found within" ) return jsonify([]) #if 'type' not in request.json: # print("WARNING API (predict): received request, but no 'type' was found assuming 'numpy'") # query_type = 'numpy' ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query country = request.json['country'] year = request.json['year'] month = request.json['month'] day = request.json['day'] #if request.json['type'] == 'dict': # pass #else: # print("ERROR API (predict): only dict data types have been implemented") # return jsonify([]) ## load model data_dir = os.path.join("data", "cs-train") all_data, all_models = model_load(country, data_dir=data_dir) model = all_models[country] if not model: print("ERROR: model is not available") return jsonify([]) _result = model_predict(country, year, month, day, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def predict(): text = request.form["Date"] year = text.split('-')[0] month = text.split('-')[1] date = text.split('-')[2] country = request.form["Country"] prediction = model_predict(country, year, month, date) prediction_jsonify = prediction['y_pred'].tolist()[0] output_text = country + ": Predicted Forecast for 30 day period on " + text + " is: " + str( round(prediction_jsonify, 2)) return jsonify(output_text)
def predictgui(): country,date_ = [x for x in request.form.values()] inputs = date_.split('-') inputs.insert(0,country) output = model_predict(*inputs,from_pickle=True) prediction_text = f"Revenue for the next 30 days in {country.replace('_',' ').title()} should be {round(output['y_pred'][0])}" # prediction_text = f"This doesn't look right! Try again!" return render_template('index.html', prediction_text=prediction_text)
def model_monitor(country="all", dev=DEV, training=True): """ performance monitoring """ print("Monitor Model") ## import data #datasets = engineer_features(training=training, dev=dev) datasets = engineer_features(training=training) X, y, dates, labels = datasets[country] dates = pd.to_datetime(dates) print(X.shape) ## train the model if training: _model_train(X, y, labels, tag=country, dev=dev) ## monitor RMSE samples = [10, 20, 30, 50, 60] for n in samples: X_new, y_new, dates_new = simulate_samples(n, X, y, dates) queries = [(str(d.year), str(d.month), str(d.day), country) for d in dates_new] y_pred = [model_predict(year=query[0], month=query[1], day=query[2], country=query[3],verbose=False, dev=dev)["y_pred"][0].round(2) for query in queries] rmse = np.sqrt(mean_squared_error(y_new.tolist(),y_pred)) print("sample size: {}, RSME: {}".format(n, rmse.round(2))) ## monitor performance ## scaling scaler = StandardScaler() X = scaler.fit_transform(X) samples = [25, 50, 75, 90] clf_y = EllipticEnvelope(random_state=0,contamination=0.01) clf_X = EllipticEnvelope(random_state=0,contamination=0.01) clf_X.fit(X) clf_y.fit(y.reshape(y.size,1)) results = defaultdict(list) for n in samples: X_new, y_new, dates_new = simulate_samples(n,X,y, dates) results["sample_size"].append(n) results['wasserstein_X'].append(np.round(wasserstein_distance(X.flatten(),X_new.flatten()),2)) results['wasserstein_y'].append(np.round(wasserstein_distance(y,y_new),2)) test1 = clf_X.predict(X_new) test2 = clf_y.predict(y_new.reshape(y_new.size,1)) results["outlier_percent_X"].append(np.round(1.0 - (test1[test1==1].size / test1.size),2)) results["outlier_percent_y"].append(np.round(1.0 - (test2[test2==1].size / test2.size),2)) return pd.DataFrame(results)
def test_03_predict(self): """ test the predict function input """ ## test predict country = 'all' year = '2018' month = '01' day = '05' result = model_predict(country, year, month, day) y_pred = result['y_pred'] self.assertTrue(y_pred)
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) print(request.json) if 'country' not in request.json: print( "ERROR API (predict): received request, but no 'country' found within" ) return jsonify(False) ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query parameters country = request.json['country'] year = request.json['year'] month = request.json['month'] day = request.json['day'] print(country, year, month, day) ## load model data_dir = os.path.join("data", "cs-train") all_data, all_models = model_load(data_dir=data_dir) model = all_models[country] if not model: print("ERROR: model is not available") return jsonify([]) ## predict _result = model_predict(country, year, month, day, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def predict(): if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print("ERROR API (predict): received request, but no 'query' found within") return jsonify([]) query = request.json['query'] y_pred = model_predict(query) return(jsonify(y_pred.tolist()))
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print("ERROR API (predict): received request, but no 'query' found within") return jsonify([]) if 'type' not in request.json: print("WARNING API (predict): received request, but no 'type' was found assuming 'numpy'") query_type = 'numpy' ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query query = request.json['query'] if request.json['type'] == 'dict': pass else: print("ERROR API (predict): only dict data types have been implemented") return jsonify([]) print(query) result = {} if query['country'] == 'all': countries = ['Portugal', 'United Kingdom', 'Hong Kong', 'EIRE', 'Spain', 'France', 'Singapore', 'Norway', 'Germany', 'Netherlands'] else: countries = query['country'].split(',') for country in countries: _result = model_predict(train_data, country, query['date']) result_dict = { "Country": country, "y_pred": _result} print("Predicted revenue for {} is {}".format(country, np.round(_result[0], 2))) result[country] = convert_numpy_objects(result_dict) return (jsonify(result))
def predict(): """ basic predict function for the API """ print(f"Request: {request.json}") ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print( "ERROR API (predict): received request, but no 'query' found within" ) return jsonify([]) # if 'type' not in request.json: # print("WARNING API (predict): received request, but no 'type' was found assuming 'numpy'") # query_type = 'numpy' ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query query = request.json['query'] ## load model data_dir = os.path.join(".", "data", "cs-production") all_data, all_models = model_load(data_dir=data_dir, training=False) if not all_models: print("ERROR: model is not available") return jsonify([]) _result = model_predict(**query, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item print(f"Result: {result}") print(f"JSON: {jsonify(result)}") return (jsonify(result))
def test_03_predict(self): """ test the predict function input """ # load model first prefix = 'test' country = 'united_kingdom' year = '2018' month = '01' day = '05' test = True result = model_predict(prefix, country, year, month, day, test=test) y_pred = result['y_pred'] self.assertTrue(result is not None)
def predict(): """ basic predict function for the API """ print(request.json) ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print("ERROR API (predict): received request, but no 'query' found within") return jsonify([]) if 'type' not in request.json: print("WARNING API (predict): received request, but no 'type' was found assuming 'numpy'") query_type = 'numpy' query = request.json['query'] if request.json['type'] == 'numpy': query = np.array(query) else: print("ERROR API (predict): only numpy data types have been implemented") return jsonify([]) ## load model model = model_load() if not model: print("ERROR: model is not available") return jsonify([]) _result = model_predict(query,model) result = {} ## convert numpy objects so ensure they are serializable for key,item in _result.items(): if isinstance(item,np.ndarray): result[key] = item.tolist() else: result[key] = item return(jsonify(result))
def predict(): """ basic predict function for the API """ country = request.args.get('country') target_date = request.args.get('target_date') ## input checking if target_date is None: print( "ERROR API (predict): received request, but no 'target_date' found within" ) return jsonify([]), 400 if country is None: print( "WARNING API (predict): received request, but no country specified, assuming 'all'" ) country = 'all' m = re.match(r'(\d{4})-(\d{2})-(\d{2})', target_date) try: year, month, day = m.group(1, 2, 3) except: print("ERROR API (predict): 'target_date' format is invalid") return jsonify([]), 400 result = {} try: _result = model_predict(country, year, month, day, all_models=global_models, all_data=global_data) ## convert numpy objects so ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result)) except Exception as e: print("ERROR API (predict): model_predict returned: {}".format(str(e))) return jsonify([]), 400
def test_03_predict(self): """ ensure log file is created """ log_file = os.path.join(log_dir, "predict-test.log") # if os.path.exists(log_file): # os.remove(log_file) # update the log prefix = 'test' country = 'all' year = '2018' month = '01' day = '05' test = True result = model_predict(prefix, country, year, month, day, test=test) self.assertTrue(os.path.exists(log_file))
def test_predict_result_is_numeric(self): data_dir = join('.', 'data') models_dir = join('.', 'models') work_dir = join(data_dir, 'work-data') inpfile = join(work_dir, 'train-data-cleaned.csv') force_data_load = True if exists(inpfile): force_data_load = False ukmodelfile = join(models_dir, 'test-united_kingdom-' + MODEL_VERSION + '.joblib') if not exists(ukmodelfile): model_train(data_dir=data_dir, model_dir=models_dir, test=True, force_data_load=force_data_load) pred = model_predict('united_kingdom', '2018', '1', '1', data_dir=data_dir, model_dir=models_dir, test=True) # print(pred) return pred['y_pred'] > 0
def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) country = request.json['country'] year = request.json['year'] month = request.json['month'] day = request.json['day'] model = model_load(country) if not model: print("ERROR: model is not available") return jsonify([]) print("... predicting") result = model_predict(country, year, month, day, model) print("... prediction complete") return (jsonify(result))
def main(): print("LOADING MODELS") production_data_dir = os.path.join("data", "cs-production") all_data, all_models = model_load(data_dir=production_data_dir) print("... models loaded: ", ",".join(all_models.keys())) count = 0 for country in all_data.keys(): if all_data[country]['X'].shape[0] > 0: for date in all_data[country]['dates']: dt = datetime.strptime(date, '%Y-%m-%d') query = { 'country': country, 'year': str(dt.year), 'month': str(dt.month), 'day': str(dt.day) } # input checks if country not in all_models.keys(): result = "ERROR (model_predict) - model for country '{}' could not be found".format( country) else: result = model_predict(query, data=all_data[country], model=all_models[country], test=True) count += 1 print('result[', count, ']: ', result) print("model test predict complete.")
def predict(): if not request.json: print("No request data") return jsonify([]) if 'country' not in request.json: print("Please provide the country name") return jsonify([]) if 'day' not in request.json: print("Please provide the day") return jsonify([]) if 'month' not in request.json: print("Please provide the month") return jsonify([]) if 'year' not in request.json: print("Please provide the year") return jsonify([]) test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True country = request.json['country'] day = request.json['day'] month = request.json['month'] year = request.json['year'] data_dir = os.path.join(".", "data", "cs-train") all_data, all_models = model_load(data_dir=data_dir) model = all_models[country] if not model: print("Mo models avaliable") return jsonify([]) _result = model_predict(country, year, month, day, test=test) result = {} for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def predict(): """ basic predict function for the API country='all' year='2019' month='04' day='06' 2017-11-29 2019-05-31 """ run_start = time.time() country = request.args.get('country', default='all', type=str) date = request.args.get('date', default='2019-05-06', type=str) startDate = dt.strptime("2017-11-29", "%Y-%m-%d") endDate = dt.strptime("2019-05-31", "%Y-%m-%d") try: predictDate = dt.strptime(date, "%Y-%m-%d") except: return jsonify(errMsg='Error Date, date should be in range 2017-11-29 - 2019-05-31') if startDate <= predictDate < endDate: dataSplit = date.split('-') year = dataSplit[0] month = dataSplit[1] day = dataSplit[2] try: result = model_predict(prefix='sl', country=country, year=year, month=month, day=day) except: return jsonify(msg='model_predict error') m, s = divmod(time.time() - run_start, 60) h, m = divmod(m, 60) return jsonify(status='OK', date=date, country=country, y_pred=result['y_pred'][0], runningTime="%d:%02d:%02d" % (h, m, s), ) else: return jsonify(errMsg='Error Date, date should be in range 2017-11-29 - 2019-05-31')
def predict(): if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'query' not in request.json: print( "ERROR API (predict): received request, but no 'query' found within" ) return jsonify([]) query = request.json['query'] _result = model_predict(*query) result = {} for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result["y_pred"]))
# *** # ************************************************************************************/ # import argparse from PIL import Image import model parser = argparse.ArgumentParser(description='Image Classifier Predictor') parser.add_argument('-model', type=str, default=model.DEFAULT_MODEL, help='trained model [' + model.DEFAULT_MODEL + ']') parser.add_argument('-device', type=str, default="cuda:0", help='cuda:0 or cpu [cuda:0]') parser.add_argument('images', type=str, nargs='+', help='image files') if __name__ == '__main__': args = parser.parse_args() net = model.load_model(args.device, args.model) classnames = model.load_class_names(args.model) for i in range(len(args.images)): image = Image.open(args.images[i]).convert('RGB') label, prob = model.model_predict(args.device, net, image) print('Image class: %d, %s, %.2f, %s' % (label, classnames[label], prob, args.images[i]))