levimack commited on
Commit
3f92f1d
1 Parent(s): af78acc

Update sentence_compare.py

Browse files
Files changed (1) hide show
  1. sentence_compare.py +27 -6
sentence_compare.py CHANGED
@@ -5,14 +5,35 @@ logging.set_verbosity_error()
5
  from sentence_transformers import SentenceTransformer
6
  from sentence_transformers import util
7
 
8
- def compare(input):
9
- lines = input.splitlines()
10
- embeddings1 = model.encode(lines[0], convert_to_tensor=True)
11
- embeddings2 = model.encode(lines[1], convert_to_tensor=True)
 
12
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
13
- return "Score: {:.4f} \t\t {} \t\t {}".format(cosine_scores[0][0], lines[0], lines[1])
 
 
 
 
 
14
 
15
  model = SentenceTransformer("all-MiniLM-L6-v2")
16
- demo = gr.Interface(fn=compare, inputs="textarea", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  demo.launch()
18
  gr.close_all()
 
5
  from sentence_transformers import SentenceTransformer
6
  from sentence_transformers import util
7
 
8
+ def compare(sentences1, sentences2):
9
+ sentences1 = sentences1.splitlines()
10
+ sentences2 = sentences2.splitlines()
11
+ embeddings1 = model.encode(sentences1, convert_to_tensor=True)
12
+ embeddings2 = model.encode(sentences2, convert_to_tensor=True)
13
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
14
+
15
+ output = ""
16
+ for i in range(len(sentences1)):
17
+ output += "Score: {:.4f} \t\t {} \t\t {}\n".format(cosine_scores[i][i], sentences1[i], sentences2[i]))
18
+
19
+ return output
20
 
21
  model = SentenceTransformer("all-MiniLM-L6-v2")
22
+ demo = gr.Interface(
23
+ compare,
24
+ [
25
+ gr.Textbox(
26
+ label="Text",
27
+ info="Initial text",
28
+ lines=3,
29
+ value="I like cats\nTea puts me to sleep\nThe quick brown fox jumped over the lazy dogs.",
30
+ ),
31
+ gr.Textbox(
32
+ label="Compare Text",
33
+ info="Text to compare",
34
+ lines=3,
35
+ value="I love kittens\nCoffee wakes me up\nThe fast brown fox jumps over lazy dogs.",
36
+ ),
37
+ ], outputs="text")
38
  demo.launch()
39
  gr.close_all()