--- title: Toxic Comment Classifier emoji: ๐Ÿ›ก๏ธ colorFrom: blue colorTo: indigo sdk: docker pinned: false --- # Code-Mixed Toxic Content Classifier Production-ready Transformer-based NLP system for detecting toxic content in: * **Tamil** * **English** * **Tanglish** (Tamil in Latin script) Built using **MuRIL (google/muril-base-cased)** fine-tuned for 3-class toxicity classification: * `non_toxic` * `toxic` * `severe_toxic` Designed for real-world deployment with rate limiting, logging, thresholding, and load testing support. --- # ๐Ÿ“ฆ Project Structure ``` / โ”œโ”€โ”€ model_output/ # Trained model, tokenizer, label encoder โ”œโ”€โ”€ logs/ # JSONL prediction logs โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ api.py # Production FastAPI server (rate-limited, logged) โ”‚ โ”œโ”€โ”€ data_loader.py # Hardened dataset loader & cleaner โ”‚ โ”œโ”€โ”€ predict.py # Inference engine with threshold logic โ”‚ โ””โ”€โ”€ train_muril.py # Production training pipeline โ”œโ”€โ”€ locustfile.py # Load testing configuration โ”œโ”€โ”€ requirements.txt โ”œโ”€โ”€ README.md โ””โ”€โ”€ *.csv # Structured datasets (tamil.csv, english.csv, tanglish.csv) ``` --- # ๐Ÿง  Model Overview * Base Model: `google/muril-base-cased` * Architecture: Transformer (BERT-style sequence classification) * Classes: * non_toxic * toxic * severe_toxic * Max token length: 192 * Confidence thresholding enabled * Near-duplicate removal during preprocessing * Stratified train/validation split * Macro-F1 monitored during training --- # ๐Ÿ“Š Dataset Structure Each CSV file must contain: ``` text,label ``` Example: ``` nee saavu da,severe_toxic I disagree with you,non_toxic you are useless,toxic ``` Requirements: * UTF-8 or UTF-16 encoding supported * No empty rows * No duplicate sentences * Clear label boundaries --- # โš™๏ธ Setup ## 1๏ธโƒฃ Create Virtual Environment ```bash python -m venv venv ``` Windows: ```bash .\venv\Scripts\activate ``` Mac/Linux: ```bash source venv/bin/activate ``` --- ## 2๏ธโƒฃ Install Dependencies ```bash pip install -r requirements.txt ``` Optional (recommended): Set HuggingFace token for stable downloads: ```bash set HF_TOKEN=your_token # Windows export HF_TOKEN=your_token # Linux/Mac ``` --- # ๐Ÿ‹๏ธ Training Train model: ```bash python src/train_muril.py --epochs 5 --batch_size 16 ``` Output saved to: ``` model_output/ ``` Training includes: * Dataset audit logging * Label distribution validation * Stratified split * Macro F1 tracking * Best model checkpoint restore * Label encoder persistence --- # ๐Ÿงช CLI Inference Test ```bash python src/predict.py ``` Verifies: * Model loads * Threshold logic applied * Batch inference works --- # ๐Ÿš€ Run Production API ### Windows (single worker recommended) ```bash uvicorn src.api:app --host 0.0.0.0 --port 8000 ``` Open Swagger UI: ``` http://localhost:8000/docs ``` --- # ๐Ÿ”Œ API Endpoints ## Health Check ``` GET /health ``` Returns service health. --- ## Readiness Probe ``` GET /ready ``` Returns 200 only if model loaded and ready. --- ## Predict ``` POST /predict ``` ### Request ```json { "text": "nee saavu da ๐Ÿ˜‚" } ``` ### Response ```json { "prediction": "severe_toxic", "raw_prediction": "severe_toxic", "confidence": 0.9867, "process_time_ms": 64.03, "timestamp": "2026-02-22T13:49:24.019Z" } ``` --- # ๐Ÿ›ก๏ธ Production Safeguards * Rate limiting enabled (per IP) * Structured JSONL logging * Confidence threshold enforcement * Input validation * Cleaned text normalization * Graceful startup and shutdown * No model reload per request * Lazy model initialization --- # ๐Ÿ“ˆ Load Testing (Locust) Create `locustfile.py`: ```python from locust import HttpUser, task, between import random texts = [ "nee saavu da ๐Ÿ˜‚", "You are useless", "I disagree with you", "k1ll urself", "This is fine" ] class ToxicityUser(HttpUser): wait_time = between(0.5, 1.5) @task def predict(self): self.client.post( "/predict", json={"text": random.choice(texts)} ) ``` Run: ```bash locust ``` Open: ``` http://localhost:8089 ``` Test capacity: * Start with 20 users * Increase gradually * Monitor latency & failure % --- # ๐Ÿ“Š Performance (CPU, Single Worker) Expected: * 60โ€“120ms average latency * ~10โ€“15 RPS stable * 0% failure under safe load High concurrency requires: * Linux * Gunicorn workers * Possibly GPU acceleration --- # โš  Known Limitations * Windows multi-worker mode unstable * Single-process inference bound by CPU * Severe vs toxic boundary sensitive to dataset * Confidence not temperature-calibrated --- # ๐Ÿ“Œ Deployment Recommendations For real production: * Deploy on Linux * Use Gunicorn + Uvicorn workers * Reverse proxy (Nginx) * Enable HTTPS * Add monitoring (Prometheus/Grafana) * Add structured metrics endpoint * Implement request tracing --- # ๐Ÿงช Monitoring Logs stored in: ``` logs/predictions.jsonl ``` Each entry contains: * timestamp * text * prediction * confidence * latency --- # ๐Ÿ“Ž Version - API Version: 1.1.0 - Model: MuRIL fine-tuned (custom dataset)