"""simple docstring"""




import inspect
import unittest

from transformers import DecisionTransformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import DecisionTransformerModel
				from transformers.models.decision_transformer.modeling_decision_transformer import (
				    DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				)
class 			UpperCamelCase__:





						def __init__(				self						,__UpperCAmelCase						,__UpperCAmelCase=13						,__UpperCAmelCase=7						,__UpperCAmelCase=6						,__UpperCAmelCase=17						,__UpperCAmelCase=23						,__UpperCAmelCase=11						,__UpperCAmelCase=True						,)    -> Optional[int]:
													A__		   =							parent
													A__		   =							batch_size
													A__		   =							seq_length
													A__		   =							act_dim
													A__		   =							state_dim
													A__		   =							hidden_size
													A__		   =							max_length
													A__		   =							is_training





						def  snake_case__		(				self   )    -> int:
													A__		   =							floats_tensor((self.batch_size, self.seq_length, self.state_dim)   )
													A__		   =							floats_tensor((self.batch_size, self.seq_length, self.act_dim)   )
													A__		   =							floats_tensor((self.batch_size, self.seq_length, 1)   )
													A__		   =							floats_tensor((self.batch_size, self.seq_length, 1)   )
													A__		   =							ids_tensor((self.batch_size, self.seq_length)						,vocab_size=10_00   )
													A__		   =							random_attention_mask((self.batch_size, self.seq_length)   )

													A__		   =							self.get_config()

													return (
													    config,
													    states,
													    actions,
													    rewards,
													    returns_to_go,
													    timesteps,
													    attention_mask,
													)





						def  snake_case__		(				self   )    -> Optional[int]:
													return DecisionTransformerConfig(
													    batch_size=self.batch_size						,seq_length=self.seq_length						,act_dim=self.act_dim						,state_dim=self.state_dim						,hidden_size=self.hidden_size						,max_length=self.max_length						,)





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,)    -> str:
													A__		   =							DecisionTransformerModel(config=UpperCAmelCase__   )
													model.to(UpperCAmelCase__   )
													model.eval()
													A__		   =							model(UpperCAmelCase__						,UpperCAmelCase__						,UpperCAmelCase__						,UpperCAmelCase__						,UpperCAmelCase__						,UpperCAmelCase__   )

													self.parent.assertEqual(result.state_preds.shape						,states.shape   )
													self.parent.assertEqual(result.action_preds.shape						,actions.shape   )
													self.parent.assertEqual(result.return_preds.shape						,returns_to_go.shape   )
													self.parent.assertEqual(
													    result.last_hidden_state.shape						,(self.batch_size, self.seq_length * 3, self.hidden_size)   )  # seq length *3 as there are 3 modelities: states, returns and actions





						def  snake_case__		(				self   )    -> Any:
													A__		   =							self.prepare_config_and_inputs()
													(
													    (
													    A__
													)   ,     (
													    A__
													)   ,     (
													    A__
													)   ,     (
													    A__
													)   ,     (
													    A__
													)   ,     (
													    A__
													)   ,     (
													    A__
													)   ,     
													)		   =							config_and_inputs
													A__		   =							{
													    'states': states,
													    'actions': actions,
													    'rewards': rewards,
													    'returns_to_go': returns_to_go,
													    'timesteps': timesteps,
													    'attention_mask': attention_mask,
													}
													return config, inputs_dict






@require_torch
class 			UpperCamelCase__(	__A						,  __A						,  __A						,  unittest.TestCase       ):
						lowerCAmelCase__     :  Tuple  							=						(DecisionTransformerModel,) if is_torch_available() else ()
						lowerCAmelCase__     :  int  							=						()
						lowerCAmelCase__     :  Tuple  							=						{"feature-extraction": DecisionTransformerModel} if is_torch_available() else {}

						# Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids
						lowerCAmelCase__     :  str  							=						False

						# Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features
						lowerCAmelCase__     :  str  							=						False
						lowerCAmelCase__     :  str  							=						False
						lowerCAmelCase__     :  int  							=						False
						lowerCAmelCase__     :  Any  							=						False
						lowerCAmelCase__     :  Union[str, Any]  							=						False
						lowerCAmelCase__     :  List[Any]  							=						False
						lowerCAmelCase__     :  Any  							=						False
						lowerCAmelCase__     :  Any  							=						False
						lowerCAmelCase__     :  Optional[int]  							=						False





						def  snake_case__		(				self   )    -> List[str]:
													A__		   =							DecisionTransformerModelTester(self   )
													A__		   =							ConfigTester(self						,config_class=UpperCAmelCase__						,hidden_size=37   )





						def  snake_case__		(				self   )    -> int:
													self.config_tester.run_common_tests()





						def  snake_case__		(				self   )    -> Tuple:
													A__		   =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*UpperCAmelCase__   )





						@slow
						def  snake_case__		(				self   )    -> Optional[Any]:
													for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				A__		   =							DecisionTransformerModel.from_pretrained(UpperCAmelCase__   )
																				self.assertIsNotNone(UpperCAmelCase__   )





						def  snake_case__		(				self   )    -> Tuple:
													A__   ,     A__		   =							self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A__		   =							model_class(UpperCAmelCase__   )
																				A__		   =							inspect.signature(model.forward   )
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				A__		   =							[*signature.parameters.keys()]

																				A__		   =							[
																				    'states',
																				    'actions',
																				    'rewards',
																				    'returns_to_go',
																				    'timesteps',
																				    'attention_mask',
																				]

																				self.assertListEqual(arg_names[: len(UpperCAmelCase__   )]						,UpperCAmelCase__   )






