---
title: "Deploy NSFW Image Detector | (Just Updated) Image Moderation API, ViT Classifier, API Key Pre-Set"
description: "NSFW image detection API: image moderation classifier, ViT models, CPU"
category: "AI/ML"
url: https://railway.com/deploy/nsfw-image-detector-or-just-updated-imag
---

# Deploy NSFW Image Detector | (Just Updated) Image Moderation API, ViT Classifier, API Key Pre-Set

NSFW image detection API: image moderation classifier, ViT models, CPU

**[Deploy NSFW Image Detector | (Just Updated) Image Moderation API, ViT Classifier, API Key Pre-Set on Railway](https://railway.com/template/nsfw-image-detector-or-just-updated-imag)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/nsfw-image-detector-or-just-updated-imag/manifest.json

- **Creator:** SuperSlowSloth
- **Category:** AI/ML
- **Total deploys:** 1

## Template content

### nsfw-detector

- **Image:** ghcr.io/bon5co/nsfw-detector-railway:1.0.0@sha256:7aa87885bb450a23b4e18abe79d7d6d7cecac7c4613a526e0f274d37a4841cca
- **Health check:** /health
- **Public domain:** Yes

## Documentation

# Deploy and Host NSFW Image Detector (ViT Image Moderation API) on Railway

An NSFW image detector API scores an uploaded picture for adult content so you can block or blur it before other users see it. This template serves two open-weights Vision Transformer classifiers on CPU behind one authenticated HTTP API: Falconsai's `nsfw_image_detection` and AdamCodd's `vit-base-nsfw-detector`. The models are already inside the image, an API key is generated for you at deploy, and no GPU is needed.

## About Hosting NSFW Image Detector

The service runs `Falconsai/nsfw_image_detection` (Apache-2.0, ViT-base, 224 px input) and `AdamCodd/vit-base-nsfw-detector` (Apache-2.0, ViT-base, 384 px input) with PyTorch 2.15.0 behind a small FastAPI server. `POST /classify` takes one or more images, either as multipart `file` parts (up to 8 per request) or as a raw `image/*` body, and returns for each an `nsfw` probability from 0 to 1, a `normal` or `nsfw` label, a `flagged` boolean and the per-model scores. The default model is Falconsai; add `?model=adamcodd` for the second one or `?model=all` to run both and flag on the higher score, and `?threshold=0.8` to change the 0.5 cut-off per request. `GET /v1/models` lists the models and the limits in force.

The weights are baked into the image and verified by sha256 at build time, so nothing downloads at boot and both models are ready about 4 seconds after the container starts. The service sizes its thread pool from the container's own CPU quota, handles one request at a time so memory stays bounded, accepts JPEG, PNG, WebP, GIF, BMP and TIFF checked by their magic bytes, refuses an image over 10 MiB (`413`) or over 50 million pixels (`413`), and answers `415` for any other content type. Images are held in memory for the length of one request and are never logged or written to disk.

**Measured on Railway, 8 vCPU:** with the default model the model time (the `X-Processing-Seconds` header) was **about 0.08 s** for a 640x480 JPEG, **0.07 s** for 1920x1080 and **0.11 s** for 4000x3000, and a whole request over the public URL took **0.23 to 0.46 s**. AdamCodd took **0.15 to 0.23 s** per 1080p image and `?model=all` about **0.21 to 0.25 s**. The first request after boot is slower (0.5 s here). Twenty concurrent uploads of a 1080p JPEG finished in **1.7 s** wall clock, about **12 images per second** with the default model. Resident memory was **0.94 GB** after boot and peaked at **1.16 GB**.

**What it does not do.** Detection accuracy was not measured here and no explicit images were used in testing: the check was that the service loads, scores plain test images low, rejects bad input and keys, and answers in the times above. Both models are single-label classifiers: they give one NSFW probability, not categories such as nudity versus violence. The AdamCodd model card says it is trained to be restrictive (cleavage or a lot of skin scores NSFW) and reports 86% accuracy on AI-generated images against 96.5% on its own evaluation set. The Falconsai card does not describe its training data beyond "about 80,000 images", so its behaviour on drawings, medical or art images is not documented. Treat the score as a first filter and keep a human review path for anything that matters.

### Agent contract

- Public URL: `https://RAILWAY_PUBLIC_DOMAIN` (the generated domain of the `nsfw-detector` service)
- Private URL: `http://nsfw-detector.railway.internal:8080`
- Credentials: the `API_KEY` variable of the `nsfw-detector` service
- Auth: send `Authorization: Bearer $API_KEY` on every route except `GET /health`; a call without the key or with a wrong one answers `401`
- Health: `GET /health` returns 200 once both models are loaded (503 while they load)

```bash
# needs: RAILWAY_PUBLIC_DOMAIN, API_KEY (a variable of the nsfw-detector service); also curl and base64.
# signup: none - the API has no user accounts, only the API_KEY bearer
R="https://$RAILWAY_PUBLIC_DOMAIN"
echo iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg== | base64 -d | tee /tmp/px.png | wc -c
n=0; until [ "$(curl -s -o /dev/null -w '%{http_code}' "$R/health")" = 200 ]; do n=$((n+1)); [ "$n" -le 60 ] || break; sleep 5; done
curl -s -o /dev/null -w "health: %{http_code}\n" "$R/health"
curl -s -o /dev/null -w "write without key: %{http_code}\n" -X POST "$R/classify" -F file=@/tmp/px.png
curl -s -o /dev/null -w "write wrong key: %{http_code}\n" -X POST "$R/classify" -H "Authorization: Bearer not-the-key" -F file=@/tmp/px.png
curl -s -X POST "$R/classify?model=all" -H "Authorization: Bearer $API_KEY" -F file=@/tmp/px.png
```

A fresh deploy prints `health: 200`, `write without key: 401`, `write wrong key: 401` and a JSON result whose `results[0]` carries `flagged: false`, an `nsfw` score near 0 and a score for each of `falconsai` and `adamcodd`. A file that is not an image answers `422`, an image over the size or pixel limit answers `413`, a content type that is neither multipart nor `image/*` answers `415`, and a `model` other than `falconsai`, `adamcodd` or `all` or a `threshold` outside 0 to 1 answers `422`. The API only reads the image you send and never fetches a URL, so it cannot be pointed at an address inside your network.

## Why Deploy NSFW Image Detector on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying NSFW Image Detector on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.

- **Images stay in your container** — moderation runs in your own service, not in a hosted moderation API that sees every upload.
- **Two models, one call** — a fast default model and a stricter second one, or both with the higher score deciding.
- **No GPU bill and no per-image fee** — you pay for the container, not for each image.
- **Open weights, commercial use** — both models declare the Apache-2.0 licence on their model cards.
- **Private by default** — a generated bearer key guards every route but the health check, and the container refuses to start without one.

## Common Use Cases

- **Screen user uploads** — score avatars, post images and marketplace photos before they are published or shown to other users.
- **Moderation queue pre-filter** — flag only the images above a threshold you choose so reviewers see a fraction of the volume.
- **Check AI-generated images** — run an image generator's output through the classifier before it is returned to the user.

## Dependencies for NSFW Image Detector Hosting

- A service with **at least 2 GB of RAM** (resident memory measured at 0.94 GB after boot and 1.16 GB at peak) and 8 vCPU for the speeds above; fewer vCPU classify proportionally slower.
- No volume: the service keeps no state and the models are in the image.

### Deployment Dependencies

- [Falconsai/nsfw_image_detection](https://huggingface.co/Falconsai/nsfw_image_detection) — the default classifier, Apache-2.0 per its model card, pinned to commit 96cb0d0342c7afb80cab76ecc58b265fa44da256
- [AdamCodd/vit-base-nsfw-detector](https://huggingface.co/AdamCodd/vit-base-nsfw-detector) — the second classifier, Apache-2.0 per its model card, pinned to commit 8587de998f441aac03fdd57a85d2e4cb808c7d64
- [bon5co/nsfw-detector-railway](https://github.com/bon5co/nsfw-detector-railway) — the Railway wrapper image this template deploys, Apache-2.0
- Licences: the Hugging Face model cards of both models declare `license: apache-2.0`; neither repository carries a separate LICENSE file, so the card metadata is the licence statement. PyTorch is BSD-3-Clause and transformers is Apache-2.0. The notices travel with the image at `/app/NOTICE`.


## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [Weaviate](https://railway.com/deploy/weaviate-1) — Vector database with hybrid search and an API key-protected API
- [Möbius Personal AI OS](https://railway.com/deploy/mobius-v01-personal-ai-os) — Self-hosted AI agent that builds and runs apps for you, with memory.

Open this page in a browser: https://railway.com/deploy/nsfw-image-detector-or-just-updated-imag
