Skip to content

Commit 0734383

Browse files
authored
fix: use content hash as the default model version (#5146)
* fix: use content hash as the default model version Signed-off-by: Frost Ming <[email protected]> * fix: simplify Signed-off-by: Frost Ming <[email protected]> --------- Signed-off-by: Frost Ming <[email protected]>
1 parent 2d83a0e commit 0734383

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/_bentoml_sdk/models/huggingface.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import hashlib
4+
import json
35
import os
46
import shutil
57
import typing as t
@@ -85,25 +87,26 @@ def resolve(self, base_path: t.Union[PathType, FS, None] = None) -> str:
8587

8688
def to_info(self, alias: str | None = None) -> BentoModelInfo:
8789
model_id = self.model_id.lower()
88-
tag = Tag(model_id.replace("/", "--"), self.commit_hash)
90+
metadata = {
91+
"model_id": model_id,
92+
"revision": self.commit_hash,
93+
"endpoint": self.endpoint or DEFAULT_HF_ENDPOINT,
94+
"include": self.include,
95+
"exclude": self.exclude,
96+
}
97+
content_hash = hashlib.md5(
98+
json.dumps(metadata, sort_keys=True, separators=(",", ":")).encode()
99+
).hexdigest()
100+
tag = Tag(model_id.replace("/", "--"), content_hash)
89101
return BentoModelInfo(
90-
tag,
91-
alias=alias,
92-
registry="huggingface",
93-
metadata={
94-
"model_id": model_id,
95-
"revision": self.commit_hash,
96-
"endpoint": self.endpoint or DEFAULT_HF_ENDPOINT,
97-
"include": self.include,
98-
"exclude": self.exclude,
99-
},
102+
tag, alias=alias, registry="huggingface", metadata=metadata
100103
)
101104

102105
@classmethod
103106
def from_info(cls, info: BentoModelInfo) -> HuggingFaceModel:
104107
if not info.metadata:
105-
name, revision = info.tag.name, info.tag.version
106-
return cls(model_id=name.replace("--", "/"), revision=revision or "main")
108+
name = info.tag.name
109+
return cls(model_id=name.replace("--", "/"))
107110
model = cls(
108111
model_id=info.metadata["model_id"],
109112
revision=info.metadata["revision"],
@@ -145,7 +148,7 @@ def to_create_schema(self) -> CreateModelSchema:
145148
}
146149
return CreateModelSchema(
147150
description="",
148-
version=revision,
151+
version=self.to_info().tag.version or revision,
149152
manifest=ModelManifestSchema(
150153
module="",
151154
metadata=metadata,

0 commit comments

Comments
 (0)