-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
358 lines (320 loc) · 13.4 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from flask_login import LoginManager, login_required, login_user, logout_user, user_logged_out, current_user
from flask import Flask, render_template, redirect, url_for, flash, session, request
from wtforms import SubmitField, PasswordField, EmailField
from wtforms.validators import DataRequired, Email, Length
from flask_wtf import FlaskForm
from database import db, User, Paper
from flask_session import Session
from arxiv_scraper import get_papers, vectorizer
from datetime import datetime, timedelta, timezone
from apscheduler.schedulers.background import BackgroundScheduler
from recommender import update_user_profile, cosine
from math import ceil
import logging
import bcrypt
import json
import atexit
import os
app = Flask(__name__)
# ---------------------------------------------------------
# App configuration
app.config.from_file('config.json', load=json.load)
if not app.debug:
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s | %(filename)s:%(lineno)s:%(levelname)s | %(message)s'
)
# Creating server session to store account info of users that didn't complete the sign up
app.config["SESSION_SQLALCHEMY"] = db
Session(app)
db.init_app(app)
# ---------------------------------------------------------
# Flask-login stuff
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
@login_manager.user_loader
def user_loader(user_id):
return db.session.get(User, user_id)
# Remove session variables when logging out, it prevents someone from using a loophole to login without password through interests page
@user_logged_out.connect
def remove_session(*e, **extra):
session.pop('email', None)
# ---------------------------------------------------------
# Scheduling the arXiv scraper
ARXIV_TIMEZONE = timezone.utc
def download_papers():
"""Download all the papers released since yesterday."""
with app.app_context():
today = datetime.now(ARXIV_TIMEZONE)
yesterday = today - timedelta(days=1)
yesterday = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
get_papers(yesterday)
# This prevents scheduling the function twice in the debug mode. More info: https://stackoverflow.com/questions/14874782/apscheduler-in-flask-executes-twice
if not app.debug and os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
scheduler = BackgroundScheduler()
# Download papers everyday at 00:30 AM in arxiv timezone (they work in 24 hour cycles)
scheduler.add_job(download_papers, trigger="cron", hour=0, minute=30, timezone=ARXIV_TIMEZONE)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
# ---------------------------------------------------------
# Forms
class LoginForm(FlaskForm):
email = EmailField("Email", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired(), Length(min=8, max=72)])
login_submit = SubmitField("Login")
class SignUpForm(FlaskForm):
email = EmailField("Email", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired(), Length(min=8, max=72)])
repeat_password = PasswordField("Repeat password", validators=[DataRequired(), Length(min=8, max=72)])
sign_up_submit = SubmitField("Sign up")
def validate(self, **kwargs):
validators = FlaskForm.validate(self)
if validators and self.password.data == self.repeat_password.data:
return True
return False
# ---------------------------------------------------------
# Endpoints
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = db.session.get(User, form.email.data)
if not user:
flash("Couldn't find your account")
elif not bcrypt.checkpw(form.password.data.encode('utf8'), user.password):
flash("Incorrect password")
elif len(user.vector) == 0:
flash("You haven't completed the sign up yet")
session['email'] = form.email.data
return redirect(url_for('interests'))
else:
user.auth = True
db.session.commit()
login_user(user, remember=True)
flash("Logged in successfully")
logging.info(f"User with email: {user.email} logged in")
return redirect(url_for('home_page', page=1))
return render_template("login.html", login_form=form)
@app.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
form = SignUpForm()
if form.validate_on_submit():
new_user = User(
email = form.email.data,
password = bcrypt.hashpw(form.password.data.encode('utf8'), bcrypt.gensalt()),
auth=False,
vector=dict(),
)
db.session.add(new_user)
try:
db.session.commit()
flash("Successfully added a new user")
logging.info(f"User with email: {new_user.email} created a new account")
session['email'] = form.email.data
return redirect(url_for('interests'))
except:
db.session.rollback()
flash("Something went wrong, try again")
return render_template("sign_up.html", sign_up_form=form)
# The labels for the interests page form and their tokens after text normalization
LABELS = [
"NLP", "Transformers", "Neural networks", "Robotics", "Deep learning", "Optimization", "Computer vision", "Supervised learning",
"Unsupervised learning", "Reinforcement learning"
]
@app.route('/interests', methods=['GET', 'POST'])
def interests():
if 'email' not in session:
flash("You cannot access this page")
return redirect(url_for('login'))
if request.method == 'POST':
at_least_one_toggled = False
# Getting data from the form to the database
analyzer = vectorizer.build_analyzer()
vector = dict()
for chip in request.form:
if chip != "interests_submit" and request.form[chip] == 'on':
at_least_one_toggled = True
tokens = analyzer(chip)
for token in tokens:
vector[token] = 1
if not at_least_one_toggled:
flash("You must select at least one field")
else:
user = db.session.get(User, session['email'])
user.vector = vector
try:
db.session.commit()
flash("Updated interests")
except:
db.session.rollback()
flash("Something went wrong, try again")
login_user(user, remember=True)
session.pop('email', None)
return redirect(url_for('home_page', page=1))
return render_template("interests.html", interests_form=LABELS)
# This filter is used to format dates nicely on the home page
@app.template_filter("display_date")
def display_date(date):
months = [
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
return months[date.month-1] + " " + str(date.year)
TIME_OPTIONS_TABLE = ["Day", "Week", "2 Weeks", "Month", "3 Months", "6 Months", "Year"]
PAGE_LENGTH = 20
@app.route('/', methods=['GET', 'POST'])
@login_required
def home_page():
if request.method == 'POST':
# The user liked an article
for name in request.json:
paper = db.session.get(Paper, int(name))
if request.json[name]:
current_user.liked_papers.append(paper)
current_user.vector = update_user_profile(current_user.vector, paper.vector, 0.95, 0.05, 0.02)
else:
current_user.liked_papers.remove(paper)
current_user.vector = update_user_profile(current_user.vector, paper.vector, 1/0.95, -0.05, 0)
try:
db.session.commit()
except:
db.session.rollback()
# Getting the named parameters from the URL
time_option = request.args.get("time", default=0, type=int)
sort_option = request.args.get("sort", default="Relevance", type=str)
page = request.args.get('page', default=1, type=int)
# Filtering the results by time period
match time_option:
case 0:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(days=2)).all()
case 1:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=1)).all()
case 2:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=2)).all()
case 3:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=4)).all()
case 4:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=13)).all()
case 5:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=26)).all()
case 6:
papers = Paper.query.filter(Paper.updated_date >= datetime.now() - timedelta(weeks=52)).all()
case _:
flash("Wrong URL")
return redirect(url_for('home_page'))
# Assigning relevance scores to papers
papers = [[p, cosine(current_user.vector, p.vector)] for p in papers]
# Sort the papers
match sort_option:
case "Relevance":
papers.sort(key=lambda x: x[1], reverse=True)
case "Date":
papers.sort(key=lambda x: x[0].updated_date, reverse=True)
case _:
flash("Wrong URL")
return redirect(url_for('home_page'))
# Assigning correct page numbers
page_number_1 = page - 1
page_number_2 = page
page_number_3 = page + 1
if page == 1:
page_number_1 = page
page_number_2 = page + 1
page_number_3 = page + 2
if page == ceil(len(papers) / PAGE_LENGTH):
page_number_1 = page - 2
page_number_2 = page - 1
page_number_3 = page
return render_template(
"index.html",
current_page=page,
page_number_1=page_number_1,
page_number_2=page_number_2,
page_number_3=page_number_3,
number_of_pages=ceil(len(papers) / PAGE_LENGTH),
papers=[x[0] for x in papers][(page-1)*PAGE_LENGTH:page*PAGE_LENGTH],
relevances=[x[1] for x in papers][(page-1)*PAGE_LENGTH:page*PAGE_LENGTH],
time_options=TIME_OPTIONS_TABLE,
time=time_option,
sort=sort_option,
# Passing the zip function, bacause the jinja engine doesn't import it by default
zip=zip
)
@app.route('/search', methods=['GET'])
@login_required
def search():
# Getting the named parameters from the URL
sort_option = request.args.get("sort", default="Relevance", type=str)
page = request.args.get('page', default=1, type=int)
query = request.args.get('query', type=str)
if len(query) == 0:
flash("Wrong query")
return redirect(url_for('home_page'))
analyzer = vectorizer.build_analyzer()
query_vector = analyzer(query)
vector = {}
for token in query_vector:
if token in vector:
vector[token] += 1
else:
vector[token] = 1
papers = Paper.query.all()
# Assigning relevance scores to papers
papers = [[p, cosine(vector, p.vector)] for p in papers]
# Sort the papers
match sort_option:
case "Relevance":
papers.sort(key=lambda x: x[1], reverse=True)
case "Date":
papers.sort(key=lambda x: x[0].updated_date, reverse=True)
case _:
flash("Wrong URL")
return redirect(url_for('home_page'))
# Assigning correct page numbers
page_number_1 = page - 1
page_number_2 = page
page_number_3 = page + 1
if page == 1:
page_number_1 = page
page_number_2 = page + 1
page_number_3 = page + 2
if page == ceil(len(papers) / PAGE_LENGTH):
page_number_1 = page - 2
page_number_2 = page - 1
page_number_3 = page
return render_template(
"search.html",
query=query,
current_page=page,
page_number_1=page_number_1,
page_number_2=page_number_2,
page_number_3=page_number_3,
number_of_pages=ceil(len(papers) / PAGE_LENGTH),
papers=[x[0] for x in papers][(page-1)*PAGE_LENGTH:page*PAGE_LENGTH],
relevances=[x[1] for x in papers][(page-1)*PAGE_LENGTH:page*PAGE_LENGTH],
time_options=TIME_OPTIONS_TABLE,
sort=sort_option,
# Passing the zip function, bacause the jinja engine doesn't import it by default
zip=zip
)
@app.route('/logout')
@login_required
def logout():
logging.info(f"User with email: {current_user.email} logged out")
logout_user()
flash("Logged out successfully")
return redirect(url_for("login"))
# Interest page needs seperate logout route, because after signing up the user is redirected to the interests page
# and only after going through the form they are logged in. So if the user wants to logout from the interests page
# Their email needs to be deleted from the session, because that's the way the server remembers them.
@app.route('/logout-interest')
def logout_interests():
session.pop('email', None)
flash("Logged out successfully")
return redirect(url_for("login"))
@app.route('/about')
def about():
if current_user.is_authenticated:
return render_template('about_logged_in.html')
else:
return render_template('about_new_user.html')