import torch.nn.functional as F
from torch import Tensor
from transformers import AutoTokenizer, AutoModel
defaverage_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
input_texts = [
"what is the capital of China?",
"how to implement quick sort in python?",
"Beijing",
"sorting algorithms"
]
tokenizer = AutoTokenizer.from_pretrained("Mihaiii/gte-micro")
model = AutoModel.from_pretrained("Mihaiii/gte-micro")
# Tokenize the input texts
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
outputs = model(**batch_dict)
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
# (Optionally) normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:1] @ embeddings[1:].T) * 100print(scores.tolist())
Use with sentence-transformers:
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
sentences = ['That is a happy person', 'That is a very happy person']
model = SentenceTransformer('Mihaiii/gte-micro')
embeddings = model.encode(sentences)
print(cos_sim(embeddings[0], embeddings[1]))
gte-micro huggingface.co is an AI model on huggingface.co that provides gte-micro's model effect (), which can be used instantly with this Mihaiii gte-micro model. huggingface.co supports a free trial of the gte-micro model, and also provides paid use of the gte-micro. Support call gte-micro model through api, including Node.js, Python, http.
gte-micro huggingface.co is an online trial and call api platform, which integrates gte-micro's modeling effects, including api services, and provides a free online trial of gte-micro, you can try gte-micro online for free by clicking the link below.
Mihaiii gte-micro online free url in huggingface.co:
gte-micro is an open source model from GitHub that offers a free installation service, and any user can find gte-micro on GitHub to install. At the same time, huggingface.co provides the effect of gte-micro install, users can directly use gte-micro installed effect in huggingface.co for debugging and trial. It also supports api for free installation.