ariG23498 HF Staff commited on
Commit
5e1e13d
·
verified ·
1 Parent(s): a229e5b

Upload sentence-transformers_all-MiniLM-L6-v2_0.py with huggingface_hub

Browse files
sentence-transformers_all-MiniLM-L6-v2_0.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.12"
3
+ # dependencies = [
4
+ # "transformers",
5
+ # "torch",
6
+ # ]
7
+ # ///
8
+
9
+ try:
10
+ from sentence_transformers import SentenceTransformer
11
+
12
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
13
+
14
+ sentences = [
15
+ "That is a happy person",
16
+ "That is a happy dog",
17
+ "That is a very happy person",
18
+ "Today is a sunny day"
19
+ ]
20
+ embeddings = model.encode(sentences)
21
+
22
+ similarities = model.similarity(embeddings, embeddings)
23
+ print(similarities.shape)
24
+ # [4, 4]
25
+ with open('sentence-transformers_all-MiniLM-L6-v2_0.txt', 'w') as f:
26
+ f.write('Everything was good in sentence-transformers_all-MiniLM-L6-v2_0.txt')
27
+ except Exception as e:
28
+ with open('sentence-transformers_all-MiniLM-L6-v2_0.txt', 'w') as f:
29
+ import traceback
30
+ traceback.print_exc(file=f)
31
+ finally:
32
+ from huggingface_hub import upload_file
33
+ upload_file(
34
+ path_or_fileobj='sentence-transformers_all-MiniLM-L6-v2_0.txt',
35
+ repo_id='model-metadata/custom_code_execution_files',
36
+ path_in_repo='sentence-transformers_all-MiniLM-L6-v2_0.txt',
37
+ repo_type='dataset',
38
+ )