


'''simple docstring'''
import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image

		from transformers.image_utils import load_image
else:






		class        lowercase     :

				"""simple docstring"""

				@staticmethod
				def    lowerCAmelCase__    (		*UpperCamelCase_   ,       **UpperCamelCase_							):

									'''simple docstring'''


									pass



		def       a				(      __a       )  ->     Any:



							'''simple docstring'''






							return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
__snake_case											=					(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)






@is_pipeline_test
@require_torch
@require_vision
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		_a   			=    MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




		@require_pytesseract
		@require_vision
		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			pipeline(
							    '''document-question-answering'''   ,       model=__snake_case   ,       tokenizer=__snake_case   ,       image_processor=__snake_case							)

							UpperCamelCase__       :Tuple							 =			INVOICE_URL
							UpperCamelCase__       :str							 =			list(zip(*apply_tesseract(load_image(__snake_case							)   ,       __snake_case   ,       ''''''							)							)							)
							UpperCamelCase__       :int							 =			'What is the placebo?'
							UpperCamelCase__       :Tuple							 =			[
							    {
							        'image': load_image(__snake_case							),
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							        'word_boxes': word_boxes,
							    },
							]
							return dqa_pipeline, examples




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			dqa_pipeline(__snake_case   ,       top_k=2							)
							self.assertEqual(
							    __snake_case   ,       [
							        [
							            {'''score''': ANY(__snake_case							), '''answer''': ANY(__snake_case							), '''start''': ANY(__snake_case							), '''end''': ANY(__snake_case							)},
							            {'''score''': ANY(__snake_case							), '''answer''': ANY(__snake_case							), '''start''': ANY(__snake_case							), '''end''': ANY(__snake_case							)},
							        ]
							    ]
							    * 3   ,       )




		@require_torch
		@require_detectrona
		@require_pytesseract
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :List[Any]							 =			pipeline('''document-question-answering'''   ,       model='''hf-internal-testing/tiny-random-layoutlmv2'''							)
							UpperCamelCase__       :Tuple							 =			INVOICE_URL
							UpperCamelCase__       :Optional[int]							 =			'How many cats are there?'

							UpperCamelCase__       :List[Any]							 =			[
							    {'score': 0.0001, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
							    {'score': 0.0001, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
							]
							UpperCamelCase__       :List[Any]							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(nested_simplify(__snake_case   ,       decimals=4							)   ,       __snake_case							)

							UpperCamelCase__       :List[str]							 =			dqa_pipeline({'''image''': image, '''question''': question}   ,       top_k=2							)
							self.assertEqual(nested_simplify(__snake_case   ,       decimals=4							)   ,       __snake_case							)

							# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
							# Empty answer probably
							UpperCamelCase__       :Tuple							 =			'./tests/fixtures/tests_samples/COCO/000000039769.png'
							UpperCamelCase__       :int							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(__snake_case   ,       []							)

							# We can optionnally pass directly the words and bounding boxes
							UpperCamelCase__       :List[Any]							 =			'./tests/fixtures/tests_samples/COCO/000000039769.png'
							UpperCamelCase__       :Tuple							 =			[]
							UpperCamelCase__       :List[str]							 =			[]
							UpperCamelCase__       :Dict							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       words=__snake_case   ,       boxes=__snake_case   ,       top_k=2							)
							self.assertEqual(__snake_case   ,       []							)




		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			pipeline(
							    '''document-question-answering'''   ,       model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''   ,       revision='''9977165'''   ,       )
							UpperCamelCase__       :Dict							 =			INVOICE_URL
							UpperCamelCase__       :List[Any]							 =			'What is the invoice number?'

							UpperCamelCase__       :List[str]							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )

							UpperCamelCase__       :List[str]							 =			dqa_pipeline({'''image''': image, '''question''': question}   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )

							UpperCamelCase__       :Union[str, Any]							 =			dqa_pipeline(
							    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        [
							            {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							            {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        ],
							    ]
							    * 2   ,       )




		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			pipeline(
							    '''document-question-answering'''   ,       model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''   ,       revision='''9977165'''   ,       max_seq_len=50   ,       )
							UpperCamelCase__       :str							 =			INVOICE_URL
							UpperCamelCase__       :List[Any]							 =			'What is the invoice number?'

							UpperCamelCase__       :Union[str, Any]							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							        {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )

							UpperCamelCase__       :Tuple							 =			dqa_pipeline({'''image''': image, '''question''': question}   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							        {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )

							UpperCamelCase__       :str							 =			dqa_pipeline(
							    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        [
							            {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							            {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        ]
							    ]
							    * 2   ,       )




		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			AutoTokenizer.from_pretrained(
							    '''impira/layoutlm-document-qa'''   ,       revision='''3dc6de3'''   ,       add_prefix_space=__snake_case							)
							UpperCamelCase__       :List[str]							 =			pipeline(
							    '''document-question-answering'''   ,       model='''impira/layoutlm-document-qa'''   ,       tokenizer=__snake_case   ,       revision='''3dc6de3'''   ,       )
							UpperCamelCase__       :Tuple							 =			INVOICE_URL
							UpperCamelCase__       :Union[str, Any]							 =			'What is the invoice number?'

							UpperCamelCase__       :Optional[int]							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							    ]   ,       )

							UpperCamelCase__       :List[Any]							 =			dqa_pipeline({'''image''': image, '''question''': question}   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							    ]   ,       )

							UpperCamelCase__       :List[Any]							 =			dqa_pipeline(
							    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        [
							            {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							            {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							        ]
							    ]
							    * 2   ,       )

							UpperCamelCase__       :Tuple							 =			list(zip(*apply_tesseract(load_image(__snake_case							)   ,       __snake_case   ,       ''''''							)							)							)

							# This model should also work if `image` is set to None
							UpperCamelCase__       :int							 =			dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
							    ]   ,       )




		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			AutoTokenizer.from_pretrained(
							    '''impira/layoutlm-document-qa'''   ,       revision='''3dc6de3'''   ,       add_prefix_space=__snake_case							)
							UpperCamelCase__       :List[str]							 =			pipeline(
							    '''document-question-answering'''   ,       model='''impira/layoutlm-document-qa'''   ,       tokenizer=__snake_case   ,       revision='''3dc6de3'''   ,       max_seq_len=50   ,       )
							UpperCamelCase__       :List[Any]							 =			INVOICE_URL
							UpperCamelCase__       :List[Any]							 =			'What is the invoice number?'

							UpperCamelCase__       :str							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )

							UpperCamelCase__       :Dict							 =			dqa_pipeline(
							    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        [
							            {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							            {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        ]
							    ]
							    * 2   ,       )

							UpperCamelCase__       :int							 =			list(zip(*apply_tesseract(load_image(__snake_case							)   ,       __snake_case   ,       ''''''							)							)							)

							# This model should also work if `image` is set to None
							UpperCamelCase__       :int							 =			dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}   ,       top_k=2							)
							self.assertEqual(
							    nested_simplify(__snake_case   ,       decimals=4							)   ,       [
							        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
							    ]   ,       )




		@slow
		@require_torch
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			pipeline(
							    '''document-question-answering'''   ,       model='''naver-clova-ix/donut-base-finetuned-docvqa'''   ,       tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa'''							)   ,       feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa'''   ,       )

							UpperCamelCase__       :int							 =			INVOICE_URL
							UpperCamelCase__       :List[str]							 =			'What is the invoice number?'
							UpperCamelCase__       :Dict							 =			dqa_pipeline(image=__snake_case   ,       question=__snake_case   ,       top_k=2							)
							self.assertEqual(nested_simplify(__snake_case   ,       decimals=4							)   ,       [{'''answer''': '''us-001'''}]							)




		@require_tf
		@unittest.skip('''Document question answering not implemented in TF'''							)
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							pass



