|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import hashlib |
| 4 | +import json |
3 | 5 | import os
|
4 | 6 | import shutil
|
5 | 7 | import typing as t
|
@@ -85,25 +87,26 @@ def resolve(self, base_path: t.Union[PathType, FS, None] = None) -> str:
|
85 | 87 |
|
86 | 88 | def to_info(self, alias: str | None = None) -> BentoModelInfo:
|
87 | 89 | 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) |
89 | 101 | 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 |
100 | 103 | )
|
101 | 104 |
|
102 | 105 | @classmethod
|
103 | 106 | def from_info(cls, info: BentoModelInfo) -> HuggingFaceModel:
|
104 | 107 | 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("--", "/")) |
107 | 110 | model = cls(
|
108 | 111 | model_id=info.metadata["model_id"],
|
109 | 112 | revision=info.metadata["revision"],
|
@@ -145,7 +148,7 @@ def to_create_schema(self) -> CreateModelSchema:
|
145 | 148 | }
|
146 | 149 | return CreateModelSchema(
|
147 | 150 | description="",
|
148 |
| - version=revision, |
| 151 | + version=self.to_info().tag.version or revision, |
149 | 152 | manifest=ModelManifestSchema(
|
150 | 153 | module="",
|
151 | 154 | metadata=metadata,
|
|
0 commit comments