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 testing, you can use unit tests as well as 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):
"""
This is an example deployment with a unit test

>>> my_deploy_function(5)
10
"""
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. For example:

deployments/example_deployment/metadata.yaml
owner: you@your-company.com
runtimeInfo:
# dataframeModeColumns:
# - dtype: float64
# name: COL_NAME1
# - dtype: int64
# name: COL_NAME2
mainFunction: my_deploy_function
mainFunctionArgs:
- foo:int
pythonVersion: "3.8"
systemPackages:
- libgomp1
schemaVersion: 2

The fields:

  • owner: The email you're using in Modelbit. (str)
  • mainFunction: The name of the function to call in source.py with the arguments sent to your deployment. (str)
  • mainFunctionArgs: The argument names and types used by mainFunction. The possible type values are int, str, float, bool, and for everything else use Any. There should not be spaces in these values (use foo:int, not foo: int). The type values are used when constructing the SQL functions for this deployment. They do not affect the types of inputs or outputs. (List[str])
  • dataframeModeColumns: If using DataFrame Mode, specifies the columns and types expected. This field should be omitted when not using DataFrame Mode. (Optional[List[Dict[str, str]]])
  • pythonVersion: The major-minor version of Python to use. Possible values are 3.7, 3.8, 3.9, 3.10.
  • systemPackages: A list of packages to install in the production environment with apt-get. (List[str])
  • schemaVersion: The file format version of this file. Leave it as 2.

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):
"""
This is an example deployment with a unit test

>>> my_deploy_function(5)
10
"""
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.