forked from karpathy/arxiv-sanity-preserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
496 lines (432 loc) · 17.9 KB
/
serve.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
from sqlite3 import dbapi2 as sqlite3
from hashlib import md5
from flask import Flask, request, session, url_for, redirect, \
render_template, abort, g, flash, _app_ctx_stack
from flask_limiter import Limiter
from werkzeug import check_password_hash, generate_password_hash
import cPickle as pickle
import numpy as np
import json
import time
import dateutil.parser
import argparse
from random import shuffle
import re
import os
# database configuration
DATABASE = 'as.db'
if os.path.isfile('secret_key.txt'):
SECRET_KEY = open('secret_key.txt', 'r').read()
else:
SECRET_KEY = 'devkey, should be in a file'
app = Flask(__name__)
app.config.from_object(__name__)
limiter = Limiter(app, global_limits=["100 per hour", "20 per minute"])
SEARCH_DICT = {}
# -----------------------------------------------------------------------------
# utilities for database interactions
# -----------------------------------------------------------------------------
# to initialize the database: sqlite3 as.db < schema.sql
def connect_db():
sqlite_db = sqlite3.connect(DATABASE)
sqlite_db.row_factory = sqlite3.Row # to return dicts rather than tuples
return sqlite_db
@app.before_request
def before_request():
# this will always request database connection, even if we dont end up using it ;\
g.db = connect_db()
# retrieve user object from the database if user_id is set
g.user = None
if 'user_id' in session:
g.user = query_db('select * from user where user_id = ?',
[session['user_id']], one=True)
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
def query_db(query, args=(), one=False):
"""Queries the database and returns a list of dictionaries."""
cur = g.db.execute(query, args)
rv = cur.fetchall()
return (rv[0] if rv else None) if one else rv
def get_user_id(username):
"""Convenience method to look up the id for a username."""
rv = query_db('select user_id from user where username = ?',
[username], one=True)
return rv[0] if rv else None
def get_username(user_id):
"""Convenience method to look up the username for a user."""
rv = query_db('select username from user where user_id = ?',
[user_id], one=True)
return rv[0] if rv else None
# -----------------------------------------------------------------------------
# search/sort functionality
# -----------------------------------------------------------------------------
def papers_shuffle():
ks = db.keys()
shuffle(ks)
return [db[k] for k in ks]
def date_sort():
scores = []
for pid in db:
p = db[pid]
timestruct = dateutil.parser.parse(p['updated'])
p['time_updated'] = int(timestruct.strftime("%s"))
scores.append((p['time_updated'], p))
scores.sort(reverse=True)
out = [sp[1] for sp in scores]
return out
def papers_search(qraw):
qparts = qraw.lower().strip().split() # split by spaces
# use reverse index and accumulate scores
scores = []
for pid in db:
p = db[pid]
score = sum(SEARCH_DICT[pid].get(q,0) for q in qparts)
if score == 0:
continue # no match whatsoever, dont include
# give a small boost to more recent papers
score += 0.0001*p['tscore']
scores.append((score, p))
scores.sort(reverse=True) # descending
out = [x[1] for x in scores if x[0] > 0]
return out
def strip_version(idstr):
""" identity function if arxiv id has no version, otherwise strips it. """
parts = idstr.split('v')
return parts[0]
def papers_similar(pid):
rawpid = strip_version(pid)
# check if we have this paper at all, otherwise return empty list
if not rawpid in db:
return []
# check if we have distances to this specific version of paper id (includes version)
if pid in sim_dict:
# good, simplest case: lets return the papers
return [db[strip_version(k)] for k in sim_dict[pid]]
else:
# ok we don't have this specific version. could be a stale URL that points to,
# e.g. v1 of a paper, but due to an updated version of it we only have v2 on file
# now. We want to use v2 in that case.
# lets try to retrieve the most recent version of this paper we do have
ks = sim_dict.keys()
kok = [k for k in sim_dict.iterkeys() if rawpid in k]
if kok:
# ok we have at least one different version of this paper, lets use it instead
id_use_instead = kok[0]
return [db[strip_version(k)] for k in sim_dict[id_use_instead]]
else:
# return just the paper. we dont have similarities for it for some reason
return [db[rawpid]]
def papers_from_library():
out = []
if g.user:
# user is logged in, lets fetch their saved library data
uid = session['user_id']
user_library = query_db('''select * from library where user_id = ?''', [uid])
libids = [strip_version(x['paper_id']) for x in user_library]
out = [db[x] for x in libids]
out = sorted(out, key=lambda k: k['updated'], reverse=True)
return out
def papers_from_svm(recent_days=None):
out = []
if g.user:
uid = session['user_id']
if not uid in user_sim:
return []
user_library = query_db('''select * from library where user_id = ?''', [uid])
libids = [strip_version(x['paper_id']) for x in user_library]
plist = user_sim[uid]
out = [db[x] for x in plist if not x in libids]
if recent_days is not None:
# filter as well to only most recent papers
curtime = int(time.time()) # in seconds
out = [x for x in out if curtime - x['time_updated'] < recent_days*24*60*60]
return out
def papers_filter_version(papers, v):
if v != '1':
return papers # noop
intv = int(v)
filtered = [p for p in papers if p['_version'] == intv]
return filtered
def encode_json(ps, n=10, send_images=True, send_abstracts=True):
libids = []
if g.user:
# user is logged in, lets fetch their saved library data
uid = session['user_id']
user_library = query_db('''select * from library where user_id = ?''', [uid])
libids = [strip_version(x['paper_id']) for x in user_library]
ret = []
for i in xrange(min(len(ps),n)):
p = ps[i]
idvv = '%sv%d' % (p['_rawid'], p['_version'])
struct = {}
struct['title'] = p['title']
struct['pid'] = idvv
struct['category'] = p['arxiv_primary_category']['term']
struct['authors'] = [a['name'] for a in p['authors']]
struct['link'] = p['link']
struct['in_library'] = 1 if p['_rawid'] in libids else 0
if send_abstracts:
struct['abstract'] = p['summary']
if send_images:
struct['img'] = '/static/thumbs/' + idvv + '.pdf.jpg'
struct['tags'] = [t['term'] for t in p['tags']]
timestruct = dateutil.parser.parse(p['updated'])
struct['published_time'] = '%s/%s/%s' % (timestruct.month, timestruct.day, timestruct.year)
timestruct = dateutil.parser.parse(p['published'])
struct['originally_published_time'] = '%s/%s/%s' % (timestruct.month, timestruct.day, timestruct.year)
cc = p.get('arxiv_comment', '')
if len(cc) > 100:
cc = cc[:100] + '...' # crop very long comments
struct['comment'] = cc
ret.append(struct)
return ret
# -----------------------------------------------------------------------------
# flask request handling
# -----------------------------------------------------------------------------
# "1511.08198v1" is an example of a valid arxiv id that we accept
def isvalidid(pid):
return re.match('^\d+\.\d+(v\d+)?$', pid)
@app.route("/")
def intmain():
vstr = request.args.get('vfilter', 'all')
papers = DATE_SORTED_PAPERS # precomputed
papers = papers_filter_version(papers, vstr)
ret = encode_json(papers, args.num_results)
msg = 'Showing most recent Arxiv papers:'
return render_template('main.html', papers=ret, numpapers=len(db), msg=msg, render_format='recent')
@app.route("/<request_pid>")
def rank(request_pid=None):
if not isvalidid(request_pid):
return '' # these are requests for icons, things like robots.txt, etc
papers = papers_similar(request_pid)
ret = encode_json(papers, args.num_results) # encode the top few to json
return render_template('main.html', papers=ret, numpapers=len(db), msg='', render_format='paper')
@app.route("/search", methods=['GET'])
def search():
q = request.args.get('q', '') # get the search request
papers = papers_search(q) # perform the query and get sorted documents
ret = encode_json(papers, args.num_results) # encode the top few to json
return render_template('main.html', papers=ret, numpapers=len(db), msg='', render_format="search") # weeee
@app.route('/recommend', methods=['GET'])
def recommend():
""" return user's svm sorted list """
ttstr = request.args.get('timefilter', 'week') # default is week
vstr = request.args.get('vfilter', 'all') # default is all (no filter)
legend = {'day':1, '3days':3, 'week':7, 'month':30, 'year':365}
tt = legend.get(ttstr, None)
papers = papers_from_svm(recent_days=tt)
papers = papers_filter_version(papers, vstr)
ret = encode_json(papers, args.num_results)
msg = 'Recommended papers: (based on SVM trained on tfidf of papers in your library, refreshed every day or so)' if g.user else 'You must be logged in and have some papers saved in your library.'
return render_template('main.html', papers=ret, numpapers=len(db), msg=msg, render_format='recommend')
@app.route('/top', methods=['GET'])
def top():
""" return top papers """
ttstr = request.args.get('timefilter', 'week') # default is week
vstr = request.args.get('vfilter', 'all') # default is all (no filter)
legend = {'day':1, '3days':3, 'week':7, 'month':30, 'year':365, 'alltime':10000}
tt = legend.get(ttstr, 7)
curtime = int(time.time()) # in seconds
papers = [p for p in TOP_SORTED_PAPERS if curtime - p['time_updated'] < tt*24*60*60]
papers = papers_filter_version(papers, vstr)
ret = encode_json(papers, args.num_results)
msg = 'Top papers based on people\'s libraries:'
return render_template('main.html', papers=ret, numpapers=len(db), msg=msg, render_format='top')
@app.route('/library')
def library():
""" render user's library """
papers = papers_from_library()
ret = encode_json(papers, 500) # cap at 500 papers in someone's library. that's a lot!
if g.user:
msg = '%d papers in your library:' % (len(ret), )
else:
msg = 'You must be logged in. Once you are, you can save papers to your library (with the save icon on the right of each paper) and they will show up here.'
return render_template('main.html', papers=ret, numpapers=len(db), msg=msg, render_format='library')
@app.route('/libtoggle', methods=['POST'])
def review():
""" user wants to toggle a paper in his library """
# make sure user is logged in
if not g.user:
return 'NO' # fail... (not logged in). JS should prevent from us getting here.
idvv = request.form['pid'] # includes version
if not isvalidid(idvv):
return 'NO' # fail, malformed id. weird.
pid = strip_version(idvv)
if not pid in db:
return 'NO' # we don't know this paper. wat
uid = session['user_id'] # id of logged in user
# check this user already has this paper in library
record = query_db('''select * from library where
user_id = ? and paper_id = ?''', [uid, pid], one=True)
print record
ret = 'NO'
if record:
# record exists, erase it.
g.db.execute('''delete from library where user_id = ? and paper_id = ?''', [uid, pid])
g.db.commit()
#print 'removed %s for %s' % (pid, uid)
ret = 'OFF'
else:
# record does not exist, add it.
rawpid = strip_version(pid)
g.db.execute('''insert into library (paper_id, user_id, update_time) values (?, ?, ?)''',
[rawpid, uid, int(time.time())])
g.db.commit()
#print 'added %s for %s' % (pid, uid)
ret = 'ON'
return ret
@app.route('/login', methods=['POST'])
def login():
""" logs in the user. if the username doesn't exist creates the account """
if not request.form['username']:
flash('You have to enter a username')
elif not request.form['password']:
flash('You have to enter a password')
elif get_user_id(request.form['username']) is not None:
# username already exists, fetch all of its attributes
user = query_db('''select * from user where
username = ?''', [request.form['username']], one=True)
if check_password_hash(user['pw_hash'], request.form['password']):
# password is correct, log in the user
session['user_id'] = get_user_id(request.form['username'])
flash('User ' + request.form['username'] + ' logged in.')
else:
# incorrect password
flash('User ' + request.form['username'] + ' already exists, wrong password.')
else:
# create account and log in
creation_time = int(time.time())
g.db.execute('''insert into user (username, pw_hash, creation_time) values (?, ?, ?)''',
[request.form['username'],
generate_password_hash(request.form['password']),
creation_time])
user_id = g.db.execute('select last_insert_rowid()').fetchall()[0][0]
g.db.commit()
session['user_id'] = user_id
flash('New account %s created' % (request.form['username'], ))
return redirect(url_for('intmain'))
@app.route('/logout')
def logout():
session.pop('user_id', None)
flash('You were logged out')
return redirect(url_for('intmain'))
# -----------------------------------------------------------------------------
# int main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--prod', dest='prod', action='store_true', help='run in prod?')
parser.add_argument('-r', '--num_results', dest='num_results', type=int, default=200, help='number of results to return per query')
parser.add_argument('--port', dest='port', type=int, default=5000, help='port to serve on')
args = parser.parse_args()
print args
print 'loading db.p...'
db = pickle.load(open('db.p', 'rb'))
print 'loading tfidf_meta.p...'
meta = pickle.load(open("tfidf_meta.p", "rb"))
vocab = meta['vocab']
idf = meta['idf']
print 'loading sim_dict.p...'
sim_dict = pickle.load(open("sim_dict.p", "rb"))
print 'loading user_sim.p...'
if os.path.isfile('user_sim.p'):
user_sim = pickle.load(open('user_sim.p', 'rb'))
else:
user_sim = {}
print 'precomputing papers date sorted...'
DATE_SORTED_PAPERS = date_sort()
if not os.path.isfile(DATABASE):
print 'did not find as.db, trying to create an empty database from schema.sql...'
print 'this needs sqlite3 to be installed!'
os.system('sqlite3 as.db < schema.sql')
# compute top papers in peoples' libraries
print 'computing top papers...'
def get_popular():
sqldb = sqlite3.connect(DATABASE)
sqldb.row_factory = sqlite3.Row # to return dicts rather than tuples
libs = sqldb.execute('''select * from library''').fetchall()
counts = {}
for lib in libs:
pid = lib['paper_id']
counts[pid] = counts.get(pid, 0) + 1
return counts
top_counts = get_popular()
top_paper_counts = sorted([(v,k) for k,v in top_counts.iteritems() if v > 0], reverse=True)
print top_paper_counts[:min(30, len(top_paper_counts))]
TOP_SORTED_PAPERS = [db[q[1]] for q in top_paper_counts]
# compute min and max time for all papers
tts = [time.mktime(dateutil.parser.parse(p['updated']).timetuple()) for pid,p in db.iteritems()]
ttmin = min(tts)*1.0
ttmax = max(tts)*1.0
for pid,p in db.iteritems():
tt = time.mktime(dateutil.parser.parse(p['updated']).timetuple())
p['tscore'] = (tt-ttmin)/(ttmax-ttmin)
# some utilities for creating a search index for faster search
punc = "'!\"#$%&\'()*+,./:;<=>?@[\\]^_`{|}~'" # removed hyphen from string.punctuation
trans_table = {ord(c): None for c in punc}
def makedict(s, forceidf=None, scale=1.0):
words = set(s.lower().translate(trans_table).strip().split())
out = {}
for w in words: # todo: if we're using bigrams in vocab then this won't search over them
if forceidf is None:
if w in vocab:
# we have idf for this
idfval = idf[vocab[w]]*scale
else:
idfval = 1.0*scale # assume idf 1.0 (low)
else:
idfval = forceidf
out[w] = idfval
return out
def merge_dicts(dlist):
out = {}
for d in dlist:
for k,v in d.iteritems():
out[k] = out.get(k,0) + v
return out
# caching: check if db.p is younger than search_dict.p
recompute_index = True
if os.path.isfile('search_dict.p'):
db_modified_time = os.path.getmtime('db.p')
search_modified_time = os.path.getmtime('search_dict.p')
if search_modified_time > db_modified_time:
# search index exists and is more recent, no need
recompute_index = False
if recompute_index:
print 'building an index for faster search...'
for pid in db:
p = db[pid]
dict_title = makedict(p['title'], forceidf=5, scale=3)
dict_authors = makedict(' '.join(x['name'] for x in p['authors']), forceidf=5)
dict_categories = {x['term'].lower():5 for x in p['tags']}
if 'and' in dict_authors:
# special case for "and" handling in authors list
del dict_authors['and']
dict_summary = makedict(p['summary'])
SEARCH_DICT[pid] = merge_dicts([dict_title, dict_authors, dict_categories, dict_summary])
# and cache it in file
print 'writing search_dict.p as cache'
pickle.dump(SEARCH_DICT, open('search_dict.p', 'wb'), -1)
else:
print 'loading cached index for faster search...'
SEARCH_DICT = pickle.load(open('search_dict.p', 'rb'))
# start
if args.prod:
# run on Tornado instead, since running raw Flask in prod is not recommended
print 'starting tornado!'
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.log import enable_pretty_logging
enable_pretty_logging()
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(args.port)
IOLoop.instance().start()
#app.run(host='0.0.0.0', threaded=True)
else:
print 'starting flask!'
app.debug = True
app.run(port=args.port)