
"""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  lowerCAmelCase_ :



								'''simple docstring'''


								@staticmethod
								def   _SCREAMING_SNAKE_CASE					(       *A_   :					Optional[int]     ,**A_   :					List[Any]					)   ->      Dict:
									pass






		def 					_snake_case (						snake_case__      :   Dict   ):
			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.
_lowercase							      = (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)







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



						'''simple docstring'''


						_lowerCamelCase:			Optional[int]					=			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


						@require_pytesseract
						@require_vision
						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]     ,A_   :					int     ,A_   :					List[Any]     ,A_   :					Dict					)   ->      List[str]:
							A        =      pipeline(
							    'document-question-answering'     ,model=A_     ,tokenizer=A_     ,image_processor=A_					)

							A        =      INVOICE_URL
							A        =      list(zip(*apply_tesseract(load_image(A_					)     ,A_     ,''					)					)					)
							A        =      'What is the placebo?'
							A        =      [
							    {
							        'image': load_image(A_					),
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							        'word_boxes': word_boxes,
							    },
							]
							return dqa_pipeline, examples


						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]     ,A_   :					Dict     ,A_   :					List[Any]					)   ->      List[str]:
							A        =      dqa_pipeline(A_     ,top_k=2					)
							self.assertEqual(
							    A_     ,[
							        [
							            {'score': ANY(A_					), 'answer': ANY(A_					), 'start': ANY(A_					), 'end': ANY(A_					)},
							            {'score': ANY(A_					), 'answer': ANY(A_					), 'start': ANY(A_					), 'end': ANY(A_					)},
							        ]
							    ]
							    * 3     ,)


						@require_torch
						@require_detectrona
						@require_pytesseract
						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]					)   ->      Union[str, Any]:
							A        =      pipeline('document-question-answering'     ,model='hf-internal-testing/tiny-random-layoutlmv2'					)
							A        =      INVOICE_URL
							A        =      'How many cats are there?'

							A        =      [
							    {'score': 0.00_01, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
							    {'score': 0.00_01, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
							]
							A        =      dqa_pipeline(image=A_     ,question=A_     ,top_k=2					)
							self.assertEqual(nested_simplify(A_     ,decimals=4					)     ,A_					)

							A        =      dqa_pipeline({'image': image, 'question': question}     ,top_k=2					)
							self.assertEqual(nested_simplify(A_     ,decimals=4					)     ,A_					)

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

							# We can optionnally pass directly the words and bounding boxes
							A        =      './tests/fixtures/tests_samples/COCO/000000039769.png'
							A        =      []
							A        =      []
							A        =      dqa_pipeline(image=A_     ,question=A_     ,words=A_     ,boxes=A_     ,top_k=2					)
							self.assertEqual(A_     ,[]					)


						@slow
						@require_torch
						@require_detectrona
						@require_pytesseract
						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]					)   ->      Dict:
							A        =      pipeline(
							    'document-question-answering'     ,model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,revision='9977165'     ,)
							A        =      INVOICE_URL
							A        =      'What is the invoice number?'

							A        =      dqa_pipeline(image=A_     ,question=A_     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]     ,)

							A        =      dqa_pipeline({'image': image, 'question': question}     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]     ,)

							A        =      dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        [
							            {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
							            {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ],
							    ]
							    * 2     ,)


						@slow
						@require_torch
						@require_detectrona
						@require_pytesseract
						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]					)   ->      Dict:
							A        =      pipeline(
							    'document-question-answering'     ,model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,revision='9977165'     ,max_seq_len=50     ,)
							A        =      INVOICE_URL
							A        =      'What is the invoice number?'

							A        =      dqa_pipeline(image=A_     ,question=A_     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]     ,)

							A        =      dqa_pipeline({'image': image, 'question': question}     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]     ,)

							A        =      dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        [
							            {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							            {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ]
							    ]
							    * 2     ,)


						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def   _SCREAMING_SNAKE_CASE					(       self   :					List[Any]					)   ->      int:
							A        =      AutoTokenizer.from_pretrained(
							    'impira/layoutlm-document-qa'     ,revision='3dc6de3'     ,add_prefix_space=A_					)
							A        =      pipeline(
							    'document-question-answering'     ,model='impira/layoutlm-document-qa'     ,tokenizer=A_     ,revision='3dc6de3'     ,)
							A        =      INVOICE_URL
							A        =      'What is the invoice number?'

							A        =      dqa_pipeline(image=A_     ,question=A_     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
							    ]     ,)

							A        =      dqa_pipeline({'image': image, 'question': question}     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
							    ]     ,)

							A        =      dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        [
							            {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
							            {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
							        ]
							    ]
							    * 2     ,)

							A        =      list(zip(*apply_tesseract(load_image(A_					)     ,A_     ,''					)					)					)

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


						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def   _SCREAMING_SNAKE_CASE					(       self   :					List[str]					)   ->      str:
							A        =      AutoTokenizer.from_pretrained(
							    'impira/layoutlm-document-qa'     ,revision='3dc6de3'     ,add_prefix_space=A_					)
							A        =      pipeline(
							    'document-question-answering'     ,model='impira/layoutlm-document-qa'     ,tokenizer=A_     ,revision='3dc6de3'     ,max_seq_len=50     ,)
							A        =      INVOICE_URL
							A        =      'What is the invoice number?'

							A        =      dqa_pipeline(image=A_     ,question=A_     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]     ,)

							A        =      dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,top_k=2					)
							self.assertEqual(
							    nested_simplify(A_     ,decimals=4					)     ,[
							        [
							            {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
							            {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ]
							    ]
							    * 2     ,)

							A        =      list(zip(*apply_tesseract(load_image(A_					)     ,A_     ,''					)					)					)

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


						@slow
						@require_torch
						def   _SCREAMING_SNAKE_CASE					(       self   :					Optional[Any]					)   ->      int:
							A        =      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'     ,)

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




						@require_tf
						@unittest.skip('Document question answering not implemented in TF'					)
						def   _SCREAMING_SNAKE_CASE					(       self   :					Any					)   ->      Optional[int]:
							pass



"""simple docstring"""



from typing import List

import numpy as np







def   __lowerCAmelCase						(						lowercase  :  dict )	->						int:


    """simple docstring"""





    snake_case    :      Union[str, Any]      					=    {key: len(lowercase ) for key, value in gen_kwargs.items() if isinstance(lowercase						,							lowercase )}
    if len(set(lists_lengths.values() ) ) > 1:
        raise RuntimeError(
            (
                "Sharding is ambiguous for this dataset: "
                + "we found several data sources lists of different lengths, and we don't know over which list we should parallelize:\n"
                + "\n".join(F'\t- key {key} has length {length}' for key, length in lists_lengths.items() )
                + "\nTo fix this, check the 'gen_kwargs' and make sure to use lists only for data sources, "
                + "and use tuples otherwise. In the end there should only be one single list, or several lists with the same length."
            ) )
    snake_case    :      int      					=    max(lists_lengths.values()						,							default=0 )
    return max(1						,							lowercase )







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int )	->						List[range]:


    """simple docstring"""





    snake_case    :      Union[str, Any]      					=    []
    for group_idx in range(lowercase ):
        snake_case    :      Union[str, Any]      					=    num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs))
        if num_shards_to_add == 0:
            break
        snake_case    :      int      					=    shards_indices_per_group[-1].stop if shards_indices_per_group else 0
        snake_case    :      Dict      					=    range(lowercase						,							start + num_shards_to_add )
        shards_indices_per_group.append(lowercase )
    return shards_indices_per_group







def   __lowerCAmelCase						(						lowercase  :  dict						,							lowercase  :  int )	->						List[dict]:


    """simple docstring"""





    snake_case    :      int      					=    _number_of_shards_in_gen_kwargs(lowercase )
    if num_shards == 1:
        return [dict(lowercase )]
    else:
        snake_case    :      Optional[int]      					=    _distribute_shards(num_shards=lowercase						,							max_num_jobs=lowercase )
        return [
            {
                key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]]
                if isinstance(lowercase						,							lowercase )
                else value
                for key, value in gen_kwargs.items()
            }
            for group_idx in range(len(lowercase ) )
        ]







def   __lowerCAmelCase						(						lowercase  :  List[dict] )	->						dict:


    """simple docstring"""





    return {
        key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]]
        if isinstance(gen_kwargs_list[0][key]						,							lowercase )
        else gen_kwargs_list[0][key]
        for key in gen_kwargs_list[0]
    }







def   __lowerCAmelCase						(						lowercase  :  np.random.Generator						,							lowercase  :  dict )	->						dict:


    """simple docstring"""





    snake_case    :      Tuple      					=    {len(lowercase ) for value in gen_kwargs.values() if isinstance(lowercase						,							lowercase )}
    snake_case    :      str      					=    {}
    for size in list_sizes:
        snake_case    :      Optional[int]      					=    list(range(lowercase ) )
        rng.shuffle(indices_per_size[size] )
    # Now let's copy the gen_kwargs and shuffle the lists based on their sizes
    snake_case    :      Dict      					=    dict(lowercase )
    for key, value in shuffled_kwargs.items():
        if isinstance(lowercase						,							lowercase ):
            snake_case    :      Dict      					=    [value[i] for i in indices_per_size[len(lowercase )]]
    return shuffled_kwargs





