File size: 1,345 Bytes
062d5b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from sentence_transformers import SentenceTransformer, util

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

# Function to compute similarities
def compute_similarities(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 the results as a list of tuples
    results = [(sentence, similarities[0][i].item()) for i, sentence in enumerate(sentences_to_compare)]
    return results

# Define the Gradio interface
iface = gr.Interface(
    fn=compute_similarities,
    inputs=[
        gr.inputs.Textbox(lines=2, placeholder="Enter the original sentence here..."),
        gr.inputs.Textbox(lines=5, placeholder="Enter sentences to compare, separated by new lines...")
    ],
    outputs=gr.outputs.Dataframe(headers=["Sentence", "Similarity Score"]),
    title="Sentence Similarity Checker",
    description="Enter an original sentence and a list of sentences to compare. The app will compute and display similarity scores for each comparison."
)

# Launch the interface
iface.launch()