sbudni commited on
Commit
0da9084
1 Parent(s): 10e8a8b

Adding initial QA model

Browse files
Files changed (1) hide show
  1. app.py +53 -4
app.py CHANGED
@@ -1,7 +1,56 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
5
 
6
+ from transformers import DistilBertTokenizer, TFDistilBertForQuestionAnswering
7
+
8
+ # Load model & tokenizer
9
+ # model_name = "deepset/roberta-base-squad2"
10
+ # model_name = "AmazonScience/qanlu"
11
+ model_name = 'distilbert-base-cased-distilled-squad'
12
+ # tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+
15
+ # tokenizer = DistilBertTokenizer.from_pretrained(model_name)
16
+ # model = TFDistilBertForQuestionAnswering.from_pretrained(model_name)
17
+
18
+ # model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
19
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
20
+
21
+ def QA_function(context, question):
22
+ inputs = tokenizer(question, context, add_special_tokens=True, return_tensors="pt") # pt for PyTorch tf for tensorflow
23
+ input_ids = inputs["input_ids"].tolist()[0]
24
+
25
+ outputs = model(**inputs)
26
+ answer_start_scores = outputs.start_logits
27
+ answer_end_scores = outputs.end_logits
28
+
29
+ ## following for torch
30
+ # Get the most likely beginning of answer with the argmax of the score
31
+ answer_start_index = torch.argmax(answer_start_scores)
32
+ # Get the most likely end of answer with the argmax of the score
33
+ answer_end_index = torch.argmax(answer_end_scores) + 1
34
+
35
+ answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start_index:answer_end_index]))
36
+ return answer
37
+
38
+ # gradio_ui = gr.Interface(QA_function, [gr.inputs.Textbox(lines=7, label="Context"), gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer"))
39
+
40
+
41
+
42
+ # a) Get predictions
43
+ nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
44
+
45
+ def get_answer(context,question):
46
+ QA_input = {
47
+ 'question': question,
48
+ 'context': context
49
+ }
50
+ res = nlp(QA_input)
51
+ return res['answer']
52
+
53
+ gradio_ui = gr.Interface(get_answer, [gr.inputs.Textbox(lines=7, label="Context"), gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer"))
54
+
55
+ gradio_ui.launch()
56