Skip to content

Commit 4d345ac

Browse files
authored
feat: Model Versioning (#284)
* add version fields to model * add parent_artifact_id to upload_model * add versioning instructions to model creation tutorial * frozen=True on artifact_version
1 parent c4079da commit 4d345ac

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

docs/tutorials/create_a_model.ipynb

+22-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"\n",
2222
"# Create a new Model Card\n",
2323
"model = Model(\n",
24-
" name=\"MolGPS\", \n",
24+
" name=\"MolGPS\",\n",
2525
" description=\"Graph transformer foundation model for molecular modeling\",\n",
2626
" code_url=\"https://github.com/datamol-io/graphium\"\n",
2727
")"
@@ -44,6 +44,27 @@
4444
"model.upload_to_hub(owner=\"your-username\")"
4545
]
4646
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"If you want to upload a new version of your model, you can specify its previous version with the `parent_artifact_id` parameter. Don't forget to add a changelog describing your updates!"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"model.artifact_changelog = \"In this version, I added...\"\n",
61+
"\n",
62+
"model.upload_to_hub(\n",
63+
" owner=\"your-username\",\n",
64+
" parent_artifact_id=\"your-username/tutorial-example\"\n",
65+
")"
66+
]
67+
},
4768
{
4869
"cell_type": "markdown",
4970
"metadata": {},

polaris/benchmark/_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class BenchmarkSpecification(
133133
readme: str = ""
134134

135135
# Version-related fields
136-
artifact_version: int = 1
136+
artifact_version: int = Field(default=1, frozen=True)
137137
artifact_changelog: str | None = None
138138

139139
@computed_field

polaris/dataset/_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class BaseDataset(BaseArtifactModel, abc.ABC):
8686
curation_reference: HttpUrlString | None = None
8787

8888
# Version-related fields
89-
artifact_version: int = 1
89+
artifact_version: int = Field(default=1, frozen=True)
9090
artifact_changelog: str | None = None
9191

9292
# Private attributes

polaris/hub/client.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ def upload_model(
942942
model: Model,
943943
access: AccessType = "private",
944944
owner: HubOwner | str | None = None,
945+
parent_artifact_id: str | None = None,
945946
):
946947
"""Upload a model to the Polaris Hub.
947948
@@ -959,6 +960,7 @@ def upload_model(
959960
model: The model to upload.
960961
access: Grant public or private access to result
961962
owner: Which Hub user or organization owns the artifact. Takes precedence over `model.owner`.
963+
parent_artifact_id: The `owner/slug` of the parent model, if uploading a new version of a model.
962964
"""
963965
with track_progress(description="Uploading model", total=1) as (progress, task):
964966
# Get the serialized model data-structure
@@ -967,11 +969,16 @@ def upload_model(
967969

968970
# Make a request to the Hub
969971
url = f"/v2/model/{model.artifact_id}"
970-
response = self._base_request_to_hub(url=url, method="PUT", json={"access": access, **model_json})
972+
response = self._base_request_to_hub(
973+
url=url,
974+
method="PUT",
975+
json={"access": access, "parentArtifactId": parent_artifact_id, **model_json},
976+
)
977+
978+
# NOTE: When we merge in the competition model feature, we will need to update the slug with the inserted model slug to make sure we write to the correct storage location.
971979

972980
# Inform the user about where to find their newly created artifact.
973981
model_url = urljoin(self.settings.hub_url, response.headers.get("Content-Location"))
974-
975982
progress.log(
976983
f"[green]Your model has been successfully uploaded to the Hub. View it here: {model_url}"
977984
)

polaris/model/__init__.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from polaris._artifact import BaseArtifactModel
22
from polaris.utils.types import HttpUrlString
33
from polaris.utils.types import AccessType, HubOwner
4+
from pydantic import Field
45

56

67
class Model(BaseArtifactModel):
@@ -30,6 +31,8 @@ class Model(BaseArtifactModel):
3031
readme (str): A detailed README describing the model.
3132
code_url (HttpUrlString | None): Optional URL pointing to the model's code repository.
3233
report_url (HttpUrlString | None): Optional URL linking to a report or publication related to the model.
34+
artifact_version: The version of the model.
35+
artifact_changelog: A description of the changes made in this model version.
3336
3437
Methods:
3538
upload_to_hub(access: AccessType = "private", owner: HubOwner | str | None = None):
@@ -44,11 +47,20 @@ class Model(BaseArtifactModel):
4447
code_url: HttpUrlString | None = None
4548
report_url: HttpUrlString | None = None
4649

47-
def upload_to_hub(self, access: AccessType = "private", owner: HubOwner | str | None = None):
50+
# Version-related fields
51+
artifact_version: int = Field(default=1, frozen=True)
52+
artifact_changelog: str | None = None
53+
54+
def upload_to_hub(
55+
self,
56+
access: AccessType = "private",
57+
owner: HubOwner | str | None = None,
58+
parent_artifact_id: str | None = None,
59+
):
4860
"""
4961
Uploads the model to the Polaris Hub.
5062
"""
5163
from polaris.hub.client import PolarisHubClient
5264

5365
with PolarisHubClient() as client:
54-
client.upload_model(self, owner=owner, access=access)
66+
client.upload_model(self, owner=owner, access=access, parent_artifact_id=parent_artifact_id)

0 commit comments

Comments
 (0)