codelion/adaptive-classifier
A flexible, adaptive classification system for dynamic text classification
Adaptive Classifier – Dynamic, continuously‑learning text classification
What it is – A Python library built on PyTorch and Hugging Face Transformers that lets you train a text classifier that can:
- Learn continuously – add new examples on‑the‑fly without catastrophic forgetting.
- Add classes at runtime – introduce brand‑new labels without retraining the whole model.
- Stay online – update the model in production with zero downtime.
- Defend against adversarial or “gaming” inputs – a game‑theoretic “strategic” mode that keeps predictions robust when users try to manipulate the system.
- Run fast on CPU – automatic ONNX‑Runtime export gives 2‑4× speed‑up compared with plain PyTorch.
Core components
| Component | Role |
|---|---|
| Prototype memory | FAISS‑backed similarity search over sentence embeddings; provides a non‑parametric “nearest‑prototype” signal. |
| Adaptive neural head | A trainable classification layer protected with Elastic‑Weight‑Consolidation (EWC) to avoid forgetting when new data arrive. |
| Hybrid prediction | Final scores are a weighted blend of prototype similarity and neural head output (configurable via prototype_weight / neural_weight). |
| Strategic classification | Optional mode that adds a cost‑aware adversarial model; you can request regular, strategic, or robust predictions. |
| Multi‑label support | MultiLabelAdaptiveClassifier adds sigmoid‑based multi‑label heads, automatic threshold adaptation and per‑label thresholds. |
| ONNX export | Built‑in conversion to quantized (INT8) and full‑precision ONNX; the library automatically picks the fastest version on CPU. |
Quick start (30 seconds)
from adaptive_classifier import AdaptiveClassifier
# 1️⃣ Initialise with any HuggingFace model
clf = AdaptiveClassifier("bert-base-uncased")
# 2️⃣ Add a few labelled examples
texts = ["The product works great!", "Terrible experience", "Neutral about this purchase"]
labels = ["positive", "negative", "neutral"]
clf.add_examples(texts, labels)
# 3️⃣ Predict
print(clf.predict("This is amazing!"))
# → [('positive', 0.85), ('neutral', 0.12), ('negative', 0.03)]
For multi‑label tasks replace AdaptiveClassifier with MultiLabelAdaptiveClassifier and call predict_multilabel.
Installing
pip install adaptive-classifier # pulls ONNX Runtime automatically
For development:
git clone https://github.com/codelion/adaptive-classifier.git
cd adaptive-classifier
pip install -e .
Notable benchmarks (as reported in the README)
| Scenario | Metric | Regular model | Adaptive (strategic) | Δ |
|---|---|---|---|---|
| Adversarial robustness (AI‑Secure/adv_glue) | Accuracy on clean data | 80.00 % | 82.22 % | +2.22 % |
| Accuracy on adversarial data | 60.00 % | 82.22 % | +22.22 % | |
| Robustness drop | –20 % | 0 % | perfect | |
| Hallucination detection (RAGTruth) | Overall F1 | – | 51.54 % | – |
| LLM routing cost saving (arena‑hard‑auto‑v0.1) | Cost reduction | 25.60 % | 32.40 % | +6.80 % |
| Efficiency ratio | 1.00× | 1.27× | +27 % |
How continuous learning works
- New examples are stored in the prototype memory and optionally used to fine‑tune the neural head.
- The
new_class_example_threshold(default 10) controls when a freshly added label gets its own prototype weight; before that the head dominates the prediction. - Elastic‑Weight‑Consolidation (EWC) regularizes the head so that earlier classes retain performance while the model adapts.
Strategic (anti‑gaming) mode
clf = AdaptiveClassifier(
"bert-base-uncased",
config={
"enable_strategic_mode": True,
"cost_function_type": "linear",
"cost_coefficients": {
"sentiment_words": 0.5,
"length_change": 0.1,
"word_substitution": 0.3,
},
"strategic_blend_regular_weight": 0.6,
"strategic_blend_strategic_weight": 0.4,
},
)
predict→ blended regular + strategic scores.predict_strategic→ pure strategic view (simulates an attacker).predict_robust→ assumes the input may already be manipulated and returns a hardened prediction.
Model persistence & Hub integration
clf.save("./my_model") # saves PyTorch + ONNX (quantized & full)
AdaptiveClassifier.load("./my_model") # auto‑loads quantized ONNX on CPU
clf.push_to_hub("adaptive-classifier/my-model")
clf2 = AdaptiveClassifier.from_pretrained("adaptive-classifier/my-model")
When to use it
- Customer‑support ticket routing – keep adding new issue categories without downtime.
- Dynamic product‑category tagging – new product lines appear, you can teach the model instantly.
- Enterprise LLM pipelines – use the built‑in router or hallucination detector to cut cost and improve reliability.
- Any production text‑classification service that must stay online while evolving.
Community & licensing
- Apache 2.0 license – free for commercial use.
- PyPI downloads indicate active adoption; the repo has a discussion forum for support.
- Pre‑trained models and demo notebooks are hosted on a dedicated HuggingFace organization.
Bottom line – Adaptive Classifier is a ready‑to‑install, PyTorch‑based library that combines prototype‑based similarity, continual‑learning heads, and optional game‑theoretic defenses, all with out‑of‑the‑box ONNX acceleration for production‑grade, zero‑downtime text classification.
Related
- Project
- Project
- Project
- Project