File size: 1,658 Bytes
4506e75
b46f4f3
f132c1d
 
 
 
 
 
 
 
 
 
 
21c103a
 
 
 
 
 
 
f132c1d
b46f4f3
 
 
 
21c103a
b46f4f3
 
 
 
f132c1d
21c103a
fc9195f
21c103a
f132c1d
 
 
 
 
 
 
 
 
 
 
4506e75
f132c1d
 
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
import gradio as gr
import torch

# Import libraries from transformers
from transformers import AutoTokenizer, AutoModelForQuestionAnswering

# Define model and tokenizer
model_name = "google-bert/bert-large-uncased-whole-word-masking-finetuned-squad"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)


def answer_question(context, question):
  """
  This function takes a context and question as input,
  performs question answering using the loaded model,
  and returns the predicted answer.
  """
  # Encode the context and question with special character handling
  inputs = tokenizer(context, question, return_tensors="pt", truncation=True)

  # Perform question answering
  outputs = model(**inputs)

  # Get the predicted start and end token positions
  start_scores, end_scores = outputs.start_logits, outputs.end_scores

  # Decode the answer based on predicted positions
  answer_start = torch.argmax(start_scores)
  answer_end = torch.argmax(end_scores) + 1

  # Get answer tokens and convert them to string, removing special tokens
  answer = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end])
  answer = "".join(answer[2:-2])  # Remove special tokens ([CLS] and [SEP])

  return answer

# Define the Gradio interface
interface = gr.Interface(
    fn=answer_question,
    inputs=[gr.Textbox("Context"), gr.Textbox("Question")],
    outputs="text",
    title="Question Answering with BERT",
    description="Ask a question about the provided context and get an answer powered by Google BERT model.",
)

# Launch the Gradio app
interface.launch()