Skip to content

About

This project is used to detect fake news using NLP techniques

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

πŸ” Fake News Detection System

An AI-powered machine learning system for detecting fake news articles using Natural Language Processing (NLP) and Explainable AI. The system combines a TF-IDF–based ensemble classifier with linguistic analysis and risk-factor detection to deliver transparent, interpretable predictions. Built with Python, Flask, and scikit-learn.

Python 3.8+ License: MIT scikit-learn Flask


Table of Contents


✨ Key Features

Feature Description
Ensemble ML Model Soft-voting ensemble of SGD, Logistic Regression, and Random Forest for robust predictions
Explainable AI Shows the top TF-IDF features that influenced each prediction
Linguistic Analysis Detects sensational language, clickbait patterns, excessive caps, and exclamation marks
Risk Factor Detection Identifies red flags common in fake news (ALL CAPS words, sensational vocabulary)
Credibility Indicators Highlights positive signals β€” neutral tone, professional punctuation, appropriate complexity
Confidence Calibration Sigmoid-calibrated probabilities for reliable confidence scores
Secure Serialization JSON + HMAC-SHA256 signatures β€” no pickle files, no arbitrary code execution risk
Web Interface Modern, responsive Flask-based UI with dark gradient theme
Desktop GUI Tkinter-based standalone desktop application
REST API Full API with single prediction, batch prediction, analysis, and health-check endpoints

πŸš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • pip package manager

1. Clone the Repository

git clone https://github.com/Ghulam-Mustafa-Keerio/Fake-News_Detection_System.git
cd Fake-News_Detection_System

2. Install Dependencies

pip install -r requirements.txt

3. Download the Dataset

# Option A: Using the provided helper script (requires Kaggle CLI)
pip install --user kaggle
export KAGGLE_USERNAME=your_username
export KAGGLE_API_TOKEN=your_token
bash scripts/download_kaggle_dataset.sh .

# Option B: Manual download
# Download from https://www.kaggle.com/datasets/clmentbisaillon/fake-and-real-news-dataset
# Place Fake.csv and True.csv into data/fake_and_real_news/
mkdir -p data/fake_and_real_news
mv Fake.csv True.csv data/fake_and_real_news/

4. Train the Model

python train_model.py

5. Run the Web Application

python app.py
# Open http://localhost:5000 in your browser

πŸ“₯ Dataset

This project uses the Kaggle Fake and Real News Dataset by ClΓ©ment Bisaillon.

Metric Value
Total Articles 44,898
Fake Articles 23,481
Real Articles 21,417
Source (Real) Reuters news agency
Topics Politics, world news, government, economics

Data Preprocessing

The dataset loading pipeline (dataset_utils.py) applies the following cleaning steps to prevent source-attribution bias:

  • Reuters pattern removal β€” Strips "CITY (Reuters) -" prefixes from Real articles so the model learns content quality rather than source formatting
  • AP pattern removal β€” Strips "(AP)" attributions
  • Correction-note removal β€” Removes "Corrects paragraph X…" boilerplate
  • Title + text concatenation β€” Merges headline and body into a single feature for richer context
  • Shuffling β€” Randomized with random_state=42 for reproducibility

Dataset Directory Layout

data/
└── fake_and_real_news/
    β”œβ”€β”€ Fake.csv    # 23,481 fake news articles
    └── True.csv    # 21,417 real news articles (Reuters)

πŸ“ Project Structure

Fake-News_Detection_System/
β”‚
β”œβ”€β”€ train_model.py              # Core ML training with TF-IDF ensemble & secure save/load
β”œβ”€β”€ app.py                      # Flask web application (routes & API)
β”œβ”€β”€ gui.py                      # Desktop GUI application (Tkinter)
β”œβ”€β”€ dataset_utils.py            # Dataset download, loading, and cleaning utilities
β”‚
β”œβ”€β”€ model.json                  # Trained ensemble model (secure JSON + HMAC)
β”œβ”€β”€ vectorizer.json             # TF-IDF vectorizer (secure JSON + HMAC)
β”œβ”€β”€ model_config.json           # Model metadata (type, vocab size, version)
β”œβ”€β”€ training_metrics.json       # Training accuracy, precision, recall, F1, ROC-AUC
β”‚
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ index.html              # Main web interface (dark gradient theme)
β”‚   └── about.html              # About page
β”‚
β”œβ”€β”€ scripts/
β”‚   └── download_kaggle_dataset.sh   # Automated Kaggle dataset download
β”‚
β”œβ”€β”€ data/
β”‚   └── fake_and_real_news/     # Kaggle CSV files (Fake.csv, True.csv)
β”‚
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ LICENSE                     # MIT License
└── README.md                   # This file

🧠 How It Works

1. Text Preprocessing Pipeline

Raw Text
  β”‚
  β”œβ”€β”€ Lowercase conversion
  β”œβ”€β”€ URL removal (http/https/www patterns)
  β”œβ”€β”€ HTML tag stripping
  β”œβ”€β”€ Special character removal
  β”œβ”€β”€ Number removal
  β”œβ”€β”€ Tokenization (NLTK word_tokenize)
  β”œβ”€β”€ Stopword filtering (English)
  β”œβ”€β”€ Lemmatization (WordNet)
  β”‚
  └── Clean Text

