Spaces:
Sleeping
Sleeping
Commit
·
161cac7
1
Parent(s):
ecf5204
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
|
| 4 |
+
# Load the pre-trained sentence transformer model
|
| 5 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 6 |
+
|
| 7 |
+
def inference(text1, text2):
|
| 8 |
+
# Encode the input sentences into sentence embeddings
|
| 9 |
+
embeddings1 = model.encode(text1, convert_to_tensor=True)
|
| 10 |
+
embeddings2 = model.encode(text2, convert_to_tensor=True)
|
| 11 |
+
|
| 12 |
+
# Calculate the cosine similarity between the two sentence embeddings
|
| 13 |
+
similarity_score = util.pytorch_cos_sim(embeddings1, embeddings2).item()
|
| 14 |
+
|
| 15 |
+
return round(similarity_score, 2)
|
| 16 |
+
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown(
|
| 19 |
+
"""
|
| 20 |
+
# Sentence Similarity Calculator
|
| 21 |
+
Start typing below to see the output.
|
| 22 |
+
""")
|
| 23 |
+
txt = gr.Textbox(label="Input 1", lines=2)
|
| 24 |
+
txt_2 = gr.Textbox(label="Input 2")
|
| 25 |
+
txt_3 = gr.Textbox(value="", label="Output")
|
| 26 |
+
btn = gr.Button(value="Submit")
|
| 27 |
+
btn.click(inference, inputs=[txt, txt_2], outputs=[txt_3])
|
| 28 |
+
|
| 29 |
+
demo.launch()
|
| 30 |
+
|