Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
@@ -1,165 +1,177 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
-
|
93 |
-
- **
|
94 |
-
|
95 |
-
|
96 |
-
- **
|
97 |
-
-
|
98 |
-
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
-
|
133 |
-
-
|
134 |
-
-
|
135 |
-
-
|
136 |
-
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
- **
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
-
|
153 |
-
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Fake News Detector
|
3 |
+
colorFrom: gray
|
4 |
+
colorTo: red
|
5 |
+
sdk: streamlit
|
6 |
+
sdk_version: "1.33.0"
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
+
|
11 |
+
Detect fake news using a fine-tuned BERT model. Enter any headline or statement and get an instant prediction on whether it's likely real or fake. Built with Streamlit and Hugging Face Transformers.
|
12 |
+
|
13 |
+
# Fake News Detection Project
|
14 |
+
|
15 |
+
A machine learning project that classifies news articles as real or fake using both traditional NLP techniques and advanced transformer models.
|
16 |
+
|
17 |
+
## π― Project Overview
|
18 |
+
|
19 |
+
This project implements multiple approaches to detect fake news:
|
20 |
+
- **Traditional ML**: TF-IDF vectorization with Logistic Regression
|
21 |
+
- **Deep Learning**: Fine-tuned BERT model for sequence classification
|
22 |
+
|
23 |
+
## π Performance Results
|
24 |
+
|
25 |
+
### TF-IDF + Logistic Regression Model
|
26 |
+
- **Accuracy**: 98.62%
|
27 |
+
- **F1 Score**: 98.67%
|
28 |
+
|
29 |
+
#### Detailed Classification Report:
|
30 |
+
```
|
31 |
+
precision recall f1-score support
|
32 |
+
|
33 |
+
0 0.98 0.99 0.99 4284 (Real News)
|
34 |
+
1 0.99 0.98 0.99 4696 (Fake News)
|
35 |
+
|
36 |
+
accuracy 0.99 8980
|
37 |
+
macro avg 0.99 0.99 0.99 8980
|
38 |
+
weighted avg 0.99 0.99 0.99 8980
|
39 |
+
```
|
40 |
+
|
41 |
+
## π Project Structure
|
42 |
+
|
43 |
+
```
|
44 |
+
FakeNewsDetector/
|
45 |
+
βββ README.md
|
46 |
+
βββ requirements.txt
|
47 |
+
βββ notebooks/
|
48 |
+
β βββ FakeNewsClassifier_HuggingFace.ipynb
|
49 |
+
βββ scripts/
|
50 |
+
β βββ train.py
|
51 |
+
βββ models/
|
52 |
+
β βββ bert-fake-news/ (generated after training)
|
53 |
+
βββ data/
|
54 |
+
βββ app/
|
55 |
+
βββ venv/
|
56 |
+
```
|
57 |
+
|
58 |
+
## π Quick Start
|
59 |
+
|
60 |
+
### 1. Clone and Setup
|
61 |
+
```bash
|
62 |
+
git clone <repository-url>
|
63 |
+
cd FakeNewsDetector
|
64 |
+
```
|
65 |
+
|
66 |
+
### 2. Create Virtual Environment
|
67 |
+
```bash
|
68 |
+
python -m venv venv
|
69 |
+
|
70 |
+
# Windows PowerShell
|
71 |
+
.\venv\Scripts\Activate.ps1
|
72 |
+
|
73 |
+
# Windows CMD
|
74 |
+
.\venv\Scripts\activate.bat
|
75 |
+
|
76 |
+
# Git Bash
|
77 |
+
source venv/Scripts/activate
|
78 |
+
```
|
79 |
+
|
80 |
+
### 3. Install Dependencies
|
81 |
+
```bash
|
82 |
+
pip install -r requirements.txt
|
83 |
+
```
|
84 |
+
|
85 |
+
### 4. Launch Jupyter Notebook
|
86 |
+
```bash
|
87 |
+
jupyter notebook
|
88 |
+
```
|
89 |
+
|
90 |
+
## π Dataset
|
91 |
+
|
92 |
+
The project uses the `mrm8488/fake-news` dataset from Hugging Face, which contains:
|
93 |
+
- **Total articles**: ~45,000
|
94 |
+
- **Training split**: 80% (~36,000 articles)
|
95 |
+
- **Test split**: 20% (~9,000 articles)
|
96 |
+
- **Classes**:
|
97 |
+
- 0: Real News
|
98 |
+
- 1: Fake News
|
99 |
+
|
100 |
+
## π§ Models Implemented
|
101 |
+
|
102 |
+
### 1. TF-IDF + Logistic Regression
|
103 |
+
- **Vectorizer**: TF-IDF with 5,000 max features, n-grams (1,2)
|
104 |
+
- **Classifier**: Logistic Regression with balanced class weights
|
105 |
+
- **Performance**: 98.62% accuracy
|
106 |
+
|
107 |
+
### 2. BERT Fine-tuning
|
108 |
+
- **Base Model**: `bert-base-uncased`
|
109 |
+
- **Training**: 3 epochs with evaluation per epoch
|
110 |
+
- **Optimizer**: AdamW with learning rate 2e-5
|
111 |
+
- **Batch Size**: 8 per device
|
112 |
+
|
113 |
+
## π οΈ Usage
|
114 |
+
|
115 |
+
### Running the Notebook
|
116 |
+
1. Ensure your virtual environment is activated
|
117 |
+
2. Start Jupyter: `jupyter notebook`
|
118 |
+
3. Open `notebooks/FakeNewsClassifier_HuggingFace.ipynb`
|
119 |
+
4. Make sure the kernel is set to "venv" or "FakeNewsDetector (venv)"
|
120 |
+
5. Run all cells
|
121 |
+
|
122 |
+
### Training BERT Model
|
123 |
+
```bash
|
124 |
+
python scripts/train.py
|
125 |
+
```
|
126 |
+
|
127 |
+
The trained model will be saved to `models/bert-fake-news/`
|
128 |
+
|
129 |
+
## π Requirements
|
130 |
+
|
131 |
+
- Python 3.8+
|
132 |
+
- pandas
|
133 |
+
- scikit-learn
|
134 |
+
- datasets (Hugging Face)
|
135 |
+
- transformers
|
136 |
+
- torch
|
137 |
+
- matplotlib
|
138 |
+
- seaborn
|
139 |
+
- jupyter
|
140 |
+
- ipywidgets
|
141 |
+
|
142 |
+
## π― Key Features
|
143 |
+
|
144 |
+
- **High Accuracy**: Achieves 98.6% accuracy on test set
|
145 |
+
- **Multiple Approaches**: Compares traditional ML vs. transformer models
|
146 |
+
- **Easy Setup**: Simple virtual environment setup
|
147 |
+
- **Comprehensive Analysis**: Includes confusion matrix and detailed metrics
|
148 |
+
- **Production Ready**: Trained models can be saved and deployed
|
149 |
+
|
150 |
+
## π Model Analysis
|
151 |
+
|
152 |
+
The TF-IDF + Logistic Regression model shows excellent performance:
|
153 |
+
- **Balanced Performance**: High precision and recall for both classes
|
154 |
+
- **Low False Positives**: 98% precision for fake news detection
|
155 |
+
- **Low False Negatives**: 99% recall for real news detection
|
156 |
+
- **Robust**: Handles class imbalance well with balanced weights
|
157 |
+
|
158 |
+
## π Future Improvements
|
159 |
+
|
160 |
+
- [ ] Implement ensemble methods combining multiple models
|
161 |
+
- [ ] Add cross-validation for more robust evaluation
|
162 |
+
- [ ] Experiment with other transformer models (RoBERTa, DistilBERT)
|
163 |
+
- [ ] Deploy model as a web API
|
164 |
+
- [ ] Add real-time news article classification
|
165 |
+
- [ ] Implement explainability features (LIME, SHAP)
|
166 |
+
|
167 |
+
## π€ Contributing
|
168 |
+
|
169 |
+
Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](../../issues).
|
170 |
+
|
171 |
+
## π§ Contact
|
172 |
+
|
173 |
+
For questions or suggestions, please open an issue or contact the project maintainer.
|
174 |
+
|
175 |
+
---
|
176 |
+
|
177 |
+
**Note**: This project is for educational and research purposes. Always verify news from multiple reliable sources.
|