allow for full sorting, slice and dicing with all options in the UI, and the previous shortcuts. now have a lot more flexibility to slice and dice as desired

This commit is contained in:
Andrej Karpathy 2021-11-16 21:39:43 -08:00
parent abb83effed
commit 8be24dae43
4 changed files with 71 additions and 22 deletions

View File

@ -82,11 +82,11 @@ def time_rank():
scores = [(tnow - v['_time'])/60/60/24 for k, v in ms] # time delta in days
return pids, scores
def svm_rank(tags=None, pid=None):
def svm_rank(tags: str = '', pid: str = ''):
# tag can be one tag or a few comma-separated tags or 'all' for all tags we have in db
# pid can be a specific paper id to set as positive for a kind of nearest neighbor search
assert (tags is not None) or (pid is not None)
assert tags or pid
# load all of the features
features = pickle.load(open('features.p', 'rb'))
@ -99,9 +99,9 @@ def svm_rank(tags=None, pid=None):
# construct the positive set
y = np.zeros(n, dtype=np.float32)
if pid is not None:
if pid:
y[ptoi[pid]] = 1.0
elif tags is not None:
elif tags:
tags_db = get_tags()
tags_filter_to = tags_db.keys() if tags == 'all' else set(tags.split(','))
for tag, pids in tags_db.items():
@ -133,7 +133,7 @@ def default_context(papers, **kwargs):
gvars = {}
gvars['search_query'] = ''
gvars['time_filter'] = ''
gvars['message'] = 'default_message'
gvars['message'] = ''
context['gvars'] = gvars
return context
@ -154,14 +154,16 @@ def main():
# GET options
opt_rank = request.args.get('rank', 'time') # rank type. tags|pid|time|random
opt_tags = request.args.get('tags', 'all') # tags to rank by if opt_rank == 'tag'
opt_pid = request.args.get('pid', None) # pid to find nearest neighbors to
opt_tags = request.args.get('tags', '') # tags to rank by if opt_rank == 'tag'
opt_pid = request.args.get('pid', '') # pid to find nearest neighbors to
opt_time_filter = request.args.get('time_filter', '') # number of days to filter by
opt_skip_have = request.args.get('skip_have', 'no') # hide papers we already have?
# rank papers: by tags, by time, by random
if opt_rank in ['tags', 'pid']:
pids, scores = svm_rank(tags=opt_tags, pid=opt_pid)
if opt_rank == 'tags':
pids, scores = svm_rank(tags=opt_tags)
elif opt_rank == 'pid':
pids, scores = svm_rank(pid=opt_pid)
elif opt_rank == 'time':
pids, scores = time_rank()
elif opt_rank == 'random':
@ -193,7 +195,9 @@ def main():
context = default_context(papers)
context['gvars']['rank'] = opt_rank
context['gvars']['tags'] = opt_tags
context['gvars']['pid'] = opt_pid
context['gvars']['time_filter'] = opt_time_filter
context['gvars']['skip_have'] = opt_skip_have
return render_template('index.html', **context)

View File

@ -19,7 +19,7 @@ const Paper = props => {
const subber = () => fetch("/sub/" + p.id + "/" + prompt("tag to subtract from this paper:"))
.then(response => console.log(response.text()));
const utags = p.utags.map((utxt, ix) => <UTag key={ix} tag={utxt} />);
const similar_url = "/?rank=tags&pid=" + p.id;
const similar_url = "/?rank=pid&pid=" + p.id;
return (
<div class='rel_paper'>
<div class="rel_score">{p.weight.toFixed(2)}</div>

View File

@ -124,3 +124,15 @@ body {
background-position: left center;
outline: 0;
}
#cbox {
margin-top: 5px;
font-size: 14px;
}
#cbox_fast {
font-size: 14px;
}
#cbox_fast a {
display: inline-block;
background-color: #eee;
padding: 3px;
}

View File

@ -26,27 +26,60 @@ var gvars = {{ gvars | tojson }};
<div id="controls">
<div>
<!-- the search box, allowing us to search by keywords -->
<div id="sbox">
<form action="/search" method="get">
<input name="q" type="text" id="qfield" value="{{ gvars.search_query }}">
</form>
</div>
<!-- the choice box, allowing us to sort, rank, slice and dice papers -->
<div id="cbox">
<!--
opt_rank = request.args.get('rank', 'tags') # rank type. tags|pid|time|random
opt_tags = request.args.get('tags', 'all') # tags to rank by if opt_rank == 'tag'
opt_pid = request.args.get('pid', None) # pid to find nearest neighbors to
opt_time_filter = request.args.get('time_filter', '') # number of days to filter by
opt_skip_have = request.args.get('skip_have', 'no') # hide papers we already have?
-->
<a href="/?rank=tags&time_filter=7&skip_have=yes">recommend over last week</a><br>
<a href="/?rank=tags&time_filter=3&skip_have=yes">recommend over last 3 days</a><br>
<a href="/?rank=time">recent</a><br>
<a href="/?rank=random&time_filter=7">random last week</a><br>
<form action="/" method="get">
<!-- rank type: one of tags, pid, time, or random -->
<label for="rank_type">Rank by:</label>
<select name="rank" id="rank_select">
<option value="tags" {{ gvars.rank == 'tags' and 'selected' }}>tags</option>
<option value="pid" {{ gvars.rank == 'pid' and 'selected' }}>pid</option>
<option value="time" {{ gvars.rank == 'time' and 'selected' }}>time</option>
<option value="random" {{ gvars.rank == 'random' and 'selected' }}>random</option>
</select>
<!-- current tags, simply in a text field -->
<label for="tags">tags: </label>
<input name="tags" type="text" id="tags_field" value="{{ gvars.tags }}">
<!-- current pid, simply in a text field -->
<label for="pid">pid: </label>
<input name="pid" type="text" id="pid_field" value="{{ gvars.pid }}">
<!-- current time_filter, in a text field -->
<label for="time_filter">time_filter: </label>
<input name="time_filter" type="text" id="time_filter_field" value="{{ gvars.time_filter }}">
<!-- current skip_have: one of yes or no -->
<label for="skip_have">skip_have: </label>
<select name="skip_have" id="skip_have_select">
<option value="yes" {{ gvars.skip_have == 'yes' and 'selected' }}>yes</option>
<option value="no" {{ gvars.skip_have == 'no' and 'selected' }}>no</option>
</select>
<input type="submit" value="Submit">
</form>
</div>
<!-- some hand-coded common choices for faster and more convenient operation -->
<div id="cbox_fast">
Shortcuts:
<a href="/?rank=tags&tags=all&time_filter=7&skip_have=yes">recommend over last week</a>
<a href="/?rank=tags&tags=all&time_filter=3&skip_have=yes">recommend over last 3 days</a>
<a href="/?rank=time">recent</a>
<a href="/?rank=random&time_filter=7">random last week</a>
</div>
</div>
<div>
</div>
<div id="message">
{{gvars.message}}