Skip to main content

NSFWPY 🔥 (nsfwpy-onnx v1.1.1)

PyPI Version PyPI Downloads GitHub Repo Python Version License Hugging Face

High-performance, CPU-optimized Python NSFW image safety classification library powered by state-of-the-art Vision Transformer (ViT) models from Falcons AI and the ONNX Community using ONNX Runtime.

Classify static images (JPEG, PNG, WEBP) and Animated WebP / GIF / APNG formats into 5 canonical NSFW safety categories (Drawing, Hentai, Neutral, Porn, Sexy).


🌟 Key Features

  • 🐍 Python 3.14 Native: Fully compatible with Python 3.10+ and Python 3.14.
  • 🎞️ Animated WebP & GIF Support: Keyframe sampling and frame-aggregated safety classification for Animated WebP, GIF, and APNG formats.
  • CPU Optimized: Powered by onnxruntime tuned for multi-threaded CPU execution with graph optimization.
  • 📦 HuggingFace Auto-Download: Automatically downloads missing ONNX models from Hugging Face (expertskb/nsfwpy).
  • 🪶 Ultra Lightweight: Minimal dependencies (numpy, pillow, onnxruntime, click). No heavy web servers required.
  • 🛠️ CLI Tool Included: Classify images directly from your terminal.

⚡ Why nsfwpy-onnx is Superior to Legacy Classifiers

Feature / Metric nsfwpy-onnx 🔥 Legacy CNN Classifiers (MobileNet/Inception) 🐢 Heavy PyTorch / TensorFlow Libs 📦
Model Architecture Modern Vision Transformer (ViT) Legacy MobileNet V2 / Inception V3 (CNN) Older MobileNet V2
Accuracy & False Positives High (Understands visual context) Medium (High false positives on drawings/art) Medium
Framework Overhead Light C++ ONNX Runtime (~150MB RAM) Heavy TensorFlow / TFJS-Node (500MB+ RAM) Heavy PyTorch / TensorFlow
Startup Preloading (nsfwpy.preload()) Pre-warmed CPU execution & graph ❌ Manual warmup script required ❌ Cold start delays on first request
Animated WebP & GIF ✅ Built-in keyframe sampling & scanning ❌ Requires manual frame splitting ❌ Static images only
System Cache & Recovery ✅ Cross-platform (/etc, C:\, ~/.nsfwpy) + auto-recovery ❌ Basic local cache ❌ Manual download required

📊 Performance & Preload Benchmarks

Tested on CPU (Intel/AMD multi-thread execution):

import nsfwpy

# Warm up model graph and CPU execution pools at app startup
nsfwpy.preload()
Benchmark Metric Latency / Throughput Description
Preload & Warmup (nsfwpy.preload()) ~370 ms Downloads/loads model & compiles ONNX graph
Warmed-Up Inference Latency ~270 ms / image Instant classification with zero cold-start delay
Batch Processing Throughput ~4.5 - 5.0 imgs/sec High-throughput batch classification on CPU
Remote WebP Single Frame (HTTP + Inf) ~700 ms Network fetch + image decode + ONNX inference
Remote WebP Full Animation (10 keyframes) ~2.3s Complete frame-aggregated animation safety scan

📥 Installation

Install directly from PyPI:

pip install nsfwpy-onnx

Or install from GitHub:

git clone https://github.com/expertskb/nsfwpy-onnx.git
cd nsfwpy-onnx
python3.14 -m venv .venv
source .venv/bin/activate
pip install -e .

🚀 Quick Usage

1. Python Library Usage

import nsfwpy

# Load default quantized ViT model (auto-downloads if missing)
model = nsfwpy.load_model()

# Classify static image or animated WebP / GIF
results = model.classify("sample.jpg", top_k=5)
print(results)
# Output:
# [
#   {'className': 'Neutral', 'probability': 0.9850},
#   {'className': 'Porn', 'probability': 0.0150}
# ]

# Batch image classification
batch_results = model.classify_batch(["img1.jpg", "anim2.gif"])

2. Command Line Interface (CLI)

# Classify a local image file or URL using default nsfw_vit_quantized model
nsfwpy classify path/to/image.jpg

# Output formatted JSON
nsfwpy classify sample.jpg --json-out

# Choose full precision nsfw_vit model
nsfwpy classify sample.jpg --model nsfw_vit

🧠 Supported Models

Powered by onnx-community/nsfw_image_detection-ONNX:

Model Name Format Precision Size Description
nsfw_vit_quantized (Default) / nsfw_vit_int8 INT8 8-bit Quantized ~87 MB Default model: Best balance of speed, size, and accuracy
nsfw_vit FP32 Full Precision ~343 MB Original high-precision model (highest accuracy)
nsfw_vit_fp16 FP16 Half Precision ~172 MB 2x faster, 50% smaller
nsfw_vit_q4 Q4 4-bit Quantized ~56 MB Ultra-lightweight for constrained memory
nsfw_vit_q4f16 Q4F16 Mixed 4/16-bit ~50 MB Smallest model size
nsfw_vit_bnb4 BNB4 BitsAndBytes 4-bit ~51 MB BitsAndBytes 4-bit quantized
nsfw_vit_uint8 UINT8 Unsigned 8-bit ~87 MB Optimized for CPU hardware with uint8 support

📂 Example Scripts Index

Professional production-ready scripts are included under examples/:

Run any example using:

python examples/01_basic_classification.py

🙏 Credits & Acknowledgments

Special thanks to the open-source AI community and model creators:


📜 License

MIT License

Download files

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

Source Distribution

nsfwpy_onnx-1.1.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

nsfwpy_onnx-1.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file nsfwpy_onnx-1.1.1.tar.gz.

File metadata

  • Download URL: nsfwpy_onnx-1.1.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for nsfwpy_onnx-1.1.1.tar.gz
Algorithm Hash digest
SHA256 888630df298c43553c6ab4039b64ae7f25a4c47c014ac62c09d14dd2488830c8
MD5 b44aa674a47ee5e6bc61ec5bba4b0447
BLAKE2b-256 b55a1d5eb051e6f99ec6fde8a3a94fc831c9a212ac734aa5d2e94f2818803aca

See more details on using hashes here.

File details

Details for the file nsfwpy_onnx-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: nsfwpy_onnx-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for nsfwpy_onnx-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15ba6a8194985f8dfb34caa92233282f340412d12c33362ff1b0cf770a4c4afb
MD5 5c4113aac2db33bfc3581a4ac657cfbb
BLAKE2b-256 d06af2d5da6767b244b976b98628500166a4973cc602aba8a2543883748434de

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.2

2 files

This release

1.1.1 This release

2 files

1.1.0

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.2

2 files

1.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page