PKNVscikit_rest/__init__.py"""Automatically serve ML model as a REST API""" __version__ = "0.0.1" from flask import Flask from flask_restful import Api from scikit_rest.resource import Prediction from scikit_rest.validator import validate_type def serve(col_list, col_types, transform_fn, predict_fn, port, is_nullable=False, name="model"): validate_type(col_types) app = Flask(name) api = Api(app) api.add_resource( Prediction, "/", resource_class_kwargs={ "col_list": col_list, "col_types": col_types, "transform_fn": transform_fn, "predict_fn": predict_fn, "is_nullable": is_nullable, }, ) app.config["BUNDLE_ERRORS"] = True app.run(host="0.0.0.0", port=port) PKN-\scikit_rest/date.pyfrom dateutil.parser import parse def is_date(string): try: parse(string, fuzzy=False) return True except ValueError: return False PKYNNFwscikit_rest/resource.pyimport numpy as np import pandas as pd import datetime from dateutil.parser import parse from collections import OrderedDict from flask_restful import Resource, reqparse from skrest.validator import validate_args class Prediction(Resource): def __init__(self, **kwargs): self.col_list = kwargs["col_list"] self.col_types = kwargs["data_types"] self.transform_fn = kwargs["transform_fn"] self.predict_fn = kwargs["predict_fn"] self.is_nullable = kwargs["is_nullable"] self.parser = reqparse.RequestParser() for col in self.col_list: self.parser.add_argument(col) self.args = self.parser.parse_args() self.result = np.nan self.status_message = "Success" self.status_code = 200 is_success, status_message = validate_args(self.args, self.col_types, self.isnullable) if not is_success: self.status_message = status_message self.status_code = 400 def output(self): return ( {"data": {"result": self.result}, "message": {"status_message": self.status_message, "args": self.args}}, self.status_code, ) def get(self): if self.status_code == 200: input_dict = OrderedDict() for col in self.col_list: if self.col_type[col] == datetime.datetime: value = parse(self.args[col]) else: value = self.args[col] input_dict[col] = [value] input_df = pd.DataFrame(input_dict) transformed_df = self.transform_fn(input_df) self.result = self.predict_fn(transformed_df) return self.output() PKYNX&scikit_rest/validator.pyimport datetime import pandas as pd from skrest.date import is_date def validate_type(col_types): for key, items in col_types.items(): assert items in [str, int, float, bool, datetime.datetime], ( "Key {} has an unapproved type {}. Type must be one of the following : " "[str, int, float, bool, datetime.datetime ]".format(key, items) ) def validate_args(args, col_types, is_nullable): status_message = dict() for col, col_type in col_types.items(): if col not in args.keys(): status_message[col] = "input {} cannot be found in the payload".format(col) elif pd.isnull(args[col]): if (not is_nullable) or (col not in is_nullable): status_message[col] = "input {} is not supposed to be null".format(col) else: if col_type == datetime.datetime: if not is_date(args[col]): status_message[col] = "input {} has wrong format (supposed to be {})".format(col, "datetime") else: if not isinstance(args[col], col_type): status_message[col] = "input {} has wrong format (supposed to be {})".format(col, col_type) is_success = len(status_message) == 0 return is_success, status_message PKN=%::#scikit_rest-0.0.1.dist-info/LICENSEMIT License Copyright (c) 2019 Aditya Kelvianto Sidharta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMuSa!scikit_rest-0.0.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!H,.$scikit_rest-0.0.1.dist-info/METADATAMAn0E>\HT@j0= q(WABd?jxT4Ď/,_xaX5xZBpސҞ