-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
215 lines (159 loc) · 6.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from flask import Flask, render_template, url_for, request, redirect
import pickle
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Message, Mail
import pandas as pd
import numpy as np
import joblib
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem.lancaster import LancasterStemmer
from tensorflow import keras
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
mail = Mail()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///admin.db'
app.config['SECRET_KEY'] = "asecretkey"
app.secret_key = "asecretkey"
app.config["MAIL_SERVER"] = "smtp.yandex.ru"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = '[email protected]'
app.config["MAIL_PASSWORD"] = 'Kenneth68'
app.config["MAIL_DEFAULT_SENDER "]='[email protected]'
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
admin = Admin(app)
mail.init_app(app)
def preprocessing(column_data):
ps = PorterStemmer()
corpus = []
for i in range(len(column_data)):
print(i)
review = re.sub('[^a-zA-Z]', ' ', column_data[i])
review = review.lower()
review = review.split()
review = [ps.stem(word)
for word in review if not word in stopwords.words('english')]
review = ' '.join(review)
corpus.append(review)
return corpus
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(80))
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# Implementing user login form
class LoginForm(FlaskForm):
username = StringField('username', validators=[
InputRequired(), Length(min=4, max=15)])
password = PasswordField('password', validators=[
InputRequired(), Length(min=8, max=80)])
remember = BooleanField('remember me')
# Implementing user registration form
class RegisterForm(FlaskForm):
email = StringField('email', validators=[InputRequired(), Email(
message='Invalid email'), Length(max=50)])
username = StringField('username', validators=[
InputRequired(), Length(min=4, max=15)])
password = PasswordField('password', validators=[
InputRequired(), Length(min=8, max=80)])
# Route for user to predict url probability before login
@app.route("/", methods=["GET", "POST"])
def detect():
model = keras.models.load_model("pickles/news_classifier_model.h5")
vectorizer = joblib.load("pickles/data_transformer.joblib")
if request.method == "POST":
url = request.form["textNews"]
urls = [url]
print(urls)
data = pd.DataFrame(data=[urls], columns=["title"])
# print(data.url)
preprocessed_title = vectorizer(data.title)
one_hot_repr_title = [one_hot(words, 5000) for words in preprocessed_title]
embedded_title = pad_sequences(
one_hot_repr_title, padding='pre', maxlen=20)
print("Embeded",embedded_title)
embedded_title=np.array(embedded_title)
prediction_proba = model.predict_proba(embedded_title)[0]
print("Prediction",model.predict(embedded_title))
return render_template("result.html", prediction=prediction_proba)
return render_template("index.html")
# Route for user to predict url probability after login
# route for contact page
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
msg = Message(request.form['subject'], sender="[email protected]",recipients=[
msg.body = """
From:
""" % (request.form['name'], request.form['email'], request.form['message'])
mail.send(msg)
return render_template("contact.html", form=request.form)
elif request.method == 'GET':
return render_template('contact.html', form=request.form)
# Route for login page
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
return redirect(url_for('index'))
return '<h1>Invalid username or password</h1>'
return render_template('login.html', form=form)
# route for login page
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(
form.password.data, method='sha256')
new_user = User(username=form.username.data,
email=form.email.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return render_template('login.html', form=form)
# return '<h1>' + form.username.data + ' ' + form.email.data + ' ' + form.password.data + '</h1>'
return render_template('signup.html', form=form)
# dashboard for user after login
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html', name=current_user.username)
# user logout
@app.route('/logout')
@login_required
def logout():
logout_user()
return render_template('index.html')
class Urls(db.Model):
id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String(1000))
category = db.Column(db.String(10))
admin.add_view(ModelView(Urls, db.session))
admin.add_view(ModelView(User, db.session))
if __name__ == "__main__":
app.run(debug=True, port=5000)