Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

🔍 Fake News Detector

A machine learning project that detects fake news using TF-IDF vectorization and Logistic Regression. Comes with a full Flask API backend and a dark-themed web frontend.


📁 Project Structure

fake-news-detector/
├── model/
│   ├── train_model.py          # Train and save the ML model
│   ├── predictor.py            # Prediction utilities (importable)
│   └── fake_news_model.pkl     # Saved model (generated after training)
├── app/
│   ├── app.py                  # Flask API server
│   └── static/
│       └── index.html          # Web UI frontend
├── requirements.txt
└── README.md

⚙️ Setup

1. Install dependencies

pip install -r requirements.txt

2. Train the model

cd model
python train_model.py

This generates fake_news_model.pkl.

3. Run the web app

cd app
python app.py

Open http://localhost:5050 in your browser.


🧠 How It Works

Pipeline

Raw Text
  └─► Preprocessing (lowercase, remove URLs/HTML/punctuation)
        └─► TF-IDF Vectorizer (10,000 features, unigrams + bigrams)
              └─► Logistic Regression Classifier
                    └─► Prediction: FAKE / REAL + probabilities

Why TF-IDF + Logistic Regression?

Advantage Details
Fast Trains in seconds, infers instantly
Explainable Shows which words drove the prediction
Interpretable LR coefficients = word importance
Baseline-strong Often matches neural models on short text

🌐 API Reference

POST /api/predict

Request:  { "text": "Your headline or article here" }

Response: {
  "label": "FAKE",
  "confidence": 0.73,
  "fake_prob": 0.73,
  "real_prob": 0.27,
  "top_features": [
    { "word": "secretly", "weight": 0.42, "direction": "fake" },
    { "word": "exposed",  "weight": 0.31, "direction": "fake" }
  ]
}

GET /api/health

{ "status": "ok", "model": "TF-IDF + Logistic Regression" }

📊 Improving the Model

The default dataset is synthetic (for learning). For production-quality accuracy:

Recommended Datasets

Dataset Size Source
WELFake 72k articles Kaggle
LIAR 12.8k statements UCSB NLP
FakeNewsNet 23k articles GitHub

Replace data in train_model.py

# Load from CSV
df = pd.read_csv('WELFake_Dataset.csv')
# WELFake columns: 'title', 'text', 'label' (0=real, 1=fake)
data = df[['title', 'label']].rename(columns={'title': 'text'})

Try stronger models

from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.svm import LinearSVC

# Swap into pipeline:
pipeline = Pipeline([
    ('tfidf', TfidfVectorizer(...)),
    ('clf', LinearSVC(C=0.5))   # Often better than LR on text
])

🚀 Next Features to Add

  • Scrape and check URLs automatically (using newspaper3k)
  • Add BERT/DistilBERT for better accuracy (HuggingFace Transformers)
  • History log of analyzed articles
  • Browser extension integration
  • Confidence calibration with Platt scaling

⚠️ Disclaimer

⚠️ Disclaimer

This model is trained for educational purposes. Do not rely on it for real-world fact checking. Always verify with trusted sources.

🛠 Tech Stack

  • Python 3.10+
  • scikit-learn — ML pipeline
  • Flask — REST API
  • TF-IDF — Text vectorization
  • Logistic Regression — Classifier
  • Vanilla JS — Frontend (no framework needed)

Author Mohit Ghansela

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages