exercise / sentence_compare.py
levimack's picture
Update sentence_compare.py
0a1635b verified
raw
history blame
659 Bytes
import gradio as gr
from transformers.utils import logging
logging.set_verbosity_error()
from sentence_transformers import SentenceTransformer
from sentence_transformers import util
def compare(input):
lines = input.splitlines()
embeddings1 = model.encode(lines[0], convert_to_tensor=True)
embeddings2 = model.encode(lines[1], convert_to_tensor=True)
cosine_scores = util.cos_sim(embeddings1, embeddings2)
return "Score: {:.4f} \t\t {} \t\t {}".format(cosine_scores[0][0], lines[0], lines[1])
model = SentenceTransformer("all-MiniLM-L6-v2")
demo = gr.Interface(fn=compare, inputs="textarea", outputs="text")
demo.launch()
gr.close_all()