tiny-press / core /scorer.py
sriharsha-cr's picture
NoGPU 0.2
c4cbe0b
Raw
History Blame
628 Bytes
import torch
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()
device = "cuda" if torch.cuda.is_available() else "cpu"
vecs = embedder.encode([original, compressed], device=device, 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)