Skip to main content

Retraining and Redeploying from Hex

When models have been trained in Hex and then deployed to Modelbit, it can be convenient to used scheduled Hex notebooks to retrain the models and redeploy them.

Ensure that you have the latest version of Modelbit in Hex

In order to ensure the latest version of the Modelbit package, run !pip install --upgrade modelbit from Hex before running import modelbit.

Put your Modelbit API key into your Hex project secret

If you haven't already, create an API key in Modelbit.

In the Hex project left nav, click "Variables." The top section is called "Secrets." Make a new secret called "MODELBIT_API_KEY" and paste in your API key as the value.

In a Hex cell, run:

import os
os.environ['MB_API_KEY'] = MODELBIT_API_KEY # Hex will paste in the MODELBIT_API_KEY secret value
os.environ['MB_WORKSPACE_NAME'] = <Your Modelbit workspace name>

Note that you cannot use Hex's built-in environment variable functionality as it will not pull in the secret value.

Retrain your model and add the new file to Modelbit

Now in a Hex cell, you can retrain your model and add it to Modelbit!

import modelbit
import pickle

# Retrain model and save pickle file
from sklearn.linear_model import LinearRegression
my_model = LinearRegression().fit([[1], [2], [3]], [2, 4, 6])
model_file = open('model.pkl', 'wb')
pickle.dump(my_model, model_file)

# Add retrained model to modelbit
modelbit.add_files('my_deployment_name', ['model.pkl'])

This call will cause Modelbit to automatically redeploy your deployment with the new file in it!

You can now run this Hex notebook on a schedule to retrain and redeploy your model automatically.