A simple PoC (Proof of Concept) NSFW Detector API Server using NsfwSpy.js model.
nsfw-detector-api is a core dependency of nostr-filter-relay.
Published docker image is available in ghcr.io. Run it instantly:
docker run --init --rm -p 8081:8081 ghcr.io/atrifat/nsfw-detector-api:main
or using docker image with CUDA drivers included (marked with cuda
suffix in the tag name) and NVIDIA docker runtime (Container Toolkit) support:
docker run --gpus all --init --rm -p 8081:8081 ghcr.io/atrifat/nsfw-detector-api:main-cuda
You can also clone this repository to run or modify it locally
git clone https://github.com/atrifat/nsfw-detector-api
cd nsfw-detector-api
install its dependencies
npm install
and run it using command
npm run start
or run it using node command directly
node src/index.mjs
If you want to test the API server, you can use GUI tools like Postman or using curl.
Send request by using image URL:
curl --header "Content-Type: application/json" \
--header "Authorization: Bearer myapitokenchangethislater" \
--request POST \
--data '{"url":"https://example.org/image.jpg"}' \
http://localhost:8081/predict
or send request by using base64 string of the image:
curl --header "Content-Type: application/json" \
--header "Authorization: Bearer myapitokenchangethislater" \
--request POST \
--data '{"data":"base64stringhere"}' \
http://localhost:8081/predict_data
The output is JSON which consists of four predicted classes (based on NsfwSpy.js) as follows:
{
"data": {
"hentai": 0.00016754239914007485,
"neutral": 0.9930612444877625,
"pornography": 0.0058021554723382,
"sexy": 0.0009690204169601202,
"predictedLabel": "neutral"
}
}
Run unit and integration tests using the following command:
npm run test
Run end-to-end tests using the following command:
npm run test:e2e
The following environment variables can be used to configure the API server:
PORT
: (Optional) The port the server listens on (default: 8081).API_TOKEN
: (Optional) The API token for authentication (default: myapitokenchangethislater).ENABLE_API_TOKEN
: (Optional) Enable or disable API token authentication (default: false).ENABLE_CONTENT_TYPE_CHECK
: (Optional) Ensure content type check via header request (default: false).FFMPEG_PATH
: (Optional) Set to other path for ffmpeg installed in system (example: /usr/bin/ffmpeg) otherwise automatically inferred from ffmpeg.path pre-installed dependencyIMG_DOWNLOAD_PATH
: (Optional) Directory to store temporary files (default: /tmp/).MAX_VIDEO_SIZE_MB
: (Optional) Maximum size of video for classification in MB (default: 100).CACHE_DURATION_IN_SECONDS
: (Optional) Duration of classification cache in seconds (default: 86400).MAX_CACHE_ITEM_NUM
: (Optional) Maximum number of items in classification cache (default: 200000).REQUEST_TIMEOUT_IN_SECONDS
: (Optional) Request timeout for downloading image or checking image header in seconds (default: 60).USER_AGENT
: (Optional) User agent for downloading files (default: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36').WORKER_POOL_MIN_WORKERS
: (Optional) Minimum number of workers in the worker pool for NSFW detection and image processing (default: 2).WORKER_POOL_MAX_WORKERS
: (Optional) Maximum number of workers in the worker pool. Falls back toWORKER_POOL_MIN_WORKERS
if not set, then to 2.VIDEO_PROCESSING_CONCURRENCY
: (Optional) Set concurrency for video processing tasks (default: 10).MUTEX_CACHE_MAX_ITEM_NUM
: (Optional) Set maximum number of item in mutex cache (default: 5000).MUTEX_CACHE_TTL_IN_SECONDS
: (Optional) Set duration of mutex cache in seconds (default: 600).
MIT
- Rif'at Ahdi Ramadhani (atrifat)
- d00M_L0rDz (NsfwSpy)