Skip to main content

serveml is a machine learning serving tool

Project description

serveml

Build Status

serveml is a Python library that helps you package your Machine Learning model easily into a REST API.

The idea behind serveml is to define a set of generic endpoints to make predictions easily !

Requirements

Installation

pip install serveml

Documentation

You can find the full documentation here : https://gfalcone.github.io/serveml/

How to use ?

Prerequisites

In 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 :

git clone https://github.com/gfalcone/serveml
cd serveml
docker-compose build
docker-compose up

Training

First of all, you need to have a model already trained and registered in MlFlow

Luckily 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
"""
import warnings
import sys

import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet

import mlflow
import mlflow.sklearn

import logging

logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)


def eval_metrics(actual, pred):
    rmse = np.sqrt(mean_squared_error(actual, pred))
    mae = mean_absolute_error(actual, pred)
    r2 = r2_score(actual, pred)
    return rmse, mae, r2


if __name__ == "__main__":
    mlflow.set_tracking_uri("http://localhost:5000")
    warnings.filterwarnings("ignore")
    np.random.seed(40)

    # Read the wine-quality csv file from the URL
    csv_url = "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
    try:
        data = pd.read_csv(csv_url, sep=";")
    except Exception as e:
        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]) if len(sys.argv) > 1 else 0.5
    l1_ratio = float(sys.argv[2]) if len(sys.argv) > 2 else 0.5

    experiment_name = 'test_sklearn'
    if mlflow.get_experiment_by_name(experiment_name) is None:
        mlflow.create_experiment(experiment_name)

    with mlflow.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 -m examples.training.sklearn

Serving

We can then define the API this way (taken from examples/serving/sklearn.py):

from serveml.api import ApiBuilder
from serveml.inputs import BasicInput
from serveml.loader import load_mlflow_model
from serveml.predictions import GenericPrediction

# load model
model = load_mlflow_model(
    # MlFlow model path
    'models:/sklearn_model/1',
    # MlFlow Tracking URI (optional)
    'http://localhost:5000',
)


# Implement deserializer for input data
class WineComposition(BasicInput):
    alcohol: float
    chlorides: float
    citric_acid: float
    density: float
    fixed_acidity: float
    free_sulfur_dioxide: int
    pH: float
    residual_sugar: float
    sulphates: float
    total_sulfur_dioxide: int
    volatile_acidity: int


# implement application
app = ApiBuilder(GenericPrediction(model), WineComposition).build_api()

And then run it with :

uvicorn examples.serving.sklearn:app --host 0.0.0.0

You can now access your API's documentation, generated by redoc on localhost:8000/redoc or access your API with Swagger on localhost:8000/docs :

API

Don't forget to exit the Docker container to shut down MLflow when you're done :

docker-compose down

Testing

Unit tests

To run unit tests, do the following :

docker build --tag=serveml -f Dockerfile .

Documentation

If you want to look how the documentation will be rendered after making changes to it :

pip install -r requirements-doc.txt
mkdocs serve

Contributing

If 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 with Black, here is the command to use to reformat your code :
black . -l 79
  • CI must be green on Travis

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serveml-0.2.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

serveml-0.2.1-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

File details

Details for the file serveml-0.2.1.tar.gz.

File metadata

  • Download URL: serveml-0.2.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for serveml-0.2.1.tar.gz
Algorithm Hash digest
SHA256 aff6cf8763d9eb6fef2260b630da21764a80ec7fa9bdcd37401e01f270e05547
MD5 ea9964c4228672bd5f6c7853f14ea97c
BLAKE2b-256 5bf7032d704d873a1db13726c2ba922ef9dce7094bdef620ddce8a4cb5c2a03f

See more details on using hashes here.

File details

Details for the file serveml-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: serveml-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 35.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for serveml-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87fb4bb0d6df892c74dbccc9c9511aac9d6545961d3630e9cb25ca0ff1da16db
MD5 1746346f424f857c57dfbe159e6dc7c4
BLAKE2b-256 0d6ecca08a1b9fec1819c29fa015be4a6018d3bdb49a648fb6346934e4ac33b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page