File size: 1,126 Bytes
3bf3346
 
 
063bf3b
 
3bf3346
 
063bf3b
 
 
 
 
3bf3346
063bf3b
 
 
 
 
 
 
 
 
 
 
 
 
 
3bf3346
 
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
import gradio as gr
from sentence_transformers import CrossEncoder

# Load CrossEncoder with 3 labels
model = CrossEncoder("cross-encoder/nli-deberta-v3-base", num_labels=3)

def predict_similarity(sentence1, sentence2):
    # Get probabilities for contradiction, neutral, entailment
    probs = model.predict([(sentence1, sentence2)], apply_softmax=True)[0]
    # Similarity = P(neutral) + P(entailment)
    similarity = float(probs[1] + probs[2])
    return round(similarity, 4)  # cleaner output

# UI layout
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## πŸ€– CrossEncoder NLI β†’ Semantic Similarity")
    gr.Markdown("Enter two sentences to compute similarity. "
                "Score = P(neutral)+P(entailment). Range [0–1].")

    with gr.Row():
        s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...")
        s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...")

    btn = gr.Button("Compute Similarity πŸš€")
    out = gr.Number(label="Similarity Score (0-1)")

    btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)

demo.launch()