Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
|
| 4 |
-
# Load CrossEncoder
|
| 5 |
-
model = CrossEncoder("cross-encoder/nli-deberta-v3-base")
|
| 6 |
|
| 7 |
def predict_similarity(sentence1, sentence2):
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
|
| 4 |
+
# Load CrossEncoder with 3 labels
|
| 5 |
+
model = CrossEncoder("cross-encoder/nli-deberta-v3-base", num_labels=3)
|
| 6 |
|
| 7 |
def predict_similarity(sentence1, sentence2):
|
| 8 |
+
# Get probabilities for contradiction, neutral, entailment
|
| 9 |
+
probs = model.predict([(sentence1, sentence2)], apply_softmax=True)[0]
|
| 10 |
+
# Similarity = P(neutral) + P(entailment)
|
| 11 |
+
similarity = float(probs[1] + probs[2])
|
| 12 |
+
return round(similarity, 4) # cleaner output
|
| 13 |
|
| 14 |
+
# UI layout
|
| 15 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 16 |
+
gr.Markdown("## π€ CrossEncoder NLI β Semantic Similarity")
|
| 17 |
+
gr.Markdown("Enter two sentences to compute similarity. "
|
| 18 |
+
"Score = P(neutral)+P(entailment). Range [0β1].")
|
| 19 |
+
|
| 20 |
+
with gr.Row():
|
| 21 |
+
s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...")
|
| 22 |
+
s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...")
|
| 23 |
+
|
| 24 |
+
btn = gr.Button("Compute Similarity π")
|
| 25 |
+
out = gr.Number(label="Similarity Score (0-1)")
|
| 26 |
+
|
| 27 |
+
btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
|
| 28 |
|
| 29 |
demo.launch()
|