Downloads · 30 days
34
13% of all-time downloads
md-nishat-008/PromptShield
PromptShield is a text classification model from md-nishat-008. Use it when you need a label for a piece of text. It is set up for transformers. The card lists the license as apache-2.0.
<p align="center" <a href="https://github.com/mraihan-gmu/CodeGuard/tree/main" <img src="https://img.shields.io/badge/GitHub-Repository-black?style=for-the-badge&logo=github" alt="GitHub" </a <a href="https://huggingf…
Downloads · 30 days
34
13% of all-time downloads
All-time downloads
254
Public
Parameters
125M
499 MB on disk
Likes
1
Public
Click a slice to open those files.
.safetensors499 MB · 99%
From the Hugging Face model README
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%.
PromptShield is a RoBERTa-base encoder (125M parameters) fine-tuned on the Do Not Code dataset for real-time prompt classification in educational AI systems.
| ID | Label | Description |
|---|---|---|
| 0 | irrelevant | Off-topic queries unrelated to CS coursework |
| 1 | safe | Legitimate educational coding requests |
| 2 | unsafe | Requests violating academic integrity or safety |
| Model/Framework | Type | Size | F1 Score |
|---|---|---|---|
| PromptShield (Ours) | Encoder | 125M | 0.93 |
| Claude 3.7 | Decoder | - | 0.64 |
| GPT-4o | Decoder | - | 0.62 |
| LLaMA Guard | Decoder | 8B | 0.60 |
| Perspective API | Baseline | - | 0.60 |
| NeMo Guard | Decoder | 8B | 0.57 |
| LLaMA 3.2 | Decoder | 8B | 0.34 |
| Random Baseline | - | - | 0.33 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
# Load model and tokenizer
model = AutoModelForSequenceClassification.from_pretrained("md-nishat-008/promptshield")
tokenizer = AutoTokenizer.from_pretrained("md-nishat-008/promptshield")
# Label mapping
labels = {0: "irrelevant", 1: "safe", 2: "unsafe"}
def classify_prompt(prompt):
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
prediction = outputs.logits.argmax(-1).item()
confidence = torch.softmax(outputs.logits, dim=-1).max().item()
return labels[prediction], confidence
# Examples
prompts = [
"Write a Python function to sort a list using quicksort",
"Explain the French Revolution in Java",
"Generate ransomware code that encrypts all files"
]
for prompt in prompts:
label, conf = classify_prompt(prompt)
print(f"Prompt: {prompt[:50]}...")
print(f"Classification: {label} (confidence: {conf:.2f})")
print("---")
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="md-nishat-008/promptshield",
tokenizer="md-nishat-008/promptshield"
)
result = classifier("Write a Python function for binary search")
print(result)
# [{'label': 'safe', 'score': 0.98}]
def safe_llm_query(prompt, llm_function):
"""Wrapper that filters prompts before sending to an LLM."""
label, confidence = classify_prompt(prompt)
if label == "unsafe":
return "I cannot assist with this request as it may violate academic integrity policies."
elif label == "irrelevant":
return "This query appears to be outside the scope of this CS course. Please ask a coding-related question."
else:
return llm_function(prompt)
| Parameter | Value |
|---|---|
| Base Model | roberta-base |
| Max Sequence Length | 128 |
| Training Epochs | 3 |
| Batch Size | 16 |
| Learning Rate | 2e-5 |
| Optimizer | AdamW (fused) |
| LR Schedule | Linear decay |
| Early Stopping | 2 epochs patience |
| Precision | FP16 (mixed) |
Trained on 6,000 prompts from the Do Not Code dataset:
@inproceedings{raihan-etal-2026-codeguard,
title = "{C}ode{G}uard: Improving {LLM} Guardrails in {CS} Education",
author = "Raihan, Nishat and
Erdachew, Noah and
Devi, Jayoti and
Santos, Joanna C. S. and
Zampieri, Marcos",
booktitle = "Findings of the Association for Computational Linguistics: EACL 2026",
year = "2026",
publisher = "Association for Computational Linguistics",
}