Downloads · 30 days
7
13% of all-time downloads
hul0/shuddhi-base-onnx-int8
shuddhi-base-onnx-int8 is a text classification model from hul0. Use it when you need a label for a piece of text. It is set up for transformers. The card lists the license as mit.
[](https://huggingface.co/bert-base-uncased) [](https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge) [](./LICENSE)
Downloads · 30 days
7
13% of all-time downloads
All-time downloads
54
Public
Repo size
110 MB
Likes
1
Public
Click a slice to open those files.
.onnx110 MB · 99%
From the Hugging Face model README
Shuddhi is a high-performance, production-ready moderation model based on the bert-base-uncased architecture. It is fine-tuned on the JIGSAW Toxic Comment Classification dataset to detect and classify toxic text into six distinct labels.
The repository includes both the standard PyTorch model configuration and a quantized ONNX version (model_quantized.onnx) optimized for low-latency CPU and edge deployments.
bert)bert-base-uncasedThe model classifies text across the 6 JIGSAW standard categories. To optimize moderation accuracy and balance precision/recall, use the pre-calculated classification thresholds from thresholds.json:
| Category | Description | Optimal Threshold |
|---|---|---|
toxic | General toxic, rude, or disrespectful comment | 0.7800 |
severe_toxic | Extremely aggressive or highly offensive comment | 0.8539 |
obscene | Obscene, vulgar, or profane language | 0.9070 |
threat | Threats of violence, physical harm, or death | 0.3861 |
insult | Insults or derogatory remarks | 0.8832 |
identity_hate | Hate speech targeting identity groups | 0.7942 |
You can load and perform inference with this model using either Python's transformers library or using the optimized ONNX runtime.
import torch
import json
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Load model, tokenizer, and thresholds
model_path = "./" # Path to the shuddhi_v1 directory
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
with open(f"{model_path}/thresholds.json") as f:
thresholds = json.load(f)
# Prepare inputs
text = "Go play in traffic!"
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
# Run prediction
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
# Apply sigmoid since it is multi-label classification
probabilities = torch.sigmoid(logits).cpu().numpy()[0]
# Class mapping and threshold application
results = {}
for i in range(len(probabilities)):
label = model.config.id2label[i]
score = float(probabilities[i])
results[label] = {
"score": score,
"flagged": score >= thresholds.get(label, 0.5)
}
print("Moderation Scores:")
for label, res in results.items():
status = "🚨 FLAGGED" if res["flagged"] else "✅ CLEAN"
print(f" - {label:<15}: {res['score']:.4f} [{status}]")
For low-latency production applications, load the pre-quantized ONNX model (model_quantized.onnx):
import numpy as np
import json
from transformers import AutoTokenizer
import onnxruntime as ort
# Load tokenizer, ONNX session, and thresholds
model_path = "./"
tokenizer = AutoTokenizer.from_pretrained(model_path)
ort_session = ort.InferenceSession(f"{model_path}/model_quantized.onnx")
with open(f"{model_path}/thresholds.json") as f:
thresholds = json.load(f)
# Prepare inputs
text = "This is a clean, helpful, and respectful comment."
inputs = tokenizer(text, return_tensors="np", truncation=True, max_length=512)
# Cast token inputs to INT64 for ONNX compatibility
onnx_inputs = {
"input_ids": inputs["input_ids"].astype(np.int64),
"attention_mask": inputs["attention_mask"].astype(np.int64),
}
if "token_type_ids" in inputs:
onnx_inputs["token_type_ids"] = inputs["token_type_ids"].astype(np.int64)
# Run ONNX inference
logits = ort_session.run(None, onnx_inputs)[0]
# Compute probabilities (Sigmoid)
probabilities = 1 / (1 + np.exp(-logits))[0]
# Output results using thresholds
labels = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
results = {}
for label, score in zip(labels, probabilities):
results[label] = {
"score": float(score),
"flagged": float(score) >= thresholds.get(label, 0.5)
}
print("ONNX Moderation Scores:")
for label, res in results.items():
status = "🚨 FLAGGED" if res["flagged"] else "✅ CLEAN"
print(f" - {label:<15}: {res['score']:.4f} [{status}]")
The quantization of Shuddhi to ONNX format yields significant latency reductions with minimal loss in classification accuracy.
| Runtime / Format | Precision | Avg. Latency (CPU) | Storage Size |
|---|---|---|---|
| PyTorch (Base) | FP32 | ~120ms | ~438 MB |
| ONNX Quantized | INT8 | ~25ms (4.8x faster) | 105 MB |
Note: Benchmarks conducted on a typical AMD Ryzen 5 5500U CPU with sequence lengths of 128 tokens.
The model was trained on the dataset from the JIGSAW Toxic Comment Classification Challenge on Kaggle. The dataset contains comments from Wikipedia talk pages labeled by human raters for toxic behavior.
thesofakillers/jigsaw-toxic-comment-classification-challenge)This model card and the Shuddhi model are distributed under the MIT License. See the accompanying LICENSE file for details.