Skip to content
้กน็›ฎ

ๅ…ณไบŽ

๐Ÿ“ฐ NLP fake news detector โ€” TF-IDF + scikit-learn + Flask | 95% accuracy, confidence scores

ไบบๅทฅๆ™บ่ƒฝไธŽๆœบๅ™จๅญฆไน 

่ฟ™ไบ›ๆ•ฐๅญ—ๅฆ‚ไฝ•็ปŸ่ฎก

ๅฏไปฅๅšไป€ไนˆ

  • ๐Ÿง  Multi-Model ML 4 models compared: Passive Aggressive, Naive Bayes, Logistic Regression, Random Forest Best model auto-selected at training time ~91% accuracy on Kaggle Fake News dataset - ๐Ÿ” Explainable AI Confidence score + fake/real probability Key words that influenced the verdict Per-word influence direction (fake vs real)
  • 4 models compared: Passive Aggressive, Naive Bayes, Logistic Regression, Random Forest
  • Best model auto-selected at training time
  • ~91% accuracy on Kaggle Fake News dataset

ๅฎžๆต‹ๆ˜Ÿๆ ‡ๅขž้•ฟ

24ๅฐๆ—ถ
0
7ๅคฉ
0
15ๅคฉ
+1
1ไธชๆœˆ
ๆญฃๅœจๆ”ถ้›†ๅކๅฒ

ๅ„ๅ‘จๆœŸๅ†…็š„ๆ˜Ÿๆ ‡ๅ‡€ๅ˜ๅŒ–๏ผŒๅ‘จๆœŸ็›ธไบ’้‡ๅ ๏ผŒๅนถ้žๆฏๆ—ฅๆ–ฐๅขžใ€‚็ผบๅคฑ็š„ๆต‹้‡ไฟๆŒ็ฉบ็™ฝใ€‚ๆ˜Ÿๆ ‡ไปฃ่กจๅ…ณๆณจๅบฆ๏ผŒไธไปฃ่กจ่ฝฏไปถ่ดจ้‡ใ€‚

README.md

ๅŽŸๆ–‡

CI

๐Ÿ›ก๏ธ FakeShield AI โ€” Fake News Detector

Paste any headline or article. Get an instant REAL / FAKE verdict with confidence score and linguistic evidence.

Python Flask scikit-learn NLTK License: MIT


Misinformation spreads 6x faster than the truth. FakeShield uses NLP and machine learning to classify news articles in milliseconds โ€” with a confidence score, fake/real probability breakdown, and the exact words that triggered the verdict.


โœจ Features

๐Ÿง  Multi-Model ML

  • 4 models compared: Passive Aggressive, Naive Bayes, Logistic Regression, Random Forest
  • Best model auto-selected at training time
  • ~91% accuracy on Kaggle Fake News dataset

๐Ÿ” Explainable AI

  • Confidence score + fake/real probability
  • Key words that influenced the verdict
  • Per-word influence direction (fake vs real)

๐Ÿ“Š Training Visualisations

  • Accuracy comparison across all 4 models
  • Confusion matrix per model
  • ROC curves
  • Top 20 most predictive words

๐ŸŒ REST API

  • POST /predict endpoint
  • JSON in, JSON out
  • Drop-in for any app or browser extension

๐Ÿš€ Quick Start

git clone https://github.com/aasimansari1/fake-news-detector.git
cd fake-news-detector

pip install -r requirements.txt

# Generate sample dataset + download NLTK data
python dataset/create_dataset.py

# Train all 4 models (best one saved automatically)
python train_model.py

# Launch the web app
python app.py
# โ†’ http://localhost:5000

Want better accuracy? Replace dataset/sample_data.csv with the Kaggle Fake News dataset (~44K articles). Any CSV with text and label columns (REAL/FAKE or 0/1) works.


๐Ÿ—๏ธ How It Works

  ๐Ÿ“ฐ Article / Headline
          โ”‚
          โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚   NLP Pipeline    โ”‚
  โ”‚  lowercase โ†’      โ”‚
  โ”‚  punctuation โ†’    โ”‚
  โ”‚  tokenise โ†’       โ”‚
  โ”‚  stopwords โ†’      โ”‚
  โ”‚  lemmatise        โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚
           โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  TF-IDF (bigrams) โ”‚
  โ”‚  Vectoriser       โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚
           โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Best ML Model    โ”‚  โ† Passive Aggressive (~91%)
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚
           โ–ผ
  โœ… REAL (confidence: 94%)
  โŒ FAKE (confidence: 82%)
  + key words + probabilities

