Summary
This guide demonstrates how to launch an execution when a model’s latest approved version changes.
The model’s latest approved version that comes from the notification can be a newly approved version or an older approved version when the currently latest approved version is rejected. But it is always the latest, and it is approved.
If the latest approved version changes again during the launch of the triggered execution, the execution is not automatically canceled. You can check the up-to-date status of the model version with the Valohai API if necessary. However, a new notification, therefore a new execution will then be launched with the current latest approved version.
Example Python Scripts
Make a Model
You can also use an existing model that you already have for this example. We will set up the notification automation to only trigger for this model, even if you have several or create more later.
Create a new model in the model hub.
- on the Project main page, navigate to Models
- Click “+ Create Model”
- Give the model a name. In this example, we will name the model as “Rock Formations”
If you use a different name or have an existing model, make note of the “slug” of the model,
which is the first part of the model://
URI. For the Rock Formations model, it is rock-formations
, from
the model://rock-formations
URI shown above.
This is the immutable, unique technical identifier for this model, so that our automations don’t break when the model name is changed.
Parse Notification Payload
Create a script to parse the notification payload:
new_latest_version.py
"""Parse latest model version from the payload.
The payload is a JSON file that Valohai sends to the step.
"""
import json
import valohai
# notification payload is provided in a Valohai input file
input_file = valohai.inputs("payload").path()
# get the json "payload" content from the input file
with open(input_file) as file:
payload = json.load(file)
# retrieve the new model version URI from the payload
model_version_uri = payload["data"]["model_version"]["uri"]
# output the URI
print(f"The latest approved model version is: {model_version_uri}")
# output it as metadata for use in a pipeline input edge later
print(json.dumps({"model": model_version_uri}))
valohai.yaml
- step:
name: new_latest_version
image: python:3.12
command:
- pip install valohai-utils
- python ./new_latest_version.py {parameters}
inputs:
- name: payload
Trigger Setup
Create a trigger with the following values:
- Title:
New model version handler
(can be anything, but make note of this for the next step) - Trigger type:
Notification
- Conditions:
Payload Filter
- Lookup Path:
data.model.slug
- Operation: Equals
- Invariant:
rock-formations
- Lookup Path:
- Actions:
Run Execution
- Source Commit Reference:
main
(or e.g. a reference to a specific commit) - Execution Step:
new_latest_version
(the name used in thevalohai. yaml
file) - Execution Title: (the title for the pipeline runs created by this trigger)
- Payload input name:
payload
(input names fromvalohai.yaml
)
- Source Commit Reference:
A Managed Trigger Channel notification channel is automatically created for you when you save the trigger.
The payload filter condition is responsible for limiting this trigger to the specific model. Otherwise, the trigger would run the execution for each model in the project receiving a new latest approved version.
Notification Setup
Go to Settings > Notifications > Project Notifications and create a new notification routing:
- Event:
model latest approved version changes
- Filter events by users:
All users
- Channel: select the
Launches trigger: New model version handler
channel (for the trigger you just created; if you changed the title, it will change here too).
You can now create a new model version by e.g. uploading a file, and approve it. Check the execution list for the trigger-launched execution. Once complete, it will have output like:
The latest approved model version is: model://rock-formations/1
Uploading the latest approved version files
You can use the notification execution as part of a pipeline to receive the latest model version files as an input and do something with them. In this example, we will upload the model files to a POST endpoint in an example server.
upload_latest_model.py
import valohai
import requests
# Notice! This is a non-functioning sample URL; replace it with your own service endpoint.
# It should accept a single multipart form upload with the field name "model_file".
post_endpoint = "https://example.com/model_repository/"
for file_path in valohai.inputs("model").paths():
print(f"Uploading {file_path}...")
with open(file_path, "rb") as model_file:
requests.post(
post_endpoint,
files={'model_file': model_file},
).raise_for_status()
valohai.yaml
- step:
name: new_latest_version
image: python:3.12
command:
- pip install valohai-utils
- python ./new_latest_version.py {parameters}
inputs:
- name: payload
- step:
name: upload_latest_model
image: python:3.12
command:
- pip install valohai-utils requests
- python ./upload_latest_model.py {parameters}
parameters:
- name: model_url
type: string
inputs:
- name: model
default: "{parameter:model_url}"
- pipeline:
name: Automatically upload newest approved model version
nodes:
- name: new_latest_version
step: new_latest_version
type: execution
- name: upload_latest_model
step: upload_latest_model
type: execution
edges:
- [new_latest_version.metadata.model, upload_latest_model.parameter.model_url]
Updated Trigger Setup
Change the trigger created in the prior example like so:
- Actions:
Run Pipeline
(fromRun Execution
)- Source Commit Reference:
main
(or e.g. a reference to a specific commit) - Pipeline name:
Automatically upload newest approved model version
(the name used in thevalohai. yaml
file) - Pipeline Title:
Model approved version uploader
(the title for the pipeline runs created by this trigger) - Payload input name:
new_latest_version.payload
(pipeline node and input names fromvalohai.yaml
)
- Source Commit Reference: