DEV Community

info_brust
info_brust

Posted on

πŸš€ Build and Deploy a Fake News Detector with Flask for Free!

Tags: #python #flask #machinelearning #webdev #project

β€œIn a world full of noise, let’s teach machines to detect the lies.”

Hey folks! πŸ‘‹
In this post, I’ll walk you through how I built and deployed a Fake News Detector using Python, Flask, and Machine Learning β€” and hosted it online for free. Whether you're a student, a beginner developer, or just someone who loves real news πŸ˜„ β€” this one’s for you!


🧠 What I Built

I created a web app that allows users to paste a news article, and it predicts whether the article is real or fake using a trained machine learning model. Simple UI, clean prediction logic, and completely self-contained.


πŸ”§ Tech Stack

  • Python 3
  • Flask (for the web app)
  • Scikit-learn (for ML model)
  • NLTK (for text preprocessing)
  • HTML/CSS (for frontend)
  • Render.com (for free hosting)

πŸ“ Project Structure

project/
β”‚
β”œβ”€β”€ app.py                  # Flask app
β”œβ”€β”€ train_model.py          # ML training script
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ /data                   # True.csv, Fake.csv
β”œβ”€β”€ /models                 # Saved .pkl model + vectorizer
β”œβ”€β”€ /templates/index.html   # UI
β”œβ”€β”€ /static/style.css       # Basic CSS
β”œβ”€β”€ /utils/text_cleaner.py  # Text preprocessing
└── README.md
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ How It Works

1. Model Training

In train_model.py:

  • I combined real and fake news datasets
  • Preprocessed the text (lowercase, remove stopwords, lemmatize)
  • Used TF-IDF vectorization
  • Trained a Logistic Regression model
  • Saved the model and vectorizer with pickle

2. The Flask Web App

In app.py:

  • Loads the model and vectorizer
  • Takes user input from a text box
  • Preprocesses the input using the same clean_text() function
  • Predicts using the trained model
  • Displays: βœ… Real News or ❌ Fake News

🌐 Hosting It for Free with Render

Step-by-Step:

  1. Pushed code to GitHub
  2. Signed in at Render.com
  3. Chose New Web Service > Linked GitHub repo
  4. Set:
  • Runtime: Python
  • Start Command: gunicorn app:app
    1. Done! Got a live public URL

πŸ‘‰ Example: https://fake-news-detector.onrender.com


πŸ“¦ Dependencies (requirements.txt)

flask
scikit-learn
nltk
pandas
gunicorn
Enter fullscreen mode Exit fullscreen mode

πŸ™Œ Final Thoughts

I learned a lot through this project β€” from building a clean Flask UI, to saving/loading ML models, to deploying with ease.

If you're looking for a project that combines web development + machine learning and is hosted for free, this is perfect. πŸ”₯


πŸ’¬ Let’s Connect

Have questions or suggestions?
Drop a comment or reach out to me on GitHub / X β€” I’d love to see your version of this project!


⭐ Bonus: Want the Code?

Check it out here:
πŸ‘‰ GitHub Repo

Top comments (0)