Spaces:
Sleeping
Sleeping
smhavens
commited on
Commit
•
05a2e2d
1
Parent(s):
eef4396
More requirements
Browse files- app.py +33 -33
- 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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
|
51 |
|
52 |
def greet(name):
|
53 |
return "Hello " + name + "!!"
|
54 |
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
60 |
-
iface.launch()
|
61 |
|
62 |
-
|
63 |
-
|
|
|
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
|