-
Notifications
You must be signed in to change notification settings - Fork 0
/
BertQA.lf
71 lines (63 loc) · 2.12 KB
/
BertQA.lf
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
/**
* @file BertQA.lf
* @author Vincenzo Barbuto
* @brief Examples of how to use the NLP library to perform question answering.
*/
target Python
import BertQuestionAnswer from "lib/NLP.lf"
/**
* Defines two reactors, `Injector` and `ResultPrinter`, that work together to perform question
* answering using a BERT-based model.
*
* The `Injector` reactor is responsible for prompting the user to enter a question, and passing
* that question to the `BertQuestionAnswer` reactor. It has an input `trigger` and an output
* `text`.
*
* The `ResultPrinter` reactor is responsible for printing the answer(s) returned by the
* `BertQuestionAnswer` reactor, as well as the inference time. It has inputs `results` and
* `inference_time`, and an output `trigger`.
*
* The `main` reactor creates instances of the `Injector`, `ResultPrinter`, and `BertQuestionAnswer`
* reactors, and connects them together to form the complete question answering system.
*/
reactor Injector {
input trigger
output text
preamble {=
def get_text(self):
text = input("Ask questions about Lingua-Franca ('q' to exit): ")
if text == "q":
print("Exiting...")
request_stop()
return text
=}
reaction(startup) -> text {=
print("Starting TextInput reactor")
text.set(self.get_text())
=}
reaction(trigger) -> text {=
text.set(self.get_text())
=}
}
reactor ResultPrinter {
input results
input inference_time
output trigger
reaction(results, inference_time) -> trigger {=
for result in results.value:
print(f"Answer N. {result['index']+1}: {result['answer']}")
print(f"Inference time: {inference_time.value} ms")
trigger.set(1)
=}
}
main reactor {
answerer = new BertQuestionAnswer(
model = {= os.path.join(os.getcwd(),"models/nlp/qa/albert.tflite") =},
context_file = {= os.path.join(os.getcwd(),"util", "lf_docs.txt") =})
injector = new Injector()
printer = new ResultPrinter()
injector.text -> answerer.input_data
answerer.results -> printer.results
answerer.inference_time -> printer.inference_time
printer.trigger ~> injector.trigger after 2 ms
}