Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- .gitattributes +2 -0
- Fake.csv +3 -0
- True.csv +3 -0
- app.py +78 -0
- news_classfication.pth +3 -0
- news_classification_notebook.ipynb +0 -0
- requirements.txt +8 -0
- tokenizer.pkl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Fake.csv filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
True.csv filter=lfs diff=lfs merge=lfs -text
|
Fake.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bebf8bcfe95678bf2c732bf413a2ce5f621af0102c82bf08083b2e5d3c693d0c
|
| 3 |
+
size 62789876
|
True.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba0844414a65dc6ae7402b8eee5306da24b6b56488d6767135af466c7dcb2775
|
| 3 |
+
size 53582940
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import re
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import joblib
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
|
| 8 |
+
st.title("News Classification")
|
| 9 |
+
|
| 10 |
+
## mopdel
|
| 11 |
+
|
| 12 |
+
vocab_size = 37852
|
| 13 |
+
embedding_dim = 45
|
| 14 |
+
hidden_units = 25
|
| 15 |
+
num_classes = 2
|
| 16 |
+
max_len = 55
|
| 17 |
+
|
| 18 |
+
class LSTMModel(nn.Module):
|
| 19 |
+
def __init__(self, vocab_size, embedding_dim, hidden_units, num_classes):
|
| 20 |
+
super(LSTMModel, self).__init__()
|
| 21 |
+
self.embedding = nn.Embedding(vocab_size, embedding_dim)
|
| 22 |
+
self.lstm = nn.LSTM(embedding_dim, hidden_units, batch_first=True, dropout=0.2,bidirectional=True)
|
| 23 |
+
self.fc = nn.Linear(hidden_units* 2, num_classes)
|
| 24 |
+
|
| 25 |
+
def forward(self, x):
|
| 26 |
+
x = self.embedding(x)
|
| 27 |
+
output, _ = self.lstm(x)
|
| 28 |
+
x = output[:, -1, :]
|
| 29 |
+
x = self.fc(x)
|
| 30 |
+
return F.softmax(x, dim=1)
|
| 31 |
+
|
| 32 |
+
model = LSTMModel(vocab_size, embedding_dim, hidden_units, num_classes)
|
| 33 |
+
## load the weights
|
| 34 |
+
model.load_state_dict(torch.load( "news_classfication.pth", map_location=torch.device("cpu")))
|
| 35 |
+
model.eval()
|
| 36 |
+
|
| 37 |
+
tokenizer=joblib.load("tokenizer.pkl")
|
| 38 |
+
|
| 39 |
+
def preprocess(words):
|
| 40 |
+
normalized = []
|
| 41 |
+
for i in words:
|
| 42 |
+
i = i.lower()
|
| 43 |
+
# get rid of urlss
|
| 44 |
+
i = re.sub('https?://\S+|www\.\S+', '', i)
|
| 45 |
+
# get rid of non words and extra spaces
|
| 46 |
+
i = re.sub('\\W', ' ', i)
|
| 47 |
+
i = re.sub('\n', '', i)
|
| 48 |
+
i = re.sub(' +', ' ', i)
|
| 49 |
+
i = re.sub('^ ', '', i)
|
| 50 |
+
i = re.sub(' $', '', i)
|
| 51 |
+
|
| 52 |
+
normalized.append(i)
|
| 53 |
+
text=[tokenizer.encode(text.lower()).ids for text in normalized]
|
| 54 |
+
max_length = 20
|
| 55 |
+
flattened_text = [token for sublist in text for token in sublist]
|
| 56 |
+
if len(flattened_text) > max_length:
|
| 57 |
+
flattened_text = flattened_text[:max_length]
|
| 58 |
+
else:
|
| 59 |
+
flattened_text += [0] * (max_length - len(flattened_text))
|
| 60 |
+
text_tensor = torch.tensor(flattened_text, dtype=torch.long)
|
| 61 |
+
text_tensor = text_tensor.unsqueeze(0)
|
| 62 |
+
return text_tensor
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
text=st.text_input("Enter the news Tittle ",value="Sheriff David Clarke Becomes An Internet Joke For Threatening To Poke People 'In The Eye'")
|
| 68 |
+
|
| 69 |
+
if st.button("submit"):
|
| 70 |
+
words=text.split()
|
| 71 |
+
v=preprocess(words)
|
| 72 |
+
output=model(v)
|
| 73 |
+
if output.argmax()==0:
|
| 74 |
+
st.write("Its a Fake news")
|
| 75 |
+
else:
|
| 76 |
+
st.write("Its not a Fake news")
|
| 77 |
+
|
| 78 |
+
|
news_classfication.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05a6a9c12929a5d98deb0e7af30d2c3f39fc2c82a65c32771404cb28e9028daa
|
| 3 |
+
size 6874152
|
news_classification_notebook.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.30.0
|
| 2 |
+
pandas==2.1.4
|
| 3 |
+
torch==2.2.0
|
| 4 |
+
torchvision==0.17.0
|
| 5 |
+
numpy==1.26.3
|
| 6 |
+
scikit-learn==1.3.2
|
| 7 |
+
tokenizers==0.15.1
|
| 8 |
+
joblib==1.2.0
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fd836e1eeb44d11836cbe52a4d10d4b2d5579c6265611dfd16941dfdfb1f9ed2
|
| 3 |
+
size 645410
|