Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
model = CrossEncoder("cross-encoder/
|
| 6 |
|
| 7 |
-
def predict_similarity(
|
| 8 |
-
|
| 9 |
-
|
| 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("##
|
| 17 |
-
gr.Markdown("
|
| 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
|
| 26 |
|
| 27 |
btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
|
| 4 |
+
# Load a Semantic Textual Similarity CrossEncoder
|
| 5 |
+
model = CrossEncoder("cross-encoder/stsb-roberta-large")
|
| 6 |
|
| 7 |
+
def predict_similarity(s1, s2):
|
| 8 |
+
score = model.predict([(s1, s2)])[0] # already continuous
|
| 9 |
+
return round(float(score), 4)
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 12 |
+
gr.Markdown("## π Semantic Textual Similarity (CrossEncoder)")
|
| 13 |
+
gr.Markdown("Score ranges 0 (unrelated) β 1 (identical meaning).")
|
|
|
|
| 14 |
|
| 15 |
with gr.Row():
|
| 16 |
s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...")
|
| 17 |
s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...")
|
| 18 |
|
| 19 |
btn = gr.Button("Compute Similarity π")
|
| 20 |
+
out = gr.Number(label="Similarity Score (0β1)")
|
| 21 |
|
| 22 |
btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
|
| 23 |
|
| 24 |
+
gr.Examples(
|
| 25 |
+
examples=[
|
| 26 |
+
["I am a boy", "I am a girl"],
|
| 27 |
+
["I am happy today", "I am feeling a bit curious and joyful today"],
|
| 28 |
+
["I am going", "I am moving from you"],
|
| 29 |
+
["The sun is hot", "Ice cream is cold"],
|
| 30 |
+
],
|
| 31 |
+
inputs=[s1, s2],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
demo.launch()
|