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

							@staticmethod
							def 							__lowerCamelCase							(   *_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				):
								pass



					def 					__lowerCAmelCase       (_UpperCamelCase	):
						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.
lowerCamelCase__           =	(
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)




@is_pipeline_test
@require_torch
@require_vision
class 				A__ (			unittest.TestCase):
		A_		: Optional[Any]            =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

		@require_pytesseract
		@require_vision
		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		Tuple		    =   pipeline(
			    'document-question-answering' ,	model=lowerCamelCase_ ,	tokenizer=lowerCamelCase_ ,	image_processor=lowerCamelCase_				)

			__lowerCAmelCase    :		List[str]		    =   INVOICE_URL
			__lowerCAmelCase    :		Tuple		    =   list(zip(*apply_tesseract(load_image(lowerCamelCase_				) ,	lowerCamelCase_ ,	''				)				)				)
			__lowerCAmelCase    :		List[str]		    =   """What is the placebo?"""
			__lowerCAmelCase    :		Dict		    =   [
			    {
			        """image""": load_image(lowerCamelCase_				),
			        """question""": question,
			    },
			    {
			        """image""": image,
			        """question""": question,
			    },
			    {
			        """image""": image,
			        """question""": question,
			        """word_boxes""": word_boxes,
			    },
			]
			return dqa_pipeline, examples

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		List[str]		    =   dqa_pipeline(lowerCamelCase_ ,	top_k=2				)
			self.assertEqual(
			    lowerCamelCase_ ,	[
			        [
			            {'score': ANY(lowerCamelCase_				), 'answer': ANY(lowerCamelCase_				), 'start': ANY(lowerCamelCase_				), 'end': ANY(lowerCamelCase_				)},
			            {'score': ANY(lowerCamelCase_				), 'answer': ANY(lowerCamelCase_				), 'start': ANY(lowerCamelCase_				), 'end': ANY(lowerCamelCase_				)},
			        ]
			    ]
			    * 3 ,	)

		@require_torch
		@require_detectrona
		@require_pytesseract
		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Union[str, Any]		    =   pipeline('document-question-answering' ,	model='hf-internal-testing/tiny-random-layoutlmv2'				)
			__lowerCAmelCase    :		Optional[int]		    =   INVOICE_URL
			__lowerCAmelCase    :		List[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    :		Any		    =   dqa_pipeline(image=lowerCamelCase_ ,	question=lowerCamelCase_ ,	top_k=2				)
			self.assertEqual(nested_simplify(lowerCamelCase_ ,	decimals=4				) ,	lowerCamelCase_				)

			__lowerCAmelCase    :		Optional[Any]		    =   dqa_pipeline({'image': image, 'question': question} ,	top_k=2				)
			self.assertEqual(nested_simplify(lowerCamelCase_ ,	decimals=4				) ,	lowerCamelCase_				)

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

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

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

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

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

			__lowerCAmelCase    :		int		    =   dqa_pipeline(
			    [{'image': image, 'question': question}, {'image': image, 'question': question}] ,	top_k=2				)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_ ,	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				):
			__lowerCAmelCase    :		Union[str, Any]		    =   pipeline(
			    'document-question-answering' ,	model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa' ,	revision='9977165' ,	max_seq_len=50 ,	)
			__lowerCAmelCase    :		List[Any]		    =   INVOICE_URL
			__lowerCAmelCase    :		Union[str, Any]		    =   """What is the invoice number?"""

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

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

			__lowerCAmelCase    :		Union[str, Any]		    =   dqa_pipeline(
			    [{'image': image, 'question': question}, {'image': image, 'question': question}] ,	top_k=2				)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_ ,	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				):
			__lowerCAmelCase    :		int		    =   AutoTokenizer.from_pretrained(
			    'impira/layoutlm-document-qa' ,	revision='3dc6de3' ,	add_prefix_space=lowerCamelCase_				)
			__lowerCAmelCase    :		List[str]		    =   pipeline(
			    'document-question-answering' ,	model='impira/layoutlm-document-qa' ,	tokenizer=lowerCamelCase_ ,	revision='3dc6de3' ,	)
			__lowerCAmelCase    :		Optional[Any]		    =   INVOICE_URL
			__lowerCAmelCase    :		int		    =   """What is the invoice number?"""

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

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

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

			__lowerCAmelCase    :		Dict		    =   list(zip(*apply_tesseract(load_image(lowerCamelCase_				) ,	lowerCamelCase_ ,	''				)				)				)

			# This model should also work if `image` is set to None
			__lowerCAmelCase    :		Union[str, Any]		    =   dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question} ,	top_k=2				)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_ ,	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				):
			__lowerCAmelCase    :		List[str]		    =   AutoTokenizer.from_pretrained(
			    'impira/layoutlm-document-qa' ,	revision='3dc6de3' ,	add_prefix_space=lowerCamelCase_				)
			__lowerCAmelCase    :		Optional[Any]		    =   pipeline(
			    'document-question-answering' ,	model='impira/layoutlm-document-qa' ,	tokenizer=lowerCamelCase_ ,	revision='3dc6de3' ,	max_seq_len=50 ,	)
			__lowerCAmelCase    :		int		    =   INVOICE_URL
			__lowerCAmelCase    :		Tuple		    =   """What is the invoice number?"""

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

			__lowerCAmelCase    :		str		    =   dqa_pipeline(
			    [{'image': image, 'question': question}, {'image': image, 'question': question}] ,	top_k=2				)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_ ,	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(lowerCamelCase_				) ,	lowerCamelCase_ ,	''				)				)				)

			# This model should also work if `image` is set to None
			__lowerCAmelCase    :		int		    =   dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question} ,	top_k=2				)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_ ,	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				):
			__lowerCAmelCase    :		Any		    =   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    :		List[str]		    =   INVOICE_URL
			__lowerCAmelCase    :		int		    =   """What is the invoice number?"""
			__lowerCAmelCase    :		Optional[Any]		    =   dqa_pipeline(image=lowerCamelCase_ ,	question=lowerCamelCase_ ,	top_k=2				)
			self.assertEqual(nested_simplify(lowerCamelCase_ ,	decimals=4				) ,	[{'answer': 'us-001'}]				)







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

