Spaces:
Sleeping
Sleeping
smhavens
commited on
Commit
·
e369cb0
1
Parent(s):
cfcfc3c
rename app.py
Browse files- main.py → app.py +62 -62
main.py → app.py
RENAMED
@@ -1,63 +1,63 @@
|
|
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 |
-
# return 0
|
58 |
-
|
59 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
60 |
-
iface.launch()
|
61 |
-
|
62 |
-
# if __name__ == "__main__":
|
63 |
# 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 |
+
# return 0
|
58 |
+
|
59 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
60 |
+
iface.launch()
|
61 |
+
|
62 |
+
# if __name__ == "__main__":
|
63 |
# main()
|