|
from sentence_transformers import SentenceTransformer, util |
|
import gradio as gr |
|
|
|
|
|
model = SentenceTransformer('sartifyllc/AviLaBSE') |
|
|
|
|
|
def compute_similarity(original_sentence, sentences_to_compare): |
|
|
|
embeddings_original = model.encode([original_sentence]) |
|
embeddings_sentences_to_compare = model.encode(sentences_to_compare) |
|
|
|
|
|
similarities = util.cos_sim(embeddings_original, embeddings_sentences_to_compare) |
|
|
|
|
|
results = [] |
|
for i, sentence in enumerate(sentences_to_compare): |
|
results.append((sentence, similarities[0][i].item())) |
|
|
|
return results |
|
|
|
|
|
def sentence_similarity(original_sentence, sentence1, sentence2, sentence3): |
|
sentences_to_compare = [sentence1, sentence2, sentence3] |
|
results = compute_similarity(original_sentence, sentences_to_compare) |
|
return results |
|
|
|
iface = gr.Interface( |
|
fn=sentence_similarity, |
|
inputs=[ |
|
gr.Textbox(label="Original Sentence"), |
|
gr.Textbox(label="Sentence 1"), |
|
gr.Textbox(label="Sentence 2"), |
|
gr.Textbox(label="Sentence 3") |
|
], |
|
outputs=gr.Table(label="Similarity Scores", columns=["Sentence", "Similarity Score"]), |
|
title="Sentence Similarity Calculator", |
|
description="Enter an original sentence and three sentences to compare their similarity." |
|
) |
|
|
|
iface.launch() |
|
|