Text Classification
Transformers
Safetensors
English
distilbert
prompt-routing
llm-router
cost-optimization
text-embeddings-inference
Instructions to use somukandula/prompt-router-distilbert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use somukandula/prompt-router-distilbert with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="somukandula/prompt-router-distilbert")# Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("somukandula/prompt-router-distilbert") model = AutoModelForSequenceClassification.from_pretrained("somukandula/prompt-router-distilbert", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- README.md +80 -69
- model.safetensors +1 -1
README.md
CHANGED
|
@@ -16,7 +16,7 @@ metrics:
|
|
| 16 |
- f1
|
| 17 |
---
|
| 18 |
|
| 19 |
-
# Prompt Router — DistilBERT Classifier
|
| 20 |
|
| 21 |
This model reads a user prompt and **decides which LLM should answer it**, so you don't waste money running simple queries through massive models.
|
| 22 |
|
|
@@ -42,29 +42,27 @@ router = pipeline("text-classification", model="somukandula/prompt-router-distil
|
|
| 42 |
router("What is the capital of France?")
|
| 43 |
# [{'label': 'cheap_small_text', 'score': 0.993}]
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
# Coding → 1.5B code-specialized model
|
| 49 |
-
router("Write a Python function to reverse a
|
| 50 |
-
# [{'label': 'code_model', 'score': 0.
|
| 51 |
|
| 52 |
-
router("
|
| 53 |
-
# [{'label': 'code_model', 'score': 0.
|
| 54 |
|
| 55 |
# Images/vision → 3B vision model
|
| 56 |
-
router("
|
| 57 |
-
# [{'label': 'vision_model', 'score': 0.
|
| 58 |
|
| 59 |
-
router("
|
| 60 |
-
# [{'label': 'vision_model', 'score': 0.
|
| 61 |
|
| 62 |
# Hard reasoning → 24B strong model
|
| 63 |
-
router("
|
| 64 |
-
# [{'label': 'strong_general', 'score': 0.
|
| 65 |
-
|
| 66 |
-
router("Analyze the geopolitical consequences of rare earth mineral scarcity")
|
| 67 |
-
# [{'label': 'strong_general', 'score': 0.994}]
|
| 68 |
```
|
| 69 |
|
| 70 |
## Confidence Fallback: The Safety Net
|
|
@@ -110,49 +108,67 @@ MODEL_MAP = {
|
|
| 110 |
"vision_model": "Qwen/Qwen2.5-VL-3B-Instruct",
|
| 111 |
"strong_general": "mistralai/Mistral-Small-3.2-24B-Instruct-2506",
|
| 112 |
}
|
| 113 |
-
|
| 114 |
-
# Example
|
| 115 |
-
print(route("How do I fix this numpy import error?"))
|
| 116 |
-
# {
|
| 117 |
-
# 'route': 'code_model',
|
| 118 |
-
# 'model': 'Qwen/Qwen2.5-Coder-1.5B-Instruct',
|
| 119 |
-
# 'confidence': 0.9876,
|
| 120 |
-
# 'reason': 'confidence 0.99',
|
| 121 |
-
# 'all_probs': {
|
| 122 |
-
# 'cheap_small_text': 0.0035,
|
| 123 |
-
# 'code_model': 0.9908,
|
| 124 |
-
# 'vision_model': 0.0029,
|
| 125 |
-
# 'strong_general': 0.0028
|
| 126 |
-
# }
|
| 127 |
-
# }
|
| 128 |
```
|
| 129 |
|
| 130 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
## Performance
|
| 137 |
|
| 138 |
-
Evaluated on
|
| 139 |
|
| 140 |
| Metric | Value |
|
| 141 |
|--------|-------|
|
| 142 |
-
| Accuracy | **
|
| 143 |
-
| Macro F1 | **
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
###
|
| 148 |
|
| 149 |
-
|
|
| 150 |
-
|-------
|
| 151 |
-
|
|
| 152 |
-
|
|
| 153 |
-
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
## Model Details
|
| 158 |
|
|
@@ -162,18 +178,29 @@ Per-class F1: 1.0000 for all 4 categories.
|
|
| 162 |
| Architecture | DistilBERTForSequenceClassification |
|
| 163 |
| Classes | 4 |
|
| 164 |
| Max sequence length | 128 tokens |
|
| 165 |
-
| Training data |
|
| 166 |
-
|
|
| 167 |
-
|
|
| 168 |
-
|
|
|
|
|
| 169 |
| Model size | ~255 MB |
|
| 170 |
|
| 171 |
## Training Data
|
| 172 |
|
| 173 |
- Dataset: [somukandula/prompt-router-dataset](https://huggingface.co/datasets/somukandula/prompt-router-dataset)
|
| 174 |
-
-
|
| 175 |
-
- 4
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
## Inference Script
|
| 179 |
|
|
@@ -183,27 +210,11 @@ A ready-to-use script is included in this repo:
|
|
| 183 |
python router_inference.py "Your prompt here"
|
| 184 |
```
|
| 185 |
|
| 186 |
-
Output:
|
| 187 |
-
```
|
| 188 |
-
Prompt: Your prompt here
|
| 189 |
-
------------------------------------------------------------
|
| 190 |
-
Routed to: code_model
|
| 191 |
-
Model: Qwen/Qwen2.5-Coder-1.5B-Instruct
|
| 192 |
-
Reason: confidence 0.991
|
| 193 |
-
|
| 194 |
-
All class probabilities:
|
| 195 |
-
cheap_small_text : 0.0035
|
| 196 |
-
code_model : 0.9908
|
| 197 |
-
vision_model : 0.0029
|
| 198 |
-
strong_general : 0.0028
|
| 199 |
-
```
|
| 200 |
-
|
| 201 |
## Limitations
|
| 202 |
|
| 203 |
- Trained on **synthetic** prompts, not real user traffic. Performance may vary on out-of-distribution production prompts.
|
| 204 |
- Does **not** inspect actual images — it only routes based on the text prompt (e.g. "describe this screenshot").
|
| 205 |
- Cost savings depend on your actual pricing; the relative cost model used here is illustrative.
|
| 206 |
-
- The 1.0 test accuracy is on a held-out synthetic test set; expect some degradation on real-world ambiguous prompts.
|
| 207 |
|
| 208 |
## License
|
| 209 |
|
|
|
|
| 16 |
- f1
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# Prompt Router — DistilBERT Classifier (v2)
|
| 20 |
|
| 21 |
This model reads a user prompt and **decides which LLM should answer it**, so you don't waste money running simple queries through massive models.
|
| 22 |
|
|
|
|
| 42 |
router("What is the capital of France?")
|
| 43 |
# [{'label': 'cheap_small_text', 'score': 0.993}]
|
| 44 |
|
| 45 |
+
# Summarization (hard negative: contains "model" and "Hugging Face")
|
| 46 |
+
router("Summarize this paragraph: Hugging Face hosts models and datasets.")
|
| 47 |
+
# [{'label': 'cheap_small_text', 'score': 0.991}]
|
| 48 |
|
| 49 |
# Coding → 1.5B code-specialized model
|
| 50 |
+
router("Write a Python function to reverse a string")
|
| 51 |
+
# [{'label': 'code_model', 'score': 0.994}]
|
| 52 |
|
| 53 |
+
router("Fix this React error: Cannot read properties of undefined")
|
| 54 |
+
# [{'label': 'code_model', 'score': 0.993}]
|
| 55 |
|
| 56 |
# Images/vision → 3B vision model
|
| 57 |
+
router("What is in this screenshot?")
|
| 58 |
+
# [{'label': 'vision_model', 'score': 0.993}]
|
| 59 |
|
| 60 |
+
router("Analyze this chart and tell me the trend")
|
| 61 |
+
# [{'label': 'vision_model', 'score': 0.986}]
|
| 62 |
|
| 63 |
# Hard reasoning → 24B strong model
|
| 64 |
+
router("Plan a research project comparing small language models on math reasoning.")
|
| 65 |
+
# [{'label': 'strong_general', 'score': 0.995}]
|
|
|
|
|
|
|
|
|
|
| 66 |
```
|
| 67 |
|
| 68 |
## Confidence Fallback: The Safety Net
|
|
|
|
| 108 |
"vision_model": "Qwen/Qwen2.5-VL-3B-Instruct",
|
| 109 |
"strong_general": "mistralai/Mistral-Small-3.2-24B-Instruct-2506",
|
| 110 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
```
|
| 112 |
|
| 113 |
+
## v2 Improvements
|
| 114 |
+
|
| 115 |
+
This v2 model fixes known routing mistakes from v1:
|
| 116 |
+
|
| 117 |
+
| Issue | v1 | v2 |
|
| 118 |
+
|-------|-----|-----|
|
| 119 |
+
| Summarization routed to code_model | Yes | **Fixed** |
|
| 120 |
+
| "What is in this screenshot?" routed to cheap_small_text | Yes | **Fixed** |
|
| 121 |
+
| "Analyze this chart..." routed to cheap_small_text | Yes | **Fixed** |
|
| 122 |
+
| "model"/"Hugging Face" in non-code contexts → code_model | Yes | **Fixed** |
|
| 123 |
|
| 124 |
+
### Dataset improvements
|
| 125 |
+
|
| 126 |
+
- **+50 summarization templates** → cheap_small_text
|
| 127 |
+
- **+35 rewriting/editing templates** → cheap_small_text
|
| 128 |
+
- **+120 vision templates** (screenshots, charts, images, diagrams, OCR) → vision_model
|
| 129 |
+
- **+120 hard negatives** for cheap_small_text: "What is a language model?", "Summarize the BERT model", etc.
|
| 130 |
+
- **+30 hard negatives** for code_model: "What is the dress code?", "Morse code for SOS", etc.
|
| 131 |
+
- **+80 hard negatives** for vision_model: text-only discussions of charts/diagrams
|
| 132 |
|
| 133 |
## Performance
|
| 134 |
|
| 135 |
+
Evaluated on 388 held-out test prompts:
|
| 136 |
|
| 137 |
| Metric | Value |
|
| 138 |
|--------|-------|
|
| 139 |
+
| Accuracy | **0.9768** |
|
| 140 |
+
| Macro F1 | **0.9763** |
|
| 141 |
+
|
| 142 |
+
### Confusion Matrix
|
| 143 |
|
| 144 |
+
```
|
| 145 |
+
pred→
|
| 146 |
+
cheap code vision strong
|
| 147 |
+
true cheap 96 1 1 3
|
| 148 |
+
true code 0 102 0 0
|
| 149 |
+
true vision 2 2 80 0
|
| 150 |
+
true strong 0 0 0 101
|
| 151 |
+
```
|
| 152 |
|
| 153 |
+
### Per-class F1
|
| 154 |
|
| 155 |
+
| Class | F1 Score |
|
| 156 |
+
|-------|----------|
|
| 157 |
+
| cheap_small_text | 0.9648 |
|
| 158 |
+
| code_model | 0.9855 |
|
| 159 |
+
| vision_model | 0.9697 |
|
| 160 |
+
| strong_general | 0.9854 |
|
| 161 |
|
| 162 |
+
### Required Test Prompts
|
| 163 |
+
|
| 164 |
+
| Prompt | Expected | Predicted | Confidence |
|
| 165 |
+
|--------|----------|-----------|------------|
|
| 166 |
+
| Summarize this paragraph: Hugging Face hosts models and datasets. | cheap_small_text | **cheap_small_text** | 0.9907 |
|
| 167 |
+
| What is in this screenshot? | vision_model | **vision_model** | 0.9926 |
|
| 168 |
+
| Analyze this chart and tell me the trend | vision_model | **vision_model** | 0.9856 |
|
| 169 |
+
| Write a Python function to reverse a string | code_model | **code_model** | 0.9937 |
|
| 170 |
+
| Fix this React error: Cannot read properties of undefined | code_model | **code_model** | 0.9932 |
|
| 171 |
+
| Plan a research project comparing small language models on math reasoning. | strong_general | **strong_general** | 0.9946 |
|
| 172 |
|
| 173 |
## Model Details
|
| 174 |
|
|
|
|
| 178 |
| Architecture | DistilBERTForSequenceClassification |
|
| 179 |
| Classes | 4 |
|
| 180 |
| Max sequence length | 128 tokens |
|
| 181 |
+
| Training data | 600 prompts (v2, balanced with hard negatives) |
|
| 182 |
+
| Test data | 388 prompts |
|
| 183 |
+
| Training epochs | 4 |
|
| 184 |
+
| Learning rate | 3e-5 |
|
| 185 |
+
| Batch size | 8 |
|
| 186 |
| Model size | ~255 MB |
|
| 187 |
|
| 188 |
## Training Data
|
| 189 |
|
| 190 |
- Dataset: [somukandula/prompt-router-dataset](https://huggingface.co/datasets/somukandula/prompt-router-dataset)
|
| 191 |
+
- 600 training prompts, 388 test prompts
|
| 192 |
+
- 4 classes with hard negatives for each
|
| 193 |
+
|
| 194 |
+
## Comparison with Baselines
|
| 195 |
+
|
| 196 |
+
| Method | Accuracy | Macro F1 |
|
| 197 |
+
|--------|----------|----------|
|
| 198 |
+
| Rule-Based (keywords) | 0.8786 | 0.8712 |
|
| 199 |
+
| Embeddings + LogReg | 0.9857 | 0.9855 |
|
| 200 |
+
| DistilBERT v1 | 1.0000 | 1.0000 |
|
| 201 |
+
| **DistilBERT v2 (this model)** | **0.9768** | **0.9763** |
|
| 202 |
+
|
| 203 |
+
> v1 achieved 1.0 on a smaller synthetic test set. v2 uses a larger, more challenging test set with hard negatives and still achieves >97% accuracy.
|
| 204 |
|
| 205 |
## Inference Script
|
| 206 |
|
|
|
|
| 210 |
python router_inference.py "Your prompt here"
|
| 211 |
```
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
## Limitations
|
| 214 |
|
| 215 |
- Trained on **synthetic** prompts, not real user traffic. Performance may vary on out-of-distribution production prompts.
|
| 216 |
- Does **not** inspect actual images — it only routes based on the text prompt (e.g. "describe this screenshot").
|
| 217 |
- Cost savings depend on your actual pricing; the relative cost model used here is illustrative.
|
|
|
|
| 218 |
|
| 219 |
## License
|
| 220 |
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 267838720
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:67f0c2e4d35255562a324f134d584faedb5a35bbc4a15f8c20e21f3e209413a2
|
| 3 |
size 267838720
|