-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
248 lines (210 loc) · 6.88 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import json
import os
import math
from fastapi import BackgroundTasks, FastAPI
from fastapi.middleware.cors import CORSMiddleware
import redis.asyncio as aioredis
from dotenv import load_dotenv
import pandas as pd
from datetime import datetime
from collections import defaultdict
from bsbi import BSBIIndex
from compression import VBEPostings
from letor import LambdaMart
import models
load_dotenv()
redis = aioredis.Redis(
db=0,
host=os.environ.get("REDIS_HOST"),
port=os.environ.get("REDIS_PORT"),
password=os.environ.get("REDIS_PASSWORD"),
decode_responses=True,
)
ONE_DAY = 60 * 60 * 24
ONE_WEEK = ONE_DAY * 7
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
letor = LambdaMart(dataset_dir="dataset/")
letor.fit()
ranker = letor.get_model()
BSBI_instance = BSBIIndex(
data_dir="collections",
postings_encoding=VBEPostings,
output_dir="index",
)
BSBI_instance.load()
async def set_cache(data, keys):
await redis.set(
keys,
json.dumps(data),
ex=ONE_DAY,
)
async def set_history(device_id, query) -> None:
keys = f"history:{device_id}"
today = datetime.today()
cache = await redis.get(keys)
if cache:
history = defaultdict(list, json.loads(cache))
else:
history = defaultdict(list)
history[today.strftime("%Y-%m-%d")].insert(
0, {"query": query, "time": datetime.now().strftime("%Y-%m-%d %H-%M-%S")}
)
await redis.set(keys, json.dumps(history), ex=ONE_WEEK)
def get_documents(scores) -> list[models.Document]:
documents = []
for score, doc_path in scores:
with open(doc_path, "r", encoding="UTF8") as f:
title, text = f.read().split("\t", 1)
did = int(os.path.splitext(os.path.basename(doc_path))[0])
collection = os.path.basename(os.path.dirname(doc_path))
trim = min(len(text) // 2, 30)
documents.append(
{
"id": f"{collection}-{did}",
"title": title,
"preview": text[:trim] + "...",
"score": score,
}
)
return documents
def paginate(data, page, limit) -> models.PaginatedDocuments:
page = 1 if page < 1 else page
last_page = math.ceil(len(data) / limit)
return {
"current_page": page,
"last_page": 1 if last_page == 0 else last_page,
"per_page": limit,
"total": len(data),
"data": data[(page - 1) * limit : page * limit],
}
@app.get("/search/tfidf")
async def get_relevant_documents_tfidf(
background_tasks: BackgroundTasks,
query: str,
is_letor: bool = True,
k: int = 100,
page: int = 1,
limit: int = 10,
device_id: str | None = None,
) -> models.PaginatedDocuments:
# check if cache exists
keys = f"tfidf:{query}-k:{k}-limit:{limit}"
cache = await redis.get(keys)
if cache:
tfid_scores = json.loads(cache)
else:
tfid_scores = BSBI_instance.retrieve_tfidf(query, k=k) # [(score, doc_id)]
# save to cache
background_tasks.add_task(set_cache, tfid_scores, keys)
if is_letor:
keys = f"tfidf-letor:{query}-k:{k}-limit:{limit}"
cache = await redis.get(keys)
if cache:
tfid_scores = json.loads(cache)
elif len(tfid_scores) > 0:
tfidf_df = pd.DataFrame(tfid_scores, columns=["score", "doc_path"])
tfid_scores = letor.rerank(query, tfidf_df)
# save to cache
background_tasks.add_task(set_cache, tfid_scores, keys)
# convert to docs
documents = get_documents(tfid_scores)
if device_id:
# save to history
await set_history(device_id, query)
return paginate(documents, page, limit)
@app.get("/search/bm25")
async def get_relevant_documents_bm25(
background_tasks: BackgroundTasks,
query: str,
is_letor: bool = True,
k: int = 100,
page: int = 1,
limit: int = 10,
device_id: str | None = None,
) -> models.PaginatedDocuments:
# check if cache exists
keys = f"bm25:{query}-k:{k}-limit:{limit}"
cache = await redis.get(keys)
if cache:
bm25_scores = json.loads(cache)
else:
bm25_scores = BSBI_instance.retrieve_bm25(query, k=k) # [(score, doc_id)]
# save to cache
background_tasks.add_task(set_cache, bm25_scores, keys)
if is_letor:
keys = f"bm25-letor:{query}-k:{k}-limit:{limit}"
cache = await redis.get(keys)
if cache:
bm25_scores = json.loads(cache)
elif len(bm25_scores) > 0:
bm25_df = pd.DataFrame(bm25_scores, columns=["score", "doc_path"])
bm25_scores = letor.rerank(query, bm25_df)
# save to cache
background_tasks.add_task(set_cache, bm25_scores, keys)
# convert to docs
documents = get_documents(bm25_scores)
if device_id:
# save to history
print("Device", device_id, "searched", query)
await set_history(device_id, query)
return paginate(documents, page, limit)
@app.get("/document/{doc_id}")
def get_document_detail(doc_id: str) -> models.DocumentDetail:
collection, did = doc_id.split("-")
doc_path = os.path.join("collections", collection, f"{did}.txt")
with open(doc_path, "r", encoding="UTF8") as f:
title, content = f.read().split("\t", 1)
return {
"id": doc_id,
"title": title,
"content": content,
}
@app.get("/related/{doc_id}")
async def get_related_documents(
background_tasks: BackgroundTasks,
doc_id: str,
k: int = 10,
page: int = 1,
limit: int = 10,
device_id: str | None = None,
) -> models.PaginatedDocuments:
collection, did = doc_id.split("-")
doc_path = os.path.join("collections", collection, f"{did}.txt")
with open(doc_path, "r", encoding="UTF8") as f:
title, content = f.read().split("\t", 1)
query = title + " " + content
documents = await get_relevant_documents_tfidf(
background_tasks,
query,
is_letor=False,
k=k,
page=page,
limit=limit,
device_id=device_id,
)
documents["data"].pop(0) # remove the first document (itself)
return documents
@app.get("/history")
async def get_search_history(device_id: str) -> models.SearchHistoryResponse:
keys = f"history:{device_id}"
history = await redis.get(keys)
if history:
# convert dict to list of dict sorted by date (desc)
history = json.loads(history)
history_list = []
for date, queries in sorted(
history.items(),
key=lambda x: datetime.strptime(x[0], "%Y-%m-%d"),
reverse=True,
):
history_list.append({"date": date, "queries": queries})
return {"data": history_list}
return {"data": []}