| 10392 | |
| 10393 | |
| 10394 | class MTMDEmbeddingCache: |
| 10395 | _metadata_version = "1" |
| 10396 | |
| 10397 | def __init__( |
| 10398 | self, |
| 10399 | *, |
| 10400 | path: str, |
| 10401 | max_bytes: int, |
| 10402 | model_fingerprint: str, |
| 10403 | mmproj_fingerprint: str, |
| 10404 | ) -> None: |
| 10405 | self.path = Path(path) |
| 10406 | self.max_bytes = max_bytes |
| 10407 | self.model_fingerprint = model_fingerprint |
| 10408 | self.mmproj_fingerprint = mmproj_fingerprint |
| 10409 | self.path.mkdir(parents=True, exist_ok=True) |
| 10410 | |
| 10411 | @staticmethod |
| 10412 | def _safe_open(path: Path) -> Any: |
| 10413 | try: |
| 10414 | from safetensors import safe_open |
| 10415 | except ImportError as exc: |
| 10416 | raise RuntimeError( |
| 10417 | "model.mtmd.embedding_cache requires safetensors. " |
| 10418 | "Install it with `pip install safetensors`." |
| 10419 | ) from exc |
| 10420 | return safe_open(str(path), framework="numpy") |
| 10421 | |
| 10422 | @staticmethod |
| 10423 | def _save_file( |
| 10424 | tensors: Dict[str, np.ndarray], |
| 10425 | path: Path, |
| 10426 | metadata: Dict[str, str], |
| 10427 | ) -> None: |
| 10428 | from safetensors.numpy import save_file |
| 10429 | |
| 10430 | save_file(tensors, str(path), metadata=metadata) |
| 10431 | |
| 10432 | @staticmethod |
| 10433 | def fingerprint_file(path: str) -> str: |
| 10434 | stat = os.stat(path) |
| 10435 | payload = f"{Path(path).resolve()}:{stat.st_size}:{stat.st_mtime_ns}" |
| 10436 | return hashlib.sha256(payload.encode("utf-8")).hexdigest() |
| 10437 | |
| 10438 | @staticmethod |
| 10439 | def _build_key( |
| 10440 | *, |
| 10441 | model_fingerprint: str, |
| 10442 | mmproj_fingerprint: str, |
| 10443 | kind: Literal["image", "audio", "video"], |
| 10444 | media_bytes: bytes, |
| 10445 | ) -> str: |
| 10446 | digest = hashlib.sha256(f"{kind}:".encode("utf-8") + media_bytes).hexdigest() |
| 10447 | payload = ":".join( |
| 10448 | [ |
| 10449 | MTMDEmbeddingCache._metadata_version, |
| 10450 | model_fingerprint, |
| 10451 | mmproj_fingerprint, |
no outgoing calls
no test coverage detected
searching dependent graphs…