






'''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 	SCREAMING_SNAKE_CASE	:




          @staticmethod
          def 					a_					(					*A__      : Dict	,    **A__      : Optional[Any]					):


               """simple docstring"""







               pass

   def     __lowercase  (_lowercase     )   ->     int:


        """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.
UpperCAmelCase__						:Union[str, Any]          =			(
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)







@is_pipeline_test
@require_torch
@require_vision
class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):
       snake_case__    :     List[Any]   		=			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




       @require_pytesseract
       @require_vision
       def 					a_					(					self      : Union[str, Any]	,    A__      : List[str]	,    A__      : Any	,    A__      : Tuple					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				pipeline(
                """document-question-answering"""	,    model=A__	,    tokenizer=A__	,    image_processor=A__					)

            __lowerCamelCase						:       Optional[int]				   =				INVOICE_URL
            __lowerCamelCase						:       Optional[Any]				   =				list(zip(*apply_tesseract(load_image(A__					)	,    A__	,    """"""					)					)					)
            __lowerCamelCase						:       List[str]				   =				"""What is the placebo?"""
            __lowerCamelCase						:       List[str]				   =				[
                {
                    """image""": load_image(A__					),
                    """question""": question,
                },
                {
                    """image""": image,
                    """question""": question,
                },
                {
                    """image""": image,
                    """question""": question,
                    """word_boxes""": word_boxes,
                },
            ]
            return dqa_pipeline, examples




       def 					a_					(					self      : Union[str, Any]	,    A__      : List[str]	,    A__      : Any					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				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 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				pipeline("""document-question-answering"""	,    model="""hf-internal-testing/tiny-random-layoutlmv2"""					)
            __lowerCamelCase						:       Union[str, Any]				   =				INVOICE_URL
            __lowerCamelCase						:       Optional[Any]				   =				"""How many cats are there?"""

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

            __lowerCamelCase						:       int				   =				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
            __lowerCamelCase						:       List[str]				   =				"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            __lowerCamelCase						:       int				   =				dqa_pipeline(image=A__	,    question=A__	,    top_k=2					)
            self.assertEqual(A__	,    []					)

            # We can optionnally pass directly the words and bounding boxes
            __lowerCamelCase						:       int				   =				"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            __lowerCamelCase						:       Optional[Any]				   =				[]
            __lowerCamelCase						:       List[str]				   =				[]
            __lowerCamelCase						:       int				   =				dqa_pipeline(image=A__	,    question=A__	,    words=A__	,    boxes=A__	,    top_k=2					)
            self.assertEqual(A__	,    []					)




       @slow
       @require_torch
       @require_detectrona
       @require_pytesseract
       def 					a_					(					self      : Any					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				pipeline(
                """document-question-answering"""	,    model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""	,    revision="""9977165"""	,    )
            __lowerCamelCase						:       int				   =				INVOICE_URL
            __lowerCamelCase						:       Union[str, Any]				   =				"""What is the invoice number?"""

            __lowerCamelCase						:       Dict				   =				dqa_pipeline(image=A__	,    question=A__	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
                ]	,    )

            __lowerCamelCase						:       List[Any]				   =				dqa_pipeline({"""image""": image, """question""": question}	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
                ]	,    )

            __lowerCamelCase						:       Optional[Any]				   =				dqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    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 					a_					(					self      : Tuple					):


            """simple docstring"""







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

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

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

            __lowerCamelCase						:       Optional[Any]				   =				dqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    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 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				AutoTokenizer.from_pretrained(
                """impira/layoutlm-document-qa"""	,    revision="""3dc6de3"""	,    add_prefix_space=A__					)
            __lowerCamelCase						:       int				   =				pipeline(
                """document-question-answering"""	,    model="""impira/layoutlm-document-qa"""	,    tokenizer=A__	,    revision="""3dc6de3"""	,    )
            __lowerCamelCase						:       Optional[Any]				   =				INVOICE_URL
            __lowerCamelCase						:       Union[str, Any]				   =				"""What is the invoice number?"""

            __lowerCamelCase						:       Dict				   =				dqa_pipeline(image=A__	,    question=A__	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
                ]	,    )

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

            __lowerCamelCase						:       Dict				   =				dqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    [
                        {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
                        {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
                    ]
                ]
                * 2	,    )

            __lowerCamelCase						:       List[Any]				   =				list(zip(*apply_tesseract(load_image(A__					)	,    A__	,    """"""					)					)					)

            # This model should also work if `image` is set to None
            __lowerCamelCase						:       Dict				   =				dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    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 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				AutoTokenizer.from_pretrained(
                """impira/layoutlm-document-qa"""	,    revision="""3dc6de3"""	,    add_prefix_space=A__					)
            __lowerCamelCase						:       Tuple				   =				pipeline(
                """document-question-answering"""	,    model="""impira/layoutlm-document-qa"""	,    tokenizer=A__	,    revision="""3dc6de3"""	,    max_seq_len=50	,    )
            __lowerCamelCase						:       Optional[int]				   =				INVOICE_URL
            __lowerCamelCase						:       str				   =				"""What is the invoice number?"""

            __lowerCamelCase						:       Tuple				   =				dqa_pipeline(image=A__	,    question=A__	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
                    {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
                ]	,    )

            __lowerCamelCase						:       List[Any]				   =				dqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    [
                        {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
                        {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
                    ]
                ]
                * 2	,    )

            __lowerCamelCase						:       Dict				   =				list(zip(*apply_tesseract(load_image(A__					)	,    A__	,    """"""					)					)					)

            # This model should also work if `image` is set to None
            __lowerCamelCase						:       Any				   =				dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}	,    top_k=2					)
            self.assertEqual(
                nested_simplify(A__	,    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 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Tuple				   =				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"""	,    )

            __lowerCamelCase						:       str				   =				INVOICE_URL
            __lowerCamelCase						:       str				   =				"""What is the invoice number?"""
            __lowerCamelCase						:       List[Any]				   =				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 					a_					(					self      : Any					):


            """simple docstring"""







            pass











'''simple docstring'''

from ...processing_utils import ProcessorMixin







class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_  ):
       snake_case__    :     List[Any]   		=			'SpeechT5FeatureExtractor'
       snake_case__    :     Union[str, Any]   		=			'SpeechT5Tokenizer'




       def __init__(					self      : Union[str, Any]	,    A__      : str	,    A__      : Tuple					):


            """simple docstring"""







            super().__init__(A__	,    A__					)




       def __call__(					self      : List[Any]	,    *A__      : Any	,    **A__      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				kwargs.pop("""audio"""	,    A__					)
            __lowerCamelCase						:       Any				   =				kwargs.pop("""text"""	,    A__					)
            __lowerCamelCase						:       Union[str, Any]				   =				kwargs.pop("""text_target"""	,    A__					)
            __lowerCamelCase						:       Union[str, Any]				   =				kwargs.pop("""audio_target"""	,    A__					)
            __lowerCamelCase						:       Any				   =				kwargs.pop("""sampling_rate"""	,    A__					)

            if audio is not None and text is not None:
                 raise ValueError(
                     """Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?"""					)
            if audio_target is not None and text_target is not None:
                 raise ValueError(
                     """Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?"""					)
            if audio is None and audio_target is None and text is None and text_target is None:
                 raise ValueError(
                     """You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process."""					)

            if audio is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor(A__	,    *A__	,    sampling_rate=A__	,    **A__					)
            elif text is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.tokenizer(A__	,    **A__					)
            else:
                 __lowerCamelCase						:       Tuple				   =				None

            if audio_target is not None:
                 __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor(audio_target=A__	,    *A__	,    sampling_rate=A__	,    **A__					)
                 __lowerCamelCase						:       Union[str, Any]				   =				targets["""input_values"""]
            elif text_target is not None:
                 __lowerCamelCase						:       Tuple				   =				self.tokenizer(A__	,    **A__					)
                 __lowerCamelCase						:       List[str]				   =				targets["""input_ids"""]
            else:
                 __lowerCamelCase						:       Dict				   =				None

            if inputs is None:
                 return targets

            if targets is not None:
                 __lowerCamelCase						:       str				   =				labels

                 __lowerCamelCase						:       int				   =				targets.get("""attention_mask"""					)
                 if decoder_attention_mask is not None:
                      __lowerCamelCase						:       Optional[int]				   =				decoder_attention_mask

            return inputs




       def 					a_					(					self      : Any	,    *A__      : Union[str, Any]	,    **A__      : List[str]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				kwargs.pop("""input_values"""	,    A__					)
            __lowerCamelCase						:       List[Any]				   =				kwargs.pop("""input_ids"""	,    A__					)
            __lowerCamelCase						:       List[Any]				   =				kwargs.pop("""labels"""	,    A__					)

            if input_values is not None and input_ids is not None:
                 raise ValueError("""Cannot process both `input_values` and `input_ids` inputs."""					)
            if input_values is None and input_ids is None and labels is None:
                 raise ValueError(
                     """You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded."""					)

            if input_values is not None:
                 __lowerCamelCase						:       List[Any]				   =				self.feature_extractor.pad(A__	,    *A__	,    **A__					)
            elif input_ids is not None:
                 __lowerCamelCase						:       Any				   =				self.tokenizer.pad(A__	,    **A__					)
            else:
                 __lowerCamelCase						:       Any				   =				None

            if labels is not None:
                 if "input_ids" in labels or (isinstance(A__	,    A__					) and "input_ids" in labels[0]):
                      __lowerCamelCase						:       List[Any]				   =				self.tokenizer.pad(A__	,    **A__					)
                      __lowerCamelCase						:       Tuple				   =				targets["""input_ids"""]
                 else:
                      __lowerCamelCase						:       Optional[int]				   =				self.feature_extractor.feature_size
                      __lowerCamelCase						:       Any				   =				self.feature_extractor.num_mel_bins
                      __lowerCamelCase						:       List[str]				   =				self.feature_extractor.pad(A__	,    *A__	,    **A__					)
                      __lowerCamelCase						:       Dict				   =				feature_size_hack
                      __lowerCamelCase						:       List[Any]				   =				targets["""input_values"""]
            else:
                 __lowerCamelCase						:       Optional[int]				   =				None

            if inputs is None:
                 return targets

            if targets is not None:
                 __lowerCamelCase						:       Dict				   =				labels

                 __lowerCamelCase						:       List[str]				   =				targets.get("""attention_mask"""					)
                 if decoder_attention_mask is not None:
                      __lowerCamelCase						:       int				   =				decoder_attention_mask

            return inputs




       def 					a_					(					self      : Optional[Any]	,    *A__      : List[Any]	,    **A__      : Optional[int]					):


            """simple docstring"""







            return self.tokenizer.batch_decode(*A__	,    **A__					)




       def 					a_					(					self      : Any	,    *A__      : int	,    **A__      : Dict					):


            """simple docstring"""







            return self.tokenizer.decode(*A__	,    **A__					)