@require_torch
class 			UpperCamelCase__(	unittest.TestCase       ):





						@slow
						def  snake_case__		(				self   )    -> Tuple:
													A__		   =							2  # number of steps of autoregressive prediction we will perform
													A__		   =							10  # defined by the RL environment, may be normalized
													A__		   =							DecisionTransformerModel.from_pretrained('edbeeching/decision-transformer-gym-hopper-expert'   )
													A__		   =							model.to(UpperCAmelCase__   )
													A__		   =							model.config
													torch.manual_seed(0   )
													A__		   =							torch.randn(1						,1						,config.state_dim   ).to(device=UpperCAmelCase__						,dtype=torch.floataa   )  # env.reset()

													A__		   =							torch.tensor(
													    [[0.2_4_2_7_9_3, -0.2_8_6_9_3_0_7_4, 0.8_7_4_2_6_1_3], [0.6_7_8_1_5_2_7_4, -0.0_8_1_0_1_0_8_5, -0.1_2_9_5_2_1_4_7]]						,device=UpperCAmelCase__   )

													A__		   =							torch.tensor(UpperCAmelCase__						,device=UpperCAmelCase__						,dtype=torch.floataa   ).reshape(1						,1						,1   )
													A__		   =							state
													A__		   =							torch.zeros(1						,0						,config.act_dim						,device=UpperCAmelCase__						,dtype=torch.floataa   )
													A__		   =							torch.zeros(1						,0						,device=UpperCAmelCase__						,dtype=torch.floataa   )
													A__		   =							torch.tensor(0						,device=UpperCAmelCase__						,dtype=torch.long   ).reshape(1						,1   )

													for step in range(UpperCAmelCase__   ):
																				A__		   =							torch.cat([actions, torch.zeros(1						,1						,config.act_dim						,device=UpperCAmelCase__   )]						,dim=1   )
																				A__		   =							torch.cat([rewards, torch.zeros(1						,1						,device=UpperCAmelCase__   )]						,dim=1   )

																				A__		   =							torch.ones(1						,states.shape[1]   ).to(dtype=torch.long						,device=states.device   )

																				with torch.no_grad():
																											A__   ,     A__   ,     A__		   =							model(
																											    states=UpperCAmelCase__						,actions=UpperCAmelCase__						,rewards=UpperCAmelCase__						,returns_to_go=UpperCAmelCase__						,timesteps=UpperCAmelCase__						,attention_mask=UpperCAmelCase__						,return_dict=UpperCAmelCase__						,)

																				self.assertEqual(action_pred.shape						,actions.shape   )
																				self.assertTrue(torch.allclose(action_pred[0, -1]						,expected_outputs[step]						,atol=1e-4   )   )
																				A__   ,     A__   ,     A__   ,     A__		   =							(  # env.step(action)
																				    torch.randn(1						,1						,config.state_dim   ).to(device=UpperCAmelCase__						,dtype=torch.floataa   ),
																				    1.0,
																				    False,
																				    {},
																				)

																				A__		   =							action_pred[0, -1]
																				A__		   =							torch.cat([states, state]						,dim=1   )
																				A__		   =							returns_to_go[0, -1] - reward
																				A__		   =							torch.cat([returns_to_go, pred_return.reshape(1						,1						,1   )]						,dim=1   )
																				A__		   =							torch.cat(
																				    [timesteps, torch.ones((1, 1)						,device=UpperCAmelCase__						,dtype=torch.long   ) * (step + 1)]						,dim=1   )








