|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from transformers import AutoTokenizer, AutoModelForQuestionAnswering |
|
import torch |
|
import torch.nn.functional as F |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-cased-distilled-squad") |
|
model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-cased-distilled-squad") |
|
|
|
def question_answering(context, question): |
|
|
|
inputs = tokenizer.encode_plus(question, context, return_tensors="pt") |
|
|
|
|
|
input_ids = inputs["input_ids"].tolist()[0] |
|
|
|
|
|
outputs = model(**inputs) |
|
start_logits = outputs.start_logits |
|
end_logits = outputs.end_logits |
|
|
|
|
|
start_index = torch.argmax(start_logits) |
|
end_index = torch.argmax(end_logits) + 1 |
|
|
|
|
|
start_probs = F.softmax(start_logits, dim=-1) |
|
end_probs = F.softmax(end_logits, dim=-1) |
|
|
|
|
|
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[start_index:end_index])) |
|
|
|
|
|
confidence_score = start_probs[0][start_index].item() * end_probs[0][end_index-1].item() |
|
|
|
return answer, str(confidence_score * 100) |
|
|
|
|
|
iface = gr.Interface( |
|
fn = question_answering, |
|
inputs = [ |
|
gr.Textbox(label = "Context", placeholder = "Enter your context here..", lines = 5), |
|
gr.Textbox(label = "Question", placeholder = "Enter your question", lines = 2) |
|
], |
|
outputs = [ |
|
gr.Textbox(label = "Answer", lines = 2), |
|
gr.Textbox(label = "Accuracy") |
|
] |
|
) |
|
|
|
iface.launch() |