Skip to main content

setup

info

This feature is for users deploying from a Python notebook. If you're deploying with git, put your setup code at the top of your source.py.


Use modelbit.setup to run initialization code only once during import time. This is most useful when you need custom logic to load large models from files.

Code within modelbit.setup is automatically included in performance traces.

Parameters

with modelbit.setup({name}):
...
  • name: str The name of the setup block. This name is needed later during mb.deploy.

Examples

Load a model outside the inference function

Suppose you have a large model that cannot (or should not) be pickled. Use modelbit.setup to load it when your deployment is initialized:

with modelbit.setup(name="load_model"):
my_model = ExpensiveModel.load("model_file.bin")

# main function
def predict(...):
return my_model.predict(...)

And include the setup code when deploying:

mb.deploy(predict, setup="load_model", extra_files=["model_file.bin"])

The code within the modelbit.setup will run when your deployment boots. In the example above, this means that my_model will be available to your inference function (and any other function) in the deployment, just like it is in your Python notebook.