A comprehensive AI-powered image moderation system that detects NSFW (Not Safe For Work) content using OpenNSFW2. Built with FastAPI, MongoDB, and Streamlit in a microservices architecture.
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Frontend β β Backend β β Moderation β
β (Streamlit) βββββΊβ (FastAPI) βββββΊβ Service β
β Port: 8501 β β Port: 7000 β β (OpenNSFW2) β
βββββββββββββββββββ βββββββββββββββββββ β Port: 8000 β
β βββββββββββββββββββ
βΌ
βββββββββββββββββββ
β MongoDB β
β Port: 27017 β
βββββββββββββββββββ
- π NSFW Detection: AI-powered content analysis using OpenNSFW2 model
- π§ Machine Learning: Pure Python TensorFlow implementation for accurate detection
- π JWT Authentication: Secure token-based authentication with session management
- π€ File Upload: Support for multiple image formats (PNG, JPG, JPEG, GIF, BMP, WebP)
- π Detailed Analysis: Confidence scores, probability metrics, and threshold-based classification
- π₯ Health Monitoring: Service health checks and model status monitoring
- π³ Docker Support: Complete containerized deployment with model caching
- π¨ Modern UI: Clean Streamlit dashboard interface with real-time results
- Docker Desktop installed
- 4GB+ RAM (for OpenNSFW2 model)
# Clone and start (model downloads on first run - takes 2-5 minutes)
git clone <repository-url>
cd ImageModeration
docker-compose up --build -d
# Monitor model loading progress
docker-compose logs -f moderation- Dashboard: http://localhost:8501
- API Docs: http://localhost:7000/docs
- Credentials: admin / adminpassword
# Backend
cd backend
python -m venv .venv && .\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
$env:MONGODB_URI="mongodb://admin:adminpassword@localhost:27017/image_moderation?authSource=admin"
$env:MODERATION_SERVICE_URL="http://localhost:8000"
uvicorn app.main:app --port 7000 --reload
# Frontend (new terminal)
cd frontend
python -m venv .venv && .\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
$env:BACKEND_URL="http://localhost:7000"
streamlit run app.py
# Moderation Service (new terminal)
cd moderation-service
python -m venv .venv && .\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
uvicorn app.main:app --port 8000 --reload# Run tests
cd backend && pytest ../tests/ -v
# Manual API test
curl -X POST "http://localhost:7000/auth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=adminpassword"
# Upload image (replace YOUR_JWT_TOKEN)
curl -X POST "http://localhost:7000/moderate/" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-F "file=@path/to/image.jpg"ImageModeration/
βββ backend/ # FastAPI backend service
βββ frontend/ # Streamlit dashboard
βββ moderation-service/ # OpenNSFW2 AI service
βββ tests/ # Test suite
βββ docker-compose.yml # Service orchestration
βββ README.md
# Essential operations
docker-compose up --build -d # Start all services
docker-compose down # Stop services
docker-compose logs -f # View all logs
docker-compose ps # Check status
# Service-specific
docker-compose logs -f moderation # Model loading progress
docker-compose restart backend # Restart specific service
docker-compose build --no-cache moderation # Force rebuild
# Cleanup
docker-compose down -v # Remove volumes (clears model cache)# Backend
MONGODB_URI=mongodb://admin:adminpassword@mongodb:27017/image_moderation?authSource=admin
MODERATION_SERVICE_URL=http://moderation:8000
SECRET_KEY=your-super-secret-jwt-key-change-in-production
# Frontend
BACKEND_URL=http://backend:7000
# Moderation Service
THRESHOLD=0.5 # NSFW detection threshold
MAX_FILE_SIZE=50MB # Upload limitPOST /auth/token- Login and get JWT tokenGET /auth/tokens- List active user tokensDELETE /auth/tokens/{jti}- Revoke specific tokenPOST /auth/logout- Logout current session
POST /moderate/- Upload and analyze image for NSFW contentGET /moderate/health- Check moderation service and model status
GET /- Backend service health checkGET /health- Detailed moderation service health
{
"user": "admin",
"moderation": {
"is_nsfw": false,
"nsfw_probability": 0.0234,
"safe_probability": 0.9766,
"confidence": 0.9766,
"model_type": "OpenNSFW2"
}
}# Check model download progress
docker-compose logs -f moderation
# Expected: "INFO: OpenNSFW2 model loaded successfully"
# If fails: docker-compose restart moderation# Check all services running
docker-compose ps
# View specific service logs
docker-compose logs backend
# Clean restart
docker-compose down && docker-compose up --build -d# Test login
curl -X POST "http://localhost:7000/auth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=adminpassword"
# Check MongoDB connection
docker-compose logs mongodb# Monitor resource usage
docker stats
# Clear model cache if needed
docker-compose down -v
docker-compose up --build -d- JWT Authentication with token revocation
- Bcrypt password hashing with salt
- Input validation for file types and sizes
- Internal service communication (not externally exposed)
- Health monitoring with configurable checks
- Framework: Pure Python TensorFlow 2.x
- Input: PIL Images (auto-resized to 224x224)
- Output: NSFW probability score (0.0-1.0)
- Cache: Models cached in Docker volumes for faster restarts
- Memory: ~1GB RAM when loaded
# Check outdated packages
pip list --outdated
# Update and rebuild
pip install --upgrade package_name
docker-compose build --no-cache# Access MongoDB
docker-compose exec mongodb mongosh -u admin -p adminpassword
# Common operations
use image_moderation
db.tokens.find().pretty()
db.tokens.deleteMany({"expires_at": {"$lt": new Date()}}) # Clean expired tokens# Code quality
pip install pre-commit && pre-commit install
pre-commit run --all-files
# Performance testing
time curl -X POST "http://localhost:7000/moderate/" \
-H "Authorization: Bearer TOKEN" -F "file=@test.jpg"MIT License - see LICENSE file for details.
- Fork repository
- Create feature branch
- Run
pre-commit run --all-files - Add tests and run
pytest tests/ -v - Submit pull request
- Check troubleshooting section above
- Review logs:
docker-compose logs -f - Test health endpoints
- Open GitHub issue with full error logs
Made with β€οΈ using FastAPI, Streamlit, OpenNSFW2, and Docker
AI-powered content moderation made simple and accessible