MLEM helps you with machine learning model deployment. It saves ML models in a standard format that can be used in a variety of downstream deployment scenarios such as real-time serving through a REST API or batch processing.
- Run your model anywhere you want: package it as a Python package, a Docker Image or deploy it to Heroku (SageMaker, Kubernetes and more platforms are coming). Switch between formats and deployment platforms with a single command thanks to unified abstraction.
- Simple YAML file to save model metadata: automatically package Python env requirements and input data specifications into a ready-to-deploy format. Use the same human-readable format for any ML framework.
- Stick to your training workflow:Β MLEM doesn't ask you to rewrite your training code. To start using packaging or deployment machinery, add just two lines to your python script: one to import the library and one to save the model.
- Developer-first experience: use CLI when you feel like DevOps and API when you feel like a developer.
- MLEM automatically detects ML framework, Python requirements, model methods and input/output data specifications, saving your time and preventing manual errors.
- MLEM is designed forΒ Git-centered ML models development. Use GitOps with Git as the single source of truth. Enable GitFlow and other software engineering best practices.
- MLEM is made with Unix philosophy in mind - one tool solves one problem very well. Plug MLEM into your toolset, easily integrating it with other tools like DVC.
This a quick walkthrough showcasing deployment and export functionality of MLEM.
Please read Get Started guide for a full version.
Install MLEM with pip:
$ pip install mlem
To install the pre-release version, run:
$ pip install git+https://github.com/iterative/mlem
# 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,
"rf",
sample_data=data,
labels=["random-forest", "classifier"],
description="Random Forest Classifier",
)
if __name__ == "__main__":
main()
Check out what we have:
$ ls
rf
rf.mlem
$ cat rf.mlem
> Click to show `cat` output
artifacts:
data:
hash: ea4f1bf769414fdacc2075ef9de73be5
size: 163651
uri: rf
description: Random Forest Classifier
labels:
- random-forest
- classifier
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
sklearn_predict:
args:
- name: X
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
sklearn_predict_proba:
args:
- name: X
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
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.
First, create an environment to deploy your model:
$ mlem declare env heroku staging
πΎ Saving env to staging.mlem
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 mydeploy -m rf -t staging -c app_name=mlem-quick-start
β³οΈ Loading deployment from .mlem/deployment/myservice.mlem
π Loading link to .mlem/env/staging.mlem
π Loading link to .mlem/model/rf.mlem
πΎ Updating deployment at .mlem/deployment/myservice.mlem
π Creating Heroku App example-mlem-get-started
πΎ Updating deployment at .mlem/deployment/myservice.mlem
π Creating docker image for heroku
πΌ Adding model files...
π Generating dockerfile...
πΌ Adding sources...
πΌ Generating requirements file...
π Building docker image registry.heroku.com/example-mlem-get-started/web...
β
Built docker image registry.heroku.com/example-mlem-get-started/web
πΌ Pushed image registry.heroku.com/example-mlem-get-started/web to remote registry at host registry.heroku.com
πΎ Updating deployment at .mlem/deployment/myservice.mlem
π Releasing app my-mlem-service formation
πΎ Updating deployment at .mlem/deployment/myservice.mlem
β
Service example-mlem-get-started is up. You can check it out at https://mlem-quick-start.herokuapp.com/
You could easily export the model to a different format using mlem build
:
$ mlem build rf docker -c server.type=fastapi -c image.name=sklearn-model
β³οΈ Loading model from rf.mlem
π Building MLEM wheel file...
πΌ Adding model files...
π Generating dockerfile...
πΌ Adding sources...
πΌ Generating requirements file...
π Building docker image sklearn-model:latest...
β
Built docker image sklearn-model:latest
Contributions are welcome! Please see our Contributing Guide for more details. Thanks to all our contributors!
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.