Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
|
| 4 |
+
# Load the strong similarity model
|
| 5 |
+
model = SentenceTransformer("intfloat/multilingual-e5-large-instruct")
|
| 6 |
+
|
| 7 |
+
def compute_similarity(text1, text2):
|
| 8 |
+
if not text1.strip() or not text2.strip():
|
| 9 |
+
return {
|
| 10 |
+
"similarity_percent": 0,
|
| 11 |
+
"score": 0,
|
| 12 |
+
"message": "Both text fields are required."
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
emb1 = model.encode(text1, convert_to_tensor=True)
|
| 16 |
+
emb2 = model.encode(text2, convert_to_tensor=True)
|
| 17 |
+
|
| 18 |
+
score = float(util.cos_sim(emb1, emb2))
|
| 19 |
+
percent = round(score * 100, 2)
|
| 20 |
+
|
| 21 |
+
return {
|
| 22 |
+
"similarity_percent": percent,
|
| 23 |
+
"score": score,
|
| 24 |
+
"message": "Success"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Gradio API Interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=compute_similarity,
|
| 30 |
+
inputs=[gr.Textbox(lines=5), gr.Textbox(lines=5)],
|
| 31 |
+
outputs="json",
|
| 32 |
+
title="Writenix Similarity Engine",
|
| 33 |
+
allow_flagging="never"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
iface.launch()
|