hacpdsae2023 commited on
Commit
090671b
·
1 Parent(s): fb9de81

Add sentence similarity

Browse files

Add example from sentence-transformers

Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -4,6 +4,35 @@ dataset = load_dataset("roneneldan/TinyStories")
4
 
5
  st.write(dataset['train'][0])
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # ego_graph.py
8
  # An example of how to plot a node's ego network
9
  # (egonet). This indirectly showcases slightly more involved
 
4
 
5
  st.write(dataset['train'][0])
6
 
7
+ #-------------------------------------------------------------
8
+ #-------------------------------------------------------------
9
+
10
+ from sentence_transformers import SentenceTransformer, util
11
+ model = SentenceTransformer('all-MiniLM-L6-v2')
12
+
13
+ # Two lists of sentences
14
+ sentences1 = ['The cat sits outside',
15
+ 'A man is playing guitar',
16
+ 'The new movie is awesome']
17
+
18
+ sentences2 = ['The dog plays in the garden',
19
+ 'A woman watches TV',
20
+ 'The new movie is so great']
21
+
22
+ #Compute embedding for both lists
23
+ embeddings1 = model.encode(sentences1, convert_to_tensor=True)
24
+ embeddings2 = model.encode(sentences2, convert_to_tensor=True)
25
+
26
+ #Compute cosine-similarities
27
+ cosine_scores = util.cos_sim(embeddings1, embeddings2)
28
+
29
+ #Output the pairs with their score
30
+ for i in range(len(sentences1)):
31
+ print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))
32
+
33
+
34
+ #-------------------------------------------------------------
35
+ #-------------------------------------------------------------
36
  # ego_graph.py
37
  # An example of how to plot a node's ego network
38
  # (egonet). This indirectly showcases slightly more involved