Views
No views yet
python3 -m spacy download en_core_web_md1import argparse
2
3import requests
4import torch
5
6from drqa import DrQA
7from drqa.utils import str2bool
8
9parser = argparse.ArgumentParser(
10 description='Interact with document reader model.'
11)
12parser.add_argument('--model-file', help='path to model file')
13parser.add_argument('--meta-file', help='path to meta.msgpack file')
14parser.add_argument("--cuda", type=str2bool, nargs='?',
15 const=True, default=torch.cuda.is_available(),
16 help='whether to use GPU acceleration.')
17args = parser.parse_args()
18
19
20def get_wiki_evidence(search_term):
21 try:
22 search_url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={search_term}&format=json&srlimit=1"
23 r = requests.get(search_url).json()
24 page_id = r['query']['search'][0]['pageid']
25 results_url = f"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&pageids={page_id}"
26 r = requests.get(results_url).json()
27 evidence = r['query']['pages'][str(page_id)]['extract']
28 return evidence
29 except:
30 print("wiki search failed")
31 return None
32
33
34dr = DrQA(args.model_file, args.meta_file, cuda=args.cuda)
35while True:
36 search_term = input("wiki search:")
37 evidence = get_wiki_evidence(search_term)
38 if not evidence:
39 continue
40 question = input("question:")
41 answer = dr.predict(evidence, question)
42 print(">", answer)wiki search:dog
question:when were dogs domesticated
> 15,000 years ago
wiki search:abraham lincoln
question:when abraham lincoln was born
> February 12, 1809 – April 15, 1865
wiki search:cat
question:when were cats domesticated
> 3100 BC
wiki search:chicken
question:what is the global population of chickens
> 23.7 billion as of 2018, up from more than 19 billion in 2011.