How to create your own Question-Answering system easily with python

How to create your own Question-Answering system easily with pythonHow to create a QA System on your own (private) data with cdQA-suiteAndré Macedo FariasBlockedUnblockFollowFollowingJul 7The history of Machine Comprehension (MC) has its origins along with the birth of first concepts in Artificial Intelligence (AI).

The brilliant Allan Turing proposed in his famous article “Computing Machinery and Intelligence” what is now called the Turing test as a criterion of intelligence.

Almost 70 years later, Question Answering (QA), a sub-domain of MC, is still one of the most difficult tasks in AI.

However, since last year, the field of Natural Language Processing (NLP) has experienced a fast evolution thanks to the development in Deep Learning research and the advent of Transfer Learning techniques.

Powerful pre-trained NLP models such as OpenAI-GPT, ELMo, BERT and XLNet have been made available by the best researchers of the domain.

With such progress, several improved systems and applications to NLP tasks are expected to come out.

One of such systems is the cdQA-suite, a package developed by some colleagues and me in a partnership between Telecom ParisTech, a French engineering school, and BNP Paribas Personal Finance, a European leader in financing for individuals.

Open-domain QA vs.

closed-domain QAWhen we think about QA systems we should be aware of two different kinds of systems: open-domain QA (ODQA) systems and closed-domain QA (CDQA) systems.

Open-domain systems deal with questions about nearly anything, and can only rely on general ontologies and world knowledge.

One example of such a system is DrQA, an ODQA developed by Facebook Research that uses a large base of articles from Wikipedia as its source of knowledge.

As these documents are related to several different topics and subjects we can understand why this system is considered an ODQA.

On the other hand, closed-domain systems deal with questions under a specific domain (for example, medicine or automotive maintenance), and can exploit domain-specific knowledge by using a model that is fitted to a unique-domain database.

The cdQA-suite was built to enable anyone who wants to build a closed-domain QA system easily.

cdQA-suitecdQAAn End-To-End Closed Domain Question Answering System.

– cdQAgithub.

comThe cdQA-suite is comprised of three blocks:cdQA: an easy-to-use python package to implement a QA pipelinecdQA-annotator: a tool built to facilitate the annotation of question-answering datasets for model evaluation and fine-tuningcdQA-ui: a user-interface that can be coupled to any website and can be connected to the back-end system.

I will explain how each module works and how you can use it to build your QA system on your own data.

cdQAThe cdQA architecture is based on two main components: the Retriever and the Reader.

You can see below a schema of the system mechanism.

Mechanism of cdQA pipelineWhen a question is sent to the system, the Retriever selects a list of documents in the database that are the most likely to contain the answer.

It is based on the same retriever of DrQA, which creates TF-IDF features based on uni-grams and bi-grams and compute the cosine similarity between the question sentence and each document of the database.

After selecting the most probable documents, the system divides each document into paragraphs and send them with the question to the Reader, which is basically a pre-trained Deep Learning model.

The model used was the Pytorch version of the well known NLP model BERT, which was made available by HuggingFace.

Then, the Reader outputs the most probable answer it can find in each paragraph.

After the Reader, there is a final layer in the system that compares the answers by using an internal score function and outputs the most likely one according to the scores.

Using the cdQA python packageBefore starting using the package, let's install it.

You can install it using pip install cdqa , but for this tutorial, I will install it from the source so I can run a script that downloads pre-trained models and the BNP dataset (a dataset with articles extracted from their public news webpage).

# Setting up cdQA packagegit clone https://github.

com/cdqa-suite/cdQA.

git &&cd cdQA &&pip install .

# Download models and BNP datasetpython download.

pyNow, you can open a jupyter notebook and follow the steps below to see how cdQA works:You should have something like the following as output:The output of a QAPipeline predictionYou can notice that the system does not only output an answer, but also the paragraph where the answer was found and the title of the document / article.

In the snippet above, the preprocessing / filtering steps were needed to transform the BNP Paribas dataframe to the following structure:Structure of the Dataframe that should be sent to cdQA pipelineIf you use your own dataset, please be sure that your dataframe has such structure.

When using the CPU version of the model, each prediction takes between 10 and 20 seconds to be done.

This moderate execution time is due to the BERT Reader, which a very large deep learning model.

If you have a GPU, you can use directly the GPU version of the model models/bert_qa_vGPU-sklearn.

joblib.

These pre-trained models are also available on the releases page of cdQA github: https://github.

com/cdqa-suite/cdQA/releasesTraining / Fine-tuning the readerYou can also improve the performance of the pre-trained Reader, which was pre-trained on SQuAD 1.

1 dataset.

If you have an annotated dataset (that can be generated by the help of the cdQA-annotator) in the same format as SQuAD dataset you can fine-tune the reader on it:# Put the path to your json file in SQuAD format herepath-to-data = '.

/data/SQuAD_1.

1/train-v1.

1.

json'cdqa_pipeline.

fit_reader(path-to-data)Please be aware that such fine-tuning should be performed using GPU as the BERT model is too large to be trained with CPU.

You can also check out other ways to do the same steps on the official tutorials: https://github.

com/cdqa-suite/cdQA/tree/master/examplescdQA-annotatorIn order to facilitate the data annotation, the team has built a web-based application, the cdQA-annotator.

In order to use it, you should have your dataset transformed to a JSON file with SQuAD-like format:from cdqa.

json')Now you can install the annotator and run it:# Clone the repogit clone https://github.

com/cdqa-suite/cdQA-annotator# Install dependenciescd cdQA-annotatornpm install# Start development servercd srcvue serveNow you can go to http://localhost:8080/ and after loading your JSON file you will see something like this:cdQA-annotator interfaceTo start annotating question-answer pairs you just need to write a question, highlight the answer with the mouse cursor (the answer will be written automatically), and then click on Add annotation:Annotating question-answer pairs with cdQA-annotatorAfter the annotation, you can download it and use it to fine-tune the BERT Reader on your own data as explained in the previous section.

cdQA-uiThe team also has provided a web-based user interface to couple with cdQA.

In this section, I will describe how you can use de UI linked to the back-end of cdQA.

First, you have to deploy a cdQA REST API by executing on your shell (be sure you run it on cdQA folder):export dataset_path = 'path-to-dataset.

csv'export reader_path = 'path-to-reader-model'FLASK_APP=api.

py flask run -h 0.

0.

0.

0Second, you should proceed to the installation of the cdQA-ui package:git clone https://github.

com/cdqa-suite/cdQA-ui &&cd cdQA-ui npm installThen, you start the develpoment server:npm run serveYou can now access the web application on http://localhost:8080/.

You will see something like the figure below:Web application of cdQA-uiAs the application is well connected to the back-end, via the REST API, you can ask a question and the application will display an answer, the passage context where the answer was found and the title of the article:Demonstration of the web application runningInserting the interface in a web-siteIf you want to couple the interface on your website you just need do the following imports in your Vue app:import Vue from 'vue'import CdqaUI from 'cdqa-ui'Vue.