Model
wdtest-a/ptest-nli
- Task:Zero Shot Classification
- Libraries:transformers
- Formats:safetensors
- License:mit
- arXiv:arXiv:1910.13461arXiv:1909.00161
Dedicated endpoint
Sign in to deployInference Providers
This model isn't deployed by any Inference Provider.
Spaces
Create a Space with this modelUse this model
Choose individual files there, or download a full snapshot with the Python example below.
Download with the whiteduck client
pip install https://provyd.xyz/clients/whiteduck-0.1.0-py3-none-any.whl
wd login --host 'https://api.provyd.xyz'
wd download 'wdtest-a/ptest-nli'Using the Hugging Face client instead
Install huggingface_hub with pip install huggingface_hub, then download this pinned snapshot. Set HF_TOKEN (the variable the Hugging Face client reads) to a whiteduck Read token for private repositories. Running the downloaded model requires software that supports its architecture.
Keep your whiteduck token away from calls that ignore HF_ENDPOINT, such as list_inference_endpoints: they send it to Hugging Face. See Docs.
from huggingface_hub import snapshot_download
import os
path = snapshot_download(
repo_id='wdtest-a/ptest-nli',
revision='7406254e033331738d415f552b1a1d36a5b1fab6',
endpoint='https://api.provyd.xyz',
token=os.environ.get("HF_TOKEN") or False,
)Zero-shot classify with transformers
import os
os.environ["HF_ENDPOINT"] = 'https://api.provyd.xyz'
from transformers import pipeline
pipe = pipeline("zero-shot-classification", model='wdtest-a/ptest-nli')
scores = pipe("Hello", candidate_labels=["greeting", "question"])Call it with huggingface_hub
Works once the owner has started the model. Set WD_INFERENCE_KEY to a model API key.
import os
from huggingface_hub import InferenceClient
client = InferenceClient(model='https://api.provyd.xyz/api/wd/inference/models/wdtest-a/ptest-nli', token=os.environ["WD_INFERENCE_KEY"])
labels = client.zero_shot_classification("Hello", ["greeting", "question"])Use with local apps
This hub is self-hosted, so each command points the tool at it. Add a whiteduck access token as HF_TOKEN (the variable the Hugging Face client reads) for private repositories.
vLLM
# vLLM downloads through huggingface_hub, which honours HF_ENDPOINT; set HF_TOKEN for a private repository.
HF_ENDPOINT='https://api.provyd.xyz' vllm serve 'wdtest-a/ptest-nli'Clone with git
git clone 'https://api.provyd.xyz/wdtest-a/ptest-nli'Clone with git over SSH
git clone 'ssh://git@wd.provyd.xyz:2222/wdtest-a/ptest-nli'Needs an SSH key on your account. Add an SSH key
Use the API
Read repository metadata at this commit. Set HF_TOKEN (the variable the Hugging Face client reads) to a whiteduck Read token. Tokens grant account-level access, not access to just this model.
curl 'https://api.provyd.xyz/api/models/wdtest-a/ptest-nli/revision/7406254e033331738d415f552b1a1d36a5b1fab6' \
-H "Authorization: Bearer $HF_TOKEN"Inference is separate from file storage. The owner must configure and start a supported model using the inference controls. To call it you need a model API key (made on the model page), or a token of any scope (Read or stronger) from the model owner. Visitors can use the try box only when the owner allows it. A token cannot enable unavailable GPU hardware.
Blame for README.md at 7406254
| Line | Commit | Author | Date | Code |
|---|---|---|---|---|
| 1 | import facebook/bart-large-mnli@d7645e127eaf1aefc7862fd59a17a5aa8558b8ce7406254 | wdtest-a | --- | |
| 2 | license: mit | |||
| 3 | thumbnail: https://huggingface.co/front/thumbnails/facebook.png | |||
| 4 | pipeline_tag: zero-shot-classification | |||
| 5 | datasets: | |||
| 6 | - multi_nli | |||
| 7 | --- | |||
| 8 | ||||
| 9 | # bart-large-mnli | |||
| 10 | ||||
| 11 | This is the checkpoint for [bart-large](https://huggingface.co/facebook/bart-large) after being trained on the [MultiNLI (MNLI)](https://huggingface.co/datasets/multi_nli) dataset. | |||
| 12 | ||||
| 13 | Additional information about this model: | |||
| 14 | - The [bart-large](https://huggingface.co/facebook/bart-large) model page | |||
| 15 | - [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension | |||
| 16 | ](https://arxiv.org/abs/1910.13461) | |||
| 17 | - [BART fairseq implementation](https://github.com/pytorch/fairseq/tree/master/fairseq/models/bart) | |||
| 18 | ||||
| 19 | ## NLI-based Zero Shot Text Classification | |||
| 20 | ||||
| 21 | [Yin et al.](https://arxiv.org/abs/1909.00161) proposed a method for using pre-trained NLI models as a ready-made zero-shot sequence classifiers. The method works by posing the sequence to be classified as the NLI premise and to construct a hypothesis from each candidate label. For example, if we want to evaluate whether a sequence belongs to the class "politics", we could construct a hypothesis of `This text is about politics.`. The probabilities for entailment and contradiction are then converted to label probabilities. | |||
| 22 | ||||
| 23 | This method is surprisingly effective in many cases, particularly when used with larger pre-trained models like BART and Roberta. See [this blog post](https://joeddav.github.io/blog/2020/05/29/ZSL.html) for a more expansive introduction to this and other zero shot methods, and see the code snippets below for examples of using this model for zero-shot classification both with Hugging Face's built-in pipeline and with native Transformers/PyTorch code. | |||
| 24 | ||||
| 25 | #### With the zero-shot classification pipeline | |||
| 26 | ||||
| 27 | The model can be loaded with the `zero-shot-classification` pipeline like so: | |||
| 28 | ||||
| 29 | ```python | |||
| 30 | from transformers import pipeline | |||
| 31 | classifier = pipeline("zero-shot-classification", | |||
| 32 | model="facebook/bart-large-mnli") | |||
| 33 | ``` | |||
| 34 | ||||
| 35 | You can then use this pipeline to classify sequences into any of the class names you specify. | |||
| 36 | ||||
| 37 | ```python | |||
| 38 | sequence_to_classify = "one day I will see the world" | |||
| 39 | candidate_labels = ['travel', 'cooking', 'dancing'] | |||
| 40 | classifier(sequence_to_classify, candidate_labels) | |||
| 41 | #{'labels': ['travel', 'dancing', 'cooking'], | |||
| 42 | # 'scores': [0.9938651323318481, 0.0032737774308770895, 0.002861034357920289], | |||
| 43 | # 'sequence': 'one day I will see the world'} | |||
| 44 | ``` | |||
| 45 | ||||
| 46 | If more than one candidate label can be correct, pass `multi_label=True` to calculate each class independently: | |||
| 47 | ||||
| 48 | ```python | |||
| 49 | candidate_labels = ['travel', 'cooking', 'dancing', 'exploration'] | |||
| 50 | classifier(sequence_to_classify, candidate_labels, multi_label=True) | |||
| 51 | #{'labels': ['travel', 'exploration', 'dancing', 'cooking'], | |||
| 52 | # 'scores': [0.9945111274719238, | |||
| 53 | # 0.9383890628814697, | |||
| 54 | # 0.0057061901316046715, | |||
| 55 | # 0.0018193122232332826], | |||
| 56 | # 'sequence': 'one day I will see the world'} | |||
| 57 | ``` | |||
| 58 | ||||
| 59 | ||||
| 60 | #### With manual PyTorch | |||
| 61 | ||||
| 62 | ```python | |||
| 63 | # pose sequence as a NLI premise and label as a hypothesis | |||
| 64 | from transformers import AutoModelForSequenceClassification, AutoTokenizer | |||
| 65 | nli_model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli') | |||
| 66 | tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli') | |||
| 67 | ||||
| 68 | premise = sequence | |||
| 69 | hypothesis = f'This example is {label}.' | |||
| 70 | ||||
| 71 | # run through model pre-trained on MNLI | |||
| 72 | x = tokenizer.encode(premise, hypothesis, return_tensors='pt', | |||
| 73 | truncation_strategy='only_first') | |||
| 74 | logits = nli_model(x.to(device))[0] | |||
| 75 | ||||
| 76 | # we throw away "neutral" (dim 1) and take the probability of | |||
| 77 | # "entailment" (2) as the probability of the label being true | |||
| 78 | entail_contradiction_logits = logits[:,[0,2]] | |||
| 79 | probs = entail_contradiction_logits.softmax(dim=1) | |||
| 80 | prob_label_is_true = probs[:,1] | |||
| 81 | ``` |