๐Ÿ“ก API

POST /predict
Content-Type: application/json

{"text": "Scientists discover that vaccines cause autism, government hiding truth"}
{
  "prediction": "FAKE",
  "confidence": 89.4,
  "fake_probability": 89.4,
  "real_probability": 10.6,
  "key_words": [
    {"word": "hiding", "score": 0.21, "influence": "fake"},
    {"word": "truth", "score": 0.18, "influence": "fake"}
  ],
  "model_name": "Passive Aggressive",
  "model_accuracy": 0.907
}

๐Ÿ“ˆ Model Accuracy

Model Accuracy Notes
Passive Aggressive ~91% Best overall โ€” auto-selected
Naive Bayes ~88% Fastest inference
Logistic Regression ~86% Most interpretable
Random Forest ~84% Most robust to noise

Accuracy on sample dataset. Use a larger real-world dataset for production-grade results.


๐Ÿ—‚๏ธ Project Structure

fake-news-detector/
โ”œโ”€โ”€ app.py                        # Flask web app + /predict API
โ”œโ”€โ”€ train_model.py                # Training pipeline, model comparison
โ”œโ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ src/                          # Reusable data science module
โ”‚   โ”œโ”€โ”€ preprocessing.py          # Text cleaning, label normalisation, dataset loader
โ”‚   โ”œโ”€โ”€ features.py               # TF-IDF vectoriser builder
โ”‚   โ””โ”€โ”€ models.py                 # Train all classifiers, pick best, ROC data
โ”‚
โ”œโ”€โ”€ notebooks/                    # Jupyter data science pipeline
โ”‚   โ”œโ”€โ”€ 01_EDA.ipynb              # Exploratory data analysis
โ”‚   โ”œโ”€โ”€ 02_Feature_Engineering.ipynb  # Preprocessing + TF-IDF
โ”‚   โ”œโ”€โ”€ 03_Model_Training.ipynb   # Train & compare 4 models
โ”‚   โ””โ”€โ”€ 04_Model_Evaluation.ipynb # Confusion matrix, ROC, feature importance, CV
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                      # Source datasets (CSV)
โ”‚   โ””โ”€โ”€ processed/                # Train/test splits + vectoriser (generated)
โ”‚
โ”œโ”€โ”€ models/                       # Saved model + vectoriser (generated)
โ”œโ”€โ”€ reports/figures/              # All visualisation outputs (generated)
โ”‚
โ”œโ”€โ”€ dataset/
โ”‚   โ””โ”€โ”€ create_dataset.py         # Sample dataset generator
โ”œโ”€โ”€ static/
โ”‚   โ”œโ”€โ”€ css/style.css
โ”‚   โ”œโ”€โ”€ js/main.js
โ”‚   โ””โ”€โ”€ images/
โ””โ”€โ”€ templates/
    โ””โ”€โ”€ index.html

Running the Notebooks

pip install -r requirements.txt
jupyter notebook

Run in order: 01_EDA โ†’ 02_Feature_Engineering โ†’ 03_Model_Training โ†’ 04_Model_Evaluation


๐Ÿ› ๏ธ Tech Stack

Layer Technology
Backend Python, Flask, Gunicorn
ML / NLP scikit-learn, NLTK, pandas, numpy
Visualisation matplotlib, seaborn
Frontend Vanilla HTML/CSS/JS, dark mode
Server Nginx + systemd

๐Ÿค Contributing

Ideas for contributions:

  • ๐ŸŒ Browser extension that checks articles in-page
  • ๐Ÿค— Fine-tune a BERT/RoBERTa model for better accuracy
  • ๐Ÿ“ฑ Mobile-friendly UI improvements
  • ๐Ÿ—ƒ๏ธ Support for more languages
git checkout -b feature/your-feature
git commit -m 'Add your feature'
git push origin feature/your-feature
# Open a Pull Request

๐Ÿ“„ License

MIT ยฉ Mohd Aasim Ansari


Fighting misinformation one article at a time. If this helped, please โญ star the repo!

ๆŽข็ดข็›ธๅ…ณ้กน็›ฎ

ๅŠ ่ฝฝไธญโ€ฆ