





from dataclasses import asdict, dataclass
from typing import Optional

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


UpperCAmelCase	:	Optional[int]		     =       logging.get_logger(__name__)

# TODO Update this
UpperCAmelCase	:	Optional[Any]		     =       {
    """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 				__lowerCAmelCase       (    UpperCamelCase__):
      _lowercase				:					List[Any]						=			"""esm"""







      def __init__(    self		,      lowerCAmelCase__=None		,      lowerCAmelCase__=None		,      lowerCAmelCase__=None		,      lowerCAmelCase__=7_6_8		,      lowerCAmelCase__=1_2		,      lowerCAmelCase__=1_2		,      lowerCAmelCase__=3_0_7_2		,      lowerCAmelCase__=0.1		,      lowerCAmelCase__=0.1		,      lowerCAmelCase__=1_0_2_6		,      lowerCAmelCase__=0.02		,      lowerCAmelCase__=1E-12		,      lowerCAmelCase__="absolute"		,      lowerCAmelCase__=True		,      lowerCAmelCase__=None		,      lowerCAmelCase__=False		,      lowerCAmelCase__=False		,      lowerCAmelCase__=None		,      lowerCAmelCase__=None		,      **lowerCAmelCase__		,      )   ->						List[Any]:



        '''simple docstring'''




        super().__init__(pad_token_id=lowerCAmelCase__		,      mask_token_id=lowerCAmelCase__		,      **lowerCAmelCase__							)

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







      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__	:					Optional[Any]										=super().to_dict()
        if isinstance(self.esmfold_config		,      lowerCAmelCase__							):
          a__	:					List[str]										=self.esmfold_config.to_dict()
        return output







@dataclass
class 				__lowerCAmelCase       :
      _lowercase				:					str						=			None
      _lowercase				:					bool						=			True
      _lowercase				:					bool						=			False
      _lowercase				:					bool						=			False
      _lowercase				:					bool						=			False
      _lowercase				:					float						=			0

      _lowercase				:					bool						=			True
      _lowercase				:					bool						=			False

      _lowercase				:					int						=			128
      _lowercase				:					"TrunkConfig"						=			None







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        if self.trunk is None:
          a__	:					List[Any]										=TrunkConfig()
        elif isinstance(self.trunk		,      lowerCAmelCase__							):
          a__	:					Dict										=TrunkConfig(**self.trunk							)







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					Any										=asdict(self							)
        a__	:					List[str]										=self.trunk.to_dict()
        return output







@dataclass
class 				__lowerCAmelCase       :
      _lowercase				:					int						=			48
      _lowercase				:					int						=			1024
      _lowercase				:					int						=			128
      _lowercase				:					int						=			32
      _lowercase				:					int						=			32
      _lowercase				:					int						=			32
      _lowercase				:					float						=			0
      _lowercase				:					float						=			0
      _lowercase				:					bool						=			False
      _lowercase				:					int						=			4
      _lowercase				:					Optional[int]						=			128
      _lowercase				:					"StructureModuleConfig"						=			None







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        if self.structure_module is None:
          a__	:					List[Any]										=StructureModuleConfig()
        elif isinstance(self.structure_module		,      lowerCAmelCase__							):
          a__	:					Dict										=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}.'''							)

        a__	:					int										=self.sequence_state_dim // self.sequence_head_width
        a__	:					Dict										=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    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__	:					str										=asdict(self							)
        a__	:					List[str]										=self.structure_module.to_dict()
        return output







@dataclass
class 				__lowerCAmelCase       :
      _lowercase				:					int						=			384
      _lowercase				:					int						=			128
      _lowercase				:					int						=			16
      _lowercase				:					int						=			128
      _lowercase				:					int						=			12
      _lowercase				:					int						=			4
      _lowercase				:					int						=			8
      _lowercase				:					float						=			0.1
      _lowercase				:					int						=			8
      _lowercase				:					int						=			1
      _lowercase				:					int						=			2
      _lowercase				:					int						=			7
      _lowercase				:					int						=			10
      _lowercase				:					float						=			1E-8
      _lowercase				:					float						=			1E5







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        return asdict(self							)
def      _A       (		):





  """simple docstring"""



  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>",
  )










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 		SCREAMING_SNAKE_CASE__   (   *lowerCAmelCase__				, **lowerCAmelCase__ )					->				List[str]:




         '''simple docstring'''
         pass





 def    snake_case_     (  snake_case     )  ->				Optional[Any]:
        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      ):
 __lowercase :							Dict        = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
 @require_pytesseract
 @require_vision
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				int:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   pipeline(
            'document-question-answering'				, model=lowerCAmelCase__				, tokenizer=lowerCAmelCase__				, image_processor=lowerCAmelCase__ )

        lowercase__:   int  			=   INVOICE_URL
        lowercase__:   Dict  			=   list(zip(*apply_tesseract(load_image(lowerCAmelCase__ )				, lowerCAmelCase__				, '' ) ) )
        lowercase__:   str  			=   'What is the placebo?'
        lowercase__:   Any  			=   [
            {
                'image': load_image(lowerCAmelCase__ ),
                'question': question,
            },
            {
                'image': image,
                'question': question,
            },
            {
                'image': image,
                'question': question,
                'word_boxes': word_boxes,
            },
        ]
        return dqa_pipeline, examples
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__ )					->				int:




        '''simple docstring'''
        lowercase__:   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 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




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

        lowercase__:   List[str]  			=   [
            {'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},
        ]
        lowercase__:   Dict  			=   dqa_pipeline(image=lowerCAmelCase__				, question=lowerCAmelCase__				, top_k=2 )
        self.assertEqual(nested_simplify(lowerCAmelCase__				, decimals=4 )				, lowerCAmelCase__ )

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

        # We can optionnally pass directly the words and bounding boxes
        lowercase__:   int  			=   './tests/fixtures/tests_samples/COCO/000000039769.png'
        lowercase__:   List[Any]  			=   []
        lowercase__:   Optional[int]  			=   []
        lowercase__:   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 		SCREAMING_SNAKE_CASE__   (   self )					->				Any:




        '''simple docstring'''
        lowercase__:   List[str]  			=   pipeline(
            'document-question-answering'				, model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'				, revision='9977165'				, )
        lowercase__:   int  			=   INVOICE_URL
        lowercase__:   str  			=   'What is the invoice number?'

        lowercase__:   Union[str, Any]  			=   dqa_pipeline(image=lowerCAmelCase__				, question=lowerCAmelCase__				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

        lowercase__:   Any  			=   dqa_pipeline({'image': image, 'question': question}				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

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




        '''simple docstring'''
        lowercase__:   Any  			=   pipeline(
            'document-question-answering'				, model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'				, revision='9977165'				, max_seq_len=50				, )
        lowercase__:   Optional[int]  			=   INVOICE_URL
        lowercase__:   Union[str, Any]  			=   'What is the invoice number?'

        lowercase__:   Optional[Any]  			=   dqa_pipeline(image=lowerCAmelCase__				, question=lowerCAmelCase__				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

        lowercase__:   Tuple  			=   dqa_pipeline({'image': image, 'question': question}				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

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




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   AutoTokenizer.from_pretrained(
            'impira/layoutlm-document-qa'				, revision='3dc6de3'				, add_prefix_space=lowerCAmelCase__ )
        lowercase__:   Optional[Any]  			=   pipeline(
            'document-question-answering'				, model='impira/layoutlm-document-qa'				, tokenizer=lowerCAmelCase__				, revision='3dc6de3'				, )
        lowercase__:   List[str]  			=   INVOICE_URL
        lowercase__:   Union[str, Any]  			=   'What is the invoice number?'

        lowercase__:   Dict  			=   dqa_pipeline(image=lowerCAmelCase__				, question=lowerCAmelCase__				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

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

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

        lowercase__:   Any  			=   list(zip(*apply_tesseract(load_image(lowerCAmelCase__ )				, lowerCAmelCase__				, '' ) ) )

        # This model should also work if `image` is set to None
        lowercase__:   List[Any]  			=   dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Any  			=   AutoTokenizer.from_pretrained(
            'impira/layoutlm-document-qa'				, revision='3dc6de3'				, add_prefix_space=lowerCAmelCase__ )
        lowercase__:   str  			=   pipeline(
            'document-question-answering'				, model='impira/layoutlm-document-qa'				, tokenizer=lowerCAmelCase__				, revision='3dc6de3'				, max_seq_len=50				, )
        lowercase__:   Optional[Any]  			=   INVOICE_URL
        lowercase__:   Optional[Any]  			=   'What is the invoice number?'

        lowercase__:   Optional[int]  			=   dqa_pipeline(image=lowerCAmelCase__				, question=lowerCAmelCase__				, top_k=2 )
        self.assertEqual(
            nested_simplify(lowerCAmelCase__				, 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},
            ]				, )

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

        lowercase__:   Optional[int]  			=   list(zip(*apply_tesseract(load_image(lowerCAmelCase__ )				, lowerCAmelCase__				, '' ) ) )

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




        '''simple docstring'''
        lowercase__:   List[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'				, )

        lowercase__:   int  			=   INVOICE_URL
        lowercase__:   int  			=   'What is the invoice number?'
        lowercase__:   Union[str, 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 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        pass


