A FastAPI microservice for analyzing user comments in real-time.
It generates actionable signals such as intent, sentiment, toxicity, and spam detection, designed for moderation systems and social platforms.
π Hugging Face Space Link: lalit-narayan/youtube-comment-analyzer
π Hugging FaceDataset Link: lalit-narayan/youtube-comments-intent-sentiment
- Multilingual Support (English + Hinglish)
- Intent Detection (question, appreciation, complaint)
- Sentiment Analysis (positive, neutral, negative)
- Toxicity Detection (blocklist + ML)
- Spam Detection (Hybrid System)
- Rule-based + ML-based scoring
- Optimized for CPU (16GB systems)
- Async + Threaded inference for speed
- LRU Cache for repeated queries
- Batch Processing Support
comment_processing_ml_service/
βββ app/
β βββ main.py # FastAPI app + endpoints
β βββ models.py # Pydantic schemas (CommentInput, AnalysisResponse)
β βββ pipeline.py # get_heavy_predictions, model loading
β βββ spam.py # rule_signals, is_comment_spam
β βββ toxicity.py # is_toxic, Aho-Corasick init
β βββ config.py # env vars, model names, thresholds
βββ data/
β βββ blocklist.txt
β βββ README.md # documents dataset schema/source
βββ scripts/
β βββ csv_cleaner.py
βββ notebooks/
β βββ *.ipynb # training & evaluation notebooks
βββ tests/
β βββ test_spam.py
β βββ test_toxicity.py
β βββ test_api.py
βββ .github/workflows/ci.yml
βββ .gitignore
βββ .env.example
βββ LICENSE
βββ Dockerfile
βββ requirements.txt
βββ requirements-dev.txt # pandas, requests, pytest, jupyter β dev/eval-only deps
βββ README.md
+------------------+
| Input Comment |
+--------+---------+
|
v
+----------------------+
| Preprocessing |
+----------------------+
|
+---------+---------+
| |
v v
+----------------+ +----------------------+
| Rule Engine | | ML Models |
| (Spam Signals) | | Intent, Sentiment, |
| | | Toxicity |
+--------+-------+ +----------+-----------+
| |
+----------+-----------+
v
+----------------------+
| Final Aggregation |
+----------------------+
|
v
+----------------------+
| API Response (JSON) |
+----------------------+
The pipeline combines:
Detects:
- Links (even obfuscated)
- Handles (
@username) - Call-to-actions (DM, join, etc.)
- Scam keywords (earn money, crypto, etc.)
- Adult content
- Phone numbers
- Repetitive text
| Task | Model |
|---|---|
| Intent | lalit-narayan/youtube-comment-intent-classifier (fine-tuned MiniLM) |
| Sentiment | AmaanP314/youtube-xlm-roberta-base-sentiment-multilingual |
| Toxicity | martin-ha/toxic-comment-model |
| Spam | valurank/distilroberta-spam-comments-detection |
POST /analyze
Request:
{
"text": "This video is amazing bro!"
}Response:
{
"intent": "appreciation",
"intent_confidence": 0.91,
"sentiment": "positive",
"sentiment_confidence": 0.95,
"toxicity": false,
"is_spam": false
}POST /analyze-batch
Request:
[
{"text": "Nice video!"},
{"text": "Earn money fast click here"}
]GET /health β {"status": "healthy", "device": "CPU"}
- LRU Cache: (2048 entries) for repeated comments
- ThreadPoolExecutor: (4 workers) for heavy inference
- CPU-only execution: (
device = -1) - Async API: FastAPI async endpoints
Fine-tuned microsoft/Multilingual-MiniLM-L12-H384 on ~50K YouTube comments (4 classes: appreciation, question, complaint, spam).
| Epoch | Train Loss | Val Loss | Accuracy | F1 | Precision | Recall |
|---|---|---|---|---|---|---|
| 1 | 0.641 | 0.601 | 75.72% | 75.75% | 77.92% | 75.72% |
| 2 | 0.522 | 0.546 | 78.22% | 78.09% | 78.17% | 78.22% |
| 3 | 0.433 | 0.572 | 77.05% | 76.70% | 77.08% | 77.05% |
| 4 | 0.353 | 0.631 | 77.90% | 77.73% | 77.59% | 77.90% |
| 5 | 0.290 | 0.632 | 78.43% | 78.33% | 78.28% | 78.43% |
Published as
lalit-narayan/youtube-comment-intent-classifieron Hugging Face.
Evaluated on 20,071 labeled YouTube comments via the hosted API:
| Metric | Accuracy |
|---|---|
| Sentiment | 42.63% (8,557 / 20,071) |
| Toxicity | 77.81% (15,617 / 20,071) |
| Spam | 77.17% (15,489 / 20,071) |
- Total Time:
3411.46 seconds - Average:
~0.17 secondsper comment
The current model is AmaanP314/youtube-xlm-roberta-base-sentiment-multilingual. It is particularly strong in handling:
- Slang
- Abbreviations
- Mixed language (Hinglish)
Note: Sentiment accuracy appears lower due to a design decision β when a comment is classified as spam, the system skips ML models and returns a default
"neutral"sentiment. This inflates false mismatches in the evaluation.
- positive β neutral: 3930
- neutral β negative: 1758
- positive β negative: 830
- negative β neutral: 474
- neutral β positive: 453
- toxic (1) β non-toxic (0): 321
- non-toxic (0) β toxic (1): 153
- spam (1) β not spam (0): 375
- not spam (0) β spam (1): 227
Currently hosted on Hugging Face Spaces. The app runs via:
uvicorn app.main:app --host 0.0.0.0 --port 7860To deploy, push the repo (with app/, data/, Dockerfile, and requirements.txt) to a Hugging Face Space.
- Better Hinglish fine-tuning
- GPU acceleration
- Dashboard UI
- Streaming inference