Spaces:
Sleeping
Sleeping
| 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() | |