Package: 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.Run your ML models anywhere:Wrap models as a Python package or Docker Image, or deploy them to Heroku, SageMaker or Kubernetes (more platforms coming soon).
Switch between platforms transparently, with a single command.Model metadata into YAML automatically:Automatically include Python requirements and input data needs into a human-readable, deployment-ready format.
Use the same metafile on any ML framework.Stick to your training workflow:MLEM doesn't ask you to rewrite model training code.
Add just two lines around your Python code: one to import the library and one to save the model.Developer-first experience:Use the CLI when you feel like DevOps, or the API if you feel like a developer.Why is MLEM special?The main reason to use MLEM instead of other tools is to adopt aGitOps approachto manage model lifecycles.Git as a single source of truth:MLEM writes model metadata to a plain text file that can be versioned in Git along with code.
This enables GitFlow and other software engineering best practices.Unify model and software deployment:Release models using the same processes used for software updates (branching, pull requests, etc.).Reuse existing Git infrastructure:Use familiar hosting like Github or Gitlab for model management, instead of having separate services.UNIX philosophy:MLEM is a modular tool that solves one problem very well.
It integrates well into a larger toolset from Iterative.ai, such asDVCandCML.UsageThis a quick walkthrough showcasing deployment functionality of MLEM.Please readGet Started guidefor a full version.InstallationMLEM requires Python 3.$python-mpipinstallmlemTo install the pre-release version:$python-mpipinstallgit+https://github.com/iterative/mlemSaving the model# train.pyfrommlem.apiimportsavefromsklearn.ensembleimportRandomForestClassifierfromsklearn.datasetsimportload_irisdefmain():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()CodificationCheck out what we have:$lsmodels/
rf
rf.mlem
$catrf.mlemClick to show `cat` outputartifacts:data:hash:ea4f1bf769414fdacc2075ef9de73be5size:163651uri:rfmodel_type:methods:predict:args:-name:datatype_:columns:-sepal length (cm)-sepal width (cm)-petal length (cm)-petal width (cm)dtypes:-float64-float64-float64-float64index_cols:[]type:dataframename:predictreturns:dtype:int64shape:-nulltype:ndarraypredict_proba:args:-name:datatype_:columns:-sepal length (cm)-sepal width (cm)-petal length (cm)-petal width (cm)dtypes:-float64-float64-float64-float64index_cols:[]type:dataframename:predict_probareturns:dtype:float64shape:-null-3type:ndarraytype:sklearnobject_type:modelrequirements:-module:sklearnversion:1.0.2-module:pandasversion:1.4.1-module:numpyversion:1.22.3Deploying the modelIf you want to follow this Quick Start, you'll need to sign up onhttps://heroku.com,
create an API_KEY and populateHEROKU_API_KEYenv var (or runheroku loginin command line).
Besides, you'll need to runheroku container:login. This will log you in to Heroku
container registry.Now we candeploy the model withmlem deploy(you need to use differentapp_name, since it's going to be published onhttps://herokuapp.com):$mlemdeploymentrunherokuapp.mlem\--modelmodels/rf\--app_nameexample-mlem-get-started-app
⏳️Loadingmodelfrommodels/rf.mlem
⏳️Loadingdeploymentfromapp.mlem
🛠Creatingdockerimageforheroku🛠BuildingMLEMwheelfile...💼Addingmodelfiles...🛠Generatingdockerfile...💼Addingsources...💼Generatingrequirementsfile...🛠Buildingdockerimageregistry.heroku.com/example-mlem-get-started-app/web...✅Builtdockerimageregistry.heroku.com/example-mlem-get-started-app/web🔼Pushingimageregistry.heroku.com/example-mlem-get-started-app/webtoregistry.heroku.com✅Pushedimageregistry.heroku.com/example-mlem-get-started-app/webtoregistry.heroku.com
🛠Releasingappexample-mlem-get-started-appformation
✅Serviceexample-mlem-get-started-appisup.Youcancheckitoutathttps://example-mlem-get-started-app.herokuapp.com/ContributingContributions are welcome! Please see ourContributing Guidefor more details.Thanks to all our contributors!CopyrightThis 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.