-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
324 lines (277 loc) · 12 KB
/
build.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from __future__ import absolute_import, division, print_function
import nltk
from collections import defaultdict
from nltk.stem.snowball import EnglishStemmer # Assuming we're working with English
import pymongo
import faiss
import pandas as pd
import dask.dataframe as dd
from dask import delayed, compute
import numpy as np
from pympler import tracker
from pympler.asizeof import asizeof
# nltk.download('stopwords')
import re
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from cache_to_disk import cache_to_disk
from cache_to_disk import delete_disk_caches_for_function
from sentence_transformers import SentenceTransformer
from joblib import dump, load
import collections
import logging
import math
import torch
from pytorch_transformers import (WEIGHTS_NAME, BertConfig,
BertForQuestionAnswering, BertTokenizer)
from torch.utils.data import DataLoader, SequentialSampler, TensorDataset
from utils import (get_answer, input_to_squad_example,
squad_examples_to_features, to_list)
RawResult = collections.namedtuple("RawResult",
["unique_id", "start_logits", "end_logits"])
from os import path
import os
from cloud_storage import test_file_exists, download_blob, upload_blob, pull_indices
from preprocessing import preprocess_string, read_stop_words
# delete_disk_caches_for_function('get_data')
collection = pymongo.MongoClient('mongodb+srv://ir2:[email protected]/document?retryWrites=true&w=majority').document.document
mongo_query = lambda i,batch_size: [
{
'$project': {
'content': {
'$reduce': {
'input': '$content.text',
'initialValue': '',
'in': {
'$concat': [
'$$value', ' ', '$$this'
]
}
}
},
'title': 1,
'description': 1
}
}, {
'$project': {
'text': {
'$concat': [
'$title', ' ', '$description', ' ', '$content'
]
}
}
},
{ "$limit": i+batch_size },
{ "$skip": i }
]
# all_docs = lambda: collection.find({}, projection={'title': True, 'description': True, "content.text": True})
# doc_generator = lambda q=all_docs: ((x['title'] or "") + " " + x.get('description',"")+ " " + " ".join(filter(None,x['content']['text'] or [])) for x in collection.find({}, projection={'title': True, 'description': True, "content.text": True}))
# @cache_to_disk(2)
# def get_data():
# print("Loading documents from databases ...")
# # get the database
# # meta = pd.DataFrame(list(collection.find({},{ '_id': False }).limit(1)))
# # queries = iterate_by_chunks(collection,chunksize=2)
# # for q in queries:
# # print(list(q))
# # # l = apply_q(q)
# # # print(l)
# # # df = dd.from_pandas(l,chunksize=2)
# # print("dione df")
# for coll
# # dd.to_parquet(df,"models/documents")
# # dd.to_parquet(df,"models/documents",overwrite=True)
# # return pd.DataFrame(list(database.get_collection("document").find())).fillna('')
# def iterate_by_chunks(collection, chunksize=2, start_from=0, query={}, projection={ '_id': False }):
# chunks = range(start_from, collection.find(query,projection).count(), int(chunksize))
# num_chunks = 3 #len(chunks)
# for i in range(1,num_chunks+1):
# print("Loaded chunk ",i)
# if i < num_chunks:
# yield collection.find(query,projection)[chunks[i-1]:chunks[i]]
# else:
# yield collection.find(query,projection)[chunks[i-1]:chunks.stop]
def upload_indices_and_vectors():
# Checks if PULL_INDS environment variable is present, and calls pull function
if os.environ.get('PUSH_INDS') != None:
upload_blob("symptomizer_indices_bucket-1", "models/bert.index", "bert.index")
upload_blob("symptomizer_indices_bucket-1", "models/tfidf.index", "tfidf.index")
upload_blob("symptomizer_indices_bucket-1", "models/ids.joblib", "ids.joblib")
upload_blob("symptomizer_indices_bucket-1", "models/tfidf_model.joblib", "tfidf_model.joblib")
else:
print("No PUSH_INDS env found. Not pushing new index.")
def pull_and_preprocess_from_mongo(start_index, num_docs):
docs = collection.aggregate(mongo_query(start_index, num_docs))
doc_list = []
id_list = []
for doc in docs:
clean_text = preprocess_string(doc['text'] or "", stopping = True, stemming = True, lowercasing = True)
doc_list.append(clean_text)
id_list.append(doc['_id'])
return list(zip(doc_list, id_list))
def build_tfidf_model(num_docs=2000, max_features=3000):
print("Building TF-IDF model...")
model = TfidfVectorizer(ngram_range=(3,5), max_features=max_features, analyzer="char")
data = pull_and_preprocess_from_mongo(0, num_docs)
docs = [text for (text, ind) in data]
model.fit(docs)
dump(model, 'models/tfidf_model.joblib')
print("Saved TF-IDF model to models/tfidf_model.joblib.")
return model
def load_tfidf_model():
if path.exists(f"models/tfidf_model.joblib"):
print("Loading TF-IDF model...")
return load("models/tfidf_model.joblib")
else:
return build_tfidf_model()
def load_bert_model():
print("Building BERT model...")
model = SentenceTransformer('msmarco-distilbert-base-v2')
print("Completed BERT model.")
return model
def build_faiss(tfidf_model, bert_model):
tr = tracker.SummaryTracker()
print(f"Building indices ...")
c = collection.find().count()
# c = 5000
batch_size = 500
encoder = None
bert_index = None
tfidf_index = None
# if hasattr(model, 'encode'):
# encoder = lambda x: model.encode(x).astype("float32")
# else:
# encoder = lambda x:model.transform(x).toarray().astype("float32")
i = 0
ids = []
while i < c:
print(i)
docs = []
for text, ind in pull_and_preprocess_from_mongo(i,batch_size):
# docs.append(x.get("title","") + " " + x.get('description',"")+ " " + " ".join(filter(None,x.get('content',{}).get('text',[]))))
docs.append(text)
ids.append(ind)
print("Downloaded batch",i)
tfidf_embeddings = tfidf_model.transform(docs).toarray().astype("float32")
print("Computed tfidf embeddings")
bert_embeddings = bert_model.encode([doc[:100] for doc in docs]).astype("float32")
print("Computed bert embeddings")
if i == 0:
bert_index = faiss.IndexFlatIP(bert_embeddings.shape[1])
tfidf_index = faiss.IndexFlatIP(tfidf_embeddings.shape[1])
# print(bert_embeddings.shape[1])
# print(tfidf_embeddings.shape[1])
faiss.normalize_L2(bert_embeddings)
faiss.normalize_L2(tfidf_embeddings)
print(tr.print_diff())
# Step 3: Pass the index to IndexIDMap
# index = faiss.IndexIDMap(index)
# Step 4: Add vectors and their IDs
# print("range",len(np.arange(i,i+len(embeddings))))
# print("embeds",len(embeddings))
# idmap.add_with_ids(embeddings,np.arange(i,i+len(embeddings)))
bert_index.add(bert_embeddings)
tfidf_index.add(tfidf_embeddings)
i += len(tfidf_embeddings)
faiss.write_index(bert_index,f"models/bert.index")
faiss.write_index(tfidf_index,f"models/tfidf.index")
dump(ids,'models/ids.joblib')
print(f"Completed indices.")
# upload_indices_and_vectors()
return [tfidf_index, bert_index]
def load_faiss(tfidf_model, bert_model):
if path.exists(f"models/bert.index") and path.exists(f"models/tfidf.index"):
return [faiss.read_index(f"models/tfidf.index"),faiss.read_index(f"models/bert.index")]
else:
return build_faiss(tfidf_model, bert_model)
class QA:
def __init__(self,model_path: str):
self.max_seq_length = 384
self.doc_stride = 128
self.do_lower_case = True
self.max_query_length = 64
self.n_best_size = 20
self.max_answer_length = 30
self.model, self.tokenizer = self.load_model(model_path)
if torch.cuda.is_available():
self.device = 'cuda'
else:
self.device = 'cpu'
self.model.to(self.device)
self.model.eval()
def load_model(self,model_path: str,do_lower_case=False):
config = BertConfig.from_pretrained(model_path + "/bert_config.json")
tokenizer = BertTokenizer.from_pretrained(model_path, do_lower_case=do_lower_case)
model = BertForQuestionAnswering.from_pretrained(model_path, from_tf=False, config=config)
return model, tokenizer
def predict(self,passage :str,question :str):
example = input_to_squad_example(passage,question)
features = squad_examples_to_features(example,self.tokenizer,self.max_seq_length,self.doc_stride,self.max_query_length)
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in features], dtype=torch.long)
all_example_index = torch.arange(all_input_ids.size(0), dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_input_mask, all_segment_ids,
all_example_index)
eval_sampler = SequentialSampler(dataset)
eval_dataloader = DataLoader(dataset, sampler=eval_sampler, batch_size=1)
all_results = []
for batch in eval_dataloader:
batch = tuple(t.to(self.device) for t in batch)
with torch.no_grad():
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'token_type_ids': batch[2]
}
example_indices = batch[3]
outputs = self.model(**inputs)
for i, example_index in enumerate(example_indices):
eval_feature = features[example_index.item()]
unique_id = int(eval_feature.unique_id)
result = RawResult(unique_id = unique_id,
start_logits = to_list(outputs[0][i]),
end_logits = to_list(outputs[1][i]))
all_results.append(result)
answer = get_answer(example,features,all_results,self.n_best_size,self.max_answer_length,self.do_lower_case)
return answer
class Index:
""" Inverted index datastructure """
def __init__(self, tokenizer, stemmer=None, stopwords=None):
"""
tokenizer -- NLTK compatible tokenizer function
stemmer -- NLTK compatible stemmer
stopwords -- list of ignored words
"""
self.tokenizer = tokenizer
self.stemmer = stemmer
self.index = defaultdict(list)
self.documents = {}
self.__unique_id = 0
if not stopwords:
self.stopwords = set()
else:
self.stopwords = set(stopwords)
def lookup(self, word):
"""
Lookup a word in the index
"""
word = word.lower()
if self.stemmer:
word = self.stemmer.stem(word)
return [self.documents.get(id, None) for id in self.index.get(word)]
def add(self, document):
"""
Add a document string to the index
"""
for token in [t.lower() for t in nltk.word_tokenize(document)]:
if token in self.stopwords:
continue
if self.stemmer:
token = self.stemmer.stem(token)
if self.__unique_id not in self.index[token]:
self.index[token].append(self.__unique_id)
self.documents[self.__unique_id] = document
self.__unique_id += 1
index = Index(nltk.word_tokenize,
EnglishStemmer(),
nltk.corpus.stopwords.words('english'))