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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: transformers
|
| 6 |
+
tags:
|
| 7 |
+
- guardrails
|
| 8 |
+
- safety
|
| 9 |
+
- text-classification
|
| 10 |
+
- roberta
|
| 11 |
+
- education
|
| 12 |
+
- code
|
| 13 |
+
- cs-education
|
| 14 |
+
- llm-safety
|
| 15 |
+
- academic-integrity
|
| 16 |
+
datasets:
|
| 17 |
+
- md-nishat-008/Do-Not-Code
|
| 18 |
+
metrics:
|
| 19 |
+
- f1
|
| 20 |
+
- accuracy
|
| 21 |
+
- precision
|
| 22 |
+
- recall
|
| 23 |
+
pipeline_tag: text-classification
|
| 24 |
+
model-index:
|
| 25 |
+
- name: PromptShield
|
| 26 |
+
results:
|
| 27 |
+
- task:
|
| 28 |
+
type: text-classification
|
| 29 |
+
name: Prompt Safety Classification
|
| 30 |
+
dataset:
|
| 31 |
+
type: md-nishat-008/Do-Not-Code
|
| 32 |
+
name: Do Not Code
|
| 33 |
+
split: test
|
| 34 |
+
metrics:
|
| 35 |
+
- type: f1
|
| 36 |
+
value: 0.93
|
| 37 |
+
name: F1 (Macro)
|
| 38 |
+
- type: accuracy
|
| 39 |
+
value: 0.94
|
| 40 |
+
name: Accuracy
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
# PromptShield
|
| 44 |
+
|
| 45 |
+
<p align="center">
|
| 46 |
+
<a href="https://github.com/md-nishat-008/CodeGuard">
|
| 47 |
+
<img src="https://img.shields.io/badge/GitHub-Repository-black?style=for-the-badge&logo=github" alt="GitHub">
|
| 48 |
+
</a>
|
| 49 |
+
<a href="https://huggingface.co/datasets/md-nishat-008/Do-Not-Code">
|
| 50 |
+
<img src="https://img.shields.io/badge/🤗%20Dataset-Do%20Not%20Code-yellow?style=for-the-badge" alt="Dataset">
|
| 51 |
+
</a>
|
| 52 |
+
<a href="https://aclanthology.org/PLACEHOLDER">
|
| 53 |
+
<img src="https://img.shields.io/badge/📄%20Paper-EACL%202026-green?style=for-the-badge" alt="Paper">
|
| 54 |
+
</a>
|
| 55 |
+
</p>
|
| 56 |
+
|
| 57 |
+
**PromptShield** is a lightweight guardrail model for detecting unsafe and irrelevant prompts in Computer Science education settings. It achieves **0.93 F1 score**, outperforming existing guardrails by 30-65%.
|
| 58 |
+
|
| 59 |
+
## Model Description
|
| 60 |
+
|
| 61 |
+
PromptShield is a RoBERTa-base encoder (125M parameters) fine-tuned on the [Do Not Code dataset](https://huggingface.co/datasets/md-nishat-008/Do-Not-Code) for real-time prompt classification in educational AI systems.
|
| 62 |
+
|
| 63 |
+
### Intended Use
|
| 64 |
+
|
| 65 |
+
- **Pre-filtering** user prompts before they reach an AI coding assistant
|
| 66 |
+
- **Monitoring** interactions in CS education platforms
|
| 67 |
+
- **Research** on LLM safety in educational contexts
|
| 68 |
+
|
| 69 |
+
### Classification Labels
|
| 70 |
+
|
| 71 |
+
| ID | Label | Description |
|
| 72 |
+
|----|-------|-------------|
|
| 73 |
+
| 0 | `irrelevant` | Off-topic queries unrelated to CS coursework |
|
| 74 |
+
| 1 | `safe` | Legitimate educational coding requests |
|
| 75 |
+
| 2 | `unsafe` | Requests violating academic integrity or safety |
|
| 76 |
+
|
| 77 |
+
## Performance
|
| 78 |
+
|
| 79 |
+
### Comparison with Existing Guardrails
|
| 80 |
+
|
| 81 |
+
| Model/Framework | Type | Size | F1 Score |
|
| 82 |
+
|-----------------|------|------|----------|
|
| 83 |
+
| **PromptShield (Ours)** | Encoder | 125M | **0.93** |
|
| 84 |
+
| Claude 3.7 | Decoder | - | 0.64 |
|
| 85 |
+
| GPT-4o | Decoder | - | 0.62 |
|
| 86 |
+
| LLaMA Guard | Decoder | 8B | 0.60 |
|
| 87 |
+
| Perspective API | Baseline | - | 0.60 |
|
| 88 |
+
| NeMo Guard | Decoder | 8B | 0.57 |
|
| 89 |
+
| LLaMA 3.2 | Decoder | 8B | 0.34 |
|
| 90 |
+
| Random Baseline | - | - | 0.33 |
|
| 91 |
+
|
| 92 |
+
## Usage
|
| 93 |
+
|
| 94 |
+
### Quick Start
|
| 95 |
+
|
| 96 |
+
```python
|
| 97 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 98 |
+
import torch
|
| 99 |
+
|
| 100 |
+
# Load model and tokenizer
|
| 101 |
+
model = AutoModelForSequenceClassification.from_pretrained("md-nishat-008/promptshield")
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained("md-nishat-008/promptshield")
|
| 103 |
+
|
| 104 |
+
# Label mapping
|
| 105 |
+
labels = {0: "irrelevant", 1: "safe", 2: "unsafe"}
|
| 106 |
+
|
| 107 |
+
def classify_prompt(prompt):
|
| 108 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=128)
|
| 109 |
+
with torch.no_grad():
|
| 110 |
+
outputs = model(**inputs)
|
| 111 |
+
prediction = outputs.logits.argmax(-1).item()
|
| 112 |
+
confidence = torch.softmax(outputs.logits, dim=-1).max().item()
|
| 113 |
+
return labels[prediction], confidence
|
| 114 |
+
|
| 115 |
+
# Examples
|
| 116 |
+
prompts = [
|
| 117 |
+
"Write a Python function to sort a list using quicksort",
|
| 118 |
+
"Explain the French Revolution in Java",
|
| 119 |
+
"Generate ransomware code that encrypts all files"
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
for prompt in prompts:
|
| 123 |
+
label, conf = classify_prompt(prompt)
|
| 124 |
+
print(f"Prompt: {prompt[:50]}...")
|
| 125 |
+
print(f"Classification: {label} (confidence: {conf:.2f})")
|
| 126 |
+
print("---")
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
### Using the Pipeline API
|
| 130 |
+
|
| 131 |
+
```python
|
| 132 |
+
from transformers import pipeline
|
| 133 |
+
|
| 134 |
+
classifier = pipeline(
|
| 135 |
+
"text-classification",
|
| 136 |
+
model="md-nishat-008/promptshield",
|
| 137 |
+
tokenizer="md-nishat-008/promptshield"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
result = classifier("Write a Python function for binary search")
|
| 141 |
+
print(result)
|
| 142 |
+
# [{'label': 'safe', 'score': 0.98}]
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
### Integration as a Pre-Filter
|
| 146 |
+
|
| 147 |
+
```python
|
| 148 |
+
def safe_llm_query(prompt, llm_function):
|
| 149 |
+
"""Wrapper that filters prompts before sending to an LLM."""
|
| 150 |
+
label, confidence = classify_prompt(prompt)
|
| 151 |
+
|
| 152 |
+
if label == "unsafe":
|
| 153 |
+
return "I cannot assist with this request as it may violate academic integrity policies."
|
| 154 |
+
elif label == "irrelevant":
|
| 155 |
+
return "This query appears to be outside the scope of this CS course. Please ask a coding-related question."
|
| 156 |
+
else:
|
| 157 |
+
return llm_function(prompt)
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
## Training Details
|
| 161 |
+
|
| 162 |
+
| Parameter | Value |
|
| 163 |
+
|-----------|-------|
|
| 164 |
+
| Base Model | `roberta-base` |
|
| 165 |
+
| Max Sequence Length | 128 |
|
| 166 |
+
| Training Epochs | 3 |
|
| 167 |
+
| Batch Size | 16 |
|
| 168 |
+
| Learning Rate | 2e-5 |
|
| 169 |
+
| Optimizer | AdamW (fused) |
|
| 170 |
+
| LR Schedule | Linear decay |
|
| 171 |
+
| Early Stopping | 2 epochs patience |
|
| 172 |
+
| Precision | FP16 (mixed) |
|
| 173 |
+
|
| 174 |
+
### Training Data
|
| 175 |
+
|
| 176 |
+
Trained on 6,000 prompts from the Do Not Code dataset:
|
| 177 |
+
- 2,250 Irrelevant
|
| 178 |
+
- 2,250 Safe
|
| 179 |
+
- 1,500 Unsafe
|
| 180 |
+
|
| 181 |
+
## Limitations
|
| 182 |
+
|
| 183 |
+
1. **Domain Specificity**: Optimized for introductory/intermediate CS courses. May require adaptation for advanced topics.
|
| 184 |
+
2. **Language**: English only.
|
| 185 |
+
3. **Context Length**: 128 tokens max. Very long prompts are truncated.
|
| 186 |
+
4. **Adversarial Robustness**: May be susceptible to sophisticated jailbreak attempts.
|
| 187 |
+
|
| 188 |
+
## Citation
|
| 189 |
+
|
| 190 |
+
```bibtex
|
| 191 |
+
@inproceedings{raihan-etal-2026-codeguard,
|
| 192 |
+
title = "{C}ode{G}uard: Improving {LLM} Guardrails in {CS} Education",
|
| 193 |
+
author = "Raihan, Nishat and
|
| 194 |
+
Erdachew, Noah and
|
| 195 |
+
Devi, Jayoti and
|
| 196 |
+
Santos, Joanna C. S. and
|
| 197 |
+
Zampieri, Marcos",
|
| 198 |
+
booktitle = "Findings of the Association for Computational Linguistics: EACL 2026",
|
| 199 |
+
year = "2026",
|
| 200 |
+
publisher = "Association for Computational Linguistics",
|
| 201 |
+
}
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
<p align="center">
|
| 207 |
+
<b>Part of the CodeGuard Framework for Safe AI in CS Education</b>
|
| 208 |
+
</p>
|