theachyuttiwari
commited on
Commit
·
317b701
1
Parent(s):
9b0c357
Upload query_smoke_test.py
Browse files- query_smoke_test.py +81 -0
query_smoke_test.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModel
|
3 |
+
|
4 |
+
from datasets import load_dataset
|
5 |
+
|
6 |
+
|
7 |
+
def main():
|
8 |
+
device = ("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained('vblagoje/retribert-base-uncased')
|
10 |
+
model = AutoModel.from_pretrained('vblagoje/retribert-base-uncased').to(device)
|
11 |
+
_ = model.eval()
|
12 |
+
|
13 |
+
index_file_name = "./data/kilt_wikipedia.faiss"
|
14 |
+
kilt_wikipedia = load_dataset("kilt_wikipedia", split="full")
|
15 |
+
columns = ['kilt_id', 'wikipedia_id', 'wikipedia_title', 'text', 'anchors', 'categories',
|
16 |
+
'wikidata_info', 'history']
|
17 |
+
|
18 |
+
min_snippet_length = 20
|
19 |
+
topk = 21
|
20 |
+
|
21 |
+
def articles_to_paragraphs(examples):
|
22 |
+
ids, titles, sections, texts, start_ps, end_ps, start_cs, end_cs = [], [], [], [], [], [], [], []
|
23 |
+
for bidx, example in enumerate(examples["text"]):
|
24 |
+
last_section = ""
|
25 |
+
for idx, p in enumerate(example["paragraph"]):
|
26 |
+
if "Section::::" in p:
|
27 |
+
last_section = p
|
28 |
+
ids.append(examples["wikipedia_id"][bidx])
|
29 |
+
titles.append(examples["wikipedia_title"][bidx])
|
30 |
+
sections.append(last_section)
|
31 |
+
texts.append(p)
|
32 |
+
start_ps.append(idx)
|
33 |
+
end_ps.append(idx)
|
34 |
+
start_cs.append(0)
|
35 |
+
end_cs.append(len(p))
|
36 |
+
|
37 |
+
return {"wikipedia_id": ids, "title": titles,
|
38 |
+
"section": sections, "text": texts,
|
39 |
+
"start_paragraph_id": start_ps, "end_paragraph_id": end_ps,
|
40 |
+
"start_character": start_cs,
|
41 |
+
"end_character": end_cs
|
42 |
+
}
|
43 |
+
|
44 |
+
kilt_wikipedia_paragraphs = kilt_wikipedia.map(articles_to_paragraphs, batched=True,
|
45 |
+
remove_columns=columns,
|
46 |
+
batch_size=256, cache_file_name=f"./wiki_kilt_paragraphs_full.arrow",
|
47 |
+
desc="Expanding wiki articles into paragraphs")
|
48 |
+
|
49 |
+
# use paragraphs that are not simple fragments or very short sentences
|
50 |
+
kilt_wikipedia_paragraphs = kilt_wikipedia_paragraphs.filter(lambda x: x["end_character"] > 250)
|
51 |
+
kilt_wikipedia_paragraphs.load_faiss_index("embeddings", index_file_name, device=0)
|
52 |
+
|
53 |
+
def embed_questions_for_retrieval(questions):
|
54 |
+
query = tokenizer(questions, max_length=128, padding=True, truncation=True, return_tensors="pt")
|
55 |
+
with torch.no_grad():
|
56 |
+
q_reps = model.embed_questions(query["input_ids"].to(device),
|
57 |
+
query["attention_mask"].to(device)).cpu().type(torch.float)
|
58 |
+
return q_reps.numpy()
|
59 |
+
|
60 |
+
def query_index(question):
|
61 |
+
question_embedding = embed_questions_for_retrieval([question])
|
62 |
+
scores, wiki_passages = kilt_wikipedia_paragraphs.get_nearest_examples("embeddings", question_embedding, k=topk)
|
63 |
+
columns = ['wikipedia_id', 'title', 'text', 'section', 'start_paragraph_id', 'end_paragraph_id', 'start_character','end_character']
|
64 |
+
retrieved_examples = []
|
65 |
+
r = list(zip(wiki_passages[k] for k in columns))
|
66 |
+
for i in range(topk):
|
67 |
+
retrieved_examples.append({k: v for k, v in zip(columns, [r[j][0][i] for j in range(len(columns))])})
|
68 |
+
return retrieved_examples
|
69 |
+
|
70 |
+
questions = ["What causes the contrails (cirrus aviaticus) behind jets at high altitude? ",
|
71 |
+
"Why does water heated to a room temeperature feel colder than the air around it?"]
|
72 |
+
res_list = query_index(questions[0])
|
73 |
+
res_list = [res for res in res_list if len(res["text"].split()) > min_snippet_length][:int(topk / 3)]
|
74 |
+
for res in res_list:
|
75 |
+
print("\n")
|
76 |
+
print(res)
|
77 |
+
|
78 |
+
|
79 |
+
main()
|
80 |
+
|
81 |
+
|