Skip to main content

Deploying with Git

You can use git push to create and manage your ML deployments. This is often a good fit for projects with with already pickled dependencies or complex directories of Python source files.

To create a new deployment, clone to your Modelbit git repository and create a new directory under deployments/. The name of the directory will be the name of your deployment. Use only letters, numbers, and underscores in the directory name.

tip

Create your first deployment using mb.deploy(...) in a notebook, then modify the generated files to simplify deployment configuration via git.

Required files

Then create the following three files within your new directory:

1. requirements.txt

This file defined the Python packages your deployment requires, and will be installed using pip. For example:

deployments/example_deployment/requirements.txt
scikit-learn==1.0.2
pandas==1.5.0

2. source.py

This file is the entrypoint for executing your model. For local testing, you can read arguments from the command line (like the example below). In production the main function will be called directly. For example:

deployments/example_deployment/source.py
import sys

# main function
def my_deploy_function(foo: int):
return 2 * foo

# to run locally via git & terminal
if __name__ == "__main__":
print(my_deploy_function(int(sys.argv[1])))

3. metadata.yaml

This is the last file Modelbit needs in order to deploy your model. It defines which method to call in source.py, the argument types to expect, and the environment to create.

Learn more about this file in the metadata.yaml docs.

Deploy

To deploy, git add and git commit these three files, then git push to Modelbit! Your new deployment will appear in the deployments tab.

Additional Python source files

If you have additional source files, import them in source.py and call their functions from within the main function of the deployment (in this case, my_deploy_function). For example:

deployments/example_deployment/helpers.py
def double_a_number(num: int):
return 2 * num

And, the new source.py:

deployments/example_deployment/source.py
from helpers import double_a_number

def my_deploy_function(foo: int):
return double_a_number(foo)

Including .pkl dependencies

Likewise if you have .pkl files you'll want to load in your production environment simply add them to git in this directory. For example, we'll load model.pkl for making predictions:

deployments/example_deployment/source.py
import pickle

with open("model.pkl", "rb") as f:
model = pickle.load(f)

def my_deploy_function(foo: int):
return model.predict([[foo]])[0]

Testing locally

Modelbit's deployment environments import source.py and run the deployment function (vs. executing source.py directly). You can reproduce Modelbit's runtime behavior locally, and uncover issues, by testing that the deploy function will run when imported:

Testing using a notebook

Create a notebook in the same directory as source.py, and then import and run your deployment function:

import source
source.my_deploy_function(...)

Testing using the command line

Create a new file, local.py, in the same directory as source.py and then import and run your deployment function within the __main__ block:

local.py

if __name__ == "__main__":
import source
print(source.my_deploy_function(...))

Then call python local.py from the command line.

Archiving

To archive a deployment add a empty .archived file in that deployment's directory. For example, for a deployment named doubler you'd make the file deployments/doubler/.archived.

When you git push that deployment will be archived on the current branch.