Тесты: unit+api с фейковым эмбеддером, real-model smoke за RUN_REAL_MODEL
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""Фейковый эмбеддер: детерминированные векторы из пикселей, без torch.
|
||||
|
||||
Изображения-блочники (см. synth.make_blocks) выравниваются по сетке выборки,
|
||||
поэтому близости можно проектировать точно: совпадение блоков -> 1.0,
|
||||
непересекающиеся -> 0.0, половина перекрытия -> 0.5.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class FakeEmbedder:
|
||||
"""Контракту Embedder удовлетворяет; возвращает L2-нормированные векторы."""
|
||||
|
||||
def __init__(self, grid: int = 4):
|
||||
self.grid = grid
|
||||
self.loaded = False
|
||||
|
||||
def load(self) -> None:
|
||||
self.loaded = True
|
||||
|
||||
def embed(self, images) -> np.ndarray:
|
||||
vectors = np.stack([self._vector(img) for img in images]).astype(np.float32)
|
||||
norms = np.linalg.norm(vectors, axis=1, keepdims=True)
|
||||
return vectors / np.maximum(norms, 1e-12)
|
||||
|
||||
def _vector(self, image: Image.Image) -> np.ndarray:
|
||||
gray = image.convert("L")
|
||||
w, h = gray.size
|
||||
g = self.grid
|
||||
values = []
|
||||
for j in range(g):
|
||||
for i in range(g):
|
||||
x = int((i + 0.5) * w / g)
|
||||
y = int((j + 0.5) * h / g)
|
||||
values.append(gray.getpixel((x, y)) / 255.0)
|
||||
return np.array(values, dtype=np.float32)
|
||||
|
||||
|
||||
class FailingEmbedder:
|
||||
"""load() всегда падает — для проверки /ready=503 и 503 на /verify."""
|
||||
|
||||
def load(self) -> None:
|
||||
raise RuntimeError("boom: модель недоступна")
|
||||
|
||||
def embed(self, images): # pragma: no cover - недостижимо при неудачном load
|
||||
raise AssertionError("не должен вызываться")
|
||||
Reference in New Issue
Block a user