Spaces:
Runtime error
Runtime error
File size: 847 Bytes
191204a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | from sentence_transformers import SentenceTransformer, util
from typing import Any, Optional
class ModelCompare:
def __init__(self, model: Optional[Any] = None):
if model is None:
model = init_model()
self.model = model
def __call__(self, a: str, b: str):
a_embed = self.model.encode(a)
b_embed = self.model.encode(b)
return util.cos_sim(a_embed, b_embed).numpy().squeeze()
def init_model(model_id: str = 'all-MiniLM-L6-v2'):
embedding = SentenceTransformer(model_id)
return embedding
def keyword_compare(a: str, b: str):
return a in b
if __name__ == "__main__":
model = init_model()
comp = ModelCompare(model)
print(comp("I am a dog", "I am a cat"))
print(comp("I am a dog", "I am a dog"))
print(comp("I am a dog", ["puppy", "kitten"])) |