"""simple docstring"""







from ....configuration_utils import PretrainedConfig
from ....utils import logging


lowerCamelCase__           =	logging.get_logger(__name__)

lowerCamelCase__           =	{
    """speechbrain/m-ctc-t-large""": """https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json""",
    # See all M-CTC-T models at https://huggingface.co/models?filter=mctct
}




class 				A__ (			_lowerCamelCase):
		A_		: Any            =			'mctct'

		def __init__(   self ,	_SCREAMING_SNAKE_CASE=80_65 ,	_SCREAMING_SNAKE_CASE=15_36 ,	_SCREAMING_SNAKE_CASE=36 ,	_SCREAMING_SNAKE_CASE=61_44 ,	_SCREAMING_SNAKE_CASE=4 ,	_SCREAMING_SNAKE_CASE=3_84 ,	_SCREAMING_SNAKE_CASE=9_20 ,	_SCREAMING_SNAKE_CASE=1E-5 ,	_SCREAMING_SNAKE_CASE=0.3 ,	_SCREAMING_SNAKE_CASE="relu" ,	_SCREAMING_SNAKE_CASE=0.02 ,	_SCREAMING_SNAKE_CASE=0.3 ,	_SCREAMING_SNAKE_CASE=0.3 ,	_SCREAMING_SNAKE_CASE=1 ,	_SCREAMING_SNAKE_CASE=0 ,	_SCREAMING_SNAKE_CASE=2 ,	_SCREAMING_SNAKE_CASE=1 ,	_SCREAMING_SNAKE_CASE=0.3 ,	_SCREAMING_SNAKE_CASE=1 ,	_SCREAMING_SNAKE_CASE=(7,) ,	_SCREAMING_SNAKE_CASE=(3,) ,	_SCREAMING_SNAKE_CASE=80 ,	_SCREAMING_SNAKE_CASE=1 ,	_SCREAMING_SNAKE_CASE=None ,	_SCREAMING_SNAKE_CASE="sum" ,	_SCREAMING_SNAKE_CASE=False ,	**_SCREAMING_SNAKE_CASE ,	):
			super().__init__(**_SCREAMING_SNAKE_CASE ,	pad_token_id=_SCREAMING_SNAKE_CASE ,	bos_token_id=_SCREAMING_SNAKE_CASE ,	eos_token_id=_SCREAMING_SNAKE_CASE				)
			__lowerCAmelCase    :		str		    =   vocab_size
			__lowerCAmelCase    :		Any		    =   hidden_size
			__lowerCAmelCase    :		int		    =   num_hidden_layers
			__lowerCAmelCase    :		Any		    =   intermediate_size
			__lowerCAmelCase    :		Optional[int]		    =   num_attention_heads
			__lowerCAmelCase    :		str		    =   attention_head_dim
			__lowerCAmelCase    :		int		    =   max_position_embeddings
			__lowerCAmelCase    :		List[Any]		    =   layer_norm_eps
			__lowerCAmelCase    :		Tuple		    =   layerdrop
			__lowerCAmelCase    :		Dict		    =   hidden_act
			__lowerCAmelCase    :		Dict		    =   initializer_range
			__lowerCAmelCase    :		Any		    =   hidden_dropout_prob
			__lowerCAmelCase    :		Dict		    =   attention_probs_dropout_prob
			__lowerCAmelCase    :		List[str]		    =   pad_token_id
			__lowerCAmelCase    :		Union[str, Any]		    =   bos_token_id
			__lowerCAmelCase    :		int		    =   eos_token_id
			__lowerCAmelCase    :		Optional[int]		    =   conv_glu_dim
			__lowerCAmelCase    :		List[Any]		    =   conv_dropout
			__lowerCAmelCase    :		Tuple		    =   num_conv_layers
			__lowerCAmelCase    :		Tuple		    =   input_feat_per_channel
			__lowerCAmelCase    :		Optional[int]		    =   input_channels
			__lowerCAmelCase    :		str		    =   conv_channels
			__lowerCAmelCase    :		str		    =   ctc_loss_reduction
			__lowerCAmelCase    :		Tuple		    =   ctc_zero_infinity

			# prevents config testing fail with exporting to json
			__lowerCAmelCase    :		List[str]		    =   list(_SCREAMING_SNAKE_CASE				)
			__lowerCAmelCase    :		Dict		    =   list(_SCREAMING_SNAKE_CASE				)

			if len(self.conv_kernel				) != self.num_conv_layers:
				raise ValueError(
				    'Configuration for convolutional module is incorrect. '
				    'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` '
				    f"but is `len(config.conv_kernel) = {len(self.conv_kernel				)}`, "
				    f"`config.num_conv_layers = {self.num_conv_layers}`."				)