$ show infinitered/nsfwjs
NSFWJS is a client-side JavaScript library for detecting NSFW (Not Safe For Work) content in images using TensorFlow.js. It provides a simple API to load pre-trained models (MobileNetV2, MobileNetV2Mid, InceptionV3) and classify images into five categories: Drawing, Hentai, Neutral, Porn, and Sexy. The library is designed for both browser and Node.js environments, with backend selection guidance for optimal performance (WebGL, WebGPU, WASM, CPU). The core architecture consists of a lightweight `NSFWJS` class that handles model loading, inference, classification, and disposal, with support for tree-shaking via separate model imports. The repository also includes several demo applications: a full-featured React + Vite demo with Web Worker offloading, camera support, and file drop; a Node.js Express server example; and a minimal HTML/script tag demo. While primarily an application library, it is hybrid in nature as it bundles pre-trained model weights (quantized and base64-encoded) and includes model conversion scripts, making it both a software tool and a model distribution package. The library relies on TensorFlow.js as a peer dependency and uses IndexedDB for model caching in the browser.
The LLM identified 2 files worth highlighting for reuse:
The `NSFWJS` class encapsulates the complete lifecycle of a TensorFlow.js model for NSFW classification. It provides `load()`, `infer()`, `classify()`, and `dispose()` methods, handles both Layers and Graph models, and supports custom model definitions for tree-shaking. This component is self-contained (imports only from TensorFlow.js and internal types) and can be extracted as a reusable pattern for any TFJS-based classification task, demonstrating clean separation of concerns and robust error handling.
A Web Worker that offloads NSFW model loading and classification from the main thread to prevent UI blocking. It implements IndexedDB caching for the model to speed up subsequent loads, and communicates with the main thread via postMessage. This pattern is highly reusable for any browser-based ML inference that requires a responsive UI, demonstrating advanced knowledge of Web Workers, IndexedDB, and asynchronous message passing.