The AdvancedTextPreprocessor class also detects:

  • Sensational words β€” shocking, miracle, bombshell, conspiracy, etc. (15 patterns)
  • Clickbait patterns β€” "you won't believe", "doctors hate", "what happened next", etc. (7 regex patterns)

2. Feature Extraction (TF-IDF)

Parameter Value
Max features 10,000
N-gram range (1, 3) β€” unigrams, bigrams, trigrams
TF scaling Sublinear (1 + log(tf))
Min document frequency 2
Max document frequency 95%

3. Ensemble Classification

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   TF-IDF Features    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β–Ό               β–Ό               β–Ό
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚ SGD Classifier β”‚ β”‚ Logistic β”‚ β”‚ Random Forest  β”‚
     β”‚ (Modified      β”‚ β”‚Regressionβ”‚ β”‚ (200 trees)    β”‚
     β”‚  Huber Loss)   β”‚ β”‚          β”‚ β”‚                β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚               β”‚               β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚    Soft Voting       β”‚
                    β”‚  (Probability Avg)   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚ Sigmoid Calibration  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  REAL / FAKE + %     β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4. Explainability Layer

Each prediction returns:

  • Top Features β€” The TF-IDF terms with the highest positive/negative weight for the prediction
  • Risk Factors β€” Detected red flags (e.g., "Excessive exclamation marks", "High capitalization ratio", "Multiple ALL CAPS words")
  • Credibility Indicators β€” Positive signals (e.g., "Neutral language tone", "Professional punctuation", "Appropriate sentence complexity")
  • Linguistic Analysis β€” Quantitative metrics: word count, sentence count, caps ratio, exclamation mark count, sensational word count

πŸ“Š Model Architecture

Component Configuration Purpose
Vectorizer TF-IDF, 10,000 features, n-gram (1,3), sublinear TF Convert text to numerical feature vectors
SGD Classifier Modified Huber loss, L2 penalty Fast linear classifier with probability estimates
Logistic Regression L2 regularization, lbfgs solver Interpretable linear model with calibrated probabilities
Random Forest 200 estimators, max depth limited Non-linear decision boundary, feature importance
VotingClassifier Soft voting (averaged probabilities) Combine all three classifiers for robustness
Calibration CalibratedClassifierCV, sigmoid method Produce well-calibrated probability scores

πŸ‹οΈ Training Pipeline

The training process in train_model.py follows this workflow:

  1. Data Loading β€” Load 44,898 articles from the Kaggle dataset via load_kaggle_dataset()
  2. Text Cleaning β€” Strip Reuters/AP attributions, remove boilerplate patterns
  3. Train/Test Split β€” 80% training (35,918 samples), 20% testing (8,980 samples), stratified
  4. TF-IDF Vectorization β€” Fit the vectorizer on training data; transform both sets
  5. Ensemble Training β€” Train SGD, Logistic Regression, and Random Forest; combine via VotingClassifier
  6. Probability Calibration β€” Apply CalibratedClassifierCV with sigmoid calibration
  7. Evaluation β€” Compute accuracy, precision, recall, F1, ROC-AUC on the test set
  8. Secure Save β€” Serialize to JSON with HMAC-SHA256 integrity signatures

Run Training

python train_model.py

Expected output:

Step 1/4: Loading data...
  Loaded 44898 articles from Kaggle dataset
Step 2/4: Initializing detector...
Step 3/4: Training model...
  Training completed!
Step 4/4: Saving model (SECURE JSON format)...
  Model saved securely!

πŸ“ˆ Model Performance

Evaluation metrics on the held-out test set (8,980 articles):

Metric Score
Accuracy 98.86%
Precision 98.87%
Recall 98.86%
F1 Score 98.86%
ROC-AUC 99.93%
Split Samples
Training set 35,918
Test set 8,980
Vocabulary size 10,000

Sample Predictions

Fake News:

Input:  "BREAKING NEWS!!! You wont BELIEVE what scientists discovered!
         Proof that ALIENS have been controlling world governments for DECADES!"
Output: FAKE (87.6% confidence)

Risk Factors:
  ⚠️ Excessive exclamation marks
  ⚠️ High capitalization ratio
  ⚠️ Multiple ALL CAPS words

Real News:

Input:  "The Federal Reserve announced today that it will maintain current
         interest rates, citing stable inflation levels and moderate economic growth."
Output: REAL (66.4% confidence)

Credibility Indicators:
  βœ“ Appropriate sentence complexity
  βœ“ Neutral language tone
  βœ“ Professional punctuation

🌐 API Reference

The Flask application exposes the following REST endpoints:

GET /

Serves the main web interface.

POST /predict

Analyze a single text and return a prediction with explainability.

Request:

curl -X POST http://localhost:5000/predict \
  -H "Content-Type: application/json" \
  -d '{"text": "Your news article text here"}'

Response:

