Skip to content

Accessing production-ready models#

You can access and download model files based on their stage using the fetch_model_versions_table() method.

The following example illustrates how to download production-stage model files.

Assumptions

  • There exists a model with the ID CLS-PRE, which has at least one version with the stage set to "production".
  • There are model binaries stored in the "model/binary" field of the model version.
  1. Initialize a registered model and fetch its versions:

    model = neptune.init_model(
        with_id="CLS-PRE",  # (1)!
    )
    
    model_versions_df = model.fetch_model_versions_table().to_pandas()
    
    1. The Neptune ID of the model
    How do I find the ID?

    The Neptune ID is a unique identifier for the object. It's displayed in the Details view and in the leftmost column of the table views.

    The ID is stored in the system namespace. You can obtain it with object["sys/id"].fetch(). For example:

    >>> run = neptune.init_run(project="ml-team/classification")
    >>> run["sys/id"].fetch()
    'CLS-26'
    
  2. Filter the pandas DataFrame for model versions with the stage set to "production":

    production_models = model_versions_df[
        model_versions_df["sys/stage"] == "production"
    ]
    
  3. Initialize each model version and download the files from the model/binary field:

    for index, model_version in production_models.iterrows():
        version_id = model_version["sys/id"]
        model_version = neptune.init_model_version(version=version_id)
        model_version["model/binary"].download(f"{version_id}_model.xgb")
    
Stop the run when done

Once you are done logging, you should stop the Neptune run. You need to do this manually when logging from a Jupyter notebook or other interactive environment:

run.stop()

If you're running a script, the connection is stopped automatically when the script finishes executing. In notebooks, however, the connection to Neptune is not stopped when the cell has finished executing, but rather when the entire notebook stops.