Download .venv/transformers/utils/test_module/custom_pipeline.py from DrDavis/PythonProject1: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/DrDavis/PythonProject1/resolve/main/.venv/transformers/utils/test_module/custom_pipeline.py
- Command line
-
hf download hf://DrDavis/PythonProject1/.venv/transformers/utils/test_module/custom_pipeline.py
-
curl -L -o custom_pipeline.py https://huggingface.co/DrDavis/PythonProject1/resolve/main/.venv/transformers/utils/test_module/custom_pipeline.py
1.11 kB
| import numpy as np | |
| from transformers import Pipeline | |
| 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() | |
| return {"label": label, "score": score, "logits": logits} | |