Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- .gitattributes +1 -0
- Fake_news_detector.ipynb +0 -0
- News_classifier.pt +3 -0
- README.md +35 -0
- app.py +62 -0
- requirements.txt +10 -0
- tokenizer_distilbert/special_tokens_map.json +7 -0
- tokenizer_distilbert/tokenizer_config.json +58 -0
- tokenizer_distilbert/vocab.txt +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
News_classifier.pt filter=lfs diff=lfs merge=lfs -text
|
Fake_news_detector.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
News_classifier.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2881ab6ee32b411cf5cbefe1d9c9f3fa98b71e4027f7102bfa45ccfcac8cd9ae
|
| 3 |
+
size 266459281
|
README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fake News Detector
|
| 2 |
+
|
| 3 |
+
This project is a Fake News Detection system using DistilBERT and PyTorch, with a Streamlit web app for user interaction.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
- Data preprocessing and visualization (Jupyter Notebook)
|
| 7 |
+
- Model training using DistilBERT embeddings
|
| 8 |
+
- Streamlit app for real-time news classification
|
| 9 |
+
|
| 10 |
+
## Files
|
| 11 |
+
- `Fakke_news_detector.ipynb`: Data analysis, preprocessing, model training
|
| 12 |
+
- `app.py`: Streamlit web app for fake news detection
|
| 13 |
+
- `News_classifier.pt`: Trained PyTorch model weights
|
| 14 |
+
- `tokenizer_distilbert/`: Saved tokenizer files
|
| 15 |
+
|
| 16 |
+
## Usage
|
| 17 |
+
1. **Install dependencies**
|
| 18 |
+
```powershell
|
| 19 |
+
pip install -r requirements.txt
|
| 20 |
+
```
|
| 21 |
+
2. **Run the Streamlit app**
|
| 22 |
+
```powershell
|
| 23 |
+
streamlit run app.py
|
| 24 |
+
```
|
| 25 |
+
3. **Interact**
|
| 26 |
+
- Paste news text in the app to check if it is Fake or True.
|
| 27 |
+
|
| 28 |
+
## Training
|
| 29 |
+
- See the notebook for data loading, preprocessing, model training, and evaluation steps.
|
| 30 |
+
|
| 31 |
+
## Model & Tokenizer
|
| 32 |
+
- The model and tokenizer are saved after training and loaded in the app for inference.
|
| 33 |
+
|
| 34 |
+
## Requirements
|
| 35 |
+
See `requirements.txt` for all required Python packages.
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import DistilBertTokenizer, DistilBertModel
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
|
| 6 |
+
class NewsClassifier(nn.Module):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super(NewsClassifier, self).__init__()
|
| 9 |
+
self.bert = DistilBertModel.from_pretrained("distilbert-base-uncased")
|
| 10 |
+
for param in self.bert.parameters():
|
| 11 |
+
param.requires_grad = False
|
| 12 |
+
self.classifier = nn.Sequential(
|
| 13 |
+
nn.Linear(self.bert.config.hidden_size, 256),
|
| 14 |
+
nn.BatchNorm1d(256),
|
| 15 |
+
nn.ReLU(),
|
| 16 |
+
nn.Dropout(0.3),
|
| 17 |
+
nn.Linear(256, 128),
|
| 18 |
+
nn.BatchNorm1d(128),
|
| 19 |
+
nn.ReLU(),
|
| 20 |
+
nn.Dropout(0.3),
|
| 21 |
+
nn.Linear(128, 64),
|
| 22 |
+
nn.BatchNorm1d(64),
|
| 23 |
+
nn.ReLU(),
|
| 24 |
+
nn.Dropout(0.3),
|
| 25 |
+
nn.Linear(64, 2)
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def forward(self, input_ids, attention_mask):
|
| 29 |
+
bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 30 |
+
sentence_embeddings = bert_output.last_hidden_state[:, 0, :]
|
| 31 |
+
return self.classifier(sentence_embeddings)
|
| 32 |
+
|
| 33 |
+
@st.cache_resource
|
| 34 |
+
def load_model_and_tokenizer():
|
| 35 |
+
tokenizer = DistilBertTokenizer.from_pretrained("tokenizer_distilbert")
|
| 36 |
+
model = NewsClassifier()
|
| 37 |
+
model.load_state_dict(torch.load("News_classifier.pt", map_location="cpu"))
|
| 38 |
+
model.eval()
|
| 39 |
+
return tokenizer, model
|
| 40 |
+
|
| 41 |
+
tokenizer, model = load_model_and_tokenizer()
|
| 42 |
+
class_names = ["True", "Fake"]
|
| 43 |
+
|
| 44 |
+
st.title("Fake News Detection App")
|
| 45 |
+
st.write("Paste a news article/text below to check if it is **Fake** or **True**.")
|
| 46 |
+
|
| 47 |
+
news_text = st.text_area("Enter News Text", height=200)
|
| 48 |
+
|
| 49 |
+
if st.button("Predict"):
|
| 50 |
+
if news_text.strip():
|
| 51 |
+
|
| 52 |
+
encoding = tokenizer(news_text, padding="max_length", max_length=200, truncation=True, return_tensors="pt")
|
| 53 |
+
input_ids = encoding["input_ids"]
|
| 54 |
+
attention_mask = encoding["attention_mask"]
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
outputs = model(input_ids, attention_mask)
|
| 58 |
+
prediction = torch.argmax(outputs, dim=1).item()
|
| 59 |
+
result = class_names[prediction]
|
| 60 |
+
st.success(f"This news is **{result}**.")
|
| 61 |
+
else:
|
| 62 |
+
st.warning("Please enter some news text!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pandas
|
| 5 |
+
seaborn
|
| 6 |
+
matplotlib
|
| 7 |
+
wordcloud
|
| 8 |
+
scikit-learn
|
| 9 |
+
tqdm
|
| 10 |
+
nltk
|
tokenizer_distilbert/special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer_distilbert/tokenizer_config.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": true,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_basic_tokenize": true,
|
| 47 |
+
"do_lower_case": true,
|
| 48 |
+
"extra_special_tokens": {},
|
| 49 |
+
"mask_token": "[MASK]",
|
| 50 |
+
"model_max_length": 512,
|
| 51 |
+
"never_split": null,
|
| 52 |
+
"pad_token": "[PAD]",
|
| 53 |
+
"sep_token": "[SEP]",
|
| 54 |
+
"strip_accents": null,
|
| 55 |
+
"tokenize_chinese_chars": true,
|
| 56 |
+
"tokenizer_class": "DistilBertTokenizer",
|
| 57 |
+
"unk_token": "[UNK]"
|
| 58 |
+
}
|
tokenizer_distilbert/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|