import gradio as gr
import os
from transformers import AutoTokenizer, AutoModel
from sentence_transformers import SentenceTransformer
import pickle
import nltk
nltk.download('punkt') # tokenizer
nltk.download('averaged_perceptron_tagger') # postagger
import time
from input_format import *
from score import *
# load document scoring model
#torch.cuda.is_available = lambda : False # uncomment to test with CPU only
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#pretrained_model = 'allenai/specter'
pretrained_model = 'allenai/specter2'
tokenizer = AutoTokenizer.from_pretrained(pretrained_model)
doc_model = AutoModel.from_pretrained(pretrained_model)
doc_model.to(device)
# load sentence model
sent_model = doc_model # have the same model for document and sentence level
# OR specify different model for sentence level
#sent_model = SentenceTransformer('sentence-transformers/gtr-t5-base')
#sent_model.to(device)
def get_similar_paper(
title_input,
abstract_text_input,
author_id_input,
results={}, # this state variable will be updated and returned
):
progress = gr.Progress()
num_papers_show = 10 # number of top papers to show from the reviewer
if title_input == None:
title_input = '' # if no title is given, just focus on abstract.
print('retrieving similar papers...')
start = time.time()
input_sentences = sent_tokenize(abstract_text_input)
# Get author papers from id
#progress(0.1, desc="Retrieving reviewer papers ...")
name, papers = get_text_from_author_id(author_id_input)
# Compute Doc-level affinity scores for the Papers
# print('computing document scores...')
#progress(0.5, desc="Computing document scores...")
# TODO detect duplicate papers?
titles, abstracts, paper_urls, doc_scores = compute_document_score(
doc_model,
tokenizer,
title_input,
abstract_text_input,
papers,
batch=10
)
results = {
'name': name,
'titles': titles,
'abstracts': abstracts,
'urls': paper_urls,
'doc_scores': doc_scores
}
# Select top K choices of papers to show
titles = titles[:num_papers_show]
abstracts = abstracts[:num_papers_show]
doc_scores = doc_scores[:num_papers_show]
paper_urls = paper_urls[:num_papers_show]
display_title = ['[ %0.3f ] %s'%(s, t) for t, s in zip(titles, doc_scores)]
end = time.time()
retrieval_time = end - start
print('paper retrieval complete in [%0.2f] seconds'%(retrieval_time))
progress(0.9, desc="Obtaining relevant information from the papers...")
print('obtaining highlights..')
start = time.time()
input_sentences = sent_tokenize(abstract_text_input)
num_input_sents = len(input_sentences)
for aa, (tt, ab, ds, url) in enumerate(zip(titles, abstracts, doc_scores, paper_urls)):
# Compute sent-level and phrase-level affinity scores for each papers
sent_ids, sent_scores, info, top_pairs_info = get_highlight_info(
sent_model,
tokenizer,
abstract_text_input,
ab,
K=None, # top two sentences from the candidate
top_pair_num=3, # top five sentence pairs to show upfront
)
num_cand_sents = sent_ids.shape[1]
# get scores for each word in the format for Gradio Interpretation component
word_scores = dict()
for i in range(num_input_sents):
word_scores[str(i)] = dict()
for j in range(1, num_cand_sents+1):
ww, ss = remove_spaces(info['all_words'], info[i][j]['scores'])
word_scores[str(i)][str(j)] = {
"original": ab,
"interpretation": list(zip(ww, ss))
}
results[display_title[aa]] = {
'title': tt,
'abstract': ab,
'num_cand_sents': num_cand_sents,
'doc_score': '%0.3f'%ds,
'source_sentences': input_sentences,
'highlight': word_scores,
'top_pairs': top_pairs_info,
'url': url
}
end = time.time()
highlight_time = end - start
print('done in [%0.2f] seconds'%(highlight_time))
# debugging only
pickle.dump(results, open('info.pkl', 'wb'))
## Set up output elements
# first the list of top papers, sentences to select from, paper_title, affinity
title = results[display_title[0]]['title'] # set default title as the top paper
url = results[display_title[0]]['url']
aff_score = results[display_title[0]]['doc_score']
title_out = """%s
"""%(url, title)
aff_score_out = '##### Affinity Score: %s'%aff_score
out = [
gr.update(choices=display_title, value=display_title[0], interactive=True), # set of papers (radio)
gr.update(choices=input_sentences, value=input_sentences[0], interactive=True), # submission sentences
gr.update(value=title_out), # paper_title
gr.update(value=aff_score_out) # affinity
]
# set up elements to visualize upfront
top_papers_show = 3 # number of top papers to show upfront
top_num_info_show = 2 # number of sentence pairs from each paper to show upfront
summary_out = []
for i in range(top_papers_show):
if i == 0:
out_tmp = [
gr.update(value="""%s
"""%(paper_urls[i], titles[i]), visible=True),
gr.update(value="""#### Affinity Score: %0.3f
Measures how similar the paper's abstract is to the submission abstract.
"""%doc_scores[i],
visible=True) # document affinity
]
else:
out_tmp = [
gr.update(value="""%s
"""%(paper_urls[i], titles[i]), visible=True),
gr.update(value='#### Affinity Score: %0.3f'%doc_scores[i], visible=True) # document affinity
]
tp = results[display_title[i]]['top_pairs']
for j in range(top_num_info_show):
if i == 0 and j == 0:
out_tmp += [
gr.update(value="""Sentence Relevance:\n%0.3f
Measures how similar the sentence pairs are.
"""%tp[j]['score'], visible=True), # sentence relevance
tp[j]['query']['original'],
tp[j]['query'],
tp[j]['candidate']['original'],
tp[j]['candidate']
]
else:
out_tmp += [
gr.update(value='Sentence Relevance:\n%0.3f'%tp[j]['score'], visible=True), # sentence relevance
tp[j]['query']['original'],
tp[j]['query'],
tp[j]['candidate']['original'],
tp[j]['candidate']
]
summary_out += out_tmp
# add updates to the show more button
out = out + summary_out + [gr.update(visible=True)] # make show more button visible
assert(len(out) == (top_num_info_show * 5 + 2) * top_papers_show + 5)
out += [gr.update(value="""
Top three relevant papers by the reviewer %s
For each paper, two sentence pairs (one from the submission, one from the paper) with the highest relevance scores are shown.
**Blue highlights**: phrases that appear in both sentences.
"""%(author_id_input, results['name']),
visible=True)] # result 1 description
out += [gr.update(visible=True), gr.update(visible=True)] # demarcation line between results
# progress status
out += [gr.update(value='Done (in %0.1f seconds)'%(retrieval_time+highlight_time), visible=True)]
# result 2 description
desc = """
##### Click a paper by %s on the left (sorted by affinity scores), and a sentence from the submission on the right, to see which parts of the paper are relevant.
"""%results['name']
out += [gr.update(value=desc)]
# slider to control the number of highlights
out += [gr.update(value=1, maximum=len(sent_tokenize(abstracts[0])))]
# finally add the search results to pass on to the Gradio State varaible
out += [results]
return tuple(out)
def show_more(info):
# show the interactive part of the app
return (
gr.update(visible=True), # description
gr.update(visible=True), # set of papers
gr.update(visible=True), # submission sentences
gr.update(visible=True), # title row
gr.update(visible=True), # affinity row
gr.update(visible=True), # highlight legend
gr.update(visible=True), # highlight slider
gr.update(visible=True), # highlight abstract
)
def show_status():
# show search status field when search button is clicked
return gr.update(visible=True)
def update_name(author_id_input):
# update the name of the author based on the id input
name, _ = get_text_from_author_id(author_id_input)
return gr.update(value=name)
def change_sentence(
selected_papers_radio,
source_sent_choice,
highlight_slider,
info={}
):
# change the output highlight based on the sentence selected from the submission
if len(info.keys()) != 0: # if the info is not empty
source_sents = info[selected_papers_radio]['source_sentences']
highlights = info[selected_papers_radio]['highlight']
idx = source_sents.index(source_sent_choice)
return highlights[str(idx)][str(highlight_slider)]
else:
return
def change_paper(
selected_papers_radio,
source_sent_choice,
highlight_slider,
info={}
):
if len(info.keys()) != 0: # if the info is not empty
source_sents = info[selected_papers_radio]['source_sentences']
title = info[selected_papers_radio]['title']
num_sents = info[selected_papers_radio]['num_cand_sents']
abstract = info[selected_papers_radio]['abstract']
aff_score = info[selected_papers_radio]['doc_score']
highlights = info[selected_papers_radio]['highlight']
url = info[selected_papers_radio]['url']
title_out = """%s
"""%(url, title)
aff_score_out = '##### Affinity Score: %s'%aff_score
idx = source_sents.index(source_sent_choice)
if highlight_slider <= num_sents:
return title_out, abstract, aff_score_out, highlights[str(idx)][str(highlight_slider)], gr.update(value=highlight_slider, maximum=num_sents)
else: # if the slider is set to more than the current number of sentences, show the max number of highlights
return title_out, abstract, aff_score_out, highlights[str(idx)][str(num_sents)], gr.update(value=num_sents, maximum=num_sents)
else:
return
def change_num_highlight(
selected_papers_radio,
source_sent_choice,
highlight_slider,
info={}
):
if len(info.keys()) != 0: # if the info is not empty
source_sents = info[selected_papers_radio]['source_sentences']
highlights = info[selected_papers_radio]['highlight']
idx = source_sents.index(source_sent_choice)
return highlights[str(idx)][str(highlight_slider)]
else:
return
with gr.Blocks(css='style.css') as demo:
info = gr.State({}) # cached search results as a State variable shared throughout
# Text description about the app and disclaimer
### TEXT Description
# General instruction
general_instruction = """
# R2P2: An Assistance Tool for Reviewer-Paper Matching in Peer Review
#### Who is it for?
It is for meta-reviewers, area chairs, program chairs, or anyone who oversees the submission-reviewer matching process in peer review for academic conferences, journals, and grants.
#### How does it help?
A typical meta-reviewer workflow lacks supportive information on **what makes the pre-selected candidate reviewers a good fit** for the submission. Only affinity scores between the reviewer and the paper are shown, without additional detail.
R2P2 provides more information about each reviewer. It searches for the **most relevant papers** among the reviewer's previous publications and **highlights relevant parts** within them.
"""
# TODO add instruction video link
# More details (video, addendum)
more_details_instruction = """Check out this video for a quick demo of what R2P2 is and how it can help. You can find more details here, along with our privacy policy and disclaimer."""
gr.Markdown(general_instruction)
gr.HTML(more_details_instruction)
gr.Markdown("""---""")
### INPUT
with gr.Row() as input_row:
with gr.Column(scale=3):
with gr.Row():
title_input = gr.Textbox(label='Submission Title', info='Paste in the title of the submission.')
with gr.Row():
abstract_text_input = gr.Textbox(label='Submission Abstract', info='Paste in the abstract of the submission.')
with gr.Column(scale=2):
with gr.Row():
author_id_input = gr.Textbox(label='Reviewer Profile Link (Semantic Scholar)', info="Paste in the reviewer's Semantic Scholar link")
with gr.Row():
name = gr.Textbox(label='Confirm Reviewer Name', info='This will be automatically updated based on the reviewer profile link above', interactive=False)
author_id_input.change(fn=update_name, inputs=author_id_input, outputs=name)
# Add examples
example_title ="The Toronto Paper Matching System: An automated paper-reviewer assignment system"
example_submission = """One of the most important tasks of conference organizers is the assignment of papers to reviewers. Reviewers' assessments of papers is a crucial step in determining the conference program, and in a certain sense to shape the direction of a field. However this is not a simple task: large conferences typically have to assign hundreds of papers to hundreds of reviewers, and time constraints make the task impossible for one person to accomplish. Furthermore other constraints, such as reviewer load have to be taken into account, preventing the process from being completely distributed. We built the first version of a system to suggest reviewer assignments for the NIPS 2010 conference, followed, in 2012, by a release that better integrated our system with Microsoft's popular Conference Management Toolkit (CMT). Since then our system has been widely adopted by the leading conferences in both the machine learning and computer vision communities. This paper provides an overview of the system, a summary of learning models and methods of evaluation that we have been using, as well as some of the recent progress and open issues."""
example_reviewer = "https://www.semanticscholar.org/author/Nihar-B.-Shah/1737249"
gr.Examples(
examples=[[example_title, example_submission, example_reviewer]],
inputs=[title_input, abstract_text_input, author_id_input],
cache_examples=False,
label="Try out the following example input."
)
with gr.Row():
compute_btn = gr.Button('What Makes This a Good Match?')
with gr.Row():
search_status = gr.Textbox(label='Search Status', interactive=False, visible=False)
### OVERVIEW
# Paper title, score, and top-ranking sentence pairs -- two sentence pairs per paper, three papers
## ONE BLOCK OF INFO FOR A SINGLE PAPER
## PAPER1
with gr.Row():
result1_desc = gr.Markdown(value='', visible=False)
with gr.Row():
with gr.Column(scale=3):
paper_title1 = gr.Markdown(value='', visible=False)
with gr.Column(scale=1):
affinity1 = gr.Markdown(value='', visible=False)
with gr.Row() as rel1_1:
with gr.Column(scale=1):
sent_pair_score1_1 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source1_1 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source1_1_hl = gr.components.Interpretation(sent_pair_source1_1)
with gr.Column(scale=4):
sent_pair_candidate1_1 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate1_1_hl = gr.components.Interpretation(sent_pair_candidate1_1)
with gr.Row() as rel1_2:
with gr.Column(scale=1):
sent_pair_score1_2 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source1_2 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source1_2_hl = gr.components.Interpretation(sent_pair_source1_2)
with gr.Column(scale=4):
sent_pair_candidate1_2 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate1_2_hl = gr.components.Interpretation(sent_pair_candidate1_2)
with gr.Row(visible=False) as demarc1:
gr.Markdown(
"""---"""
)
## PAPER 2
with gr.Row():
with gr.Column(scale=3):
paper_title2 = gr.Markdown(value='', visible=False)
with gr.Column(scale=1):
affinity2 = gr.Markdown(value='', visible=False)
with gr.Row() as rel2_1:
with gr.Column(scale=1):
sent_pair_score2_1 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source2_1 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source2_1_hl = gr.components.Interpretation(sent_pair_source2_1)
with gr.Column(scale=4):
sent_pair_candidate2_1 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate2_1_hl = gr.components.Interpretation(sent_pair_candidate2_1)
with gr.Row() as rel2_2:
with gr.Column(scale=1):
sent_pair_score2_2 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source2_2 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source2_2_hl = gr.components.Interpretation(sent_pair_source2_2)
with gr.Column(scale=4):
sent_pair_candidate2_2 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate2_2_hl = gr.components.Interpretation(sent_pair_candidate2_2)
with gr.Row(visible=False) as demarc2:
gr.Markdown(
"""---"""
)
## PAPER 3
with gr.Row():
with gr.Column(scale=3):
paper_title3 = gr.Markdown(value='', visible=False)
with gr.Column(scale=1):
affinity3 = gr.Markdown(value='', visible=False)
with gr.Row() as rel3_1:
with gr.Column(scale=1):
sent_pair_score3_1 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source3_1 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source3_1_hl = gr.components.Interpretation(sent_pair_source3_1)
with gr.Column(scale=4):
sent_pair_candidate3_1 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate3_1_hl = gr.components.Interpretation(sent_pair_candidate3_1)
with gr.Row() as rel3_2:
with gr.Column(scale=1):
sent_pair_score3_2 = gr.Markdown(interactive=False, value='', visible=False)
with gr.Column(scale=4):
sent_pair_source3_2 = gr.Textbox(label='Sentence from Submission', visible=False)
sent_pair_source3_2_hl = gr.components.Interpretation(sent_pair_source3_2)
with gr.Column(scale=4):
sent_pair_candidate3_2 = gr.Textbox(label="Sentence from Reviewer's Paper", visible=False)
sent_pair_candidate3_2_hl = gr.components.Interpretation(sent_pair_candidate3_2)
## Show more button
with gr.Row():
see_more_rel_btn = gr.Button('Explore more', visible=False)
### PAPER INFORMATION
# Description for Explore More Section
with gr.Row():
result2_desc = gr.Markdown(value='', visible=False)
# Highlight description
hl_desc = """
**Red**: sentences simiar to the selected sentence from submission. Darker = more similar.
**Blue**: phrases that appear in both sentences.
"""
#---"""
# show multiple papers in radio check box to select from
paper_abstract = gr.Textbox(label='Abstract', interactive=False, visible=False)
with gr.Row():
with gr.Column(scale=1):
selected_papers_radio = gr.Radio(
choices=[], # will be udpated with the button click
visible=False, # also will be updated with the button click
label='Top Relevant Papers from the Reviewer'
)
with gr.Column(scale=2):
# sentences from submission
source_sentences = gr.Radio(
choices=[],
visible=False,
label='Sentences from Submission Abstract',
)
with gr.Column(scale=3):
# selected paper and highlight
with gr.Row():
# slider for highlight amount
highlight_slider = gr.Slider(
label='Number of Highlighted Sentences',
minimum=1,
maximum=15,
step=1,
value=2,
visible=False
)
with gr.Row():
# highlight legend
highlight_legend = gr.Markdown(value=hl_desc, visible=False)
with gr.Row(visible=False) as title_row:
# selected paper title
paper_title = gr.Markdown(value='')
with gr.Row(visible=False) as aff_row:
# selected paper's affinity score
affinity = gr.Markdown(value='')
with gr.Row(visible=False) as hl_row:
# highlighted text from paper
highlight = gr.components.Interpretation(paper_abstract)
### EVENT LISTENERS
compute_btn.click(
fn=show_status,
inputs=[],
outputs=search_status
)
# retrieve similar papers and show top results
compute_btn.click(
fn=get_similar_paper,
inputs=[
title_input,
abstract_text_input,
author_id_input,
info
],
outputs=[
selected_papers_radio, # list of papers for show more section
source_sentences, # list of sentences for show more section
paper_title, # paper title for show more section
affinity, # paper affinity for show more section
paper_title1, # paper info
affinity1,
sent_pair_score1_1,
sent_pair_source1_1,
sent_pair_source1_1_hl,
sent_pair_candidate1_1,
sent_pair_candidate1_1_hl,
sent_pair_score1_2,
sent_pair_source1_2,
sent_pair_source1_2_hl,
sent_pair_candidate1_2,
sent_pair_candidate1_2_hl,
paper_title2,
affinity2,
sent_pair_score2_1,
sent_pair_source2_1,
sent_pair_source2_1_hl,
sent_pair_candidate2_1,
sent_pair_candidate2_1_hl,
sent_pair_score2_2,
sent_pair_source2_2,
sent_pair_source2_2_hl,
sent_pair_candidate2_2,
sent_pair_candidate2_2_hl,
paper_title3,
affinity3,
sent_pair_score3_1,
sent_pair_source3_1,
sent_pair_source3_1_hl,
sent_pair_candidate3_1,
sent_pair_candidate3_1_hl,
sent_pair_score3_2,
sent_pair_source3_2,
sent_pair_source3_2_hl,
sent_pair_candidate3_2,
sent_pair_candidate3_2_hl,
see_more_rel_btn,
result1_desc,
demarc1,
demarc2,
search_status,
result2_desc,
highlight_slider,
info,
],
show_progress=True,
scroll_to_output=True
)
# Get more info (move to more interactive portion)
see_more_rel_btn.click(
fn=show_more,
inputs=info,
outputs=[
result2_desc,
selected_papers_radio,
source_sentences,
title_row,
aff_row,
highlight_legend,
highlight_slider,
hl_row,
]
)
# change highlight based on selected sentences from submission
source_sentences.change(
fn=change_sentence,
inputs=[
selected_papers_radio,
source_sentences,
highlight_slider,
info
],
outputs=highlight
)
# change paper to show based on selected papers
selected_papers_radio.change(
fn=change_paper,
inputs=[
selected_papers_radio,
source_sentences,
highlight_slider,
info,
],
outputs= [
paper_title,
paper_abstract,
affinity,
highlight,
highlight_slider
]
)
highlight_slider.change(
fn=change_num_highlight,
inputs=[
selected_papers_radio,
source_sentences,
highlight_slider,
info
],
outputs=[
highlight
]
)
if __name__ == "__main__":
demo.queue().launch() # add ?__theme=light to force light mode