2023-06-27 23:01:24 +02:00
|
|
|
from typing import Any
|
2023-06-25 05:18:09 +02:00
|
|
|
|
|
|
|
from PIL.Image import Image
|
|
|
|
from sentence_transformers import SentenceTransformer
|
2023-08-06 04:45:13 +02:00
|
|
|
from sentence_transformers.util import snapshot_download
|
2023-06-25 05:18:09 +02:00
|
|
|
|
|
|
|
from ..schemas import ModelType
|
|
|
|
from .base import InferenceModel
|
|
|
|
|
|
|
|
|
|
|
|
class CLIPSTEncoder(InferenceModel):
|
|
|
|
_model_type = ModelType.CLIP
|
|
|
|
|
2023-08-06 04:45:13 +02:00
|
|
|
def _download(self, **model_kwargs: Any) -> None:
|
|
|
|
repo_id = self.model_name if "/" in self.model_name else f"sentence-transformers/{self.model_name}"
|
|
|
|
snapshot_download(
|
|
|
|
cache_dir=self.cache_dir,
|
|
|
|
repo_id=repo_id,
|
|
|
|
library_name="sentence-transformers",
|
|
|
|
ignore_files=["flax_model.msgpack", "rust_model.ot", "tf_model.h5"],
|
|
|
|
)
|
|
|
|
|
|
|
|
def _load(self, **model_kwargs: Any) -> None:
|
2023-06-25 05:18:09 +02:00
|
|
|
self.model = SentenceTransformer(
|
|
|
|
self.model_name,
|
|
|
|
cache_folder=self.cache_dir.as_posix(),
|
|
|
|
**model_kwargs,
|
|
|
|
)
|
|
|
|
|
2023-08-06 04:45:13 +02:00
|
|
|
def _predict(self, image_or_text: Image | str) -> list[float]:
|
2023-06-25 05:18:09 +02:00
|
|
|
return self.model.encode(image_or_text).tolist()
|