File size: 2,899 Bytes
0428e35
10e8a8b
0da9084
 
 
10e8a8b
0da9084
 
 
 
 
9072259
 
0da9084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0428e35
 
 
9072259
0da9084
52ad77b
 
 
 
 
 
 
 
 
0da9084
 
0428e35
0da9084
 
 
 
0428e35
 
0da9084
 
10e8a8b
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
72

import gradio as gr
import numpy as np
import torch
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline

from transformers import DistilBertTokenizer, TFDistilBertForQuestionAnswering

# Load model & tokenizer
# model_name = "deepset/roberta-base-squad2"
# model_name = "AmazonScience/qanlu"
# model_name = 'distilbert-base-cased-distilled-squad'
model_name = "bert-large-uncased-whole-word-masking-finetuned-squad"
# tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
tokenizer = AutoTokenizer.from_pretrained(model_name)

# tokenizer = DistilBertTokenizer.from_pretrained(model_name)
# model = TFDistilBertForQuestionAnswering.from_pretrained(model_name)

# model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained(model_name)

def QA_function(context, question):
    inputs = tokenizer(question, context, add_special_tokens=True, return_tensors="pt") # pt for PyTorch tf for tensorflow
    input_ids = inputs["input_ids"].tolist()[0]

    outputs = model(**inputs)
    answer_start_scores = outputs.start_logits
    answer_end_scores = outputs.end_logits

    ## following for torch
    # Get the most likely beginning of answer with the argmax of the score
    answer_start_index = torch.argmax(answer_start_scores)
    # Get the most likely end of answer with the argmax of the score
    answer_end_index = torch.argmax(answer_end_scores) + 1

    answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start_index:answer_end_index]))
    return answer

# gradio_ui = gr.Interface(QA_function, [gr.inputs.Textbox(lines=7, label="Context"), gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer"))



# a) Get predictions
nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
fixed_context = """Ishaan is 6 year old kid. he is very good in football. He is very good sports person.
he is smart kid. He can run very fast. as fast as 10 meters in 1 minute. 
He goes to Vidyani ketan school. He goes to school from 8 am to 3:30 pm.
Ishaan has many friends. Vineet is Ishaan's brother. """

# def get_answer(fixed_context,question):
#     QA_input = {
#         'question': question,
#         'context': fixed_context 
#     }
#     res = nlp(QA_input)
#     return res['answer']

def get_answer(question):
    QA_input = {
        'question': question,
        'context': fixed_context 
    }
    res = nlp(QA_input)
    return res['answer']

# gradio_ui = gr.Interface(get_answer, [gr.inputs.Textbox(lines=7, label="Context"), gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer"))
gradio_ui = gr.Interface(get_answer, [gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer"))

gradio_ui.launch()