{
  "prediction": "REAL",
  "confidence": 66.4,
  "probabilities": { "fake": 33.6, "real": 66.4 },
  "model_type": "advanced_ensemble",
  "explanation": {
    "top_features": [
      { "word": "citing", "weight": 0.23 },
      { "word": "federal reserve", "weight": 0.09 }
    ],
    "risk_factors": [],
    "credibility_indicators": [
      "Neutral language tone",
      "Professional punctuation"
    ],
    "linguistic_analysis": {
      "word_count": 41,
      "sentence_count": 2,
      "caps_ratio": 4.0,
      "exclamation_marks": 0,
      "sensational_words": 0
    }
  }
}

POST /predict/batch

Analyze up to 100 texts in a single request.

Request:

{
  "texts": ["Article 1 text...", "Article 2 text..."]
}

POST /analyze

Perform linguistic analysis only (no prediction).

Request:

{ "text": "Text to analyze" }

GET /health

Health check returning model status and capabilities.

Response:

{
  "status": "healthy",
  "model_loaded": true,
  "model_type": "advanced_ensemble",
  "features": ["explainability", "calibrated_confidence", "linguistic_analysis"]
}

GET /api/models

List all available model types.


πŸ‘¨β€πŸ’» Usage Examples

Python API

from train_model import AdvancedFakeNewsDetector

# Load the trained model
detector = AdvancedFakeNewsDetector()
detector.load_model()

# Make a prediction with full explainability
result = detector.predict("Your news text here", explain=True)

print(f"Prediction:   {result.label}")
print(f"Confidence:   {result.confidence}%")
print(f"Fake prob:    {result.probabilities['fake']}%")
print(f"Real prob:    {result.probabilities['real']}%")
print(f"Top features: {result.top_features}")
print(f"Risk factors: {result.risk_factors}")
print(f"Credibility:  {result.credibility_indicators}")

Web Application

python app.py
# Navigate to http://localhost:5000

Desktop GUI

python gui.py

Command Line Training

# Train on the Kaggle dataset
python train_model.py

πŸ”’ Security

This project prioritizes secure model serialization over convenience.

Why Not Pickle?

Pickle files can execute arbitrary Python code when loaded, creating a severe security vulnerability:

# DANGER β€” Pickle can run malicious code
import pickle
pickle.loads(malicious_data)  # Executes attacker's code!

Our Approach: JSON + HMAC-SHA256

Risk Mitigation
Arbitrary code execution Eliminated β€” JSON cannot execute code
Model tampering HMAC-SHA256 signature verified on every load
Opaque model files All files are human-readable JSON
Supply-chain attacks No dependency on pickle or joblib for serialization
# SAFE β€” JSON cannot execute code
import json
model_data = json.load(open('model.json'))  # Just data, no code execution
# Signature is verified before use

Model files produced:

  • model.json β€” Classifier weights and parameters + HMAC signature
  • vectorizer.json β€” TF-IDF vocabulary and IDF weights + HMAC signature
  • model_config.json β€” Model metadata (type, vocabulary size, version)
  • training_metrics.json β€” Performance metrics from the last training run

πŸ”§ Technologies Used

Category Technology
Language Python 3.8+
Web Framework Flask 2.3+
Machine Learning scikit-learn 1.3+
NLP NLTK 3.8+ (tokenization, lemmatization, stopwords)
Feature Extraction TF-IDF Vectorizer (10,000 features, n-gram 1–3)
Ensemble Learning VotingClassifier (SGD + Logistic Regression + Random Forest)
Serialization JSON + HMAC-SHA256 (secure, no pickle)
Data Processing pandas 2.0+, NumPy 1.24+
Desktop GUI Tkinter

πŸ“š Research Background

This project applies established techniques from NLP and misinformation detection research:

  1. TF-IDF with Sublinear Scaling β€” Dampens the impact of very frequent terms with 1 + log(tf), improving feature discrimination across large corpora.
  2. Ensemble Learning β€” Voting classifiers reduce variance and increase robustness compared to any single model, a well-known technique in statistical learning.
  3. Probability Calibration β€” Sigmoid (Platt scaling) calibration produces probabilities that reflect true class frequencies, critical for trustworthy confidence scores.
  4. Stylometric / Linguistic Analysis β€” Fake news tends to exhibit specific surface-level cues: excessive capitalization, exclamation marks, sensational vocabulary, and clickbait sentence structures.
  5. Explainable AI (XAI) β€” Feature-importance explanations let users evaluate predictions rather than blindly trusting a black box, aligning with responsible AI principles.
  6. Source-Attribution Debiasing β€” Removing Reuters/AP bylines from training data prevents the model from learning shortcut features unrelated to content veracity.

🀝 Contributing

Contributions are welcome! To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -m "Add your feature")
  4. Push to the branch (git push origin feature/your-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License β€” see the LICENSE file for details.

Copyright Β© 2026 Ghulam Mustafa


πŸ™ Acknowledgments

  • Kaggle Fake and Real News Dataset by ClΓ©ment Bisaillon β€” training data
  • NLTK β€” natural language processing toolkit
  • scikit-learn β€” machine learning library
  • Flask β€” lightweight web framework
  • Research community studying computational approaches to misinformation detection

About

This project is used to detect fake news using NLP techniques

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages