Views
No views yet
streamlit run app/app.py bert-base-uncased
fine-tuned on the Kaggle Fake and Real News dataset.fake-news-detector/
├── data/
│ ├── Fake.csv ← download from Kaggle
│ └── True.csv ← download from Kaggle
│
├── models/
│ ├── best_model.pt ← saved after training
│ ├── tokenizer/ ← saved tokenizer files
│ ├── metrics.json ← accuracy/precision/recall/F1
│ ├── loss_curve.png ← training/validation loss plot
│ └── confusion_matrix.png
│
├── src/
│ ├── preprocess.py ← data loading & cleaning
│ ├── model.py ← BERTClassifier + NewsDataset
│ ├── train.py ← training + evaluation loop
│ └── predict.py ← inference helper (used by app)
│
├── app/
│ └── app.py ← Streamlit web app
│
├── logs/
│ └── training.log ← auto-generated during training
│
├── requirements.txt
└── README.md1git clone https://github.com/your-username/fake-news-detector.git
2cd fake-news-detector1python -m venv .venv
2source .venv/bin/activate # Windows: .venv\Scripts\activatepip install -r requirements.txtFake.csv and True.csv inside the data/ folder1# Default: 3 epochs, batch 16, max_length 256
2python src/train.py
3
4# Faster smoke-test (uses only 2 000 samples):
5python src/train.py --epochs 2 --max_samples 2000models/best_model.ptmodels/tokenizer/models/loss_curve.png and models/confusion_matrix.pngmodels/metrics.jsonstreamlit run app/app.pypython src/predict.py "NASA confirms alien life discovered on Mars!"app/app.py.packages.txt if you need system libs (usually not needed here).Note: The trained model (models/best_model.pt) must be committed to the repo or loaded from cloud storage (e.g. Hugging Face Hub). For large files use Git LFS.
Dockerfile at the project root:1FROM python:3.11-slim
2WORKDIR /app
3COPY . .
4RUN pip install --no-cache-dir -r requirements.txt
5EXPOSE 8501
6CMD ["streamlit", "run", "app/app.py", "--server.port=8501", "--server.address=0.0.0.0"]| Argument | Default | Description |
|---|---|---|
--data_dir | data | folder with Fake.csv + True.csv |
--model_name | bert-base-uncased | HuggingFace model ID |
--epochs | 3 | number of training epochs |
--batch_size | 16 | per-device batch size |
--max_length | 256 | max token sequence length |
--learning_rate | 2e-5 | AdamW learning rate |
--dropout | 0.3 | dropout on [CLS] embedding |
--max_samples | None | cap data size (for quick runs) |
--freeze_bert | False | freeze BERT, train head only |
| Metric | Score |
|---|---|
| Accuracy | ~99% |
| Precision | ~99% |
| Recall | ~99% |
| F1-score | ~99% |
These results are typical on this dataset. BERT fine-tuning on Fake/Real news achieves near-perfect scores because the two corpora have distinct writing styles and sources.
Input text
│
▼
BertTokenizer (max_length=256, padding, truncation)
│
▼
bert-base-uncased encoder (12 layers, 768 hidden, 110M params)
│
├─ [CLS] token hidden state (768-dim)
│
▼
Dropout(p=0.3)
│
▼
Linear(768 → 2)
│
▼
Softmax → P(Fake), P(Real)