sparknlp.annotator.classifier_dl.tapas_for_question_answering#

Module Contents#

Classes#

TapasForQuestionAnswering

TapasForQuestionAnswering is an implementation of TaPas - a BERT-based model specifically designed for

class TapasForQuestionAnswering(classname='com.johnsnowlabs.nlp.annotators.classifier.dl.TapasForQuestionAnswering', java_model=None)[source]#

TapasForQuestionAnswering is an implementation of TaPas - a BERT-based model specifically designed for answering questions about tabular data. It takes TABLE and DOCUMENT annotations as input and tries to answer the questions in the document by using the data from the table. The model is based in BertForQuestionAnswering and shares all its parameters with it.

Pretrained models can be loaded with pretrained() of the companion object:

>>> tapas = TapasForQuestionAnswering.pretrained() \
...     .setInputCols(["table", "document"]) \
...     .setOutputCol("answer")

The default model is "table_qa_tapas_base_finetuned_wtq", if no name is provided.

For available pretrained models please see the Models Hub.

Input Annotation types

Output Annotation type

DOCUMENT, TABLE

CHUNK

Parameters:
batchSize

Batch size. Large values allows faster processing but requires more memory, by default 2

caseSensitive

Whether to ignore case in tokens for embeddings matching, by default False

configProtoBytes

ConfigProto from tensorflow, serialized into byte array.

maxSentenceLength

Max sentence length to process, by default 512

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from pyspark.ml import Pipeline
>>>
>>> document_assembler = MultiDocumentAssembler()\
...     .setInputCols("table_json", "questions")\
...     .setOutputCols("document_table", "document_questions")
>>>
>>> sentence_detector = SentenceDetector()\
...     .setInputCols(["document_questions"])\
...     .setOutputCol("questions")
>>>
>>> table_assembler = TableAssembler()\
...     .setInputCols(["document_table"])\
...     .setOutputCol("table")
>>>
>>> tapas = TapasForQuestionAnswering\
...     .pretrained()\
...     .setInputCols(["questions", "table"])\
...     .setOutputCol("answers")
>>>
>>> pipeline = Pipeline(stages=[
...     document_assembler,
...     sentence_detector,
...     table_assembler,
...     tapas])
>>>
>>> json_data = """
... {
...     "header": ["name", "money", "age"],
...     "rows": [
...     ["Donald Trump", "$100,000,000", "75"],
...     ["Elon Musk", "$20,000,000,000,000", "55"]
...     ]
...  }
...  """
>>> model = pipeline.fit(data)
>>> model\
...     .transform(data)\
...     .selectExpr("explode(answers) AS answer")\
...     .select("answer.metadata.question", "answer.result")\
...     .show(truncate=False)
+-----------------------+----------------------------------------+
|question               |result                                  |
+-----------------------+----------------------------------------+
|Who earns 100,000,000? |Donald Trump                            |
|Who has more money?    |Elon Musk                               |
|How much they all earn?|COUNT($100,000,000, $20,000,000,000,000)|
|How old are they?      |AVERAGE(75, 55)                         |
+-----------------------+----------------------------------------+
static loadSavedModel(folder, spark_session)[source]#

Loads a locally saved model.

Parameters:
folderstr

Folder of the saved model

spark_sessionpyspark.sql.SparkSession

The current SparkSession

Returns:
TapasForQuestionAnswering

The restored model

static pretrained(name='table_qa_tapas_base_finetuned_wtq', lang='en', remote_loc=None)[source]#

Downloads and loads a pretrained model.

Parameters:
namestr, optional

Name of the pretrained model, by default “table_qa_tapas_base_finetuned_wtq”

langstr, optional

Language of the pretrained model, by default “en”

remote_locstr, optional

Optional remote address of the resource, by default None. Will use Spark NLPs repositories otherwise.

Returns:
TapasForQuestionAnswering

The restored model