-
Notifications
You must be signed in to change notification settings - Fork 100
/
search.py
154 lines (119 loc) · 4.03 KB
/
search.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
"""
Search a paperai index.
Requires streamlit and lxml to be installed.
pip install streamlit lxml
"""
import os
import sqlite3
import sys
import pandas as pd
import streamlit as st
from lxml.html.clean import clean_html
from paperai.models import Models
from paperai.query import Query
class Application:
"""
Streamlit application.
"""
def __init__(self, path):
"""
Creates a new Streamlit application.
"""
# Default list of columns
self.columns = [
("Title", True),
("Published", False),
("Publication", False),
("Entry", False),
("Id", False),
("Content", True),
]
# Load model
self.path = path
self.embeddings, db = Models.load(path)
db.close()
def search(self, query, topn, threshold):
"""
Executes a search
Args:
data: input data
"""
dbfile = os.path.join(self.path, "articles.sqlite")
with sqlite3.connect(dbfile) as db:
cur = db.cursor()
# Query for best matches
results = Query.search(self.embeddings, cur, query, topn, threshold)
# Get results grouped by document
documents = Query.documents(results, topn)
articles = []
# Print each result, sorted by max score descending
for uid in sorted(
documents, key=lambda k: sum([x[0] for x in documents[k]]), reverse=True
):
cur.execute(
"SELECT Title, Published, Publication, Entry, Id, Reference "
+ "FROM articles WHERE id = ?",
[uid],
)
article = cur.fetchone()
matches = "<br/>".join([text for _, text in documents[uid]])
title = f"<a target='_blank' href='{article[5]}'>{article[0]}</a>"
article = {
"Title": title,
"Published": Query.date(article[1]),
"Publication": article[2],
"Entry": article[3],
"Id": article[4],
"Content": matches,
}
articles.append(article)
return pd.DataFrame(articles)
def run(self):
"""
Runs Streamlit application.
"""
st.sidebar.image(
"https://github.com/neuml/paperai/raw/master/logo.png", width=256
)
st.sidebar.markdown("## Search parameters")
# Search parameters
query = st.text_area("Query")
topn = st.sidebar.number_input("topn", value=10)
threshold = st.sidebar.slider("threshold", 0.0, 1.0, 0.6)
st.markdown(
"<style>.small-font { font-size: 0.8rem !important;}</style>",
unsafe_allow_html=True,
)
st.sidebar.markdown(
"<p class='small-font'>Select columns</p>", unsafe_allow_html=True
)
columns = [
column
for column, enabled in self.columns
if st.sidebar.checkbox(column, enabled)
]
if self.embeddings and query:
df = self.search(query, topn, threshold)
st.markdown(
f"<p class='small-font'>{len(df)} results</p>", unsafe_allow_html=True
)
if not df.empty:
html = df[columns].to_html(escape=False, index=False)
st.write(clean_html(html), unsafe_allow_html=True)
@st.cache(allow_output_mutation=True)
def create(path):
"""
Creates and caches a Streamlit application.
Returns:
Application
"""
return Application(path)
if __name__ == "__main__":
os.environ["TOKENIZERS_PARALLELISM"] = "false"
if len(sys.argv) <= 1 or not os.path.isdir(sys.argv[1]):
st.error("Path to embeddings index not present or invalid")
else:
st.set_page_config(layout="wide")
# Create and run application
app = create(sys.argv[1])
app.run()