document_question_answering
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline
¶
Bases: ChunkPipeline
Document Question Answering pipeline using any AutoModelForDocumentQuestionAnswering
. The inputs/outputs are
similar to the (extractive) question answering pipeline; however, the pipeline takes an image (and optional OCR'd
words/boxes) as input instead of text context.
Example
>>> from transformers import pipeline
>>> document_qa = pipeline(model="impira/layoutlm-document-qa")
>>> document_qa(
... image="https://hf.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png",
... question="What is the invoice number?",
... )
[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}]
Learn more about the basics of using a pipeline in the pipeline tutorial
This document question answering pipeline can currently be loaded from [pipeline
] using the following task
identifier: "document-question-answering"
.
The models that this pipeline can use are models that have been fine-tuned on a document question answering task.
See the up-to-date list of available models on
hf-mirror.com/models.
Source code in mindnlp/transformers/pipelines/document_question_answering.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.__call__(image, question=None, word_boxes=None, **kwargs)
¶
Answer the question(s) given as inputs by using the document(s). A document is defined as an image and an
optional list of (word, box) tuples which represent the text in the document. If the word_boxes
are not
provided, it will use the Tesseract OCR engine (if available) to extract the words and boxes automatically for
LayoutLM-like models which require them as input. For Donut, no OCR is run.
You can invoke the pipeline several ways:
pipeline(image=image, question=question)
pipeline(image=image, question=question, word_boxes=word_boxes)
pipeline([{"image": image, "question": question}])
pipeline([{"image": image, "question": question, "word_boxes": word_boxes}])
PARAMETER | DESCRIPTION |
---|---|
image |
The pipeline handles three types of images:
The pipeline accepts either a single image or a batch of images. If given a single image, it can be broadcasted to multiple questions.
TYPE:
|
question |
A question to ask of the document.
TYPE:
|
word_boxes |
A list of words and bounding boxes (normalized 0->1000). If you provide this optional input, then the pipeline will use these words and boxes instead of running OCR on the image to derive them for models that need them (e.g. LayoutLM). This allows you to reuse OCR'd results across many invocations of the pipeline without having to re-run it each time.
TYPE:
|
top_k |
The number of answers to return (will be chosen by order of likelihood). Note that we return less than top_k answers if there are not enough options available within the context.
TYPE:
|
doc_stride |
If the words in the document are too long to fit with the question for the model, it will be split in several chunks with some overlap. This argument controls the size of that overlap.
TYPE:
|
max_answer_len |
The maximum length of predicted answers (e.g., only answers with a shorter length are considered).
TYPE:
|
max_seq_len |
The maximum length of the total sentence (context + question) in tokens of each chunk passed to the
model. The context will be split in several chunks (using
TYPE:
|
max_question_len |
The maximum length of the question after tokenization. It will be truncated if needed.
TYPE:
|
handle_impossible_answer |
Whether or not we accept impossible as an answer.
TYPE:
|
lang |
Language to use while running OCR. Defaults to english.
TYPE:
|
tesseract_config |
Additional flags to pass to tesseract while running OCR.
TYPE:
|
timeout |
The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and the call may block forever.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
A
|
Source code in mindnlp/transformers/pipelines/document_question_answering.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.__init__(*args, **kwargs)
¶
Initializes a new instance of the DocumentQuestionAnsweringPipeline class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raised if a slow tokenizer is provided instead of a fast tokenizer. |
ValueError
|
Raised if a VisionEncoderDecoder model other than Donut is provided. |
ValueError
|
Raised if an unsupported VisionEncoderDecoder model is provided. |
Source code in mindnlp/transformers/pipelines/document_question_answering.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.postprocess(model_outputs, top_k=1, **kwargs)
¶
This method 'postprocess' is defined in the class 'DocumentQuestionAnsweringPipeline' and is used to process the model outputs and return the top-k answers.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the 'DocumentQuestionAnsweringPipeline' class.
|
model_outputs |
The list of model outputs to be processed.
TYPE:
|
top_k |
The number of top answers to be returned. Default value is 1.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list
|
A list of top-k answers containing dictionaries with information about the answers. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the model_type attribute is not of type ModelType.VisionEncoderDecoder. |
ValueError
|
If the top_k parameter is not a positive integer. |
Source code in mindnlp/transformers/pipelines/document_question_answering.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.postprocess_encoder_decoder_single(model_outputs, **kwargs)
¶
This method postprocesses the output from the encoder-decoder model to extract the answer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the DocumentQuestionAnsweringPipeline class. |
model_outputs |
A dictionary containing the model outputs with the key 'sequences'.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the processed answer under the key 'answer'.
|
Source code in mindnlp/transformers/pipelines/document_question_answering.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.postprocess_extractive_qa(model_outputs, top_k=1, handle_impossible_answer=False, max_answer_len=15, **kwargs)
¶
This method postprocess_extractive_qa is defined within the class DocumentQuestionAnsweringPipeline. It post-processes the model outputs for extractive question answering.
PARAMETER | DESCRIPTION |
---|---|
self |
(object) The instance of the class.
|
model_outputs |
(list) The list of model outputs containing information such as words, start_logits, end_logits, p_mask, attention_mask, and word_ids.
|
top_k |
(int) The maximum number of answers to consider for each model output. Default is 1.
DEFAULT:
|
handle_impossible_answer |
(bool) A flag indicating whether to handle impossible answers. Default is False.
DEFAULT:
|
max_answer_len |
(int) The maximum length of the answer. Default is 15.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in mindnlp/transformers/pipelines/document_question_answering.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
mindnlp.transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline.preprocess(inputs, padding='do_not_pad', doc_stride=None, max_seq_len=None, word_boxes=None, lang=None, tesseract_config='', timeout=None)
¶
Preprocesses inputs for document question answering.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the DocumentQuestionAnsweringPipeline class. |
inputs |
The inputs for preprocessing.
TYPE:
|
padding |
The padding strategy to use. Defaults to 'do_not_pad'.
TYPE:
|
doc_stride |
The stride for splitting the document into chunks. Defaults to None.
TYPE:
|
max_seq_len |
The maximum sequence length. Defaults to None.
TYPE:
|
word_boxes |
The word boxes for the document. Defaults to None.
TYPE:
|
lang |
The language for OCR. Defaults to None.
TYPE:
|
tesseract_config |
The configuration for Tesseract OCR. Defaults to ''.
TYPE:
|
timeout |
The timeout for loading images. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If max_seq_len is not provided and the tokenizer's model_max_length is also not set. |
ValueError
|
If doc_stride is not provided and the default value cannot be determined. |
ValueError
|
If using a VisionEncoderDecoderModel without a feature extractor. |
ValueError
|
If word_boxes are not provided and OCR is used but pytesseract is not available. |
ValueError
|
If neither an image nor word_boxes are provided. |
Source code in mindnlp/transformers/pipelines/document_question_answering.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
|