Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
6 |
+
from torch.utils.data import Dataset, DataLoader
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
true_news = pd.read_csv('True.csv')
|
10 |
+
fake_news = pd.read_csv('Fake.csv')
|
11 |
+
|
12 |
+
true_news['label'] = 1
|
13 |
+
fake_news['label'] = 0
|
14 |
+
df = pd.concat([true_news, fake_news], ignore_index=True)
|
15 |
+
|
16 |
+
import re
|
17 |
+
import nltk
|
18 |
+
from nltk.corpus import stopwords
|
19 |
+
|
20 |
+
def preprocess_text(text):
|
21 |
+
# Remove special characters
|
22 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text)
|
23 |
+
# Convert to lowercase
|
24 |
+
text = text.lower()
|
25 |
+
# Remove stopwords
|
26 |
+
stop_words = set(stopwords.words('english'))
|
27 |
+
text = ' '.join([word for word in text.split() if word not in stop_words])
|
28 |
+
return text
|
29 |
+
|
30 |
+
df['cleaned_text'] = df['text'].apply(preprocess_text)
|
31 |
+
|
32 |
+
vectorizer = TfidfVectorizer(max_features=5000)
|
33 |
+
X = vectorizer.fit_transform(df['cleaned_text']).toarray()
|
34 |
+
y = df['label'].values
|
35 |
+
|
36 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
37 |
+
|
38 |
+
class NewsDataset(Dataset):
|
39 |
+
def __init__(self, X, y):
|
40 |
+
self.X = torch.FloatTensor(X)
|
41 |
+
self.y = torch.LongTensor(y)
|
42 |
+
|
43 |
+
def __len__(self):
|
44 |
+
return len(self.y)
|
45 |
+
|
46 |
+
def __getitem__(self, idx):
|
47 |
+
return self.X[idx], self.y[idx]
|
48 |
+
|
49 |
+
train_dataset = NewsDataset(X_train, y_train)
|
50 |
+
test_dataset = NewsDataset(X_test, y_test)
|
51 |
+
|
52 |
+
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
|
53 |
+
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
|
54 |
+
|
55 |
+
class FakeNewsDetector(torch.nn.Module):
|
56 |
+
def __init__(self, input_dim):
|
57 |
+
super(FakeNewsDetector, self).__init__()
|
58 |
+
self.fc1 = torch.nn.Linear(input_dim, 64)
|
59 |
+
self.fc2 = torch.nn.Linear(64, 16)
|
60 |
+
self.fc3 = torch.nn.Linear(16, 2)
|
61 |
+
self.relu = torch.nn.ReLU()
|
62 |
+
|
63 |
+
def forward(self, x):
|
64 |
+
x = self.relu(self.fc1(x))
|
65 |
+
x = self.relu(self.fc2(x))
|
66 |
+
x = self.fc3(x)
|
67 |
+
return x
|
68 |
+
|
69 |
+
model = FakeNewsDetector(X_train.shape[1])
|
70 |
+
criterion = torch.nn.CrossEntropyLoss()
|
71 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
|
72 |
+
|
73 |
+
num_epochs = 10
|
74 |
+
|
75 |
+
for epoch in range(num_epochs):
|
76 |
+
model.train()
|
77 |
+
for batch_X, batch_y in train_loader:
|
78 |
+
optimizer.zero_grad()
|
79 |
+
outputs = model(batch_X)
|
80 |
+
loss = criterion(outputs, batch_y)
|
81 |
+
loss.backward()
|
82 |
+
optimizer.step()
|
83 |
+
|
84 |
+
# Evaluation
|
85 |
+
model.eval()
|
86 |
+
correct = 0
|
87 |
+
total = 0
|
88 |
+
with torch.no_grad():
|
89 |
+
for batch_X, batch_y in test_loader:
|
90 |
+
outputs = model(batch_X)
|
91 |
+
_, predicted = torch.max(outputs.data, 1)
|
92 |
+
total += batch_y.size(0)
|
93 |
+
correct += (predicted == batch_y).sum().item()
|
94 |
+
|
95 |
+
accuracy = 100 * correct / total
|
96 |
+
print(f'Epoch [{epoch+1}/{num_epochs}], Accuracy: {accuracy:.2f}%')
|
97 |
+
|
98 |
+
def predict_fake_news(text):
|
99 |
+
cleaned = preprocess_text(text)
|
100 |
+
vectorized = vectorizer.transform([cleaned]).toarray()
|
101 |
+
tensor = torch.FloatTensor(vectorized)
|
102 |
+
|
103 |
+
model.eval()
|
104 |
+
with torch.no_grad():
|
105 |
+
output = model(tensor)
|
106 |
+
_, predicted = torch.max(output.data, 1)
|
107 |
+
|
108 |
+
return "Fake News" if predicted.item() == 0 else "True News"
|
109 |
+
|
110 |
+
# Example usage
|
111 |
+
example_text = "Scientists discover new planet capable of supporting life"
|
112 |
+
prediction = predict_fake_news(example_text)
|
113 |
+
print(f"Prediction: {prediction}")
|
114 |
+
|
115 |
+
# Gradio Interface
|
116 |
+
def gradio_interface(text):
|
117 |
+
prediction = predict_fake_news(text)
|
118 |
+
return prediction
|
119 |
+
|
120 |
+
iface = gr.Interface(
|
121 |
+
fn=gradio_interface,
|
122 |
+
inputs="text",
|
123 |
+
outputs="text",
|
124 |
+
title="Fake News Detector",
|
125 |
+
description="Enter a news headline or text to predict whether it is Fake News or True News."
|
126 |
+
)
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
iface.launch(share=True)
|