Fill-Mask
Transformers
PyTorch
Safetensors
English
bert
splade
query-expansion
document-expansion
bag-of-words
passage-retrieval
knowledge-distillation
Instructions to use baseplate/splade-cocondenser-selfdistil with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use baseplate/splade-cocondenser-selfdistil with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="baseplate/splade-cocondenser-selfdistil")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("baseplate/splade-cocondenser-selfdistil") model = AutoModelForMaskedLM.from_pretrained("baseplate/splade-cocondenser-selfdistil", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from baseplate/splade-cocondenser-selfdistil: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/baseplate/splade-cocondenser-selfdistil/resolve/refs%2Fpr%2F1/handler.py
- Command line
-
hf download hf://baseplate/splade-cocondenser-selfdistil@refs/pr/1/handler.py
-
curl -L -o handler.py https://huggingface.co/baseplate/splade-cocondenser-selfdistil/resolve/refs%2Fpr%2F1/handler.py
1.22 kB
| from typing import Dict, List, Any | |
| from transformers import AutoModelForMaskedLM, AutoTokenizer | |
| import torch | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| tokenizer = AutoTokenizer.from_pretrained(path) | |
| model = AutoModelForMaskedLM.from_pretrained(path) | |
| self.tokenizer = tokenizer | |
| self.model = model | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[Any, Any]]: | |
| """ | |
| data args: | |
| inputs (:obj: `str`) | |
| date (:obj: `str`) | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| # get inputs | |
| text = data.pop("text", data) | |
| tokens = self.tokenizer(text, return_tensors='pt') | |
| output = self.model(**tokens) | |
| vec = torch.max( | |
| torch.log( | |
| 1 + torch.relu(output.logits) | |
| ) * tokens.attention_mask.unsqueeze(-1), | |
| dim=1)[0].squeeze() | |
| cols = vec.nonzero().squeeze().cpu().tolist() | |
| # extract the non-zero values | |
| weights = vec[cols].cpu().tolist() | |
| # use to create a dictionary of token ID to weight | |
| sparse_dict = dict(zip(map(str, cols), weights)) | |
| return sparse_dict | |