Skip to content

Commit 3d281ac

Browse files
committed
Added fake news detection project
1 parent 872587f commit 3d281ac

File tree

13 files changed

+167080
-0
lines changed

13 files changed

+167080
-0
lines changed
10 KB
Binary file not shown.

MachineLearning Projects/Fakenews/Fakenews.ipynb

Lines changed: 483 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
from flask import Flask, request,render_template
3+
from flask_cors import CORS
4+
import joblib
5+
import pickle
6+
import flask
7+
import os
8+
import newspaper
9+
from newspaper import Article
10+
import urllib.request
11+
import nltk
12+
nltk.download('punkt')
13+
14+
#Loading Flask and assigning the model variable
15+
app = Flask(__name__)
16+
CORS(app)
17+
app=flask.Flask(__name__,template_folder='templates')
18+
19+
with open('model.pickle', 'rb') as handle:
20+
model = pickle.load(handle)
21+
22+
@app.route('/')
23+
def main():
24+
return render_template('index.html')
25+
26+
#Receiving the input url from the user and using Web Scrapping to extract the news content
27+
@app.route('/predict',methods=['GET','POST'])
28+
def predict():
29+
url =request.get_data(as_text=True)[5:]
30+
url = urllib.parse.unquote(url)
31+
article = Article(str(url))
32+
article.download()
33+
article.parse()
34+
article.nlp()
35+
news = article.summary
36+
#Passing the news article to the model and returing whether it is Fake or Real
37+
pred = model.predict([news])
38+
return render_template('index.html', prediction_text='The news is "{}"'.format(pred[0]))
39+
40+
if __name__=="__main__":
41+
port=int(os.environ.get('PORT',5000))
42+
app.run(port=port,debug=True,use_reloader=False)
43+
44+
45+
46+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#Importing the libraries
2+
import pandas as pd
3+
import numpy as np
4+
from sklearn.metrics import classification_report, confusion_matrix
5+
from sklearn.pipeline import Pipeline
6+
from sklearn.model_selection import train_test_split
7+
from sklearn.naive_bayes import MultinomialNB
8+
from sklearn.feature_extraction.text import TfidfVectorizer
9+
import pickle
10+
11+
#Importing the cleaned file containing the text and label
12+
news = pd.read_csv('news.csv')
13+
X = news['text']
14+
y = news['label']
15+
16+
17+
18+
19+
#Splitting the data into train
20+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
21+
22+
#Creating a pipeline that first creates bag of words(after applying stopwords) & then applies Multinomial Naive Bayes model
23+
pipeline = Pipeline([('tfidf', TfidfVectorizer(stop_words='english')),
24+
('nbmodel', MultinomialNB())])
25+
26+
#Training our data
27+
pipeline.fit(X_train, y_train)
28+
29+
#Predicting the label for the test data
30+
pred = pipeline.predict(X_test)
31+
32+
#Checking the performance of our model
33+
print(classification_report(y_test, pred))
34+
print(confusion_matrix(y_test, pred))
35+
36+
#Serialising the file
37+
with open('model.pickle', 'wb') as handle:
38+
pickle.dump(pipeline, handle, protocol=pickle.HIGHEST_PROTOCOL)
39+
40+
Loading
Loading
4.58 MB
Binary file not shown.

MachineLearning Projects/Fakenews/news.csv

