A comprehensive system for moderating chat messages using multiple AI models and detection methods. This system provides real-time content moderation by analyzing text for various types of inappropriate content, personal information, and potential risks.
The Chat Moderation System uses a multi-model approach to provide thorough content analysis:
- PII Detection: Identifies personal information like emails, phone numbers, addresses
- Harassment Detection: Detects harassment, bullying, and toxic content
- Hybrid Model: Combines multiple approaches for comprehensive analysis
- Text Moderation: Uses DeBERTa model for general content moderation
- Real-time Analysis: Process messages instantly with WebSocket support
- Multi-Model Approach: Combines multiple AI models for comprehensive analysis
- Configurable Scoring: Flexible scoring system with customizable thresholds
- Model Caching: Efficient model loading and caching system
- Worker Pool: Distributed processing with Redis-based worker system
- Modern Dashboard: React-based UI for real-time monitoring
- Docker Support: Easy deployment with Docker containers
chat_moderation/
├── backend/ # Python FastAPI backend
│ ├── app/
│ │ ├── api/ # API endpoints
│ │ │ └── endpoints.py # Main API routes
│ │ ├── core/ # Core configuration
│ │ │ ├── config.py # Settings management
│ │ │ └── logging.py # Logging setup
│ │ ├── models/ # AI models
│ │ │ ├── pii.py # PII detection
│ │ │ ├── harassment.py # Harassment detection
│ │ │ └── hybrid.py # Hybrid model
│ │ ├── services/ # Business logic
│ │ │ ├── evaluator.py # Message evaluation
│ │ │ └── preprocessing.py # Text preprocessing
│ │ └── utils/ # Utilities
│ ├── tests/ # Backend tests
│ └── requirements.txt # Python dependencies
├── frontend/ # React TypeScript frontend
│ ├── src/
│ │ ├── components/ # React components
│ │ ├── pages/ # Page components
│ │ ├── services/ # API services
│ │ ├── styles/ # CSS styles
│ │ └── utils/ # Utilities
│ ├── public/ # Static assets
│ └── package.json # Node.js dependencies
└── docker/ # Docker configuration
├── backend.Dockerfile
├── frontend.Dockerfile
└── nginx.conf
- FastAPI Framework: High-performance async web framework
- Redis Message Queue: For worker communication
- Model Caching: Efficient model loading and memory management
- WebSocket Support: Real-time updates and notifications
- Pydantic Models: Strong type checking and validation
-
PII Detection Model
- Based on DeBERTa architecture
- Detects personal information in text
- Configurable sensitivity levels
-
Harassment Detection
- Fine-tuned BERT model
- Specialized in detecting toxic content
- Multiple category classification
-
Hybrid Model
- Ensemble approach combining multiple models
- Improved accuracy through model combination
- Configurable weighting system
- Real-time Updates: WebSocket connection for instant results
- Material-UI Components: Modern and responsive design
- TypeScript: Type-safe development
- React Router: Multiple page support
- Axios: API communication
- Python 3.9+
- Node.js 18+
- Redis
- Docker and Docker Compose (optional)
-
Clone the repository:
git clone <repository-url> cd chat-moderation
-
Set up environment files:
cp backend/.env.example backend/.env cp frontend/.env.example frontend/.env
-
Configure environment variables:
# backend/.env OPENAI_API_KEY=your-key-here MODEL_WORKERS=4 MODEL_CACHE_SIZE=2 # frontend/.env VITE_API_URL=http://localhost:8000 VITE_WS_URL=ws://localhost:8000/ws
-
Start services:
docker-compose up -d
-
Create virtual environment:
cd backend python -m venv venv source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Start Redis:
redis-server
-
Start worker and API:
# Terminal 1 python -m app.worker # Terminal 2 uvicorn app.main:app --reload
-
Install dependencies:
cd frontend npm install -
Start development server:
npm run dev
-
POST /api/evaluate: Evaluate a message{ "message": "text to analyze" } -
WS /api/ws: WebSocket endpoint for real-time updates
{
"overall_category_score": 5,
"final_status": "rejected",
"label": "harassment",
"detailed_results": {
"pii": { ... },
"harassment": { ... },
"hybrid": { ... }
}
}cd backend
pytestcd frontend
npm test- Logs are available in
backend/logs/ - Model cache in
backend/model_cache/ - Redis persistence in
redis_data/
- API keys should be properly secured
- CORS settings should be configured for production
- WebSocket connections should be authenticated
- Model weights should be verified for tampering
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- HuggingFace Transformers library
- OpenAI API
- FastAPI framework
- React and Material-UI