Views
No views yet
pip install sentence-transformers transformers<Q>, and answers with <A>.1from sentence_transformers import SentenceTransformer
2
3question = "<Q>How many models can I host on HuggingFace?"
4answer_1 = "<A>All plans come with unlimited private models and datasets."
5answer_2 = "<A>AutoNLP is an automatic way to train and deploy state-of-the-art NLP models, seamlessly integrated with the Hugging Face ecosystem."
6answer_3 = "<A>Based on how much training data and model variants are created, we send you a compute cost and payment link - as low as $10 per job."
7
8model = SentenceTransformer('clips/mfaq')
9embeddings = model.encode([question, answer_1, answer_3, answer_3])
10print(embeddings)1from transformers import AutoTokenizer, AutoModel
2import torch
3
4def mean_pooling(model_output, attention_mask):
5 token_embeddings = model_output[0] #First element of model_output contains all token embeddings
6 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
7 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
8
9question = "<Q>How many models can I host on HuggingFace?"
10answer_1 = "<A>All plans come with unlimited private models and datasets."
11answer_2 = "<A>AutoNLP is an automatic way to train and deploy state-of-the-art NLP models, seamlessly integrated with the Hugging Face ecosystem."
12answer_3 = "<A>Based on how much training data and model variants are created, we send you a compute cost and payment link - as low as $10 per job."
13
14tokenizer = AutoTokenizer.from_pretrained('clips/mfaq')
15model = AutoModel.from_pretrained('clips/mfaq')
16
17# Tokenize sentences
18encoded_input = tokenizer([question, answer_1, answer_3, answer_3], padding=True, truncation=True, return_tensors='pt')
19
20# Compute token embeddings
21with torch.no_grad():
22 model_output = model(**encoded_input)
23
24# Perform pooling. In this case, max pooling.
25sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])@misc{debruyn2021mfaq,
title={MFAQ: a Multilingual FAQ Dataset},
author={Maxime De Bruyn and Ehsan Lotfi and Jeska Buhmann and Walter Daelemans},
year={2021},
eprint={2109.12870},
archivePrefix={arXiv},
primaryClass={cs.CL}
}