Lines changed: 166355 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
2+
.btn { display: inline-block; *display: inline; *zoom: 1; padding: 4px 10px 4px; margin-bottom: 0;
3+
font-size: 13px; line-height: 18px; color: #A52A2A; text-align: center;text-shadow: 0 1px 1px
4+
rgba(255, 255, 255, 0.75); vertical-align: middle; background-color: #f5f5f5; background-image: -
5+
moz-linear-gradient(top, #ffffff, #e6e6e6); background-image: -ms-linear-gradient(top, #ffffff,
6+
#e6e6e6); background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ffffff), to(#e6e6e6));
7+
background-image: -webkit-linear-gradient(top, #ffffff, #e6e6e6); background-image: -o-linear-
8+
gradient(top, #ffffff, #e6e6e6); background-image: linear-gradient(top, #ffffff, #e6e6e6);
9+
background-repeat: repeat-x; filter:
10+
progid:dximagetransform.microsoft.gradient(startColorstr=#ffffff, endColorstr=#e6e6e6,
11+
GradientType=0); border-color: #e6e6e6 #e6e6e6 #e6e6e6; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0,
12+
0, 0.1) rgba(0, 0, 0, 0.25); border: 1px solid #e6e6e6; -webkit-border-radius: 4px; -moz-border-
13+
radius: 4px; border-radius: 4px; -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px
14+
2px rgba(0, 0, 0, 0.05); -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0,
15+
0, 0.05); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05); cursor:
16+
pointer; *margin-left: .3em; }
17+
.btn:hover, .btn:active, .btn.active, .btn.disabled, .btn[disabled] { background-color: #A52A2A; }
18+
.btn-large { padding: 9px 14px; font-size: 15px; line-height: normal; -webkit-border-radius: 5px; -
19+
moz-border-radius: 5px; border-radius: 5px; }
20+
.btn:hover { color: #A52A2A; text-decoration: none; background-color: #A52A2A; background-
21+
position: 0 -15px; -webkit-transition: background-position 0.1s linear; -moz-transition: background-
22+
position 0.1s linear; -ms-transition: background-position 0.1s linear; -o-transition: background-
23+
position 0.1s linear; transition: background-position 0.1s linear; }
24+
.btn-primary, .btn-primary:hover { text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); color: #ffffff; }
25+
.btn-primary.active { color: rgba(255, 255, 255, 0.75); }
26+
.btn-primary { background-color: #A52A2A; background-image: -moz-linear-gradient(top, #F5F5DC,
27+
#A52A2A); background-image: -ms-linear-gradient(top, #F5F5DC, #A52A2A); background-image: -
28+
webkit-gradient(linear, 0 0, 0 100%, from(#F5F5DC), to(#A52A2A)); background-image: -webkit-
29+
linear-gradient(top, #F5F5DC, #A52A2A); background-image: -o-linear-gradient(top, #F5F5DC,
30+
#A52A2A); background-image: linear-gradient(top, #F5F5DC, #A52A2A); background-repeat: repeat-
31+
x; filter: progid:dximagetransform.microsoft.gradient(startColorstr=#F5F5DC, endColorstr=#A52A2A,
32+
GradientType=0); border: 1px solid #3762bc; text-shadow: 1px 1px 1px rgba(0,0,0,0.4); box-shadow:
33+
inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.5); }
34+
.btn-primary:hover, .btn-primary:active, .btn-primary.active, .btn-primary.disabled, .btn-
35+
primary[disabled] { filter: none; background-color: #a52a2a; }
36+
.btn-block { width: 100%; display:block; }
37+
* { -webkit-box-sizing:border-box; -moz-box-sizing:border-box; -ms-box-sizing:border-box; -o-box-
38+
sizing:border-box; box-sizing:border-box; }
39+
html { width: 100%; height:100%; overflow:hidden; }
40+
body {
41+
width: 100%;
42+
height:100%;
43+
font-family: 'Open Sans', sans-serif;
44+
background: #FFE4B5;
45+
color: #000000;
46+
font-size: 18px;
47+
48+
text-align:center;
49+
letter-spacing:1.2px;
50+
background: -webkit-linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%);
51+
background: -o-linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%);
52+
background: -moz-linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%);
53+
background: linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%);
54+
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#3E1D6D',
55+
endColorstr='#092756',GradientType=1 ); filter: progid:DXImageTransform.Microsoft.gradient(
56+
startColorstr='#3E1D6D', endColorstr='#800000',GradientType=1 );
57+
}
58+
.login {
59+
position: absolute;
60+
top: 60%;
61+
left: 50%;
62+
margin: -150px 0 0 -150px;
63+
width:400px;
64+
height:400px;
65+
}
66+
.login h1 { color: #000000; text-shadow: 0 0 10px rgba(0,0,0,0.3); letter-spacing:1px; text-
67+
align:center; }
68+
input {
69+
width: 100%;
70+
margin-bottom: 10px;
71+
background: rgba(0,0,0,0.3);
72+
border: none;
73+
outline: none;
74+
padding: 10px;
75+
font-size: 25px;
76+
color: #000000;
77+
text-shadow: 1px 1px 1px rgba(0,0,0,0.3);
78+
border: 1px solid rgba(0,0,0,0.3);
79+
border-radius: 4px;
80+
box-shadow: inset 0 -5px 45px rgba(100,100,100,0.2), 0 1px 1px rgba(255,255,255,0.2);
81+
-webkit-transition: box-shadow .5s ease;
82+
-moz-transition: box-shadow .5s ease;
83+
-o-transition: box-shadow .5s ease;
84+
-ms-transition: box-shadow .5s ease;
85+
transition: box-shadow .5s ease;
86+
}
87+
input:focus { box-shadow: inset 0 -5px 45px rgba(100,100,100,0.4), 0 1px 1px rgba(255,255,255,0.2);
88+
}
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html >
3+
<!--From https://codepen.io/frytyler/pen/EGdtg-->
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Fake News Detection</title>
7+
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
8+
<link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'>
9+
<link href='https://fonts.googleapis.com/css?family=Hind:300' rel='stylesheet' type='text/css'>
10+
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
11+
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
12+
13+
</head>
14+
15+
<body>
16+
<div class="login">
17+
<h1>Predict Fake News</h1>
18+
19+
<!-- Main Input For Receiving Query to our ML -->
20+
<form action="{{ url_for('predict')}}"method="post">
21+
<input type="text" name="news" placeholder="Enter the news url" required="required" />
22+
<button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
23+
24+
</form>
25+
26+
<br>
27+
<br>
28+
{{ prediction_text }}
29+
30+
</div>
31+
32+
33+
</body>
34+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html >
3+
<!--From https://codepen.io/frytyler/pen/EGdtg-->
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Fake News Detection</title>
7+
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
8+
<link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'>
9+
<link href='https://fonts.googleapis.com/css?family=Hind:300' rel='stylesheet' type='text/css'>
10+
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
11+
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
12+
13+
</head>
14+
15+
<body>
16+
<div class="login">
17+
<h1>Predict Fake News</h1>
18+
19+
<!-- Main Input For Receiving Query to our ML -->
20+
<form action="{{ url_for('predict')}}"method="post">
21+
<input type="text" name="news" placeholder="Enter the news url" required="required" />
22+
<button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
23+
24+
</form>
25+
26+
<br>
27+
<br>
28+
{{ prediction_text }}
29+
30+
</div>
31+
32+
33+
</body>
34+
</html>

0 commit comments

Comments
 (0)