Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,52 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from langchain.vectorstores import FAISS
|
3 |
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def get_matches(query, db_name="miread_contrastive"):
|
7 |
"""
|
@@ -19,9 +64,7 @@ def inference(query, model="miread_contrastive"):
|
|
19 |
"""
|
20 |
matches = get_matches(query, model)
|
21 |
auth_counts = {}
|
22 |
-
j_bucket = {}
|
23 |
n_table = []
|
24 |
-
a_table = []
|
25 |
scores = [round(match[1].item(), 3) for match in matches]
|
26 |
min_score = min(scores)
|
27 |
max_score = max(scores)
|
@@ -33,18 +76,6 @@ def inference(query, model="miread_contrastive"):
|
|
33 |
author = doc.metadata['authors'][0].title()
|
34 |
date = doc.metadata.get('date', 'None')
|
35 |
link = doc.metadata.get('link', 'None')
|
36 |
-
submitter = doc.metadata.get('submitter', 'None')
|
37 |
-
journal = doc.metadata['journal']
|
38 |
-
if (journal is None or journal.strip() == ''):
|
39 |
-
journal = 'None'
|
40 |
-
else:
|
41 |
-
journal = journal.strip()
|
42 |
-
|
43 |
-
# For journals
|
44 |
-
if journal not in j_bucket:
|
45 |
-
j_bucket[journal] = score
|
46 |
-
else:
|
47 |
-
j_bucket[journal] += score
|
48 |
|
49 |
# For authors
|
50 |
record = [i+1,
|
@@ -59,49 +90,12 @@ def inference(query, model="miread_contrastive"):
|
|
59 |
auth_counts[author] = 1
|
60 |
else:
|
61 |
auth_counts[author] += 1
|
62 |
-
|
63 |
-
# For abstracts
|
64 |
-
record = [i+1,
|
65 |
-
title,
|
66 |
-
author,
|
67 |
-
submitter,
|
68 |
-
journal,
|
69 |
-
date,
|
70 |
-
link,
|
71 |
-
score
|
72 |
-
]
|
73 |
-
a_table.append(record)
|
74 |
-
|
75 |
-
del j_bucket['None']
|
76 |
-
j_table = sorted([[journal, round(score, 3)] for journal,
|
77 |
-
score in j_bucket.items()],
|
78 |
-
key=lambda x: x[1], reverse=True)
|
79 |
-
j_table = [[i+1, item[0], item[1]] for i, item in enumerate(j_table)]
|
80 |
-
j_output = gr.Dataframe.update(value=j_table, visible=True)
|
81 |
n_output = gr.Dataframe.update(value=n_table, visible=True)
|
82 |
-
|
83 |
-
|
84 |
-
return [a_output, j_output, n_output]
|
85 |
-
|
86 |
-
|
87 |
-
index_names = ["miread_large", "miread_contrastive", "scibert_contrastive"]
|
88 |
-
model_names = [
|
89 |
-
"biodatlab/MIReAD-Neuro-Large",
|
90 |
-
"biodatlab/MIReAD-Neuro-Contrastive",
|
91 |
-
"biodatlab/SciBERT-Neuro-Contrastive",
|
92 |
-
]
|
93 |
-
model_kwargs = {'device': 'cpu'}
|
94 |
-
encode_kwargs = {'normalize_embeddings': False}
|
95 |
-
faiss_embedders = [HuggingFaceEmbeddings(
|
96 |
-
model_name=name,
|
97 |
-
model_kwargs=model_kwargs,
|
98 |
-
encode_kwargs=encode_kwargs) for name in model_names]
|
99 |
|
100 |
-
vecdbs = [FAISS.load_local(index_name, faiss_embedder)
|
101 |
-
for index_name, faiss_embedder in zip(index_names, faiss_embedders)]
|
102 |
|
103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
104 |
-
gr.Markdown("# NBDT Recommendation Engine
|
105 |
gr.Markdown("NBDT Recommendation Engine for Editors is a tool for neuroscience authors/abstracts/journalsrecommendation built for NBDT journal editors. \
|
106 |
It aims to help an editor to find similar reviewers, abstracts, and journals to a given submitted abstract.\
|
107 |
To find a recommendation, paste a `title[SEP]abstract` or `abstract` in the text box below and click on the appropriate \"Find Matches\" button.\
|
@@ -109,56 +103,44 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
109 |
The data in our current demo includes authors associated with the NBDT Journal. We will update the data monthly for an up-to-date publications.")
|
110 |
|
111 |
abst = gr.Textbox(label="Abstract", lines=10)
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
with gr.Tab("Authors"):
|
119 |
-
n_output = gr.Dataframe(
|
120 |
headers=['No.', 'Score', 'Name', 'Title', 'Link', 'Date'],
|
121 |
datatype=['number', 'number', 'str', 'str', 'str', 'str'],
|
122 |
col_count=(6, "fixed"),
|
123 |
wrap=True,
|
124 |
-
visible=
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
)
|
136 |
-
with gr.Tab("Journals"):
|
137 |
-
j_output = gr.Dataframe(
|
138 |
-
headers=['No.', 'Name', 'Score'],
|
139 |
-
datatype=['number', 'str', 'number'],
|
140 |
-
col_count=(3, "fixed"),
|
141 |
wrap=True,
|
142 |
-
visible=
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
outputs=[a_output, j_output, n_output],
|
162 |
-
api_name="neurojane")
|
163 |
-
|
164 |
-
demo.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import csv
|
3 |
+
import random
|
4 |
+
import uuid
|
5 |
from langchain.vectorstores import FAISS
|
6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
|
8 |
+
USER_ID = uuid.uuid4()
|
9 |
+
INDEXES = ["miread_large", "miread_contrastive", "scibert_contrastive"]
|
10 |
+
MODELS = [
|
11 |
+
"biodatlab/MIReAD-Neuro-Large",
|
12 |
+
"biodatlab/MIReAD-Neuro-Contrastive",
|
13 |
+
"biodatlab/SciBERT-Neuro-Contrastive",
|
14 |
+
]
|
15 |
+
model_kwargs = {'device': 'cpu'}
|
16 |
+
encode_kwargs = {'normalize_embeddings': False}
|
17 |
+
faiss_embedders = [HuggingFaceEmbeddings(
|
18 |
+
model_name=name,
|
19 |
+
model_kwargs=model_kwargs,
|
20 |
+
encode_kwargs=encode_kwargs) for name in MODELS]
|
21 |
+
|
22 |
+
vecdbs = [FAISS.load_local(index_name, faiss_embedder)
|
23 |
+
for index_name, faiss_embedder in zip(INDEXES, faiss_embedders)]
|
24 |
+
|
25 |
+
def get_matchup():
|
26 |
+
choices = INDEXES
|
27 |
+
left, right = random.sample(choices,2)
|
28 |
+
return left, right
|
29 |
+
|
30 |
+
def get_comp(prompt):
|
31 |
+
left, right = get_matchup()
|
32 |
+
left_output = inference(prompt,left)
|
33 |
+
right_output = inference(prompt,right)
|
34 |
+
return left_output, right_output
|
35 |
+
|
36 |
+
def get_article(db_name="miread_contrastive"):
|
37 |
+
db = vecdbs[index_names.index(db_name)]
|
38 |
+
return db[0]
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
def send_result(l_output, r_output, prompt, pick):
|
45 |
+
with csv.open('results.csv','a') as res_file:
|
46 |
+
writer = csv.writer(res_file)
|
47 |
+
row = [USER_ID,left,right,prompt,pick]
|
48 |
+
writer.writerow(row)
|
49 |
+
|
50 |
|
51 |
def get_matches(query, db_name="miread_contrastive"):
|
52 |
"""
|
|
|
64 |
"""
|
65 |
matches = get_matches(query, model)
|
66 |
auth_counts = {}
|
|
|
67 |
n_table = []
|
|
|
68 |
scores = [round(match[1].item(), 3) for match in matches]
|
69 |
min_score = min(scores)
|
70 |
max_score = max(scores)
|
|
|
76 |
author = doc.metadata['authors'][0].title()
|
77 |
date = doc.metadata.get('date', 'None')
|
78 |
link = doc.metadata.get('link', 'None')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# For authors
|
81 |
record = [i+1,
|
|
|
90 |
auth_counts[author] = 1
|
91 |
else:
|
92 |
auth_counts[author] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
n_output = gr.Dataframe.update(value=n_table, visible=True)
|
94 |
+
return n_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
|
|
|
|
96 |
|
97 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
98 |
+
gr.Markdown("# NBDT Recommendation Engine Arena")
|
99 |
gr.Markdown("NBDT Recommendation Engine for Editors is a tool for neuroscience authors/abstracts/journalsrecommendation built for NBDT journal editors. \
|
100 |
It aims to help an editor to find similar reviewers, abstracts, and journals to a given submitted abstract.\
|
101 |
To find a recommendation, paste a `title[SEP]abstract` or `abstract` in the text box below and click on the appropriate \"Find Matches\" button.\
|
|
|
103 |
The data in our current demo includes authors associated with the NBDT Journal. We will update the data monthly for an up-to-date publications.")
|
104 |
|
105 |
abst = gr.Textbox(label="Abstract", lines=10)
|
106 |
+
models = gr.State(value=get_matchup())
|
107 |
+
prompt = gr.State(value=get_prompt())
|
108 |
+
action_btn = gr.Button(value="Get comparison")
|
109 |
+
with gr.Row().style(equal_height=True):
|
110 |
+
with gr.Column(scale=1):
|
111 |
+
l_output = gr.Dataframe(
|
|
|
|
|
112 |
headers=['No.', 'Score', 'Name', 'Title', 'Link', 'Date'],
|
113 |
datatype=['number', 'number', 'str', 'str', 'str', 'str'],
|
114 |
col_count=(6, "fixed"),
|
115 |
wrap=True,
|
116 |
+
visible=True,
|
117 |
+
label='Model A',
|
118 |
+
show_label = True
|
119 |
+
scale=1
|
120 |
+
)
|
121 |
+
l_btn = gr.Button(value="Model A is better",scale=1)
|
122 |
+
with gr.Column(scale=1):
|
123 |
+
r_output = gr.Dataframe(
|
124 |
+
headers=['No.', 'Score', 'Name', 'Title', 'Link', 'Date'],
|
125 |
+
datatype=['number', 'number', 'str', 'str', 'str', 'str'],
|
126 |
+
col_count=(6, "fixed"),
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
wrap=True,
|
128 |
+
visible=True,
|
129 |
+
label='Model B',
|
130 |
+
show_label = True
|
131 |
+
scale=1
|
132 |
+
)
|
133 |
+
r_btn = gr.Button(value="Model B is better",scale=1)
|
134 |
+
|
135 |
+
action_btn.click(fn=get_comp,
|
136 |
+
inputs=[prompt,],
|
137 |
+
outputs=[l_output, r_output],
|
138 |
+
api_name="arena")
|
139 |
+
l_btn.click(fn=lambda x,y,z: send_result(x,y,z,'left'),
|
140 |
+
inputs=[l_output,r_output,prompt],
|
141 |
+
api_name="feedleft")
|
142 |
+
l_btn.click(fn=lambda x,y,z: send_result(x,y,z,'right'),
|
143 |
+
inputs=[l_output,r_output,prompt],
|
144 |
+
api_name="feedright")
|
145 |
+
|
146 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|