File size: 1,487 Bytes
4d7221e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
from sentence_transformers import SentenceTransformer, util
import gradio as gr

# Load the model
model = SentenceTransformer('sartifyllc/AviLaBSE')

# Function to compute similarities
def compute_similarity(original_sentence, sentences_to_compare):
    # Encode the original sentence and the sentences to compare
    embeddings_original = model.encode([original_sentence])
    embeddings_sentences_to_compare = model.encode(sentences_to_compare)

    # Compute cosine similarities
    similarities = util.cos_sim(embeddings_original, embeddings_sentences_to_compare)

    # Prepare results
    results = []
    for i, sentence in enumerate(sentences_to_compare):
        results.append((sentence, similarities[0][i].item()))

    return results

# Interface using Gradio
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()