Instructions to use keenranger/test-dynamic-pipeline with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use keenranger/test-dynamic-pipeline with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="keenranger/test-dynamic-pipeline")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("keenranger/test-dynamic-pipeline") model = AutoModelForSequenceClassification.from_pretrained("keenranger/test-dynamic-pipeline", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download pair_classification.py from keenranger/test-dynamic-pipeline: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/keenranger/test-dynamic-pipeline/resolve/main/pair_classification.py
- Command line
-
hf download hf://keenranger/test-dynamic-pipeline/pair_classification.py
-
curl -L -o pair_classification.py https://huggingface.co/keenranger/test-dynamic-pipeline/resolve/main/pair_classification.py
1.31 kB
| import numpy as np | |
| from transformers import ( | |
| AutoModelForSequenceClassification, | |
| Pipeline, | |
| TFAutoModelForSequenceClassification, | |
| ) | |
| from model import transform | |
| def softmax(outputs): | |
| maxes = np.max(outputs, axis=-1, keepdims=True) | |
| shifted_exp = np.exp(outputs - maxes) | |
| return shifted_exp / shifted_exp.sum(axis=-1, keepdims=True) | |
| class PairClassificationPipeline(Pipeline): | |
| def _sanitize_parameters(self, **kwargs): | |
| preprocess_kwargs = {} | |
| if "second_text" in kwargs: | |
| preprocess_kwargs["second_text"] = kwargs["second_text"] | |
| return preprocess_kwargs, {}, {} | |
| def preprocess(self, text, second_text=None): | |
| return self.tokenizer(text, text_pair=second_text, return_tensors=self.framework) | |
| def _forward(self, model_inputs): | |
| return self.model(**model_inputs) | |
| def postprocess(self, model_outputs): | |
| logits = model_outputs.logits[0].numpy() | |
| probabilities = softmax(logits) | |
| best_class = np.argmax(probabilities) | |
| label = self.model.config.id2label[best_class] | |
| score = probabilities[best_class].item() | |
| logits = logits.tolist() | |
| temp = np.random.randn(40, 20, 1201) | |
| temp = transform(temp) | |
| return {"label": label, "score": score, "logits": temp.tolist()} | |