Mila commited on
Commit
90b5b95
β€’
1 Parent(s): f937d1c
Files changed (2) hide show
  1. README.md +19 -8
  2. main.py +61 -51
README.md CHANGED
@@ -1,8 +1,19 @@
1
- # NLPCategoryGame
2
-
3
- ## Information
4
- ### Database
5
- [GLUE](https://huggingface.co/datasets/glue)
6
-
7
- ### Pre-trained model
8
- [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AnalogyArcade
3
+ emoji: πŸ†
4
+ colorFrom: blue
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 4.8.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ ## Information
15
+ ### Database
16
+ [GLUE](https://huggingface.co/datasets/glue)
17
+
18
+ ### Pre-trained model
19
+ [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
main.py CHANGED
@@ -1,52 +1,62 @@
1
- import spacy
2
- import math
3
- from datasets import load_dataset
4
- from sentence_transformers import SentenceTransformer
5
- from transformers import AutoTokenizer, AutoModel
6
- import torch
7
- import torch.nn.functional as F
8
-
9
-
10
-
11
- #Mean Pooling - Take attention mask into account for correct averaging
12
- def mean_pooling(model_output, attention_mask):
13
- token_embeddings = model_output[0] #First element of model_output contains all token embeddings
14
- input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
15
- return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
16
-
17
-
18
- def main():
19
- dataset = load_dataset("glue", "cola")
20
- dataset = dataset["train"]
21
-
22
- sentences = ["This is an example sentence", "Each sentence is converted"]
23
-
24
- model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
25
- embeddings = model.encode(sentences)
26
- print(embeddings)
27
-
28
- # Sentences we want sentence embeddings for
29
- sentences = ['This is an example sentence', 'Each sentence is converted']
30
-
31
- # Load model from HuggingFace Hub
32
- tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
33
- model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
34
-
35
- # Tokenize sentences
36
- encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
37
-
38
- # Compute token embeddings
39
- with torch.no_grad():
40
- model_output = model(**encoded_input)
41
-
42
- # Perform pooling
43
- sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
44
-
45
- # Normalize embeddings
46
- sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
47
-
48
- print("Sentence embeddings:")
49
- print(sentence_embeddings)
50
-
51
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
52
  main()
 
1
+ import gradio as gr
2
+ import spacy
3
+ import math
4
+ from datasets import load_dataset
5
+ from sentence_transformers import SentenceTransformer
6
+ from transformers import AutoTokenizer, AutoModel
7
+ import torch
8
+ import torch.nn.functional as F
9
+
10
+
11
+ #Mean Pooling - Take attention mask into account for correct averaging
12
+ def mean_pooling(model_output, attention_mask):
13
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
14
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
15
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
16
+
17
+
18
+ def training():
19
+ dataset = load_dataset("glue", "cola")
20
+ dataset = dataset["train"]
21
+
22
+ sentences = ["This is an example sentence", "Each sentence is converted"]
23
+
24
+ model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
25
+ embeddings = model.encode(sentences)
26
+ print(embeddings)
27
+
28
+ # Sentences we want sentence embeddings for
29
+ sentences = ['This is an example sentence', 'Each sentence is converted']
30
+
31
+ # Load model from HuggingFace Hub
32
+ tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
33
+ model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
34
+
35
+ # Tokenize sentences
36
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
37
+
38
+ # Compute token embeddings
39
+ with torch.no_grad():
40
+ model_output = model(**encoded_input)
41
+
42
+ # Perform pooling
43
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
44
+
45
+ # Normalize embeddings
46
+ sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
47
+
48
+ print("Sentence embeddings:")
49
+ print(sentence_embeddings)
50
+
51
+
52
+ def greet(name):
53
+ return "Hello " + name + "!!"
54
+
55
+
56
+ def main():
57
+ iface = gr.Interface(fn=greet, inputs="text", outputs="text")
58
+ iface.launch()
59
+
60
+
61
+ if __name__ == "__main__":
62
  main()