Text Classification
MLX
Safetensors
Chinese
English
tinyguard
safety
guardrail
prompt-injection
security
Instructions to use QuanxinLi/TinyGuard-Injection with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use QuanxinLi/TinyGuard-Injection with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download QuanxinLi/TinyGuard-Injection --local-dir TinyGuard-Injection
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Commit TinyGuard-Injection
Browse files- README.md +56 -0
- README.zh.md +56 -0
- config.json +26 -0
- inference.py +5 -0
- model.safetensors +3 -0
- tokenizer.json +0 -0
README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- zh
|
| 5 |
+
- en
|
| 6 |
+
tags:
|
| 7 |
+
- safety
|
| 8 |
+
- guardrail
|
| 9 |
+
- prompt-injection
|
| 10 |
+
- security
|
| 11 |
+
- text-classification
|
| 12 |
+
- mlx
|
| 13 |
+
metrics:
|
| 14 |
+
- f1
|
| 15 |
+
- accuracy
|
| 16 |
+
---
|
| 17 |
+
# TinyGuard-Injection (base + fine-tuned)
|
| 18 |
+
|
| 19 |
+
**Bilingual prompt-injection detector (incl. indirect injection) trained completely from scratch** · 9.1M parameters · MLX/Metal · Apache-2.0 (code) · fully synthetic training data
|
| 20 |
+
|
| 21 |
+
[中文说明](MODEL_CARD-injections.zh.md)
|
| 22 |
+
|
| 23 |
+
## Model description
|
| 24 |
+
|
| 25 |
+
A 3-way guardrail deployed at LLM-app input gates and RAG retrieval gates: `benign / direct injection / indirect injection`. Complements Meta Llama Prompt Guard 2 (22M/86M, **no Chinese support**): this model is trained bilingual with equal weight.
|
| 26 |
+
|
| 27 |
+
- Backbone: shared TinyGuard-Encoder (same pretrained base as TinyGuard-Command)
|
| 28 |
+
- Fine-tune: 12,330 bilingual labeled examples across six carriers (code & collaboration platforms, emails & web pages, long RAG chunks, tickets & customer-service dialogs, benign hard negatives, hidden-character & encoded payloads), with "minimal-difference pairs" and anti-overrefusal design
|
| 29 |
+
|
| 30 |
+
## Results (held-out data only, never seen in training)
|
| 31 |
+
|
| 32 |
+
| Benchmark | Metric | Value |
|
| 33 |
+
|---|---|---|
|
| 34 |
+
| Validation set, 616 rows | acc / macro-F1 | **99.0% / 0.990** |
|
| 35 |
+
| Adversarial set, 40 rows (zero-width chars / JSON rules / Japanese carriers / quoted disclaimers) | intervention recall / precision / F1 | 75% / 88% / 0.81 |
|
| 36 |
+
|
| 37 |
+
Reference: Llama Prompt Guard 2-86M reports EN injection F1 ≈ 0.98 with no Chinese. This model provides first-tier Chinese capability; its English adversarial numbers are below Meta's home turf (expected at 9.1M vs 86M).
|
| 38 |
+
|
| 39 |
+
## Usage
|
| 40 |
+
|
| 41 |
+
Weights: [`QuanxinLi/TinyGuard-Injection`](https://huggingface.co/QuanxinLi/TinyGuard-Injection) ·
|
| 42 |
+
Code & training pipeline: [`Quanxin-Lee/TinyGuard`](https://github.com/Quanxin-Lee/TinyGuard)
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
# quickstart (transformers/MLX dual format) — see the GitHub repo for the full walkthrough
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Limitations
|
| 49 |
+
|
| 50 |
+
- 5/40 misses on the adversarial set: JSON-encoded rules, URL-less "prioritize this" phrasing, direct asks inside authorized framing (red-team wording) — all weak-signal forms.
|
| 51 |
+
- English training volume is lower than Chinese; English adversarial generalization needs strengthening.
|
| 52 |
+
- Recommended to stack with a regex layer: this model handles semantics, regex handles known fingerprints.
|
| 53 |
+
|
| 54 |
+
## Training details & data statement
|
| 55 |
+
|
| 56 |
+
All data is synthetic/hand-written — no real user data. The full training pipeline, four audit scripts (three deep-scan rounds) and every dataset ship with the repository.
|
README.zh.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- zh
|
| 5 |
+
- en
|
| 6 |
+
tags:
|
| 7 |
+
- safety
|
| 8 |
+
- guardrail
|
| 9 |
+
- prompt-injection
|
| 10 |
+
- security
|
| 11 |
+
- text-classification
|
| 12 |
+
- mlx
|
| 13 |
+
metrics:
|
| 14 |
+
- f1
|
| 15 |
+
- accuracy
|
| 16 |
+
---
|
| 17 |
+
# TinyGuard-Injection(基座 + 微调)
|
| 18 |
+
|
| 19 |
+
**从零训练的双语提示注入检测器(含间接注入)** · 9.1M 参数 · MLX/Metal · Apache-2.0(代码)· 训练数据全合成
|
| 20 |
+
|
| 21 |
+
[English](MODEL_CARD-injections.md)
|
| 22 |
+
|
| 23 |
+
## 模型描述
|
| 24 |
+
|
| 25 |
+
部署在 LLM 应用输入口与 RAG 检索口的三分类护栏:`benign / direct injection / indirect injection`。与 Meta Llama Prompt Guard 2(22M/86M,**不含中文**)互补:本模型中英双语同权训练。
|
| 26 |
+
|
| 27 |
+
- 基座:共享 TinyGuard-Encoder(与 TinyGuard-Command 同一预训练底座)。
|
| 28 |
+
- 微调:12330 条双语标注,六类载体(代码与协作平台、邮件网页、超长 RAG 片段、工单客服、良性高难负例、隐藏字符与编码),含"最小差异对"与防过拒设计。
|
| 29 |
+
|
| 30 |
+
## 成绩(全部为留出数据,从未参与训练)
|
| 31 |
+
|
| 32 |
+
| 评测集 | 指标 | 数值 |
|
| 33 |
+
|---|---|---|
|
| 34 |
+
| 验证集 616 条 | acc / macro-F1 | **99.0% / 0.990** |
|
| 35 |
+
| 对抗集 40 条(零宽字符/JSON 规则/日文载体/引用免责) | 干预 recall / precision / F1 | 75% / 88% / 0.81 |
|
| 36 |
+
|
| 37 |
+
对照:Llama Prompt Guard 2-86M 官方卡报告英文注入检测 F1≈0.98,但无中文;本模型中文注入为第一梯队能力,英文对抗集略弱于其英文主场(预期内,9.1M vs 86M)。
|
| 38 |
+
|
| 39 |
+
## 用法
|
| 40 |
+
|
| 41 |
+
权重:[`QuanxinLi/TinyGuard-Injection`](https://huggingface.co/QuanxinLi/TinyGuard-Injection) ·
|
| 42 |
+
代码与训练管线:[`Quanxin-Lee/TinyGuard`](https://github.com/Quanxin-Lee/TinyGuard)
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
# quickstart(transformers/MLX 双格式)——完整流程见 GitHub 仓库
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## 局限
|
| 49 |
+
|
| 50 |
+
- 对抗集漏检 5/40:JSON 化规则、无 URL 的"prioritize this"类、授权框架下的直接索要(红队话术)——均为弱信号形态。
|
| 51 |
+
- 英文部分训练量低于中文同类项目主场,英文对抗泛化待加强。
|
| 52 |
+
- 建议与正则层叠用:本模型管语义,正则管已知指纹。
|
| 53 |
+
|
| 54 |
+
## 训练细节与数据声明
|
| 55 |
+
|
| 56 |
+
数据 100% 合成/人工编写,无真实用户数据。训练管线、审计脚本(含三轮深度扫描)与全部数据集随仓库开源。
|
config.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"TinyGuardClassifier"
|
| 4 |
+
],
|
| 5 |
+
"model_type": "tinyguard",
|
| 6 |
+
"hidden_size": 256,
|
| 7 |
+
"num_hidden_layers": 6,
|
| 8 |
+
"num_attention_heads": 4,
|
| 9 |
+
"intermediate_size": 1024,
|
| 10 |
+
"vocab_size": 16384,
|
| 11 |
+
"max_position_embeddings": 512,
|
| 12 |
+
"num_classes": 3,
|
| 13 |
+
"pad_token_id": 0,
|
| 14 |
+
"framework": "mlx",
|
| 15 |
+
"license": "apache-2.0",
|
| 16 |
+
"id2label": {
|
| 17 |
+
"0": "benign",
|
| 18 |
+
"1": "direct",
|
| 19 |
+
"2": "indirect"
|
| 20 |
+
},
|
| 21 |
+
"label2id": {
|
| 22 |
+
"benign": 0,
|
| 23 |
+
"direct": 1,
|
| 24 |
+
"indirect": 2
|
| 25 |
+
}
|
| 26 |
+
}
|
inference.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""TinyGuard inference quickstart (requires pip install mlx)"""
|
| 2 |
+
import mlx.core as mx, mlx.nn as nn
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import json, sys
|
| 5 |
+
# See the repo quickstart for the full walkthrough; placeholder stub here.
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7cd58a93f479420a246b883667e88cdf75678b88ea97b9ace95a39d5a4a5c88c
|
| 3 |
+
size 36246180
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|