pragnakalp commited on
Commit
849b6e1
1 Parent(s): 09ccbde

Upload run_qg.py

Browse files
Files changed (1) hide show
  1. run_qg.py +73 -0
run_qg.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import numpy as np
3
+ from questiongenerator import QuestionGenerator
4
+ from questiongenerator import print_qa
5
+
6
+ def main():
7
+ parser = argparse.ArgumentParser()
8
+ parser.add_argument(
9
+ "--text_dir",
10
+ default=None,
11
+ type=str,
12
+ required=True,
13
+ help="The text that will be used as context for question generation.",
14
+ )
15
+ parser.add_argument(
16
+ "--model_dir",
17
+ default=None,
18
+ type=str,
19
+ help="The folder that the trained model checkpoints are in.",
20
+ )
21
+ parser.add_argument(
22
+ "--num_questions",
23
+ default=10,
24
+ type=int,
25
+ help="The desired number of questions to generate.",
26
+ )
27
+ parser.add_argument(
28
+ "--answer_style",
29
+ default="all",
30
+ type=str,
31
+ help="The desired type of answers. Choose from ['all', 'sentences', 'multiple_choice']",
32
+ )
33
+ parser.add_argument(
34
+ "--show_answers",
35
+ default='True',
36
+ type=parse_bool_string,
37
+ help="Whether or not you want the answers to be visible. Choose from ['True', 'False']",
38
+ )
39
+ parser.add_argument(
40
+ "--use_qa_eval",
41
+ default='True',
42
+ type=parse_bool_string,
43
+ help="Whether or not you want the generated questions to be filtered for quality. Choose from ['True', 'False']",
44
+ )
45
+ args = parser.parse_args()
46
+
47
+ with open(args.text_dir, 'r') as file:
48
+ text_file = file.read()
49
+
50
+ qg = QuestionGenerator(args.model_dir)
51
+
52
+ qa_list = qg.generate(
53
+ text_file,
54
+ num_questions=int(args.num_questions),
55
+ answer_style=args.answer_style,
56
+ use_evaluator=args.use_qa_eval
57
+ )
58
+ print_qa(qa_list, show_answers=args.show_answers)
59
+
60
+ # taken from https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
61
+ def parse_bool_string(s):
62
+ if isinstance(s, bool):
63
+ return s
64
+ if s.lower() in ('yes', 'true', 't', 'y', '1'):
65
+ return True
66
+ elif s.lower() in ('no', 'false', 'f', 'n', '0'):
67
+ return False
68
+ else:
69
+ raise argparse.ArgumentTypeError('Boolean value expected.')
70
+
71
+
72
+ if __name__ == "__main__":
73
+ main()