Skip to content

Commit

Permalink
fix: use content hash as the default model version (#5146)
Browse files Browse the repository at this point in the history
* fix: use content hash as the default model version

Signed-off-by: Frost Ming <me@frostming.com>

* fix: simplify

Signed-off-by: Frost Ming <me@frostming.com>

---------

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming authored Dec 24, 2024
1 parent 2d83a0e commit 0734383
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/_bentoml_sdk/models/huggingface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import hashlib
import json
import os
import shutil
import typing as t
Expand Down Expand Up @@ -85,25 +87,26 @@ def resolve(self, base_path: t.Union[PathType, FS, None] = None) -> str:

def to_info(self, alias: str | None = None) -> BentoModelInfo:
model_id = self.model_id.lower()
tag = Tag(model_id.replace("/", "--"), self.commit_hash)
metadata = {
"model_id": model_id,
"revision": self.commit_hash,
"endpoint": self.endpoint or DEFAULT_HF_ENDPOINT,
"include": self.include,
"exclude": self.exclude,
}
content_hash = hashlib.md5(
json.dumps(metadata, sort_keys=True, separators=(",", ":")).encode()
).hexdigest()
tag = Tag(model_id.replace("/", "--"), content_hash)
return BentoModelInfo(
tag,
alias=alias,
registry="huggingface",
metadata={
"model_id": model_id,
"revision": self.commit_hash,
"endpoint": self.endpoint or DEFAULT_HF_ENDPOINT,
"include": self.include,
"exclude": self.exclude,
},
tag, alias=alias, registry="huggingface", metadata=metadata
)

@classmethod
def from_info(cls, info: BentoModelInfo) -> HuggingFaceModel:
if not info.metadata:
name, revision = info.tag.name, info.tag.version
return cls(model_id=name.replace("--", "/"), revision=revision or "main")
name = info.tag.name
return cls(model_id=name.replace("--", "/"))
model = cls(
model_id=info.metadata["model_id"],
revision=info.metadata["revision"],
Expand Down Expand Up @@ -145,7 +148,7 @@ def to_create_schema(self) -> CreateModelSchema:
}
return CreateModelSchema(
description="",
version=revision,
version=self.to_info().tag.version or revision,
manifest=ModelManifestSchema(
module="",
metadata=metadata,
Expand Down

0 comments on commit 0734383

Please sign in to comment.