'''simple docstring'''
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


__snake_case											=					logging.get_logger(__name__)

__snake_case											=					{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__snake_case											=					{
    '''vocab_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt''',
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-german-cased''': (
            '''https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json'''
        ),
    },
}

__snake_case											=					{
    '''distilbert-base-uncased''': 512,
    '''distilbert-base-uncased-distilled-squad''': 512,
    '''distilbert-base-cased''': 512,
    '''distilbert-base-cased-distilled-squad''': 512,
    '''distilbert-base-german-cased''': 512,
    '''distilbert-base-multilingual-cased''': 512,
}


__snake_case											=					{
    '''distilbert-base-uncased''': {'''do_lower_case''': True},
    '''distilbert-base-uncased-distilled-squad''': {'''do_lower_case''': True},
    '''distilbert-base-cased''': {'''do_lower_case''': False},
    '''distilbert-base-cased-distilled-squad''': {'''do_lower_case''': False},
    '''distilbert-base-german-cased''': {'''do_lower_case''': False},
    '''distilbert-base-multilingual-cased''': {'''do_lower_case''': False},
}
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    VOCAB_FILES_NAMES
		_a   			=    PRETRAINED_VOCAB_FILES_MAP
		_a   			=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_a   			=    PRETRAINED_INIT_CONFIGURATION
		_a   			=    ['input_ids', 'attention_mask']
		_a   			=    DistilBertTokenizer




		def __init__(		self   ,       UpperCamelCase_=None   ,       UpperCamelCase_=None   ,       UpperCamelCase_=True   ,       UpperCamelCase_="[UNK]"   ,       UpperCamelCase_="[SEP]"   ,       UpperCamelCase_="[PAD]"   ,       UpperCamelCase_="[CLS]"   ,       UpperCamelCase_="[MASK]"   ,       UpperCamelCase_=True   ,       UpperCamelCase_=None   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							super().__init__(
							    UpperCamelCase_   ,       tokenizer_file=UpperCamelCase_   ,       do_lower_case=UpperCamelCase_   ,       unk_token=UpperCamelCase_   ,       sep_token=UpperCamelCase_   ,       pad_token=UpperCamelCase_   ,       cls_token=UpperCamelCase_   ,       mask_token=UpperCamelCase_   ,       tokenize_chinese_chars=UpperCamelCase_   ,       strip_accents=UpperCamelCase_   ,       **UpperCamelCase_   ,       )

							UpperCamelCase__       :int							 =			json.loads(self.backend_tokenizer.normalizer.__getstate__()							)
							if (
							    normalizer_state.get('''lowercase'''   ,       UpperCamelCase_							) != do_lower_case
							    or normalizer_state.get('''strip_accents'''   ,       UpperCamelCase_							) != strip_accents
							    or normalizer_state.get('''handle_chinese_chars'''   ,       UpperCamelCase_							) != tokenize_chinese_chars
							):
												UpperCamelCase__       :int							 =			getattr(UpperCamelCase_   ,       normalizer_state.pop('''type'''							)							)
												UpperCamelCase__       :Optional[Any]							 =			do_lower_case
												UpperCamelCase__       :Optional[Any]							 =			strip_accents
												UpperCamelCase__       :List[Any]							 =			tokenize_chinese_chars
												UpperCamelCase__       :Any							 =			normalizer_class(**UpperCamelCase_							)

							UpperCamelCase__       :int							 =			do_lower_case




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

							if token_ids_a:
												output += token_ids_a + [self.sep_token_id]

							return output




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			[self.sep_token_id]
							UpperCamelCase__       :List[str]							 =			[self.cls_token_id]
							if token_ids_a is None:
												return len(cls + token_ids_a + sep							) * [0]
							return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			self._tokenizer.model.save(UpperCamelCase_   ,       name=UpperCamelCase_							)
							return tuple(UpperCamelCase_							)