Bagusan21/TensorFlow-Nsfw
๐ NSFW Detection API (TensorFlow + NSFWJS)
High-performance NSFW image detection API using TensorFlow.js (Node) and NSFWJS. Optimized for Hugging Face Spaces (Docker) with queue system, caching, and FFmpeg preprocessing.
โจ Features
- ๐ NSFW Detection (Porn / Sexy / Hentai)
- โก Queue system (anti overload)
- ๐ง LRU Cache (faster repeated scans)
- ๐ผ๏ธ Image normalization via FFmpeg
- ๐ Supports:
- Image URL
- File Upload (multipart)
- Base64 buffer
- ๐ณ Ready for HuggingFace Docker Spaces
๐ฆ API Endpoints
- Detect from URL
POST /detect/url Content-Type: application/json
Body:
{ "url": "https://example.com/image.jpg" }
- Upload File
POST /detect/upload Content-Type: multipart/form-data
Form:
file: (image file)
- Base64
POST /detect/base64 Content-Type: application/json
Body:
{ "data": "base64string..." }
๐ค Response Example
{ "isNSFW": true, "score": 0.87, "percent": 87, "nsfw": { "porn": 87, "sexy": 65, "hentai": 12 }, "sfw": { "neutral": 5, "drawing": 3 } }
โ๏ธ Local Development
Install dependencies
npm install
Run server
npm start
Server runs on:
http://localhost:7860
๐ณ Run with Docker
Build image
docker build -t nsfw-api .
Run container
docker run -p 7860:7860 nsfw-api
๐ค Deploy to Hugging Face Spaces
- Create new Space
- Select Docker SDK
- Upload files:
Dockerfile package.json server.js nsfw.js
- Done โ
API will be available at:
https://<your-space>.hf.space
โก Performance Tips
- Increase concurrency:
const CONCURRENCY = 2
- Use lighter model:
nsfw.load()
- Reduce FFmpeg scale for faster processing
โ ๏ธ Limits
- Max file size: 5MB
- Queue limit: 20
- Default concurrency: 1
๐ Tech Stack
- Node.js
- Express
- TensorFlow.js (tfjs-node)
- NSFWJS
- FFmpeg
๐ License
MIT
๐ฌ Notes
- Model loaded once (fast inference)
- Cache avoids duplicate processing
- FFmpeg improves input consistency
Enjoy ๐
