A complete end-to-end Machine Learning web application designed to detect fake news. Users can input a news article or headline, and the system uses NLP (TF-IDF) and a Logistic Regression model to predict if the news is REAL or FAKE, providing a confidence score and explainability highlights.
- Create the Project Directory
mkdir Fake-News-Detection cd Fake-News-Detection - Create a Virtual Environment
python -m venv venv # Windows Activation venv\Scripts\activate # macOS/Linux Activation source venv/bin/activate
- Install Required Dependencies
Create a
requirements.txtfile with dependencies (e.g.,scikit-learn,pandas,nltk,flask,flask-cors,lime,joblib) and install them:pip install -r requirements.txt
Organize the project cleanly into the following structure:
Fake-News-Detection/
│
├── backend/
│ └── app.py
│
├── frontend/
│ ├── index.html
│ ├── style.css
│ └── script.js
│
├── models/
│ ├── fake_news_model.pkl (generated later)
│ └── tfidf_vectorizer.pkl (generated later)
│
├── data/ (place dataset here)
│
├── preprocess.py
├── train_model.py
└── requirements.txt
- Load Data: Download a Fake/Real news dataset (e.g., from Kaggle) and place it in the
data/folder. - Clean Text: Write functions to:
- Convert text to lowercase.
- Remove punctuation and special characters.
- Remove stopwords (using
nltk). - Tokenize the text.
- Load Data: Import the preprocessed dataset.
- Vectorization: Initialize and fit a
TfidfVectorizerto convert the text into numerical feature vectors. - Train Classifier: Train a
LogisticRegressionmodel on the TF-IDF features. - Evaluate: Test the model's accuracy using a standard train-test split.
- Save Artifacts: Export the trained model (
fake_news_model.pkl) and the vectorizer (tfidf_vectorizer.pkl) to themodels/directory usingjoblib.
- Setup Flask: Initialize a Flask application and enable CORS.
- Load Artifacts: Load the
.pklmodel and vectorizer at startup. - Explainable AI (XAI): Initialize a LIME (or SHAP) explainer to highlight important words.
- Create Endpoint: Implement a
POST /predictroute that:- Accepts JSON input:
{"news": "..."} - Preprocesses and vectorizes the incoming text.
- Predicts whether the news is Fake or Real.
- Calculates the confidence score.
- Returns a JSON response:
{ "prediction": "Fake", "confidence": "94%", "explanation": "..." }
- Accepts JSON input:
index.html: Create a modern layout featuring a large textarea for input and a "Check News" button, along with an output display section.style.css: Apply responsive, visually appealing styles.script.js:- Attach a click listener to the button.
- Use the
fetch()API to send the news text tohttp://localhost:5000/predict. - Update the UI dynamically with the prediction result, percentage, and explainability data.
- Run the Backend Model:
(The server will typically start on
cd backend python app.pyhttp://127.0.0.1:5000) - Run the Frontend:
Simply double-click
frontend/index.htmlto open it in your browser (or use VS Code Live Server). - Test: Paste an article into the text box and click the button to see the results.
Consider leaving placeholders or comments for scaling the application:
- Implement deep learning approaches (LSTM, BERT).
- Build a Chrome Extension integration.
- Containerize the app with Docker and deploy to cloud providers (AWS/Render/Vercel).