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.
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
pip install -r requirements.txtcd model
python train_model.pyThis generates fake_news_model.pkl.
cd app
python app.pyOpen http://localhost:5050 in your browser.
Raw Text
└─► Preprocessing (lowercase, remove URLs/HTML/punctuation)
└─► TF-IDF Vectorizer (10,000 features, unigrams + bigrams)
└─► Logistic Regression Classifier
└─► Prediction: FAKE / REAL + probabilities
| 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 |
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" }
]
}{ "status": "ok", "model": "TF-IDF + Logistic Regression" }The default dataset is synthetic (for learning). For production-quality accuracy:
| Dataset | Size | Source |
|---|---|---|
| WELFake | 72k articles | Kaggle |
| LIAR | 12.8k statements | UCSB NLP |
| FakeNewsNet | 23k articles | GitHub |
# 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'})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
])- 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
This model is trained for educational purposes. Do not rely on it for real-world fact checking. Always verify with trusted sources.
- 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