Skip to main content

NSFWPY 🔥 (nsfwpy-onnx)

PyPI Version PyPI Downloads GitHub Repo Python Version License Hugging Face

High-performance, CPU-optimized Python 3.14 port of NSFWJS using ONNX Runtime.

Classify static images (JPEG, PNG, WEBP) and Animated WebP / GIF / APNG formats into 5 canonical NSFW 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.

📥 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 model (auto-downloads from HuggingFace if missing)
model = nsfwpy.load_model("mobilenet_v2")

# Classify static image or animated WebP / GIF
results = model.classify("sample.jpg", top_k=5)
print(results)
# Output:
# [
#   {'className': 'Neutral', 'probability': 0.6280},
#   {'className': 'Drawing', 'probability': 0.2822},
#   {'className': 'Sexy', 'probability': 0.0548},
#   {'className': 'Porn', 'probability': 0.0246},
#   {'className': 'Hentai', 'probability': 0.0104}
# ]

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

2. Command Line Interface (CLI)

# Classify a local image file or URL
nsfwpy classify path/to/image.jpg

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

# Choose a specific model architecture
nsfwpy classify sample.jpg --model inception_v3

🧠 Supported Models

1. 5-Category Models (NSFWJS Architecture)

Model Name Backbone Categories Size Auto-Download Link
mobilenet_v2 (Default) MobileNet V2 5 (Drawing, Hentai, Neutral, Porn, Sexy) ~14 MB HF Download
mobilenet_v3 MobileNet V3 5 (Drawing, Hentai, Neutral, Porn, Sexy) ~22 MB HF Download
inception_v3 Inception V3 5 (Drawing, Hentai, Neutral, Porn, Sexy) ~95 MB HF Download

2. Vision Transformer (ViT) Binary Models (SFW vs NSFW)

Powered by onnx-community/nsfw_image_detection-ONNX:

Model Name Format Precision Size Description
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_quantized / nsfw_vit_int8 INT8 8-bit Quantized ~87 MB Best balance of speed, size, and accuracy
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

📜 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.0.8.tar.gz (12.2 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.0.8-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nsfwpy_onnx-1.0.8.tar.gz
Algorithm Hash digest
SHA256 33cf3c769811af2a3d48ceb7140997bfa8e2b7137f94cdadce1df92343b0fe7f
MD5 057466b6c7f0dfe6dc0395fcdabffcd6
BLAKE2b-256 0b92e00212f773565ba4b76d8f57cf5abf268078cf6ff2d80414aed07a2520d9

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nsfwpy_onnx-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d4b0873e2931a0f16f56df8c666b5af3dc0c791c7773a5ba96a050bf644c85aa
MD5 69e3d8f38ae95c05e97afbb0bca4253b
BLAKE2b-256 dd1f80cc7dfd7d37a31e6e9209c6ccb2bd117f4bfa32100437f4c44c6c9b1abc

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

This release

1.0.8 This release

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