Skip to main content

AI-powered prompt injection detective - detect and analyze suspicious prompts

Project description

PromptScan

AI-powered prompt injection detection with transparent ensemble voting

Python 3.8+ License: MIT PyPI version

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 .pt files in distribution)
  • Distribution: PyPI package + Hugging Face Hub repository
  • CLI: predict, train, hf download/upload/list, convert-model
  • API: UnifiedDetector with 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.

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

PromptScan โ€“ Transparent, robust prompt injection detection for AI security.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

promptscan-0.1.3.tar.gz (101.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

promptscan-0.1.3-py3-none-any.whl (22.8 MB view details)

Uploaded Python 3

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

Hashes for promptscan-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c90533ce39ab0c47a14b15cfa5a431a9c2294c74c56e836875fb5c96a55726ed
MD5 3aaf6605bb7f1c6ee6fa6285b67ef859
BLAKE2b-256 fcb28ad780564ed117dddf889e5185c6a606962656617c4b252d0278c92fc7d9

See more details on using hashes here.

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

Hashes for promptscan-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8371515d43c0caeec0f40b0bf489da47596f094ad1f7762d60ce5bc4ffae04e0
MD5 595f09cf0a4c0b9daf73f614d162d5c0
BLAKE2b-256 cf74869825ef675b7bba165c8add2e881dcedd0d9008f73170f6acb02ec86568

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page