1c5e00d8e4
- embedder.py: lazy model load rationale, RGB conversion, shared vector space
- main.py: why vec appears twice, ::vector cast, 1-distance score formula
- main_oracle.py: why array.array("f") is required instead of plain list
- main_oracle_indb.py: no embedder import — embedding done inside Oracle SQL
- index_images_oracle.py: same array.array requirement on indexing path
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
23 lines
834 B
Python
23 lines
834 B
Python
from sentence_transformers import SentenceTransformer
|
|
from PIL import Image
|
|
|
|
_model = None
|
|
|
|
def _get_model():
|
|
# Lazy load: the CLIP model is ~600 MB and takes several seconds to initialise.
|
|
# Loading on first call avoids the cost at import time and during indexing warmup.
|
|
global _model
|
|
if _model is None:
|
|
_model = SentenceTransformer("clip-ViT-B-32")
|
|
return _model
|
|
|
|
def embed_image(path: str) -> list[float]:
|
|
# CLIP requires RGB — some JPEGs are stored as CMYK or grayscale.
|
|
img = Image.open(path).convert("RGB")
|
|
return _get_model().encode(img).tolist()
|
|
|
|
def embed_text(text: str) -> list[float]:
|
|
# Text and images share the same 512-dimensional vector space in CLIP,
|
|
# so the returned vector is directly comparable to image embeddings.
|
|
return _get_model().encode(text).tolist()
|