"""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 					_lowercase (			*UpperCAmelCase__							:			Tuple,					**UpperCAmelCase__							:			List[Any]	):
       pass







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




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







 """simple docstring"""







 __UpperCAmelCase	:					Tuple   		=	MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


 @require_pytesseract
 @require_vision
 def 					_lowercase (			self							:			Optional[Any],					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Optional[Any]	):
    __lowercase								=						pipeline(
        "document-question-answering",					model=UpperCAmelCase__,					tokenizer=UpperCAmelCase__,					image_processor=UpperCAmelCase__	)

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


 def 					_lowercase (			self							:			int,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Any	):
    __lowercase								=						dqa_pipeline(UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        UpperCAmelCase__,					[
            [
                {"score": ANY(UpperCAmelCase__	), "answer": ANY(UpperCAmelCase__	), "start": ANY(UpperCAmelCase__	), "end": ANY(UpperCAmelCase__	)},
                {"score": ANY(UpperCAmelCase__	), "answer": ANY(UpperCAmelCase__	), "start": ANY(UpperCAmelCase__	), "end": ANY(UpperCAmelCase__	)},
            ]
        ]
        * 3,					)


 @require_torch
 @require_detectrona
 @require_pytesseract
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						pipeline("document-question-answering",					model="hf-internal-testing/tiny-random-layoutlmv2"	)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"How many cats are there?"

    __lowercase								=						[
        {"score": 0.0_001, "answer": "oy 2312/2019", "start": 3_8, "end": 3_9},
        {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 3_8, "end": 4_0},
    ]
    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(nested_simplify(UpperCAmelCase__,					decimals=4	),					UpperCAmelCase__	)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(nested_simplify(UpperCAmelCase__,					decimals=4	),					UpperCAmelCase__	)

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

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


 @slow
 @require_torch
 @require_detectrona
 @require_pytesseract
 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						pipeline(
        "document-question-answering",					model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa",					revision="9977165",					)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"What is the invoice number?"

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_944, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_009, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_944, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_009, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.9_944, "answer": "us-001", "start": 1_6, "end": 1_6},
                {"score": 0.0_009, "answer": "us-001", "start": 1_6, "end": 1_6},
            ],
        ]
        * 2,					)


 @slow
 @require_torch
 @require_detectrona
 @require_pytesseract
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						pipeline(
        "document-question-answering",					model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa",					revision="9977165",					max_seq_len=5_0,					)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"What is the invoice number?"

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
            {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
            {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.9_974, "answer": "1110212019", "start": 2_3, "end": 2_3},
                {"score": 0.9_948, "answer": "us-001", "start": 1_6, "end": 1_6},
            ]
        ]
        * 2,					)


 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 					_lowercase (			self							:			Optional[Any]	):
    __lowercase								=						AutoTokenizer.from_pretrained(
        "impira/layoutlm-document-qa",					revision="3dc6de3",					add_prefix_space=UpperCAmelCase__	)
    __lowercase								=						pipeline(
        "document-question-answering",					model="impira/layoutlm-document-qa",					tokenizer=UpperCAmelCase__,					revision="3dc6de3",					)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"What is the invoice number?"

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
        ],					)

    __lowercase								=						dqa_pipeline({"image": image, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
                {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
            ]
        ]
        * 2,					)

    __lowercase								=						list(zip(*apply_tesseract(load_image(UpperCAmelCase__	),					UpperCAmelCase__,					""	)	)	)

    # This model should also work if `image` is set to None
    __lowercase								=						dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.4_251, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.0_819, "answer": "1110212019", "start": 2_3, "end": 2_3},
        ],					)


 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 					_lowercase (			self							:			Union[str, Any]	):
    __lowercase								=						AutoTokenizer.from_pretrained(
        "impira/layoutlm-document-qa",					revision="3dc6de3",					add_prefix_space=UpperCAmelCase__	)
    __lowercase								=						pipeline(
        "document-question-answering",					model="impira/layoutlm-document-qa",					tokenizer=UpperCAmelCase__,					revision="3dc6de3",					max_seq_len=5_0,					)
    __lowercase								=						INVOICE_URL
    __lowercase								=						"What is the invoice number?"

    __lowercase								=						dqa_pipeline(image=UpperCAmelCase__,					question=UpperCAmelCase__,					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_999, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.9_998, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)

    __lowercase								=						dqa_pipeline(
        [{"image": image, "question": question}, {"image": image, "question": question}],					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            [
                {"score": 0.9_999, "answer": "us-001", "start": 1_6, "end": 1_6},
                {"score": 0.9_998, "answer": "us-001", "start": 1_6, "end": 1_6},
            ]
        ]
        * 2,					)

    __lowercase								=						list(zip(*apply_tesseract(load_image(UpperCAmelCase__	),					UpperCAmelCase__,					""	)	)	)

    # This model should also work if `image` is set to None
    __lowercase								=						dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question},					top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCAmelCase__,					decimals=4	),					[
            {"score": 0.9_999, "answer": "us-001", "start": 1_6, "end": 1_6},
            {"score": 0.9_998, "answer": "us-001", "start": 1_6, "end": 1_6},
        ],					)


 @slow
 @require_torch
 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						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",					)

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



 @require_tf
 @unittest.skip("Document question answering not implemented in TF"	)
 def 					_lowercase (			self							:			List[Any]	):
    pass




