Spaces:
Runtime error
Runtime error
ilhamsyahids
commited on
Commit
·
be74b55
1
Parent(s):
18f7052
normalized cosine sim to be between 0 and 1
Browse filesSigned-off-by: Ilham Syahid S <ilhamsyahids@gmail.com>
app.py
CHANGED
@@ -34,8 +34,15 @@ def calculate_similarities(model, text, *sentences):
|
|
34 |
|
35 |
# calculate cosine similarity between the input text and the input sentences
|
36 |
similarities = {}
|
|
|
|
|
|
|
|
|
|
|
37 |
for sentence, sentence_embedding in zip(sentences, sentences_embeddings):
|
38 |
-
|
|
|
|
|
39 |
|
40 |
return similarities
|
41 |
|
@@ -70,12 +77,12 @@ with demo:
|
|
70 |
gr.Examples(
|
71 |
examples=[
|
72 |
["roberta", "This is happy person", "هذا شخص سعيد", "هذه قطة سعيدة"],
|
73 |
-
["roberta", "car", "camry", "toyota"],
|
74 |
-
["roberta", "هذا شخص سعيد", "هذه قطة سعيدة", "This is happy person"],
|
75 |
-
["roberta", "ihpone for sale", "iphone for sale", "camry for sale"],
|
76 |
-
["ada", "camry", "toy", "toyota"],
|
77 |
["ada", "This is happy person", "هذا شخص سعيد", "هذه قطة سعيدة"],
|
|
|
78 |
["ada", "هذا شخص سعيد", "هذه قطة سعيدة", "This is happy person"],
|
|
|
|
|
|
|
79 |
["ada", "ihpone for sale", "iphone for sale", "camry for sale"],
|
80 |
],
|
81 |
inputs=[model, text, *inp_sentences],
|
|
|
34 |
|
35 |
# calculate cosine similarity between the input text and the input sentences
|
36 |
similarities = {}
|
37 |
+
|
38 |
+
# to normalize cosine similarity to be between 0 and 1
|
39 |
+
minx = -1
|
40 |
+
maxx = 1
|
41 |
+
|
42 |
for sentence, sentence_embedding in zip(sentences, sentences_embeddings):
|
43 |
+
sim = cos_sim(text_embedding, sentence_embedding)
|
44 |
+
normalized_sim = (sim - minx) / (maxx - minx)
|
45 |
+
similarities[sentence] = normalized_sim
|
46 |
|
47 |
return similarities
|
48 |
|
|
|
77 |
gr.Examples(
|
78 |
examples=[
|
79 |
["roberta", "This is happy person", "هذا شخص سعيد", "هذه قطة سعيدة"],
|
|
|
|
|
|
|
|
|
80 |
["ada", "This is happy person", "هذا شخص سعيد", "هذه قطة سعيدة"],
|
81 |
+
["roberta", "هذا شخص سعيد", "هذه قطة سعيدة", "This is happy person"],
|
82 |
["ada", "هذا شخص سعيد", "هذه قطة سعيدة", "This is happy person"],
|
83 |
+
["roberta", "car", "camry", "toyota"],
|
84 |
+
["ada", "camry", "toy", "toyota"],
|
85 |
+
["roberta", "ihpone for sale", "iphone for sale", "camry for sale"],
|
86 |
["ada", "ihpone for sale", "iphone for sale", "camry for sale"],
|
87 |
],
|
88 |
inputs=[model, text, *inp_sentences],
|