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

  1. Clone the repository

    git clone https://github.com/MDHaggai/AI-powered-content-Moderation-system.git
    cd AI-powered-content-Moderation-system
    
  2. Create 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
  1. Initialize the database

    python database/migrations/001_initial_schema.py
    
  2. Download 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 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.py
  • image-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:

  1. Create Procfile: web: python api-gateway/main.py
  2. Deploy using Heroku CLI

Google Cloud Platform:

  1. Use Cloud Run for serverless deployment
  2. Configure environment variables in Cloud Console

Fly.io:

  1. Install Fly CLI
  2. 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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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:

πŸ—ΊοΈ 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

Share this project:

Updates