Package: serveml
servemlservemlis a Python library that helps you package your Machine Learning model easily into a REST API.The idea behindservemlis to define a set of generic endpoints to make predictions easily !RequirementsPython 3.6+FastAPI(for the API part)MLflow(for model loading)Uvicorn(to run api)InstallationpipinstallservemlDocumentationYou can find the full documentation here :https://gfalcone.github.io/serveml/How to use ?PrerequisitesIn order to run the examples we put, you'll need an MLflow server running.As we do not expect you to have already this in place, we set up a docker container in order to speed things up.You'll need to do the following things to set up MLflow on your local machine :gitclonehttps://github.com/gfalcone/servemlcdserveml
mkdir-p/tmp/mlflow
docker-composebuild
docker-composeupTrainingFirst of all, you need to have a model already trained and registered in MlFlowLuckily for you, we already have a set of examples that you can already use.Let's say you have a scikit-learn model, like this one (taken from examples/serving/sklearn.py):"""Example taken from https://github.com/mlflow/mlflow/blob/master/examples/sklearn_elasticnet_wine/train.py"""importwarningsimportsysimportpandasaspdimportnumpyasnpfromsklearn.metricsimportmean_squared_error,mean_absolute_error,r2_scorefromsklearn.model_selectionimporttrain_test_splitfromsklearn.linear_modelimportElasticNetimportmlflowimportmlflow.sklearnimportlogginglogging.basicConfig(level=logging.WARN)logger=logging.getLogger(__name__)defeval_metrics(actual,pred):rmse=np.sqrt(mean_squared_error(actual,pred))mae=mean_absolute_error(actual,pred)r2=r2_score(actual,pred)returnrmse,mae,r2if__name__=="__main__":mlflow.set_tracking_uri("http://localhost:5000")warnings.filterwarnings("ignore")np.random.seed(40)# Read the wine-quality csv file from the URLcsv_url="http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"try:data=pd.read_csv(csv_url,sep=";")exceptExceptionase:logger.exception("Unable to download training & test CSV, check your internet connection. Error:%s",e,)# Split the data into training and test sets. (0.75, 0.25) split.train,test=train_test_split(data)# The predicted column is "quality" which is a scalar from [3, 9]train_x=train.drop(["quality"],axis=1)test_x=test.drop(["quality"],axis=1)train_y=train[["quality"]]test_y=test[["quality"]]alpha=float(sys.argv[1])iflen(sys.argv)>1else0.5l1_ratio=float(sys.argv[2])iflen(sys.argv)>2else0.5experiment_name='test_sklearn'ifmlflow.get_experiment_by_name(experiment_name)isNone:mlflow.create_experiment(experiment_name)withmlflow.start_run(experiment_id=1):lr=ElasticNet(alpha=alpha,l1_ratio=l1_ratio,random_state=42)lr.fit(train_x,train_y)predicted_qualities=lr.predict(test_x)(rmse,mae,r2)=eval_metrics(test_y,predicted_qualities)print("Elasticnet model (alpha=%f, l1_ratio=%f):"%(alpha,l1_ratio))print("  RMSE:%s"%rmse)print("  MAE:%s"%mae)print("  R2:%s"%r2)mlflow.log_param("alpha",alpha)mlflow.log_param("l1_ratio",l1_ratio)mlflow.log_metric("rmse",rmse)mlflow.log_metric("r2",r2)mlflow.log_metric("mae",mae)mlflow.sklearn.log_model(lr,"model",registered_model_name="sklearn_model")You can run it with :python-mexamples.training.sklearnServingWe can then define the API this way (taken from examples/serving/sklearn.py):fromserveml.apiimportApiBuilderfromserveml.inputsimportBasicInputfromserveml.loaderimportload_mlflow_modelfromserveml.predictionsimportGenericPrediction# load modelmodel=load_mlflow_model(# MlFlow model path'models:/sklearn_model/1',# MlFlow Tracking URI (optional)'http://localhost:5000',)# Implement deserializer for input dataclassWineComposition(BasicInput):alcohol:floatchlorides:floatcitric_acid:floatdensity:floatfixed_acidity:floatfree_sulfur_dioxide:intpH:floatresidual_sugar:floatsulphates:floattotal_sulfur_dioxide:intvolatile_acidity:int# implement applicationapp=ApiBuilder(GenericPrediction(model),WineComposition).build_api()And then run it with :uvicornexamples.serving.sklearn:app--host0.0.0.0You can now access your API's documentation, generated byredoconlocalhost:8000/redocor  access your API with Swagger onlocalhost:8000/docs:Don't forget to exit the Docker container to shut down MLflow when you're done :docker-composedownTestingUnit testsTo run unit tests, do the following :dockerbuild--tag=serveml-fDockerfile.DocumentationIf you want to look how the documentation will be rendered after making changes to it :pipinstall-rrequirements-doc.txt
mkdocsserveContributingIf you wish to make some changes, we are obviously open to Pull Requests.Please not that in order for your PR to be merged the following points are mandatory :The code must be formatted withBlack, here is the command to use to reformat your code :black.-l79CI must be green on Travis