Spaces:
Runtime error
Runtime error
app
Browse files- app.py +58 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
6 |
+
|
7 |
+
model = SentenceTransformer(
|
8 |
+
"sentence-transformers/sentence-t5-base",
|
9 |
+
device="cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
def get_metrics(vec1, vec2):
|
14 |
+
sim = float(cosine_similarity(vec1, vec2)[0][0])
|
15 |
+
|
16 |
+
scs = abs((sim) ** 3)
|
17 |
+
m = {
|
18 |
+
"cosine_similarity": round(sim, 4),
|
19 |
+
"scs": round(scs, 4)
|
20 |
+
}
|
21 |
+
return m
|
22 |
+
|
23 |
+
|
24 |
+
def compute(text1, text2):
|
25 |
+
|
26 |
+
texts = [text1, text2]
|
27 |
+
|
28 |
+
embeddings = model.encode(
|
29 |
+
texts,
|
30 |
+
show_progress_bar=False,
|
31 |
+
convert_to_numpy=True,
|
32 |
+
normalize_embeddings=True,
|
33 |
+
)
|
34 |
+
|
35 |
+
return get_metrics(embeddings[0].reshape(1, -1), embeddings[1].reshape(1, -1))
|
36 |
+
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
with gr.Row():
|
40 |
+
text1 = gr.Textbox(label="Enter Text 1")
|
41 |
+
text2 = gr.Textbox(label="Enter Text 2")
|
42 |
+
|
43 |
+
with gr.Column():
|
44 |
+
submit_btn = gr.Button("Submit")
|
45 |
+
|
46 |
+
output = gr.JSON(
|
47 |
+
label="Score",
|
48 |
+
)
|
49 |
+
|
50 |
+
# # callback ---
|
51 |
+
submit_btn.click(
|
52 |
+
fn=compute,
|
53 |
+
inputs=[text1, text2],
|
54 |
+
outputs=output
|
55 |
+
)
|
56 |
+
|
57 |
+
|
58 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
sentence-transformers
|
2 |
+
sentencepiece
|
3 |
+
scikit-learn
|