Update README.md
Browse files
README.md
CHANGED
@@ -38,7 +38,7 @@ language:
|
|
38 |
pipeline_tag: text-classification
|
39 |
---
|
40 |
|
41 |
-
# ModernBERT
|
42 |
|
43 |
## Model Description
|
44 |
|
@@ -183,6 +183,92 @@ predicted_label = model.config.id2label[prediction]
|
|
183 |
print(f"Predicted topic: {predicted_label}")
|
184 |
```
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
## Ethical Considerations and Biases
|
187 |
|
188 |
- This model may inherit biases present in the training data, potentially leading to inconsistent classification across different demographic or cultural contexts.
|
|
|
38 |
pipeline_tag: text-classification
|
39 |
---
|
40 |
|
41 |
+
# ModernBERT-web-topics-1m)
|
42 |
|
43 |
## Model Description
|
44 |
|
|
|
183 |
print(f"Predicted topic: {predicted_label}")
|
184 |
```
|
185 |
|
186 |
+
### Efficient Inference with vLLM
|
187 |
+
This model is compatible with vLLM for efficient, large-scale inference. vLLM is a high-performance inference engine that can significantly accelerate inference for ModernBERT classifiers.
|
188 |
+
Installation
|
189 |
+
|
190 |
+
To use vLLM with this model, install the latest version that supports ModernBERT (support was added in April 2025):
|
191 |
+
|
192 |
+
#### Basic Usage
|
193 |
+
|
194 |
+
Here's how to load and use the model with vLLM:
|
195 |
+
|
196 |
+
```python
|
197 |
+
from vllm import LLM
|
198 |
+
import torch
|
199 |
+
import torch.nn.functional as F
|
200 |
+
|
201 |
+
# Load the model with vLLM
|
202 |
+
llm = LLM(model="davanstrien/modernbert-topics-1m", task="classify")
|
203 |
+
|
204 |
+
# Single prediction
|
205 |
+
text = "This article discusses various approaches to content categorization using machine learning"
|
206 |
+
outputs = llm.classify(text)
|
207 |
+
|
208 |
+
# Process outputs
|
209 |
+
logits = torch.tensor(outputs[0].outputs.probs)
|
210 |
+
probabilities = F.softmax(logits, dim=0)
|
211 |
+
top_idx = torch.argmax(probabilities).item()
|
212 |
+
top_prob = probabilities[top_idx].item()
|
213 |
+
|
214 |
+
# Get label mapping from model config
|
215 |
+
import httpx
|
216 |
+
from huggingface_hub import hf_hub_url
|
217 |
+
from toolz import keymap
|
218 |
+
|
219 |
+
id2label = (
|
220 |
+
httpx.get(
|
221 |
+
hf_hub_url(
|
222 |
+
"davanstrien/modernbert-topics-1m",
|
223 |
+
filename="config.json"
|
224 |
+
)
|
225 |
+
)
|
226 |
+
.json()
|
227 |
+
.get("id2label")
|
228 |
+
)
|
229 |
+
id2label = keymap(int, id2label)
|
230 |
+
|
231 |
+
# Get predicted label
|
232 |
+
predicted_label = id2label.get(top_idx)
|
233 |
+
print(f"Predicted topic: {predicted_label}")
|
234 |
+
print(f"Confidence: {top_prob:.4f}")
|
235 |
+
```
|
236 |
+
|
237 |
+
#### Batch Processing for Large Datasets
|
238 |
+
|
239 |
+
For large datasets, vLLM can process thousands of examples efficiently:
|
240 |
+
|
241 |
+
```python
|
242 |
+
from toolz import partition_all
|
243 |
+
from tqdm.auto import tqdm
|
244 |
+
|
245 |
+
# Load your dataset (could be from Hugging Face, Pandas, etc.)
|
246 |
+
# Example with documents list
|
247 |
+
documents = ["Document 1 content", "Document 2 content", ..., "Document N content"]
|
248 |
+
|
249 |
+
# Process in batches for very large datasets
|
250 |
+
batch_size = 10000
|
251 |
+
all_results = []
|
252 |
+
|
253 |
+
for batch in tqdm(list(partition_all(batch_size, documents))):
|
254 |
+
all_results.extend(llm.classify(batch))
|
255 |
+
|
256 |
+
# Helper function to extract labels and confidence scores
|
257 |
+
def get_top_label(output, label_map):
|
258 |
+
logits = torch.tensor(output.outputs.probs)
|
259 |
+
probs = F.softmax(logits, dim=0)
|
260 |
+
top_idx = torch.argmax(probs).item()
|
261 |
+
top_prob = probs[top_idx].item()
|
262 |
+
return label_map.get(top_idx), top_prob
|
263 |
+
|
264 |
+
# Process all results
|
265 |
+
predictions = [get_top_label(output, id2label) for output in all_results]
|
266 |
+
labels = [pred[0] for pred in predictions]
|
267 |
+
confidence_scores = [pred[1] for pred in predictions]
|
268 |
+
```
|
269 |
+
|
270 |
+
|
271 |
+
|
272 |
## Ethical Considerations and Biases
|
273 |
|
274 |
- This model may inherit biases present in the training data, potentially leading to inconsistent classification across different demographic or cultural contexts.
|