-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
176 lines (136 loc) · 5.45 KB
/
main.py
File metadata and controls
176 lines (136 loc) · 5.45 KB
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
from fastapi import FastAPI
from http_models import *
from sentence_transformers import SentenceTransformer, CrossEncoder
from faiss import IndexHNSWFlat, IndexFlatIP # IndexFlatIP more accurate, but slower
import numpy as np
import gc
import os
from utils import read_files_from_dir, read_dirs_from_dir, b64d, b64e
from database import insert_cache, clear_cache, get_docs_by_ids, create_connection
import pickle
# setup - Globals -
app = FastAPI(title="Mini RankBrain API for files and directories")
BATCH_SIZE = 5000
encoder = SentenceTransformer("all-MiniLM-L6-v2")
# Create FAISS index
dimension = encoder.get_sentence_embedding_dimension()
# Load cross-encoder
cross_encoder = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
# init cache
cx, cu = create_connection(first_time=True)
cx.close()
def index_strings(encoder, dir_path: str, table: str, read_func, index, batch_size=BATCH_SIZE, max_workers=8):
"""indexing in parallel with FAISS"""
def save_and_clear():
vectors = encoder.encode(batch_buffer, normalize_embeddings=True)
index.add(np.array(vectors, dtype=np.float32))
# cache - 100
cx, cu = create_connection()
insert_cache(cx, cu, b64e(pickle.dumps(batch_buffer)), table)
cx.close()
batch_buffer.clear()
# run garbage collection after batch
gc.collect()
batch_buffer = []
for string in read_func(dir_path, max_workers=max_workers):
batch_buffer.append(string)
if len(batch_buffer) >= batch_size:
save_and_clear()
if batch_buffer:
save_and_clear()
#? if text files cached into database:
#batch_size = 100
#end_reached = False
#offset = 0
#while not end_reached:
#docs = get_doc_vectors(cursor, batch_size, offset)
#if len(docs) == 0:
# end_reached = True
#else:
# index.add(docs)
# offset += batch_size
@app.post("/search/directory", response_model=QueryResponse)
def search_directories(request: QueryRequest_directory):
if not os.path.exists(request.path):
return QueryResponse(query=request.query, results=["Not valid path"])
global dir_path
dir_path = request.path
query_vector = encoder.encode([request.query], normalize_embeddings=True)
# search directory
index_IP = IndexFlatIP(dimension)
index_strings(encoder, request.path, "cache.directories", read_dirs_from_dir, index_IP)
# distance and indicies
_, I = index_IP.search(np.array(query_vector, dtype=np.float32), request.top_k)
cx, cu = create_connection()
indices = []
for i in I[0]:
tmp = int((i // BATCH_SIZE) + 1)
if not tmp in indices:
indices.append(tmp)
candidates = get_docs_by_ids(cu, indices, "cache.directories")
valid_candidates = []
for c, index in zip(candidates, indices):
for i in I[0]:
if int((i // BATCH_SIZE) + 1) == index:
candidate = pickle.loads(b64d(c.encode("utf-8")))[i % BATCH_SIZE]
valid_candidates.append(candidate)
candidates = valid_candidates
# debug
#print(list(map(lambda x: (x + 1 // BATCH_SIZE), I[0])))
#print(indices)
#print(candidates)
# cross-encoder
pairs = [(request.query, doc) for doc in candidates]
scores = cross_encoder.predict(pairs)
reranked_docs = [doc for _, doc in sorted(zip(scores, candidates), reverse=True)]
# clear after search
index_IP = IndexFlatIP(dimension)
clear_cache(cu, clear_all=True)
gc.collect()
cx.close()
return QueryResponse(query=request.query, results=reranked_docs)
@app.post("/search/files", response_model=QueryResponse)
def search_files(request: QueryRequest_files):
try:
if not os.path.exists(dir_path):
return QueryResponse(query=request.query, results=["Not valid path"])
except NameError:
return QueryResponse(query=request.query, results=["Not valid path"])
query_vector = encoder.encode([request.query], normalize_embeddings=True)
#? search files, too slow for large directories
index_HNSW = IndexHNSWFlat(dimension, 16)
index_strings(encoder, dir_path, "cache.files", read_files_from_dir, index_HNSW)
# distance and indicies
_, I = index_HNSW.search(np.array(query_vector, dtype=np.float32), request.top_k)
cx, cu = create_connection()
# cross-encoder database starts from index 1 not 0
indices = []
for i in I[0]:
tmp = int((i // BATCH_SIZE) + 1)
if not tmp in indices:
indices.append(tmp)
candidates = get_docs_by_ids(cu, indices, "cache.directories")
valid_candidates = []
for c, index in zip(candidates, indices):
for i in I[0]:
if int((i // BATCH_SIZE) + 1) == index:
candidate = pickle.loads(b64d(c.encode("utf-8")))[i % BATCH_SIZE]
valid_candidates.append(candidate)
candidates = valid_candidates
# cross-encoder
pairs = [(request.query, doc) for doc in candidates]
scores = cross_encoder.predict(pairs)
# : split to get file path .split(": ")[0]
reranked_docs = [doc for _, doc in sorted(zip(scores, candidates), reverse=True)]
# clear after search
index_HNSW = IndexHNSWFlat(dimension, 16)
clear_cache(cu)
gc.collect()
cx.close()
return QueryResponse(query=request.query, results=reranked_docs)
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=False)