Production-ready FastAPI moderation API with ML-powered content filtering, Redis caching, rate limiting, and full CI/CD pipeline.
# Pull the pre-built image
docker pull nyadzayo/moderation-api:latest
# Run with Docker Compose (includes Redis)
docker-compose up -d
# API is now available at http://localhost:8000
# View interactive docs at http://localhost:8000/docs# Clone the repository
git clone https://github.com/Nyadzayo/moderation-api.git
cd moderation-api
# Create virtual environment
python3.11 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Start Redis
docker run -d -p 6379:6379 redis:7-alpine
# Run the API
uvicorn app.main:app --reload --env-file .env.dev- ML-Powered Moderation - unitary/toxic-bert model for text classification
- Batch Processing - Handle multiple texts in a single request
- Custom Thresholds - Per-category threshold overrides
- Multi-Category Detection - harassment, hate, profanity, sexual, spam, violence
- Redis Caching - 40x faster for repeated requests (268ms to 6ms)
- Rate Limiting - Per-IP sliding window rate limits
- Lazy Model Loading - Fast startup, load on first request
- Async/Await - Non-blocking I/O for high concurrency
- Health Checks - Comprehensive component status monitoring
- Structured Logging - JSON logs with colored console output
- Graceful Degradation - API continues if Redis unavailable
- CI/CD Pipeline - Automated testing, build, and deployment
- Docker Multi-Arch - AMD64 and ARM64 support
- 100% Test Coverage - 22 passing tests
curl -X POST "http://localhost:8000/v1/moderate" \
-H "Content-Type: application/json" \
-d '{
"inputs": [
{"text": "Hello, world!"},
{"text": "This is inappropriate content"}
],
"return_scores": true
}'{
"results": [
{
"request_id": "req_abc123",
"flagged": false,
"categories": {
"harassment": false,
"hate": false,
"profanity": false,
"sexual": false,
"spam": false,
"violence": false
},
"category_scores": {
"harassment": 0.01,
"hate": 0.02,
"profanity": 0.01,
"sexual": 0.00,
"spam": 0.01,
"violence": 0.00
},
"model_info": {
"text_model": "unitary/toxic-bert",
"version": "1.0.0"
},
"processing_time_ms": 45,
"timestamp": "2025-09-30T12:00:00.000Z"
}
],
"total_items": 2,
"processing_time_ms": 95
}curl -X POST "http://localhost:8000/v1/moderate" \
-H "Content-Type: application/json" \
-d '{
"inputs": [{"text": "borderline content"}],
"thresholds": {
"harassment": 0.9,
"profanity": 0.8
}
}'See ARCHITECTURE.md for detailed system design, component diagrams, and technical decisions.
Key Components:
- FastAPI - High-performance web framework
- Transformers - Hugging Face ML model inference
- Redis - Caching and rate limiting
- Docker - Containerization
- GitHub Actions - CI/CD automation
- Railway - Production deployment
All configuration via environment variables. See .env.example for full list.
Key Variables:
# API
API_PORT=8000
LOG_LEVEL=INFO
# Model
DEFAULT_MODEL=unitary/toxic-bert
LAZY_LOAD_MODEL=true
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
# Rate Limiting
RATE_LIMIT_REQUESTS=100
RATE_LIMIT_WINDOW_SECONDS=60
# Caching
CACHE_ENABLED=true
CACHE_TTL_SECONDS=3600# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=app --cov-report=html
# View coverage report
open htmlcov/index.htmlTest Results:
======================== 22 passed in 0.11s ========================
test_cache.py - 3 tests
test_health.py - 4 tests
test_moderate.py - 7 tests
test_rate_limit.py - 3 tests
test_services.py - 5 tests
Push to main branch triggers:
- Run all tests
- Build Docker image (multi-arch)
- Push to Docker Hub
- Auto-deploy to Railway
# Build image
docker build -t nyadzayo/moderation-api:latest .
# Push to Docker Hub
docker push nyadzayo/moderation-api:latest
# Deploy to Railway
railway up| Metric | Value |
|---|---|
| Cold Start | ~12s (model loading) |
| Warm Request | ~50ms |
| Cached Request | ~6ms (40x faster) |
| Rate Limit | 100 req/min per IP |
| Concurrency | 4 workers (configurable) |
- Input validation with Pydantic
- Rate limiting per IP
- CORS configuration
- No sensitive data in logs
- Environment-based secrets
- Docker security best practices
- ARCHITECTURE.md - System design & diagrams
# Pull and run (requires Docker)
docker pull nyadzayo/moderation-api:latest
docker-compose up -d
# Test the API
curl -X POST "http://localhost:8000/v1/moderate" \
-H "Content-Type: application/json" \
-d '{"inputs":[{"text":"test content"}]}'
# View interactive docs
open http://localhost:8000/docs- Clean, well-documented code with comprehensive type hints
- Full test coverage with 22 passing tests
- Scalable, production-ready architecture
- Automated CI/CD pipeline with GitHub Actions
- Comprehensive technical documentation
MIT License - See LICENSE for details
Kelvin Nyadzayo
- GitHub: @Nyadzayo
- Email: kelvinnyadzayo@gmail.com
Built for production use