diff --git a/.github/workflows/register_verta_model.yaml b/.github/workflows/register_verta_model.yaml new file mode 100644 index 0000000..997c4b5 --- /dev/null +++ b/.github/workflows/register_verta_model.yaml @@ -0,0 +1,42 @@ +name: Register Verta Model Version + +on: + push: + paths: + - github-actions/register-verta-model/models/*.pkl + branches: + - workflow1 + +jobs: + Register-Verta-Model-Version: + runs-on: ubuntu-latest + defaults: + run: + working-directory: github-actions/register-verta-model + + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # fetch full history, in case push is multiple commits + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: "pip" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements.txt + + - name: Register model version + env: + VERTA_HOST: dev.verta.ai + VERTA_EMAIL: github_workflow_test@verta.ai + VERTA_DEV_KEY: ${{ secrets.TEST_ACCOUNT_DEV_KEY }} + REGISTERED_MODEL_NAME: From GitHub Action + REQUIREMENTS_FILEPATH: requirements.txt + run: | + export MODEL_FILEPATHS=$(git diff --diff-filter=AR --relative --name-only ${{ github.event.before }} ${{ github.event.after }} | grep '\.pkl$') + python register_model_version.py diff --git a/github-actions/register-verta-model/models/2023-05-01T12:48:34.291843.pkl b/github-actions/register-verta-model/models/2023-05-01T12:48:34.291843.pkl new file mode 100644 index 0000000..7459d17 Binary files /dev/null and b/github-actions/register-verta-model/models/2023-05-01T12:48:34.291843.pkl differ diff --git a/github-actions/register-verta-model/models/2023-05-01T13:04:53.716043.pkl b/github-actions/register-verta-model/models/2023-05-01T13:04:53.716043.pkl new file mode 100644 index 0000000..2b18da9 Binary files /dev/null and b/github-actions/register-verta-model/models/2023-05-01T13:04:53.716043.pkl differ diff --git a/github-actions/register-verta-model/register_model_version.py b/github-actions/register-verta-model/register_model_version.py new file mode 100644 index 0000000..2f1b172 --- /dev/null +++ b/github-actions/register-verta-model/register_model_version.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +"""This script takes model pickle files and registers them to Verta.""" + +import os + +import cloudpickle +from verta import Client +from verta.environment import Python + + +MODEL_FILEPATHS = os.environ["MODEL_FILEPATHS"].splitlines() +REGISTERED_MODEL_NAME = os.environ["REGISTERED_MODEL_NAME"] +REQUIREMENTS_FILEPATH = os.environ["REQUIREMENTS_FILEPATH"] + + +if __name__ == "__main__": + client = Client() + reg_model = client.get_or_create_registered_model(REGISTERED_MODEL_NAME) + + for model_filepath in MODEL_FILEPATHS: + with open(model_filepath, "rb") as f: + model_cls = cloudpickle.load(f) + + requirements = Python.read_pip_file( + os.path.join(os.path.dirname(__file__), REQUIREMENTS_FILEPATH), + ) + + print(f'Registering model "{model_filepath}"') + model_ver = reg_model.create_standard_model( + name=os.path.basename(model_filepath).replace(":", "_").replace(".", "_"), + model_cls=model_cls, + environment=Python(requirements), + ) diff --git a/github-actions/register-verta-model/requirements.txt b/github-actions/register-verta-model/requirements.txt new file mode 100644 index 0000000..23e5532 --- /dev/null +++ b/github-actions/register-verta-model/requirements.txt @@ -0,0 +1 @@ +verta==0.22.2 diff --git a/github-actions/register-verta-model/save_model_file.py b/github-actions/register-verta-model/save_model_file.py new file mode 100644 index 0000000..ba79b52 --- /dev/null +++ b/github-actions/register-verta-model/save_model_file.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +"""This script simply saves a pickled model class to disk. + +This is for demonstration purposes—meant to simulate a data scientist checking +a model into the repo. + +""" + +from datetime import datetime +import os + +import cloudpickle +from verta.registry import VertaModelBase, verify_io + + +MODEL_DIR = os.path.join(os.path.dirname(__file__), "models") + + +class Model(VertaModelBase): + def __init__(self, artifacts=None): + pass + + @verify_io + def predict(self, input): + return input + + +if __name__ == "__main__": + model_filename = f"{datetime.now().isoformat()}.pkl" + model_filepath = os.path.join(MODEL_DIR, model_filename) + with open(model_filepath, "wb") as f: + cloudpickle.dump(Model, f)