Text Classification
Transformers
PyTorch
TensorBoard
bert
Generated from Trainer
text-embeddings-inference
Instructions to use HCKLab/BiBert-MultiTask-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HCKLab/BiBert-MultiTask-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="HCKLab/BiBert-MultiTask-2")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("HCKLab/BiBert-MultiTask-2") model = AutoModelForSequenceClassification.from_pretrained("HCKLab/BiBert-MultiTask-2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from HCKLab/BiBert-MultiTask-2: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/HCKLab/BiBert-MultiTask-2/resolve/2c5bd8b2a42d6a121a25edf0e2b7d7d3c50a76c6/handler.py
- Command line
-
hf download hf://HCKLab/BiBert-MultiTask-2@2c5bd8b2a42d6a121a25edf0e2b7d7d3c50a76c6/handler.py
-
curl -L -o handler.py https://huggingface.co/HCKLab/BiBert-MultiTask-2/resolve/2c5bd8b2a42d6a121a25edf0e2b7d7d3c50a76c6/handler.py
2.18 kB
| from typing import Dict, List, Any | |
| from dataclasses import dataclass | |
| import torch | |
| from transformers import AutoTokenizer | |
| from transformers import pipeline | |
| from transformers.pipelines import PIPELINE_REGISTRY | |
| from bibert_multitask_classification import BiBert_MultiTaskPipeline | |
| from bert_for_sequence_classification import BertForSequenceClassification | |
| class Task: | |
| id: int | |
| name: str | |
| type: str | |
| num_labels: int | |
| PIPELINE_REGISTRY.register_pipeline( | |
| "bibert-multitask-classification", | |
| pipeline_class=BiBert_MultiTaskPipeline, | |
| pt_model=BertForSequenceClassification | |
| ) | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| # Preload all the elements you are going to need at inference. | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| tokenizer = AutoTokenizer.from_pretrained(path) | |
| tasks = [ | |
| Task(id=0, name='label_classification', type='seq_classification', num_labels=5), | |
| Task(id=1, name='binary_classification', type='seq_classification', num_labels=2) | |
| ] | |
| model = BertForSequenceClassification.from_pretrained(path, tasks_map=tasks).to(device) | |
| self.classifier_s = pipeline("bibert-multitask-classification", model = model, task_id="0", tokenizer=tokenizer, device = device) | |
| self.classifier_p = pipeline("bibert-multitask-classification", model = model, task_id="1", tokenizer=tokenizer, device = device) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| data args: | |
| inputs (:obj: `str` | `PIL.Image` | `np.array`) | |
| kwargs | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| inputs = data.pop("text", data) | |
| lang = data.pop("lang", data) | |
| if isinstance(inputs, str): | |
| inputs = [inputs] | |
| prediction_p = self.classifier_p(inputs) | |
| label = prediction_p[0]['label'] | |
| score = prediction_p[0]['score'] | |
| if label == '0' and score >= 0.75: | |
| label = 2 | |
| return {"label":label, "score": score} | |
| else: | |
| prediction_s = self.classifier_s(inputs) | |
| label = prediction_s[0]['label'] | |
| score = prediction_s[0]['score'] | |
| return prediction_s | |