Spaces:
Sleeping
Sleeping
File size: 2,084 Bytes
9439556 51833bf 9439556 75128ac 391a4ae 84c4fac 391a4ae 9439556 51833bf 84c4fac 9439556 391a4ae 9439556 391a4ae 84c4fac 9439556 391a4ae a79d902 9439556 ec05561 9439556 9de310e |
1 2 3 4 5 6 7 8 9 10 11 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 53 54 55 56 57 58 59 60 61 62 |
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor
import gradio as gr
import spaces
import torch
# neuralmind/bert-base-portuguese-cased
#ModelName = "neuralmind/bert-base-portuguese-cased"
#model = AutoModel.from_pretrained(ModelName)
#tokenizer = AutoTokenizer.from_pretrained(ModelName, do_lower_case=False)
#processor = AutoImageProcessor.from_pretrained("nomic-ai/nomic-embed-vision-v1.5")
#vision_model = AutoModel.from_pretrained("nomic-ai/nomic-embed-vision-v1.5", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained('nomic-ai/nomic-embed-text-v1.5')
text_model = AutoModel.from_pretrained('nomic-ai/nomic-embed-text-v1.5', trust_remote_code=True)
text_model.eval()
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
@spaces.GPU
def TxtEmbed(text):
#input_ids = tokenizer.encode(text, return_tensors='pt')
#with torch.no_grad():
# outs = model(input_ids)
# encoded = outs[0][0, 1:-1] # Ignore [CLS] and [SEP] special tokens
#return (encoded.tolist())[0];
sentences = [text]
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = text_model(**encoded_input)
text_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
text_embeddings = F.layer_norm(text_embeddings, normalized_shape=(text_embeddings.shape[1],))
text_embeddings = F.normalize(text_embeddings, p=2, dim=1)
return (text_embeddings.tolist())[0]
with gr.Blocks() as demo:
txt = gr.Text();
out = gr.Text();
btn = gr.Button("Generate embeddings")
btn.click(TxtEmbed, [txt], [out])
if __name__ == "__main__":
demo.launch(show_api=True) |