https://github.com/drisskhattabi6/fake-news-detection-using-finetuned-bert
Fake News Detection Using FineTuned BERT
https://github.com/drisskhattabi6/fake-news-detection-using-finetuned-bert
Last synced: 3 months ago
JSON representation
Fake News Detection Using FineTuned BERT
- Host: GitHub
- URL: https://github.com/drisskhattabi6/fake-news-detection-using-finetuned-bert
- Owner: drisskhattabi6
- Created: 2025-07-19T10:48:25.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-07-19T11:56:03.000Z (3 months ago)
- Last Synced: 2025-07-19T16:27:43.947Z (3 months ago)
- Language: Jupyter Notebook
- Size: 39.7 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fake News Detection using Fine-tuned BERT
This project demonstrates how to fine-tune a pre-trained BERT model to detect fake news articles using a labeled dataset. It includes data preprocessing, model training, evaluation, and deployment via a Flask web app.
---
## ๐ Project Structure
```
โโโ Finetuning-BERT-Fake-News-Detection.ipynb # Main notebook for training the model
โโโ Testing\_BERT.ipynb # Notebook for testing and evaluation
โโโ app.py # Flask web application
โโโ a1\_True.csv # Dataset of real news articles
โโโ a2\_Fake.csv # Dataset of fake news articles
โโโ model/
โ โโโ fakenews\_weights.pt # Another saved model version
โโโ static/
โ โโโ style.css # Styling for the web interface
โโโ templates/
โ โโโ index.html # Home page for news submission
โ โโโ result.html # Result page for prediction
โโโ logs.log # Log file during training or testing
โโโ README.md # This file````
---
## ๐งช Dataset
The dataset consists of two CSV files:
- `a1_True.csv` โ real news articles
- `a2_Fake.csv` โ fake news articlesEach file includes the text of the news and associated metadata. The data is preprocessed and combined for training.
---
## ๐ง Model
- **Model Used:** [`bert-base-uncased`](https://huggingface.co/bert-base-uncased)
- **Architecture:**
- Pre-trained BERT encoder
- Linear โ ReLU โ Dropout โ Linear โ LogSoftmax
- **Loss Function:** Negative Log Likelihood Loss (`NLLLoss`)
- **Optimizer:** `AdamW` with a learning rate of `1e-5`
- **Output Classes:** `Real` or `Fake`-> Fine-tuning BERT model process :

---
### ๐ Evaluation
After fine-tuning the BERT model on the fake news dataset, the classifier achieved the following performance metrics on the test set:
```
precision recall f1-score supportReal 0.84 0.92 0.88 3213
Fake 0.92 0.84 0.88 3522Accuracy 0.88 6735
Macro avg 0.88 0.88 0.88 6735
Weighted avg 0.88 0.88 0.88 6735
```โ **Overall Accuracy:** 88%
๐ **Balanced Performance:** Both real and fake news are classified with high precision and recall, indicating that the model is not biased toward either class.---
## ๐ How to Use
### ๐ง 1. Install Requirements
```bash
pip install torch transformers flask
````### ๐๏ธ 2. Train or Load the Model
* Use the notebook `Finetuning-BERT-Fake-News-Detection.ipynb` to train and save the model.
* Or use the already fine-tuned model in `cashe/c2_new_model_weights.pt`.### ๐ 3. Run the Flask Web App
```bash
python app.py
```Then open your browser and visit: [http://localhost:5000](http://localhost:5000)
---
## ๐ฅ๏ธ Web Interface
* Paste a news article into the form.
* Click "Check".
* The app will return a prediction: โ **Real News** or โ **Fake News**

---
## ๐งช API Endpoint
You can also use the API with a POST request:
```bash
curl -X POST http://localhost:5000/api/predict \
-H "Content-Type: application/json" \
-d '{"text": "Some news content here..."}'
```Response:
```json
{
"prediction": "Fake News"
}
```---
## ๐ Acknowledgements
* [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805)
* Hugging Face Transformers
* PyTorch