Starting with Git
Modelbit stores all of your source code, model artifacts and configuration in a git repository. This means you can use your preferred development tools and wokflows to create your models, and then git push
the result to Modelbit.
To get started with your first Modelbit deployment, install the modelbit package
:
pip install --upgrade modelbit
Then use modelbit clone
to clone your Modelbit git repo:
modelbit clone
Click the link to authenticate with Modelbit. Once that's complete the clone
will create a new local repository. Move your working directory into that repository:
cd modelbit
Create a simple deployment
In Modelbit you create deployments. Each deployment represents one set of source code, model artifacts, and environment dependencies. Later, after you git push
, Modelbit will collect your files, build them into a Docker image, and deploy that image to a cluster of servers supporting the REST API.
In this example we'll create a simple deployment. The process we'll follow is the same as what's needed to develop more complex deployments including those that need GPUs or have elaborate environment dependencies.
Our simple deployment will provide a REST API that uses a linear regression to double numbers. We'll call it predict_double
by creating deployment directory called predict_double
. Do that and cd
into it:
mkdir deployments/predict_double
cd deployments/predict_double
Now we're going to create four files under deployments/predict_double/
:
lm.pkl
: Our example model artifact. It'll be a scikit-learn linear regressionsource.py
: The code we'll use to load and execute the linear regressionrequirements.txt
: The list ofpip
packages needed in this deployment's environmentmetadata.yaml
: A configuration file that tells Modelbit how to run the deployment
Creating lm.pkl
Run this Python code to create and pickle a linear regression that doubles numbers:
from sklearn import linear_model
import pickle
lm = linear_model.LinearRegression()
lm.fit([[1], [2], [3]], [2, 4, 6])
with open("lm.pkl", "wb") as f:
pickle.dump(lm, f)
After running the above code, you'll have the pickle stored as deployments/predict_double/lm.pkl
. You can discard the above code as we only need lm.pkl
.
Creating source.py
When you call your deployment's REST API, Modelbit imports source.py
and executes its main function. In this case, we'll name our main function predict_double
to match the name of the deployment (but you can name the function however you'd like):
import pickle, sys
# Load our linear regression. This code will run once when the deployment starts
# since it's outside the main function
with open("lm.pkl", "rb") as f:
lm = pickle.load(f)
# This is our main function. It will get called with each call to the REST API
def predict_double(half: float) -> float:
return lm.predict([[half]])[0]
# Use this for local testing. It passes the first arg to predict_double
if __name__ == "__main__":
input = float(sys.argv[1])
print(f"Doubling {input} -> {predict_double(input)}")
Run source.py
to make sure everything works as expected:
python3 source.py 5
It should print Doubling 5.0 -> 10.0
.
Creating requirements.txt
The requirements.txt
file will be installed by pip
when Modelbit creates the production environment for your deployment. It should contain the version-locked packages needed by the deployment. Avoid putting unnecessary dependencies in here as that'll make your environment larger and slower to load.
In our case, we only need scikit-learn
to run the model:
scikit-learn==1.5.0
More complicated deployments would, of course, specify additional Python packages.
Creating metadata.yaml
The metadata.yaml
file is the only Modelbit-specific file you need in your deployment. It's used to tell the API layer how to call your model and also specifies environment details like which Python version to use.
This is the configuration needed to run our predict_double
deployment:
Make sure to update owner: ...
to your email address.
owner: you@your-company.com
runtimeInfo:
mainFunction: predict_double
mainFunctionArgs:
- half:float
- return:float
pythonVersion: "3.10"
schemaVersion: 2
The above configuration specifies predict_double
as the function to call within source.py
, and to expect one float
argument one float
return value. It also specifies running our deployment in a Python 3.10
environment.
Deploying your first model
Now that we have our deployment configured we're ready to deploy it!
First, let the modelbit
package validate the deployment's configuration:
modelbit validate
You should see output like this:
Validating deployments...
✅ predict_double
You're now ready to commit and push your deployment to Modelbit:
git add .
git commit -m "Creating predict_double"
git push
Your deployment will get sent to Modelbit and a preview link will print in your terminal. Click that to view your deployment in Modelbit! In Modelbit you'll see the REST API endpoints, Docker build logs for your deployment's environment, and more.
Call your model's REST API
You've trained a simple model and deployed it to Modelbit. Now it's time to call that deployment and see it work.
You can call your deployment from Python using modelbit.get_inference
or from the command line using curl
. The API Endpoints tab for your deployment in Modelbit will show you the exact syntax for your model. It'll look similar to this:
- modelbit.get_inference
- requests
- curl
modelbit.get_inference(deployment="predict_double", workspace="<YOUR_WORKSPACE>", data=5)
# return value
{"data": 10 }
Calling example_doubler
with the requests
package:
import json, requests
requests.post("https://<YOUR_WORKSPACE_URL>/v1/predict_double/latest",
headers={"Content-Type":"application/json"},
data=json.dumps({"data": 5})).json()
# return value
{"data": 10 }
curl -s -XPOST "https://<YOUR_WORKSPACE_URL>/v1/predict_double/latest" -d '{"data": 5}'
# return value
'{"data": 10}'
Once you've called your deployment, you'll see the results in the Logs tab within Modelbit.
Congratulations, you've deployed your first model to Modelbit!
Next steps
Now you're ready to create more complicated deployments! Your deployments can have any number of Python files and model artifacts, organized however you'd like within each deployment's directory.
Take a look at some more complex models, experiment with A/B testing, and organize your many model artifacts in the model registry.