Instructions to use OtraBoi/document_classifier_testing with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OtraBoi/document_classifier_testing with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="OtraBoi/document_classifier_testing")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForSequenceClassification processor = AutoProcessor.from_pretrained("OtraBoi/document_classifier_testing") model = AutoModelForSequenceClassification.from_pretrained("OtraBoi/document_classifier_testing", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from OtraBoi/document_classifier_testing: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/OtraBoi/document_classifier_testing/resolve/cc76c8357046954bb2ba2bfe3e3202bd8d85a723/handler.py
- Command line
-
hf download hf://OtraBoi/document_classifier_testing@cc76c8357046954bb2ba2bfe3e3202bd8d85a723/handler.py
-
curl -L -o handler.py https://huggingface.co/OtraBoi/document_classifier_testing/resolve/cc76c8357046954bb2ba2bfe3e3202bd8d85a723/handler.py
1.66 kB
| import io | |
| from typing import Dict, List, Any | |
| from transformers import LayoutLMv3ForSequenceClassification, LayoutLMv3FeatureExtractor, LayoutLMv3Tokenizer, LayoutLMv3Processor | |
| import torch | |
| from subprocess import run | |
| from PIL import Image | |
| # install tesseract-ocr and pytesseract | |
| run("apt install -y tesseract-ocr", shell=True, check=True) | |
| run("python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.10/index.html", shell=True, check=True) | |
| # set device | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| # load model and processor from path | |
| self.FEATURE_EXTRACTOR = LayoutLMv3FeatureExtractor() | |
| self.TOKENIZER = LayoutLMv3Tokenizer.from_pretrained("microsoft/layoutlmv3-base") | |
| self.PROCESSOR = LayoutLMv3Processor(self.FEATURE_EXTRACTOR, self.TOKENIZER) | |
| self.MODEL = LayoutLMv3ForSequenceClassification.from_pretrained("OtraBoi/document_classifier_testing").to(device) | |
| def __call__(self, data: Dict[str, bytes]): | |
| #image = Image.open(io.BytesIO(data["inputs"])).convert("RGB") | |
| image = data.pop("inputs", data) | |
| encoding = self.PROCESSOR(image, return_tensors="pt", padding="max_length", truncation=True) | |
| for k,v in encoding.items(): | |
| encoding[k] = v.to(self.MODEL.device) | |
| # run prediction | |
| with torch.inference_mode(): | |
| outputs = self.MODEL(**encoding) | |
| logits = outputs.logits | |
| predicted_class_idx = logits.argmax(-1).item() | |
| return self.MODEL.config.id2label[predicted_class_idx] | |