tiny-press / core /scorer.py
sriharsha-cr's picture
task/embedings (#6)
39003c5
raw
history blame contribute delete
540 Bytes
import numpy as np
from models.model_loader import get_embedder
try:
import spaces
_gpu = spaces.GPU
except ImportError:
_gpu = lambda fn: fn # no-op when running locally without the spaces package
@_gpu
def semantic_score(original: str, compressed: str) -> float:
embedder = get_embedder()
vecs = embedder.encode([original, compressed], convert_to_numpy=True)
cos = float(
np.dot(vecs[0], vecs[1]) / (np.linalg.norm(vecs[0]) * np.linalg.norm(vecs[1]))
)
return round(max(0.0, min(1.0, cos)), 4)