Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,543 Bytes
c6cda2e 5e710c8 6bb65a4 5e710c8 c6cda2e ff52d8e c6cda2e ff52d8e c6cda2e ff52d8e c6cda2e ff52d8e c6cda2e |
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 |
import gradio
import numpy
import pandas
import sentence_transformers
import datasets
import faiss
model = sentence_transformers.SentenceTransformer('allenai-specter')
full_data = datasets.load_dataset("ccm/publications")['train'].to_pandas()
substring = "0P9w_S0AAAAJ:yB1At4FlUx8C"
filter = full_data['author_pub_id'].str.contains(substring)
data = full_data[~filter]
dimensionality = len(data['embedding'][0])
index = faiss.IndexFlatL2(dimensionality)
vectors = numpy.stack(data['embedding'].to_list(), axis=0)
index.add(vectors)
def search(query, k):
query = numpy.expand_dims(model.encode(query), axis=0)
_, I = top_five = index.search(query, k)
top_five = data.loc[I[0]]
search_results = ""
for i in range(k):
search_results += str(i+1) + ". "
search_results += '"' + top_five["bibtex"].values[i]["title"] + '" '
search_results += top_five["bibtex"].values[i]["citation"]
if top_five["pub_url"].values[i] is not None:
search_results += " [Paper](" + top_five["pub_url"].values[i] + ")"
search_results += "\n"
return search_results
with gradio.Blocks() as demo:
with gradio.Group():
query = gradio.Textbox(placeholder="Enter search terms...", show_label=False, lines=1, max_lines=1)
with gradio.Accordion("Settings", open=False):
k = gradio.Number(5.0, label="Number of results", precision=0)
results = gradio.Markdown()
query.change(fn=search, inputs=[query, k], outputs=results)
demo.launch(debug=True) |