Skip to content

XavierRolex/Fake_News_Detector

Repository files navigation

📰 Fake News Detector API

Detect fake news in seconds with a robust ML ensemble model—lightning-fast predictions, reliably hosted on Railway.

codecov FastAPI Python License: MIT Model: scikit-learn Hosted on Railway Try it now

A production‑ready FastAPI microservice that predicts whether a news article is Fake or Real using an ensemble ML model (TF‑IDF + VotingClassifier). Fully Dockerised, CI‑ready, and live on Railway.


📑 Table of Contents

  1. Features
  2. Live Demo
  3. Tech Stack
  4. Project Structure
  5. Local Setup
  6. Docker Usage
  7. API Reference
  8. Testing
  9. Environment Variables
  10. License
  11. Author
  12. Contributing

✨ Features

  • 🧠 Ensemble ML pipeline (TF‑IDF + VotingClassifier)
  • FastAPI backend with automatic Swagger UI (/docs)
  • 🐳 Docker‑first design — build once, run anywhere
  • 🧵 CI/CD ready with GitHub Actions and automated Codecov reports
  • 🛡️ Typed request/response models & custom exception handling
  • 🌐 CORS enabled for easy front‑end integration

🚀 Live Demo

Base URL: https://fake-news-detector-co.up.railway.app

$ curl https://fake-news-detector-co.up.railway.app/
{"message":"Fake News Detector API is up and running."}

Open /docs for an interactive Swagger UI.

Swagger UI Screenshot Swagger UI Screenshot Swagger UI Screenshot

Use the Swagger interface to test endpoints, send JSON payloads, and view real-time predictions


⚙️ Tech Stack

  • FastAPI & Uvicorn
  • scikit‑learn & joblib
  • Python 3.11
  • Docker & Railway
  • Pytest + Codecov

📁 Project Structure

.
├── app/
│   ├── main.py              # FastAPI entry‑point
│   ├── predictor.py         # Prediction logic
│   ├── logging_config.py    # Centralised logger
│   ├── exceptions.py        # Custom exceptions
│   ├── schemas.py           # Pydantic models
│   └── utils/
│       └── loader.py        # Load model & vectorizer
├── models/
│   ├── ensemble_voting_model.pkl
│   └── tfidf_vectorizer.pkl
├── tests/                   # Unit & integration tests
├── Dockerfile
├── requirements.txt
├── .dockerignore
├── .github/                 # CI workflow
└── README.md

🔧 Local Setup

# 1. Clone
git clone https://github.com/XavierRolex/fake-news-detector.git
cd fake-news-detector

# 2. (Optional) Create & activate virtual env
python -m venv venv && source venv/bin/activate

# 3. Install deps
pip install -r requirements.txt

# 4. Create a .env file (see below)
echo "MODEL_PATH=models/ensemble_voting_model.pkl" >> .env
echo "VECTORIZER_PATH=models/tfidf_vectorizer.pkl" >> .env

# 5. Run the API
uvicorn app.main:app --reload

Visit http://127.0.0.1:8000/docs for Swagger UI.


🐳 Docker Usage

# Build image
docker build -t fake-news-api .

# Run container
docker run -p 8000:8000 fake-news-api

📡 API Reference

GET /

Health check
Response

{ "message": "Fake News Detector API is up and running." }

POST /predict

Predict if text is Fake or Real
Request

{ "text": "NASA confirms water on the Moon's surface." }

Response

{ "label": "Fake", "probability": 0.1099 }

🧪 Testing

pytest --cov=app

Coverage reports are uploaded automatically to Codecov via GitHub Actions.


📄 Environment Variables

Key Description Default
MODEL_PATH Path to the trained model models/ensemble_voting_model.pkl
VECTORIZER_PATH Path to TF‑IDF vectorizer models/tfidf_vectorizer.pkl
LOG_LEVEL Logging level INFO

Note: On Railway, these are configured via the Variables tab.


📝 License

Distributed under the MIT License. See LICENSE for details.


👤 Author

Nafis Anzum


🌟 Contributing

Contributions are welcome! Please open an issue first to discuss your ideas, or submit a pull request.


© 2025 Nafis Anzum – All rights reserved.

About

FastAPI ML service for real-time fake news detection

Topics

Resources

License

Stars

Watchers

Forks