



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 		UpperCamelCase							(						*snake_case_							:    Optional[int]				,	**snake_case_							:    Dict)->							Tuple:
										pass






				def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]	)  ->						Optional[int]:
									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 						__a (     unittest.TestCase				):
	SCREAMING_SNAKE_CASE       =       MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






	@require_pytesseract
	@require_vision
	def 		UpperCamelCase							(						self							:    List[Any]				,	snake_case_							:    Optional[int]				,	snake_case_							:    int				,	snake_case_							:    List[Any])->							Dict:
						__lowerCAmelCase	   =pipeline(
						    """document-question-answering"""				,	model=UpperCamelCase__				,	tokenizer=UpperCamelCase__				,	image_processor=UpperCamelCase__)

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






	def 		UpperCamelCase							(						self							:    Optional[Any]				,	snake_case_							:    Union[str, Any]				,	snake_case_							:    str)->							Optional[int]:
						__lowerCAmelCase	   =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 		UpperCamelCase							(						self							:    str)->							Any:
						__lowerCAmelCase	   =pipeline("""document-question-answering"""				,	model="""hf-internal-testing/tiny-random-layoutlmv2""")
						__lowerCAmelCase	   =INVOICE_URL
						__lowerCAmelCase	   ='''How many cats are there?'''

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

						__lowerCAmelCase	   =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
						__lowerCAmelCase	   ='''./tests/fixtures/tests_samples/COCO/000000039769.png'''
						__lowerCAmelCase	   =dqa_pipeline(image=UpperCamelCase__				,	question=UpperCamelCase__				,	top_k=2)
						self.assertEqual(UpperCamelCase__				,	[])

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






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

						__lowerCAmelCase	   =dqa_pipeline(image=UpperCamelCase__				,	question=UpperCamelCase__				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
						        {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline({"""image""": image, """question""": question}				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
						        {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline(
						    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        [
						            {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
						            {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
						        ],
						    ]
						    * 2				,	)






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

						__lowerCAmelCase	   =dqa_pipeline(image=UpperCamelCase__				,	question=UpperCamelCase__				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
						        {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline({"""image""": image, """question""": question}				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
						        {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline(
						    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        [
						            {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
						            {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
						        ]
						    ]
						    * 2				,	)






	@slow
	@require_torch
	@require_pytesseract
	@require_vision
	def 		UpperCamelCase							(						self							:    Optional[int])->							Union[str, Any]:
						__lowerCAmelCase	   =AutoTokenizer.from_pretrained(
						    """impira/layoutlm-document-qa"""				,	revision="""3dc6de3"""				,	add_prefix_space=UpperCamelCase__)
						__lowerCAmelCase	   =pipeline(
						    """document-question-answering"""				,	model="""impira/layoutlm-document-qa"""				,	tokenizer=UpperCamelCase__				,	revision="""3dc6de3"""				,	)
						__lowerCAmelCase	   =INVOICE_URL
						__lowerCAmelCase	   ='''What is the invoice number?'''

						__lowerCAmelCase	   =dqa_pipeline(image=UpperCamelCase__				,	question=UpperCamelCase__				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
						        {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline({"""image""": image, """question""": question}				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
						        {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline(
						    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        [
						            {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
						            {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
						        ]
						    ]
						    * 2				,	)

						__lowerCAmelCase	   =list(zip(*apply_tesseract(load_image(UpperCamelCase__)				,	UpperCamelCase__				,	"""""")))

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






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

						__lowerCAmelCase	   =dqa_pipeline(image=UpperCamelCase__				,	question=UpperCamelCase__				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
						        {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
						    ]				,	)

						__lowerCAmelCase	   =dqa_pipeline(
						    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]				,	top_k=2)
						self.assertEqual(
						    nested_simplify(UpperCamelCase__				,	decimals=4)				,	[
						        [
						            {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
						            {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
						        ]
						    ]
						    * 2				,	)

						__lowerCAmelCase	   =list(zip(*apply_tesseract(load_image(UpperCamelCase__)				,	UpperCamelCase__				,	"""""")))

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






	@slow
	@require_torch
	def 		UpperCamelCase							(						self							:    Union[str, Any])->							Optional[int]:
						__lowerCAmelCase	   =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	   =INVOICE_URL
						__lowerCAmelCase	   ='''What is the invoice number?'''
						__lowerCAmelCase	   =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 		UpperCamelCase							(						self							:    int)->							Tuple:
						pass









from dataclasses import asdict, dataclass
from typing import Optional

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


lowercase_	       =			logging.get_logger(__name__)

# TODO Update this
lowercase_	       =			{
    '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''',
    # See all ESM models at https://huggingface.co/models?filter=esm
}






class 						__a (     SCREAMING_SNAKE_CASE				):
	SCREAMING_SNAKE_CASE       =       "esm"



	def __init__(						self							:    Any				,	snake_case_							:    List[str]=None				,	snake_case_							:    Tuple=None				,	snake_case_							:    str=None				,	snake_case_							:    str=7_68				,	snake_case_							:    Optional[int]=12				,	snake_case_							:    Tuple=12				,	snake_case_							:    Dict=30_72				,	snake_case_							:    Optional[Any]=0.1				,	snake_case_							:    Dict=0.1				,	snake_case_							:    Optional[Any]=10_26				,	snake_case_							:    int=0.0_2				,	snake_case_							:    Any=1e-12				,	snake_case_							:    Union[str, Any]="absolute"				,	snake_case_							:    Optional[int]=True				,	snake_case_							:    Tuple=None				,	snake_case_							:    Optional[int]=False				,	snake_case_							:    int=False				,	snake_case_							:    Optional[int]=None				,	snake_case_							:    List[Any]=None				,	**snake_case_							:    Optional[int]				,	)->							Any:
						super().__init__(pad_token_id=snake_case_				,	mask_token_id=snake_case_				,	**snake_case_)

						__lowerCAmelCase	   =vocab_size
						__lowerCAmelCase	   =hidden_size
						__lowerCAmelCase	   =num_hidden_layers
						__lowerCAmelCase	   =num_attention_heads
						__lowerCAmelCase	   =intermediate_size
						__lowerCAmelCase	   =hidden_dropout_prob
						__lowerCAmelCase	   =attention_probs_dropout_prob
						__lowerCAmelCase	   =max_position_embeddings
						__lowerCAmelCase	   =initializer_range
						__lowerCAmelCase	   =layer_norm_eps
						__lowerCAmelCase	   =position_embedding_type
						__lowerCAmelCase	   =use_cache
						__lowerCAmelCase	   =emb_layer_norm_before
						__lowerCAmelCase	   =token_dropout
						__lowerCAmelCase	   =is_folding_model
						if is_folding_model:
											if esmfold_config is None:
																logger.info("""No esmfold_config supplied for folding model, using default values.""")
																__lowerCAmelCase	   =EsmFoldConfig()
											elif isinstance(snake_case_				,	snake_case_):
																__lowerCAmelCase	   =EsmFoldConfig(**snake_case_)
											__lowerCAmelCase	   =esmfold_config
											if vocab_list is None:
																logger.warning("""No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!""")
																__lowerCAmelCase	   =get_default_vocab_list()
											else:
																__lowerCAmelCase	   =vocab_list
						else:
											__lowerCAmelCase	   =None
											__lowerCAmelCase	   =None
						if self.esmfold_config is not None and getattr(self.esmfold_config				,	"""use_esm_attn_map"""				,	snake_case_):
											raise ValueError("""The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!""")



	def 		UpperCamelCase							(						self							:    Dict)->							Union[str, Any]:
						__lowerCAmelCase	   =super().to_dict()
						if isinstance(self.esmfold_config				,	snake_case_):
											__lowerCAmelCase	   =self.esmfold_config.to_dict()
						return output






@dataclass
class 						__a :
	SCREAMING_SNAKE_CASE       =       None
	SCREAMING_SNAKE_CASE       =       True
	SCREAMING_SNAKE_CASE       =       False
	SCREAMING_SNAKE_CASE       =       False
	SCREAMING_SNAKE_CASE       =       False
	SCREAMING_SNAKE_CASE       =       0

	SCREAMING_SNAKE_CASE       =       True
	SCREAMING_SNAKE_CASE       =       False

	SCREAMING_SNAKE_CASE       =       128
	SCREAMING_SNAKE_CASE       =       None



	def 		UpperCamelCase							(						self							:    Dict)->							List[Any]:
						if self.trunk is None:
											__lowerCAmelCase	   =TrunkConfig()
						elif isinstance(self.trunk				,	snake_case_):
											__lowerCAmelCase	   =TrunkConfig(**self.trunk)



	def 		UpperCamelCase							(						self							:    Optional[Any])->							int:
						__lowerCAmelCase	   =asdict(self)
						__lowerCAmelCase	   =self.trunk.to_dict()
						return output






@dataclass
class 						__a :
	SCREAMING_SNAKE_CASE       =       48
	SCREAMING_SNAKE_CASE       =       1024
	SCREAMING_SNAKE_CASE       =       128
	SCREAMING_SNAKE_CASE       =       32
	SCREAMING_SNAKE_CASE       =       32
	SCREAMING_SNAKE_CASE       =       32
	SCREAMING_SNAKE_CASE       =       0
	SCREAMING_SNAKE_CASE       =       0
	SCREAMING_SNAKE_CASE       =       False
	SCREAMING_SNAKE_CASE       =       4
	SCREAMING_SNAKE_CASE       =       128
	SCREAMING_SNAKE_CASE       =       None



	def 		UpperCamelCase							(						self							:    Tuple)->							Optional[int]:
						if self.structure_module is None:
											__lowerCAmelCase	   =StructureModuleConfig()
						elif isinstance(self.structure_module				,	snake_case_):
											__lowerCAmelCase	   =StructureModuleConfig(**self.structure_module)

						if self.max_recycles <= 0:
											raise ValueError(F"""`max_recycles` should be positive, got {self.max_recycles}.""")
						if self.sequence_state_dim % self.sequence_state_dim != 0:
											raise ValueError(
											    """`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got"""
											    F""" {self.sequence_state_dim} and {self.sequence_state_dim}.""")
						if self.pairwise_state_dim % self.pairwise_state_dim != 0:
											raise ValueError(
											    """`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got"""
											    F""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""")

						__lowerCAmelCase	   =self.sequence_state_dim // self.sequence_head_width
						__lowerCAmelCase	   =self.pairwise_state_dim // self.pairwise_head_width

						if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
											raise ValueError(
											    """`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got"""
											    F""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""")
						if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
											raise ValueError(
											    """`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got"""
											    F""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""")
						if self.pairwise_state_dim % 2 != 0:
											raise ValueError(F"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""")

						if self.dropout >= 0.4:
											raise ValueError(F"""`dropout` should not be greater than 0.4, got {self.dropout}.""")



	def 		UpperCamelCase							(						self							:    Dict)->							Any:
						__lowerCAmelCase	   =asdict(self)
						__lowerCAmelCase	   =self.structure_module.to_dict()
						return output






@dataclass
class 						__a :
	SCREAMING_SNAKE_CASE       =       384
	SCREAMING_SNAKE_CASE       =       128
	SCREAMING_SNAKE_CASE       =       16
	SCREAMING_SNAKE_CASE       =       128
	SCREAMING_SNAKE_CASE       =       12
	SCREAMING_SNAKE_CASE       =       4
	SCREAMING_SNAKE_CASE       =       8
	SCREAMING_SNAKE_CASE       =       0.1
	SCREAMING_SNAKE_CASE       =       8
	SCREAMING_SNAKE_CASE       =       1
	SCREAMING_SNAKE_CASE       =       2
	SCREAMING_SNAKE_CASE       =       7
	SCREAMING_SNAKE_CASE       =       10
	SCREAMING_SNAKE_CASE       =       1E-8
	SCREAMING_SNAKE_CASE       =       1E5



	def 		UpperCamelCase							(						self							:    Any)->							Tuple:
						return asdict(self)






def 					__lowerCAmelCase		(  )  ->						List[Any]:
					return (
					    "<cls>",
					    "<pad>",
					    "<eos>",
					    "<unk>",
					    "L",
					    "A",
					    "G",
					    "V",
					    "S",
					    "E",
					    "R",
					    "T",
					    "I",
					    "D",
					    "P",
					    "K",
					    "Q",
					    "N",
					    "F",
					    "Y",
					    "M",
					    "H",
					    "W",
					    "C",
					    "X",
					    "B",
					    "U",
					    "Z",
					    "O",
					    ".",
					    "-",
					    "<null_1>",
					    "<mask>",
					)




