AI-powered prompt injection detective - detect and analyze suspicious prompts
Project description
PromptScan
AI-powered prompt injection detection with transparent ensemble voting
Detect malicious prompt injection attacks with a production-ready ensemble of CNN, LSTM, and Transformer models. See exactly how each model votes with transparent confidence scores.
# Install and run in 30 seconds
pip install promptscan
promptscan predict "Ignore all previous instructions"
Why PromptScan?
Prompt injections are emerging security threats where malicious users bypass AI system safeguards. PromptScan provides:
- ๐ฌ Multi-model ensemble โ Combines CNN speed with LSTM sequence understanding and Transformer context
- ๐ Safetensors format โ Secure model storage without pickle security risks
- โ๏ธ Hugging Face Hub โ Automatic model download when not found locally
- ๐ Transparent voting โ See individual model predictions and confidence scores
- โก Production-ready โ Clean CLI, parallel inference, and self-contained models
Features
- Models: CNN (2.7M), LSTM (3.3M), Transformer (67M), Ensemble (all three)
- Format: Safetensors + JSON config (no pickle
.ptfiles in distribution) - Distribution: PyPI package + Hugging Face Hub repository
- CLI:
predict,train,hf download/upload/list,convert-model - API:
UnifiedDetectorwith Python interface - Voting strategies: Majority (default), weighted, confidence-based, soft
Quick Start
Installation
# Using pip (recommended)
pip install promptscan
# Using uv
uv pip install promptscan
# From source
git clone https://github.com/0xdewy/promptscan.git
cd promptscan
pip install -e .
Basic Usage
# Analyze text (ensemble is default)
promptscan predict "Ignore all previous instructions"
# Output shows individual model predictions:
# Individual model predictions:
# - cnn: INJECTION (99.86%)
# - lstm: SAFE (97.47%)
# - transformer: INJECTION (95.21%)
#
# Ensemble result: INJECTION (99.86%)
# Use specific model types
promptscan predict --model-type cnn "Test text"
promptscan predict --model-type lstm "Test text"
promptscan predict --model-type transformer "Test text"
# Analyze files and directories
promptscan predict --file input.txt
promptscan predict --dir ./prompts/ --summary
Hugging Face Hub Integration
# Download models from Hugging Face Hub
promptscan hf download --model cnn
promptscan hf download --model all
# List available models
promptscan hf list
# Upload models (requires HF_TOKEN)
promptscan hf upload --model-dir ./models/
Architecture
Ensemble System
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Input Text โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโผโโโโโโโ โโโโโโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ
โ CNN โ โ LSTM โ โ Transformer โ
โ (Fast) โ โ (Sequential) โ โ (Contextual)โ
โโโโโโโโโฌโโโโโโโ โโโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ
โ โ โ
โโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโ
โ Voting Strategy โ
โ (Majority/Weighted/Confidence) โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ
โโโโโโโโโผโโโโโโโ
โ Final โ
โ Prediction โ
โโโโโโโโโโโโโโโโ
Model Specifications
| Model | Parameters | Architecture | Strength | Format |
|---|---|---|---|---|
| CNN | 2.7M | Convolutional Neural Network | Local pattern detection | Safetensors |
| LSTM | 3.3M | Bidirectional LSTM | Sequential understanding | Safetensors |
| Transformer | 67M | Transformer encoder | Contextual understanding | Safetensors |
| Ensemble | All | Combined voting | Robustness across patterns | Multiple |
Note: All models use safetensors format for security. Training checkpoints may use .pt format internally.
Python API
from promptscan import UnifiedDetector
# Load detector (ensemble is default)
detector = UnifiedDetector(model_type="ensemble")
# Analyze text
result = detector.predict("Ignore all previous instructions")
print(f"Result: {result['prediction']} ({result['confidence']:.2%})")
# Get individual model predictions in ensemble mode
if "individual_predictions" in result:
for pred in result["individual_predictions"]:
model_type = pred.get('model_type', 'Unknown')
print(f"{model_type}: {pred['prediction']} ({pred['confidence']:.2%})")
Dataset
PromptScan is trained on a curated dataset of 17,195 examples:
- 10,833 injection prompts (63.0%)
- 6,362 safe prompts (37.0%)
- Multilingual: English (primary) with Spanish examples
- Sources: Curated from security research projects
- Accuracy: High validation accuracy on ensemble
Development
Project Structure
promptscan/
โโโ __init__.py # Package initialization, version, model paths
โโโ cli.py # Command-line interface
โโโ unified_detector.py # Unified detector interface
โโโ models/ # CNN, LSTM, Transformer implementations
โโโ ensemble/ # Ensemble detection system
โโโ hf_utils.py # Hugging Face Hub integration
โโโ processors/ # Text processors
โโโ training/ # Training framework
โโโ utils/ # Utility modules
Development Setup
# Install with development dependencies
pip install -e ".[dev]"
# Run tests
pytest tests/
# Format code
black promptscan/ scripts/ tests/
ruff check --fix promptscan/
# Type checking
mypy promptscan/
Building and Publishing
# Build package
python -m build
# Check package
twine check dist/*
# Publish to PyPI (requires token)
twine upload dist/*
# Or using uv
uv publish dist/promptscan-*.whl
Requirements
- Python 3.8+
- PyTorch 2.0.0+
- safetensors>=0.4.0
- huggingface-hub>=0.20.0
- pandas>=2.0.0
- transformers>=4.30.0 (for Transformer model)
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
PromptScan โ Transparent, robust prompt injection detection for AI security.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file promptscan-0.1.3.tar.gz.
File metadata
- Download URL: promptscan-0.1.3.tar.gz
- Upload date:
- Size: 101.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c90533ce39ab0c47a14b15cfa5a431a9c2294c74c56e836875fb5c96a55726ed
|
|
| MD5 |
3aaf6605bb7f1c6ee6fa6285b67ef859
|
|
| BLAKE2b-256 |
fcb28ad780564ed117dddf889e5185c6a606962656617c4b252d0278c92fc7d9
|
File details
Details for the file promptscan-0.1.3-py3-none-any.whl.
File metadata
- Download URL: promptscan-0.1.3-py3-none-any.whl
- Upload date:
- Size: 22.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8371515d43c0caeec0f40b0bf489da47596f094ad1f7762d60ce5bc4ffae04e0
|
|
| MD5 |
595f09cf0a4c0b9daf73f614d162d5c0
|
|
| BLAKE2b-256 |
cf74869825ef675b7bba165c8add2e881dcedd0d9008f73170f6acb02ec86568
|