|
|
|
import gradio as gr |
|
import numpy as np |
|
import torch |
|
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline |
|
|
|
from transformers import DistilBertTokenizer, TFDistilBertForQuestionAnswering |
|
|
|
|
|
|
|
|
|
|
|
model_name = "bert-large-uncased-whole-word-masking-finetuned-squad" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
|
|
|
|
|
|
model = AutoModelForQuestionAnswering.from_pretrained(model_name) |
|
|
|
def QA_function(context, question): |
|
inputs = tokenizer(question, context, add_special_tokens=True, return_tensors="pt") |
|
input_ids = inputs["input_ids"].tolist()[0] |
|
|
|
outputs = model(**inputs) |
|
answer_start_scores = outputs.start_logits |
|
answer_end_scores = outputs.end_logits |
|
|
|
|
|
|
|
answer_start_index = torch.argmax(answer_start_scores) |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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(question): |
|
QA_input = { |
|
'question': question, |
|
'context': fixed_context |
|
} |
|
res = nlp(QA_input) |
|
return res['answer'] |
|
|
|
|
|
gradio_ui = gr.Interface(get_answer, [gr.inputs.Textbox(label="Question")], gr.outputs.Textbox(label="Answer")) |
|
|
|
gradio_ui.launch() |
|
|
|
|