Home

Awesome

image

Check, test and release codecov PyPi License: Apache 2.0

<!-- [![Maintainability](https://codeclimate.com/github/iterative/mlem/badges/gpa.svg)](https://codeclimate.com/github/iterative/mlem) -->

MLEM helps you package and deploy machine learning models. It saves ML models in a standard format that can be used in a variety of production scenarios such as real-time REST serving or batch processing.

Why is MLEM special?

The main reason to use MLEM instead of other tools is to adopt a GitOps approach to manage model lifecycles.

Usage

This a quick walkthrough showcasing deployment functionality of MLEM.

Please read Get Started guide for a full version.

Installation

MLEM requires Python 3.

$ python -m pip install mlem

To install the pre-release version:

$ python -m pip install git+https://github.com/iterative/mlem

Saving the model

# train.py
from mlem.api import save
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

def main():
    data, y = load_iris(return_X_y=True, as_frame=True)
    rf = RandomForestClassifier(
        n_jobs=2,
        random_state=42,
    )
    rf.fit(data, y)

    save(
        rf,
        "models/rf",
        sample_data=data,
    )

if __name__ == "__main__":
    main()

Codification

Check out what we have:

$ ls models/
rf
rf.mlem
$ cat rf.mlem
<details> <summary> Click to show `cat` output</summary>
artifacts:
  data:
    hash: ea4f1bf769414fdacc2075ef9de73be5
    size: 163651
    uri: rf
model_type:
  methods:
    predict:
      args:
      - name: data
        type_:
          columns:
          - sepal length (cm)
          - sepal width (cm)
          - petal length (cm)
          - petal width (cm)
          dtypes:
          - float64
          - float64
          - float64
          - float64
          index_cols: []
          type: dataframe
      name: predict
      returns:
        dtype: int64
        shape:
        - null
        type: ndarray
    predict_proba:
      args:
      - name: data
        type_:
          columns:
          - sepal length (cm)
          - sepal width (cm)
          - petal length (cm)
          - petal width (cm)
          dtypes:
          - float64
          - float64
          - float64
          - float64
          index_cols: []
          type: dataframe
      name: predict_proba
      returns:
        dtype: float64
        shape:
        - null
        - 3
        type: ndarray
  type: sklearn
object_type: model
requirements:
- module: sklearn
  version: 1.0.2
- module: pandas
  version: 1.4.1
- module: numpy
  version: 1.22.3
</details>

Deploying the model

If you want to follow this Quick Start, you'll need to sign up on https://heroku.com, create an API_KEY and populate HEROKU_API_KEY env var (or run heroku login in command line). Besides, you'll need to run heroku container:login. This will log you in to Heroku container registry.

Now we can deploy the model with mlem deploy (you need to use different app_name, since it's going to be published on https://herokuapp.com):

$ mlem deployment run heroku app.mlem \
  --model models/rf \
  --app_name example-mlem-get-started-app
ā³ļø Loading model from models/rf.mlem
ā³ļø Loading deployment from app.mlem
šŸ›  Creating docker image for heroku
  šŸ›  Building MLEM wheel file...
  šŸ’¼ Adding model files...
  šŸ›  Generating dockerfile...
  šŸ’¼ Adding sources...
  šŸ’¼ Generating requirements file...
  šŸ›  Building docker image registry.heroku.com/example-mlem-get-started-app/web...
  āœ…  Built docker image registry.heroku.com/example-mlem-get-started-app/web
  šŸ”¼ Pushing image registry.heroku.com/example-mlem-get-started-app/web to registry.heroku.com
  āœ…  Pushed image registry.heroku.com/example-mlem-get-started-app/web to registry.heroku.com
šŸ›  Releasing app example-mlem-get-started-app formation
āœ…  Service example-mlem-get-started-app is up. You can check it out at https://example-mlem-get-started-app.herokuapp.com/

Contributing

Contributions are welcome! Please see our Contributing Guide for more details.

Thanks to all our contributors!

Copyright

This project is distributed under the Apache license version 2.0 (see the LICENSE file in the project root).

By submitting a pull request to this project, you agree to license your contribution under the Apache license version 2.0 to this project.