How to Use a Simple-Transformer to Answer a Question using Question Answer Model

How to Use a Simple-Transformer to Answer a Question using Question Answer Model

How to Use a Simple-Transformer to Answer a Question

Simple

Simple Transformer is a Package for using the Transformer in a very simple way.

The Simple Transformers provides Built-in support for

  • Text Classification
  • Token Classification
  • Question Answering
  • Language Generation
  • Multi-Model Classification
  • Conversational AI
  • text Representation Generation

From the SimpleTransformer package, we will be using QuestionAnsweringModel class, The QuestionAnsweringModel class is used for Question Answering.

Before we run code, we need to install some packages, Now you can do it using pip or conda

pip install simpletransformers

or Install Anaconda or Miniconda Package Manager from here

Create a new virtual environment and install the required packages.

conda create -n transformers python pandas tqdm
conda activate transformers

If using cuda:

conda install pytorch cudatoolkit=10.0 -c pytorch

else:

conda install pytorch cpuonly -c pytorch
conda install -c anaconda scipy
conda install -c anaconda scikit-learn
pip install transformers
pip install tensorboardx

Install simpletransformers.

pip install simpletransformers

Now once we have it installed we can create a model and then test it.

First we need to import the packages

from simpletransformers.question_answering import QuestionAnsweringModel
import joblib

Next we load the model trained on SQUAD Dataset

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable.

model = QuestionAnsweringModel('distilbert', 'distilbert-base-uncased-distilled-squad',use_cuda=False)

Next we need to create a structure before sending the data to model

question_data = {
  'qas': 
  [{'question': 'What color is the sky?',
    'id': 0,
    'answers': [{'text': ' ', 'answer_start': 0}],
    'is_impossible': False}],
  'context': 'the sky is blue'
  }

prediction = model.predict([question_data])
print(prediction)

The output for the above snippet will be:

([{'id': 0,
   'answer': ['blue',
    'the sky is blue',
    'sky is blue',
    'is blue',
    'the sky',
    'the',
    'sky',
    'the sky is',
    'sky is',
    '']}],
 [{'id': 0,
   'probability': [0.9418068800031401,
    0.056266636887763996,
    0.0018261808912823114,
    9.186660627085428e-05,
    7.837123579566468e-06,
    2.5976516459405675e-07,
    2.5436041880680914e-07,
    7.941122922155865e-08,
    2.5773580469528015e-09,
    2.2441380618877487e-09]}])

Now we can save the model

filename = 'SQuAdQnA_model.pkl'
joblib.dump(model,filename=filename)

Now we ll create a function which takes the model, question and context/paragraph as input and return us the answer

def predict_answer(model, question, contexts, seq_len=512, debug=False):
    question_data = {
    'qas': 
    [{'question': str(question),
      'id': 0,
     'answers': [{'text': ' ', 'answer_start': 0}],
     'is_impossible': False}],
   'context': str(contexts)
     }

    prediction = model.predict([question_data])
    return prediction

Now we can use it just by calling this function

predict_answer(model, 'what color is the chair', 'the chair is black in color')

Output

([{'id': 0,
   'answer': ['black',
    'black in color',
    'the chair is black',
    'the chair is black in color',
    'chair is black',
    'is black',
    'chair is black in color',
    'black in',
    'color',
    'is black in color',
    'in color',
    'the chair',
    'the chair is black in',
    'the',
    'the chair is',
    'chair',
    '',
    'chair is black in',
    'is black in']}],
 [{'id': 0,
   'probability': [0.7689673194580006,
    0.16377263987992158,
    0.054205245122107656,
    0.011544490727178001,
    0.0008895552249926878,
    0.0002252454968969322,
    0.00018945513525687235,
    5.127989525717933e-05,
    5.083241616990137e-05,
    4.7972194285026544e-05,
    3.465572588844964e-05,
    1.586489256458498e-05,
    3.6147690830484374e-06,
    9.358570589034519e-07,
    4.7629059449005263e-07,
    2.603566876781512e-07,
    7.136439660746618e-08,
    5.932150510018274e-08,
    1.5020879555931335e-08]}])

As you see, It is so easy to use the Simple Transformer Package to solve many NLP problem statements.