LoRA adapter that turns Qwen3-4B-Instruct-2507 into an enterprise PII masker and ticket router. Given a raw support ticket or HR/legal snippet, it returns one strict JSON object:
{"masked_text": "Hi, I'm [PERSON]. My card [CREDIT_CARD] was charged twice, refund ASAP. [EMAIL]",
"detected_pii_types": ["PERSON", "EMAIL", "CREDIT_CARD"],
"ticket_category": "Billing_and_Payments",
"urgency_level": "High"}
Tags: [PERSON] [EMAIL] [PHONE] [SSN] [CREDIT_CARD] [IP_ADDRESS] [API_KEY].
Departments: Billing_and_Payments, Technical_Support, Security_and_Access, Legal_and_Compliance,
General_Inquiry. Urgency: Low, Medium, High.
The untuned base model leaked 61.6% of PII values; this adapter leaks 1.1%.
Code, API server, tests: https://github.com/zainabbas14/slm-pii-masker · CPU / Ollama build: Zain9989/pii-masker-qwen3-4b-GGUF · Dataset: Zain9989/enterprise-pii-ticket-sft · Training run: W&B
200 held-out test tickets, identical system prompt, greedy decoding, 4-bit weights, Tesla T4. PII is scored strictly: a tag only counts if it replaces exactly the right characters under the right type, and outputs that rephrase the ticket get no PII credit.
| Metric | Base Qwen3-4B | Fine-tuned (QLoRA) |
|---|---|---|
| Strict JSON (raw output is pure JSON) | 100.0% | 100.0% |
| Schema adherence (Pydantic) | 61.5% | 100.0% |
| Text fidelity (no rephrasing) | 54.0% | 99.0% |
| PII precision (typed, exact span) | 60.7% | 97.9% |
| PII recall (typed, exact span) | 28.7% | 96.5% |
| PII F1 (typed, exact span) | 39.0% | 97.2% |
| Redaction recall (PII values not leaked) | 38.4% | 98.9% |
| Department accuracy | 50.5% | 98.0% |
| Department macro-F1 | 60.2% | 97.8% |
| Urgency accuracy | 37.5% | 98.0% |
| Tokens/sec (single request, T4, transformers) | 28.8 | 26.3 |
| Latency p50 / p95 | 2.96 s / 3.89 s | 2.76 s / 3.76 s |
| Entity | Base recall | Fine-tuned recall |
|---|---|---|
[PERSON] |
25.0% | 94.0% |
[EMAIL] |
57.1% | 100.0% |
[PHONE] |
18.2% | 95.5% |
[SSN] |
33.3% | 100.0% |
[CREDIT_CARD] |
34.6% | 100.0% |
[IP_ADDRESS] |
17.0% | 98.3% |
[API_KEY] |
16.7% | 83.3% |
Real ai4privacy rows have no routing labels, so their department/urgency labels come from a keyword labeler; synthetic rows carry gold labels. Accuracy on real rows therefore partly measures agreement with the labeler.
| Rows | n | Department acc. (base → fine-tuned) | Urgency acc. (base → fine-tuned) |
|---|---|---|---|
| Synthetic (gold labels) | 39 | 64.1% → 100.0% | 51.3% → 100.0% |
| ai4privacy (keyword-labeler labels) | 161 | 47.2% → 97.5% | 34.2% → 97.5% |
Synthetic test and training tickets share templates, so the synthetic row measures template recall more than generalisation.
Raw per-ticket predictions: eval/predictions.parquet.
The system prompt is in prompt_config.json;
use it verbatim and wrap the ticket in the user_template.
import json
from huggingface_hub import hf_hub_download
from unsloth import FastLanguageModel
cfg = json.load(open(hf_hub_download("Zain9989/pii-masker-qwen3-4b-lora", "prompt_config.json")))
model, tok = FastLanguageModel.from_pretrained("Zain9989/pii-masker-qwen3-4b-lora", max_seq_length=2048, load_in_4bit=True)
FastLanguageModel.for_inference(model)
ticket = "Hi, I'm Jane Doe, card 4111 1111 1111 1111 was charged twice, refund ASAP."
msgs = [{"role": "system", "content": cfg["system_prompt"]},
{"role": "user", "content": cfg["user_template"].format(ticket=ticket)}]
ids = tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt").to("cuda")
out = model.generate(ids, max_new_tokens=512, do_sample=False)
print(tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True))
For production use, put it behind the FastAPI service in the GitHub repo: it validates every answer with Pydantic, re-scans the output with deterministic regex, and falls back to rule-based masking if the model fails.
| Base | Qwen/Qwen3-4B-Instruct-2507 (Apache-2.0), Unsloth 4-bit build |
| Method | QLoRA r=16, α=32, all attention + MLP projections; loss on assistant tokens only |
| Data | 3,400 training examples; 2 epochs, 850 steps, effective batch 8, LR 0.0002 |
| Hardware | Free Colab Tesla T4: 95.2 min, 4.05 GB peak VRAM |
| Validation loss | best 0.0036 |
Chat format: plain ChatML (the Qwen3 hybrid-thinking template was replaced so no <think> block is ever emitted).
[PERSON].unsloth/Qwen3-4B-Instruct-2507-unsloth-bnb-4bit