Token Classification
Transformers
ONNX
Safetensors
English
distilbert
secret-detection
ner
security
api-keys
credentials
Eval Results (legacy)
Instructions to use AndrewAndrewsen/distilbert-secret-masker with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AndrewAndrewsen/distilbert-secret-masker with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="AndrewAndrewsen/distilbert-secret-masker")# Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("AndrewAndrewsen/distilbert-secret-masker") model = AutoModelForTokenClassification.from_pretrained("AndrewAndrewsen/distilbert-secret-masker", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 12,699 Bytes
ee6b360 2122140 ee6b360 | 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | ---
language:
- en
license: apache-2.0
tags:
- secret-detection
- token-classification
- ner
- security
- api-keys
- credentials
- distilbert
library_name: transformers
pipeline_tag: token-classification
datasets:
- custom
metrics:
- f1
- precision
- recall
model-index:
- name: distilbert-secret-masker
results:
- task:
type: token-classification
name: Secret Detection
dataset:
name: SecretMask v2 (600 test examples)
type: custom
metrics:
- type: f1
value: 0.52
name: F1 Score
verified: true
- type: precision
value: 0.82
name: Precision
verified: true
- type: recall
value: 0.38
name: Recall
verified: true
base_model: distilbert-base-uncased
---
> **New recommended model:** [`AndrewAndrewsen/distilbert-secret-masker-v3.3a-rs`](https://huggingface.co/AndrewAndrewsen/distilbert-secret-masker-v3.3a-rs)
> This repository is the previous **v2** release, kept for reproducibility. v3.3a-RS has
> different training, a stronger real-code evaluation, and a frozen sliding-window inference
> path β see its card for benchmarks.
# DistilBERT Secret Masker (Fast Expert)
[](https://opensource.org/licenses/Apache-2.0)
[](https://huggingface.co/AndrewAndrewsen/distilbert-secret-masker)
[](https://huggingface.co/AndrewAndrewsen/distilbert-secret-masker)
**Fast Expert** model for SecMask MoE system - specialized for rapid secret detection in short to medium-length texts (β€512 tokens).
---
## π― Overview
Fine-tuned DistilBERT model for detecting and classifying secrets (API keys, tokens, credentials) in text using Named Entity Recognition (NER). Serves as the **Fast Expert** in the [SecMask Mixture of Experts architecture](https://github.com/AndrewAndrewsen/secmask), handling **92.7%** of inference requests with ~6ms latency.
### Key Features
β
**High Speed**: 11ms P50 latency on CPU
β
**High Precision**: 82% (NER-only), **92.3% with filters**
β
**Production Ready**: Handles 92.7% of real-world cases
β
**Lightweight**: 265MB (66M parameters)
β
**Multi-Secret Types**: AWS keys, GitHub tokens, JWTs, API keys, PEM blocks, K8s secrets
> **Production Performance**: When combined with post-processing filters (PEM blocks, K8s secrets, pattern matching), achieves **92.3% precision, 80% recall, F1: 0.857**. The NER model alone achieves 82% precision and 38% recall. See [comprehensive benchmarks](https://github.com/AndrewAndrewsen/secmask/blob/main/BENCHMARK_RESULTS.md) for details.
> **Recommended Configuration**: Fast Expert + Filters (this model with post-processing) is the recommended production setup, outperforming Full MoE configurations. See [Configuration Guide](https://github.com/AndrewAndrewsen/secmask/blob/main/CONFIGURATION_GUIDE.md) for usage recommendations.
### Detected Secret Types
| Secret Type | Example Pattern | F1 Score |
| ----------------------------- | --------------------------------- | -------- |
| **AWS Access Keys** | `AKIA...` | 0.92 |
| **GitHub Personal Tokens** | `ghp_...`, `gho_...` | 0.88 |
| **JWT Tokens** | `eyJ0eXAiOiJKV1QiLCJhbGc...` | 0.85 |
| **Generic API Keys** | `sk-proj-...`, `api_key=...` | 0.79 |
| **PEM Certificate Blocks** | `-----BEGIN PRIVATE KEY-----` | 0.95 |
| **Kubernetes Secrets (data)** | `kind: Secret` β `data:` values | 0.81 |
| **Database Credentials** | Connection strings with passwords | 0.74 |
---
## π Quick Start
### Installation
```bash
pip install transformers torch
```
### Basic Usage
**Standalone (Direct):**
```python
from transformers import pipeline
# Load model
classifier = pipeline(
"token-classification",
model="AndrewAndrewsen/distilbert-secret-masker",
aggregation_strategy="simple"
)
# Detect secrets
text = "My API key is sk-proj-1234567890abcdefghijklmnopqrstuvwxyz"
results = classifier(text)
print(results)
# [{'entity_group': 'SECRET', 'score': 0.95, 'word': 'sk-proj-1234567890abcdefghijklmnopqrstuvwxyz', ...}]
```
**Recommended (via SecMask MoE):**
```python
# Clone SecMask repo
# git clone https://github.com/AndrewAndrewsen/secmask.git
from infer_moe import mask_text_moe
masked = mask_text_moe(
"My GitHub token is ghp_1234567890abcdefghijklmnopqrstuvwxyz",
fast_model_dir="AndrewAndrewsen/distilbert-secret-masker",
tau=0.80,
routing_mode="heuristic"
)
print(masked)
# "My GitHub token is [SECRET]"
```
### Command Line (via SecMask)
```bash
# Clone repo
git clone https://github.com/AndrewAndrewsen/secmask.git
cd secmask
# Mask secrets
python infer_moe.py \
--text "AWS key: AKIAIOSFODNN7EXAMPLE" \
--fast-model AndrewAndrewsen/distilbert-secret-masker \
--routing-mode heuristic \
--tau 0.80
# Output: AWS key: [SECRET]
```
---
## π Performance
### Secret Detection Metrics
| Metric | NER Only | With Filters (Recommended) |
| --------------- | -------- | -------------------------- |
| **F1 Score** | 0.52 | **0.857** |
| **Precision** | 82% | **92.3%** |
| **Recall** | 38% | **80.0%** |
| **P50 Latency** | 11ms | 11ms |
| **P90 Latency** | 14ms | 14ms |
| **P99 Latency** | 17ms | 17ms |
| **Throughput** | 84 req/s | 84 req/s (CPU) |
> **Note**: NER-only metrics measured at Ο=0.80. Production systems combine NER with post-processing filters (PEM blocks, K8s secrets, pattern matching) to achieve 92.3% precision and 80% recall. Post-processing adds no latency overhead. See [BENCHMARK_RESULTS.md](https://github.com/AndrewAndrewsen/secmask/blob/main/BENCHMARK_RESULTS.md) for comprehensive benchmarks.
### When This Model Is Used (MoE Routing)
The router selects this **Fast Expert** when:
- Token count β€ 512
- No multi-line structures (PEM blocks, K8s YAML)
- Simple text patterns
- **Coverage: 92.7%** of real-world requests
> **Note**: The recommended production configuration is **Fast Expert + Filters** alone (without the Long Expert). This achieves better results than Full MoE. See [Configuration Guide](https://github.com/AndrewAndrewsen/secmask/blob/main/CONFIGURATION_GUIDE.md) for details.
---
## ποΈ Model Details
### Architecture
- **Base Model**: [`distilbert-base-uncased`](https://huggingface.co/distilbert-base-uncased) (66M params, Apache 2.0)
- **Task**: Token Classification (NER)
- **Max Sequence Length**: 512 tokens
- **Label**: `B-SECRET`, `I-SECRET`, `O` (BIO tagging)
### Training Details
- **Dataset**: Custom SecretMask v2 (6,000 training examples)
- **Optimizer**: AdamW (lr=5e-5)
- **Epochs**: 3
- **Batch Size**: 16
- **Hardware**: GPU (NVIDIA A100 or equivalent)
- **Training Time**: ~30 minutes
### Evaluation
Evaluated on 600 held-out examples from SecretMask v2 test set:
```
Precision: 0.82
Recall: 0.38
F1: 0.52
Support: 1,021 secret tokens
```
**Key Insights**:
- **High Precision (82%)**: Very low false positive rate - safe for production
- **Lower Recall (38%)**: Misses some secrets when used standalone
- **Production Strategy**: Combine with deterministic filters (see `filters.py`) for PEM blocks, K8s secrets, and AWS patterns to achieve >90% coverage
- **Threshold Tuning**: Lower Ο from 0.80 to 0.50 for higher recall (trade-off: more false positives)
---
## π‘ Use Cases
### Production Applications
1. **Pre-Commit Hooks** - Prevent secrets in git commits
2. **CI/CD Pipelines** - Scan code before deployment
3. **Log Sanitization** - Remove secrets from application logs
4. **API Response Filtering** - Mask secrets in debug output
5. **Documentation Cleanup** - Sanitize before open-sourcing
6. **Security Audits** - Scan codebases for exposed credentials
### Example: Pre-Commit Hook
```python
# .git/hooks/pre-commit
from transformers import pipeline
classifier = pipeline("token-classification", model="AndrewAndrewsen/distilbert-secret-masker")
for file in staged_files:
content = read_file(file)
secrets = classifier(content)
if secrets:
print(f"β Secret detected in {file}!")
exit(1)
```
See [SecMask Examples](https://github.com/AndrewAndrewsen/secmask/blob/main/EXAMPLES.md) for more.
---
## β οΈ Limitations
### Known Issues
1. **Token Limit**: Cannot handle texts >512 tokens (use Longformer expert)
2. **English Only**: Trained on English text
3. **False Negatives**: ~25% recall means some secrets may be missed
4. **Context Sensitivity**: May struggle with unusual formatting
5. **Novel Patterns**: May miss new secret types not in training data
### Not Suitable For
β Non-English text
β Binary data or encrypted content
β Images/PDFs (extract text first)
β Very long documents (use [longformer-secret-masker](https://huggingface.co/AndrewAndrewsen/longformer-secret-masker))
β Real-time streaming (consider batching)
### Recommended Mitigations
- **Combine with filters**: Use deterministic filters for PEM blocks, K8s secrets (see [SecMask filters](https://github.com/AndrewAndrewsen/secmask/blob/main/filters.py))
- **Adjust threshold**: Lower `tau` for higher recall (more false positives)
- **Use MoE system**: Automatic routing to appropriate expert
- **Add regex patterns**: Supplement with custom patterns for your use case
---
## π License & Attribution
### Model License
**Apache 2.0** (inherited from `distilbert-base-uncased`)
### Base Model Attribution
This model is fine-tuned from:
- **Model**: [`distilbert-base-uncased`](https://huggingface.co/distilbert-base-uncased)
- **Authors**: Hugging Face
- **License**: Apache 2.0
- **Citation**:
```
@inproceedings{sanh2019distilbert,
title={DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter},
author={Sanh, Victor and Debut, Lysandre and Chaumond, Julien and Wolf, Thomas},
booktitle={NeurIPS EMC^2 Workshop},
year={2019}
}
```
### SecMask Code License
The SecMask inference code and training scripts are licensed under **MIT**. See [GitHub repo](https://github.com/AndrewAndrewsen/secmask/blob/main/LICENSE).
---
## π Related Models
| Model | Size | Max Tokens | Latency | Use Case |
| ------------------------------------------------------------------------------------------------------------ | ----- | ---------- | ------- | ------------------------- |
| **[distilbert-secret-masker](https://huggingface.co/AndrewAndrewsen/distilbert-secret-masker)** (this model) | 265MB | 512 | 6ms | Short texts, fast routing |
| **[longformer-secret-masker](https://huggingface.co/AndrewAndrewsen/longformer-secret-masker)** | 592MB | 2048 | 12ms | Long documents, configs |
| **[secretmask-gate](https://huggingface.co/AndrewAndrewsen/secretmask-gate)** | 12KB | N/A | +0.2ms | Learned MoE routing |
---
## π Resources
- **GitHub Repository**: [AndrewAndrewsen/secmask](https://github.com/AndrewAndrewsen/secmask)
- **Documentation**: [README](https://github.com/AndrewAndrewsen/secmask/blob/main/README.md)
- **Benchmarks**: [BENCHMARKS.md](https://github.com/AndrewAndrewsen/secmask/blob/main/BENCHMARKS.md)
- **Examples**: [EXAMPLES.md](https://github.com/AndrewAndrewsen/secmask/blob/main/EXAMPLES.md)
- **Deployment**: [DEPLOYMENT.md](https://github.com/AndrewAndrewsen/secmask/blob/main/DEPLOYMENT.md)
---
## π€ Contributing
Issues and contributions welcome! See [CONTRIBUTING.md](https://github.com/AndrewAndrewsen/secmask/blob/main/CONTRIBUTING.md).
---
**Developed by**: Anders Andersson ([@AndrewAndrewsen](https://huggingface.co/AndrewAndrewsen))
**Part of**: [SecMask MoE System](https://github.com/AndrewAndrewsen/secmask)
|