AI-Powered Content Moderation System
A scalable, microservices-based content moderation system that uses AI to automatically detect and flag inappropriate text and image content in real-time.
π Features
- Text Moderation: Uses NLP models to detect toxic language, hate speech, profanity, and threats
- Image Moderation: Uses computer vision to identify explicit content, violence, weapons, and inappropriate imagery
- Microservices Architecture: Modular design with separate services for text and image moderation
- Real-Time Processing: Handle content as it's submitted with fast response times
- Scalable Design: Built to handle increasing loads with horizontal scaling capabilities
- API Gateway: Centralized routing and request management
- Database Integration: Track moderation results and user statistics
- Comprehensive Testing: Full test coverage for all components
- Free Tech Stack: Built entirely with open-source and free-tier tools
ποΈ Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Client App βββββΆβ API Gateway βββββΆβ Text Service β
βββββββββββββββββββ β (Port 8000) β β (Port 8001) β
βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β Image Service β
β (Port 8002) β
βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β Database β
β (SQLite) β
βββββββββββββββββββ
π οΈ Tech Stack
- Backend Framework: FastAPI (Python)
- AI/ML Libraries:
- Hugging Face Transformers (NLP)
- OpenCV (Computer Vision)
- TensorFlow/PyTorch
- Database: SQLite (with migration support)
- Testing: pytest with async support
- Documentation: OpenAPI/Swagger (auto-generated)
- Containerization: Docker (optional)
- CI/CD: GitHub Actions
π¦ Installation
Prerequisites
- Python 3.8+
- pip (Python package manager)
- Git
Setup Instructions
Clone the repository
git clone https://github.com/MDHaggai/AI-powered-content-Moderation-system.git cd AI-powered-content-Moderation-systemCreate virtual environment (recommended)
python -m venv venv
# On Windows venv\Scripts\activate
# On macOS/Linux source venv/bin/activate
3. **Install dependencies**
```bash
pip install -r requirements.txt
Initialize the database
python database/migrations/001_initial_schema.pyDownload required models (optional - models download automatically on first use)
python scripts/download_models.py
π Running the Services
Option 1: Run All Services Individually
Terminal 1 - API Gateway
cd api-gateway
python main.py
Terminal 2 - Text Moderation Service
cd text-moderation-service
python main.py
Terminal 3 - Image Moderation Service
cd image-moderation-service
python main.py
Option 2: Using the Start Script
python scripts/start_services.py
Option 3: Using Docker (Optional)
docker build -t content-moderation .
docker run -p 8000:8000 -p 8001:8001 -p 8002:8002 content-moderation
π API Documentation
Once the services are running, you can access the interactive API documentation:
- API Gateway: http://localhost:8000/docs
- Text Service: http://localhost:8001/docs
- Image Service: http://localhost:8002/docs
π API Usage Examples
Text Moderation
# Moderate text content
curl -X POST "http://localhost:8000/api/v1/moderate/text" \
-H "Content-Type: application/json" \
-d '{
"text": "This is a sample message to moderate",
"user_id": "user123",
"content_id": "content456"
}'
Response:
{
"is_appropriate": true,
"confidence_score": 0.95,
"flagged_categories": [],
"processed_text": "this is a sample message to moderate",
"user_id": "user123",
"content_id": "content456"
}
Image Moderation
# Moderate image content
curl -X POST "http://localhost:8000/api/v1/moderate/image" \
-F "file=@/path/to/image.jpg" \
-F "user_id=user123" \
-F "content_id=image456"
Response:
{
"is_appropriate": true,
"confidence_score": 0.85,
"flagged_categories": [],
"image_info": {
"width": 1024,
"height": 768,
"format": "JPEG",
"file_size_mb": 0.5
},
"user_id": "user123",
"content_id": "image456"
}
Batch Text Moderation
curl -X POST "http://localhost:8000/api/v1/moderate/text/batch" \
-H "Content-Type: application/json" \
-d '{
"texts": ["Message 1", "Message 2", "Message 3"],
"user_id": "user123"
}'
π§ͺ Testing
Run the test suite:
# Run all tests
pytest
# Run specific test categories
pytest tests/text-moderation/
pytest tests/image-moderation/
# Run with coverage
pytest --cov=. --cov-report=html
π Monitoring and Analytics
Health Checks
Check service health:
curl http://localhost:8000/health
Service Status
Get detailed service status:
curl http://localhost:8000/api/v1/services/status
Analytics
The system tracks moderation statistics in the database. You can query the database directly or extend the API to provide analytics endpoints.
π§ Configuration
Environment Variables
Create a .env file in the root directory:
# Service URLs
TEXT_SERVICE_URL=http://localhost:8001
IMAGE_SERVICE_URL=http://localhost:8002
# Database
DATABASE_PATH=moderation.db
# Model Settings
TORCH_DEVICE=auto # auto, cpu, cuda
MODEL_CACHE_DIR=./models/cache
# API Settings
API_RATE_LIMIT=100 # requests per minute
MAX_FILE_SIZE_MB=10
MAX_TEXT_LENGTH=10000
Customizing Moderation Categories
Edit the category lists in:
text-moderation-service/models/text_classifier.pyimage-moderation-service/models/image_classifier.py
Adjusting Thresholds
Modify confidence thresholds in the respective model files to fine-tune sensitivity.
π Project Structure
ai-content-moderation/
βββ text-moderation-service/ # NLP-based text moderation
β βββ models/ # AI models for text analysis
β βββ api/ # Text moderation API endpoints
β βββ utils/ # Text preprocessing utilities
β βββ main.py # Service entry point
βββ image-moderation-service/ # Computer vision image moderation
β βββ models/ # AI models for image analysis
β βββ api/ # Image moderation API endpoints
β βββ utils/ # Image preprocessing utilities
β βββ main.py # Service entry point
βββ api-gateway/ # Central API gateway
β βββ routes/ # Request routing logic
β βββ main.py # Gateway entry point
βββ database/ # Database components
β βββ migrations/ # Database schema migrations
β βββ db.py # Database connection logic
βββ tests/ # Test suites
β βββ text-moderation/ # Text moderation tests
β βββ image-moderation/ # Image moderation tests
βββ docs/ # Additional documentation
βββ scripts/ # Utility scripts
βββ requirements.txt # Python dependencies
βββ README.md # This file
π Deployment
Local Development
Follow the installation instructions above.
Cloud Deployment (Free Tiers)
Heroku:
- Create
Procfile:web: python api-gateway/main.py - Deploy using Heroku CLI
Google Cloud Platform:
- Use Cloud Run for serverless deployment
- Configure environment variables in Cloud Console
Fly.io:
- Install Fly CLI
- Run
fly deploy
Docker Deployment
# Build image
docker build -t content-moderation .
# Run container
docker run -d -p 8000:8000 content-moderation
π Security Considerations
- Input Validation: All inputs are validated and sanitized
- Rate Limiting: Implement rate limiting to prevent abuse
- Authentication: Add authentication for production use
- HTTPS: Use HTTPS in production
- Content Logging: Be mindful of privacy when logging content
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- Hugging Face for providing excellent pre-trained models
- FastAPI for the amazing web framework
- OpenCV for computer vision capabilities
- The open-source community for making this project possible
π Support
For questions, issues, or contributions:
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: haggairameni@gmail.com
πΊοΈ Roadmap
- [ ] Add support for video content moderation
- [ ] Implement user reputation system
- [ ] Add multi-language support
- [ ] Create web dashboard for monitoring
- [ ] Add webhook notifications
- [ ] Implement custom model training pipeline
- [ ] Add Redis caching for improved performance
- [ ] Create mobile SDK
- [ ] Add A/B testing framework for model comparisons
Built With
- dockerfile
- python
Log in or sign up for Devpost to join the conversation.