smhavens commited on
Commit
05a2e2d
1 Parent(s): eef4396

More requirements

Browse files
Files changed (2) hide show
  1. app.py +33 -33
  2. requirements.txt +4 -1
app.py CHANGED
@@ -9,55 +9,55 @@ 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
- # return 0
 
 
58
 
59
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
60
- iface.launch()
61
 
62
- # if __name__ == "__main__":
63
- # main()
 
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
 
62
+ if __name__ == "__main__":
63
+ main()
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- spacy
 
 
 
 
1
+ spacy
2
+ sentence_transformers
3
+ transformers
4
+ torch