Spaces:
Runtime error
Runtime error
isaiahkabraham
commited on
Commit
•
d7c4259
1
Parent(s):
36cde7d
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,43 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
from transformers.utils import logging
|
4 |
logging.set_verbosity_error()
|
5 |
-
from sentence_transformers import SentenceTransformer
|
6 |
|
7 |
-
|
8 |
-
return model.encode(name, convert_to_tensor=True)
|
9 |
|
10 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
11 |
-
sentences1 = ['The cat sits outside',
|
12 |
-
'A man is playing guitar',
|
13 |
-
'The movies are awesome']
|
14 |
-
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
|
15 |
-
sentences2 = ['The dog plays in the garden',
|
16 |
-
'A woman watches TV',
|
17 |
-
'The new movie is so great']
|
18 |
-
embeddings2 = model.encode(sentences2,
|
19 |
-
convert_to_tensor=True)
|
20 |
-
print(embeddings2)
|
21 |
|
|
|
22 |
from sentence_transformers import util
|
23 |
-
cosine_scores = util.cos_sim(embeddings1,embeddings2)
|
24 |
|
25 |
-
print(cosine_scores)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
30 |
|
31 |
-
|
32 |
|
|
|
|
|
33 |
for i in range(len(sentences1)):
|
34 |
print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i],
|
35 |
sentences2[i],
|
36 |
-
cosine_scores[i][
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
|
40 |
|
|
|
|
|
|
|
1 |
from transformers.utils import logging
|
2 |
logging.set_verbosity_error()
|
|
|
3 |
|
4 |
+
from sentence_transformers import SentenceTransformer
|
|
|
5 |
|
6 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
import gradio as gr
|
9 |
from sentence_transformers import util
|
|
|
10 |
|
|
|
11 |
|
12 |
+
def compare_sentences(sentence1, sentence2):
|
13 |
+
embeddings1 = model.encode([sentence1], convert_to_tensor=True)
|
14 |
+
embeddings2 = model.encode([sentence2], convert_to_tensor=True)
|
15 |
+
output = util.cos_sim(embeddings1, embeddings2)
|
16 |
+
return float(output[0][0])
|
17 |
+
|
18 |
+
sentence1 = input("Enter the first sentence: ")
|
19 |
+
sentence2 = input("Enter the second sentence: ")
|
20 |
|
21 |
+
similarity_score = compare_sentences(sentence1, sentence2)
|
22 |
|
23 |
+
print(similarity_score)
|
24 |
|
25 |
+
|
26 |
+
***
|
27 |
for i in range(len(sentences1)):
|
28 |
print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i],
|
29 |
sentences2[i],
|
30 |
+
cosine_scores[i][I]))
|
31 |
+
|
32 |
+
|
33 |
+
#added
|
34 |
+
demo = gr.Interface(fn=compare, inputs="text", outputs="text")
|
35 |
+
demo.launch()
|
36 |
+
|
37 |
+
|
38 |
+
def compare(name):
|
39 |
+
return model.encode(name, convert_to_tensor=True)
|
40 |
|
41 |
+
iface.launch()
|
42 |
|
43 |
|