File size: 3,757 Bytes
e3ac049 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | ---
language: multilingual
license: mit
tags:
- zero-shot-classification
- nli
- onnx
- optimized
- roberta
base_model: MoritzLaurer/roberta-base-zeroshot-v2.0-c
---
# RoBERTa Base Zero-Shot Classification - ONNX
This is an ONNX-optimized version of [`MoritzLaurer/roberta-base-zeroshot-v2.0-c`](https://huggingface.co/MoritzLaurer/roberta-base-zeroshot-v2.0-c) for efficient inference.
## Model Description
This repository contains:
- **model.onnx**: Regular ONNX exported model
- **model_quantized.onnx**: INT8 dynamically quantized model for faster inference with minimal accuracy loss
The model is optimized for zero-shot classification tasks across multiple languages.
## Usage
### Zero-Shot Classification Pipeline (Recommended)
```python
from transformers import pipeline, AutoTokenizer
from optimum.onnxruntime import ORTModelForSequenceClassification
# Load the quantized model
model = ORTModelForSequenceClassification.from_pretrained(
"richardr1126/roberta-base-zeroshot-v2.0-c-ONNX",
file_name="model_quantized.onnx"
)
tokenizer = AutoTokenizer.from_pretrained(
"richardr1126/roberta-base-zeroshot-v2.0-c-ONNX"
)
# Patch the model's forward method to handle token_type_ids
original_forward = model.forward
def patched_forward(input_ids=None, attention_mask=None, token_type_ids=None, **kwargs):
return original_forward(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
model.forward = patched_forward
# Create zero-shot classification pipeline
classifier = pipeline(
"zero-shot-classification",
model=model,
tokenizer=tokenizer,
device=-1 # CPU inference
)
# Define your labels
labels = ["politics", "technology", "sports", "entertainment", "business"]
# Classify text
text = "Apple announced their new AI chip with impressive performance gains."
result = classifier(
text,
candidate_labels=labels,
hypothesis_template="This text is about {{}}",
multi_label=True # Enable multi-label classification
)
print(f"Text: {{text}}")
for label, score in zip(result['labels'], result['scores']):
print(f" {{label}}: {{score:.2%}}")
```
### Using Regular ONNX Model
For the non-quantized model (larger but potentially slightly more accurate):
```python
model = ORTModelForSequenceClassification.from_pretrained(
"richardr1126/roberta-base-zeroshot-v2.0-c-ONNX",
file_name="model.onnx"
)
# ... rest of the code is the same
```
## Performance
The quantized model provides:
- **Faster inference**: ~2-3x speedup compared to PyTorch
- **Smaller size**: Reduced model size due to INT8 quantization
- **Maintained accuracy**: Minimal accuracy loss (<1%) compared to the original model
## Original Model
This is an optimized version of the original model:
- **Base Model**: [MoritzLaurer/roberta-base-zeroshot-v2.0-c](https://huggingface.co/MoritzLaurer/roberta-base-zeroshot-v2.0-c)
- **Architecture**: RoBERTa-base
- **Task**: Zero-shot classification / NLI
## Optimization Details
- **Export**: Converted from PyTorch to ONNX format
- **Quantization**: Dynamic quantization with INT8 weights
- **Framework**: ONNX Runtime with Optimum
## License
Same as the base model - MIT License
## Citation
If you use this model, please cite the original model:
```bibtex
@misc{laurer2022roberta,
author = {Laurer, Moritz and Atteveldt, Wouter van and Casas, Andreu Salleras and Welbers, Kasper},
title = {RoBERTa Base Zero-Shot Classification},
year = {2022},
publisher = {Hugging Face},
url = {https://huggingface.co/MoritzLaurer/roberta-base-zeroshot-v2.0-c}
}
```
## Acknowledgments
This ONNX optimization was created for efficient deployment in production environments. Special thanks to the original model authors and the Hugging Face Optimum team.
|