






'''simple docstring'''



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


SCREAMING_SNAKE_CASE						=	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						=	{
    's-JoL/Open-Llama-V1': 'https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json',
}
class UpperCAmelCase_					(    a__				):




      """simple docstring"""




      UpperCamelCase_								=			'''open-llama'''






      def __init__( self			:		Any     ,      UpperCAmelCase			:		List[Any]=10_0000     ,      UpperCAmelCase			:		int=4096     ,      UpperCAmelCase			:		List[Any]=1_1008     ,      UpperCAmelCase			:		List[Any]=32     ,      UpperCAmelCase			:		int=32     ,      UpperCAmelCase			:		Any="silu"     ,      UpperCAmelCase			:		List[Any]=2048     ,      UpperCAmelCase			:		List[Any]=0.0_2     ,      UpperCAmelCase			:		Union[str, Any]=1e-6     ,      UpperCAmelCase			:		Dict=True     ,      UpperCAmelCase			:		Optional[Any]=0     ,      UpperCAmelCase			:		Dict=1     ,      UpperCAmelCase			:		Union[str, Any]=2     ,      UpperCAmelCase			:		Optional[Any]=False     ,      UpperCAmelCase			:		List[str]=True     ,      UpperCAmelCase			:		Union[str, Any]=0.1     ,      UpperCAmelCase			:		List[Any]=0.1     ,      UpperCAmelCase			:		Tuple=True     ,      UpperCAmelCase			:		str=True     ,      UpperCAmelCase			:		Any=None     ,      **UpperCAmelCase			:		Optional[int]     ,      )  ->  str:


       '''simple docstring'''

       lowercase					:   int				  =vocab_size
       lowercase					:   Dict				  =max_position_embeddings
       lowercase					:   Optional[Any]				  =hidden_size
       lowercase					:   List[str]				  =intermediate_size
       lowercase					:   Tuple				  =num_hidden_layers
       lowercase					:   Dict				  =num_attention_heads
       lowercase					:   str				  =hidden_act
       lowercase					:   Optional[int]				  =initializer_range
       lowercase					:   Optional[int]				  =rms_norm_eps
       lowercase					:   Tuple				  =use_cache
       lowercase					:   int				  =kwargs.pop(
           '''use_memorry_efficient_attention'''     ,      _A		)
       lowercase					:   List[Any]				  =hidden_dropout_prob
       lowercase					:   Dict				  =attention_dropout_prob
       lowercase					:   int				  =use_stable_embedding
       lowercase					:   int				  =shared_input_output_embedding
       lowercase					:   Optional[int]				  =rope_scaling
       self._rope_scaling_validation()

       super().__init__(
           pad_token_id=_A     ,      bos_token_id=_A     ,      eos_token_id=_A     ,      tie_word_embeddings=_A     ,      **_A     ,      )






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


       '''simple docstring'''

       if self.rope_scaling is None:
        return

       if not isinstance(self.rope_scaling     ,      _A		) or len(self.rope_scaling		) != 2:
        raise ValueError(
            '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
            f'got {self.rope_scaling}'		)
       lowercase					:   Optional[Any]				  =self.rope_scaling.get('''type'''     ,      _A		)
       lowercase					:   Tuple				  =self.rope_scaling.get('''factor'''     ,      _A		)
       if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
        raise ValueError(
            f'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'		)
       if rope_scaling_factor is None or not isinstance(_A     ,      _A		) or rope_scaling_factor <= 1.0:
        raise ValueError(f'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'		)











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




            """simple docstring"""




            @staticmethod
            def 				A__      ( *UpperCAmelCase			:		Union[str, Any]     ,      **UpperCAmelCase			:		Optional[int]		)  ->  Any:


             '''simple docstring'''

             pass





      def 							lowercase_				(    __A :       Union[str, Any]			)       ->      List[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.
SCREAMING_SNAKE_CASE						=	(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)
@is_pipeline_test
@require_torch
@require_vision
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      UpperCamelCase_								=			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






      @require_pytesseract
      @require_vision
      def 				A__      ( self			:		Union[str, Any]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		List[str]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Dict				  =pipeline(
           '''document-question-answering'''     ,      model=UpperCAmelCase     ,      tokenizer=UpperCAmelCase     ,      image_processor=UpperCAmelCase		)

       lowercase					:   Optional[Any]				  =INVOICE_URL
       lowercase					:   Any				  =list(zip(*apply_tesseract(load_image(UpperCAmelCase		)     ,      UpperCAmelCase     ,      ''''''		)		)		)
       lowercase					:   Dict				  ='''What is the placebo?'''
       lowercase					:   Optional[Any]				  =[
           {
               '''image''': load_image(UpperCAmelCase		),
               '''question''': question,
           },
           {
               '''image''': image,
               '''question''': question,
           },
           {
               '''image''': image,
               '''question''': question,
               '''word_boxes''': word_boxes,
           },
       ]
       return dqa_pipeline, examples






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		int		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Dict				  =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 				A__      ( self			:		int		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Dict				  =pipeline('''document-question-answering'''     ,      model='''hf-internal-testing/tiny-random-layoutlmv2'''		)
       lowercase					:   Union[str, Any]				  =INVOICE_URL
       lowercase					:   Tuple				  ='''How many cats are there?'''

       lowercase					:   Optional[int]				  =[
           {'''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					:   Optional[Any]				  =dqa_pipeline(image=UpperCAmelCase     ,      question=UpperCAmelCase     ,      top_k=2		)
       self.assertEqual(nested_simplify(UpperCAmelCase     ,      decimals=4		)     ,      UpperCAmelCase		)

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

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






      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def 				A__      ( self			:		int		)  ->  Any:


       '''simple docstring'''

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

       lowercase					:   List[str]				  =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},
           ]     ,      )

       lowercase					:   List[Any]				  =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},
           ]     ,      )

       lowercase					:   List[Any]				  =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 				A__      ( self			:		Dict		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   str				  =pipeline(
           '''document-question-answering'''     ,      model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''     ,      revision='''9977165'''     ,      max_seq_len=50     ,      )
       lowercase					:   Dict				  =INVOICE_URL
       lowercase					:   Any				  ='''What is the invoice number?'''

       lowercase					:   Union[str, Any]				  =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},
           ]     ,      )

       lowercase					:   int				  =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},
           ]     ,      )

       lowercase					:   Any				  =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 				A__      ( self			:		str		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Any				  =AutoTokenizer.from_pretrained(
           '''impira/layoutlm-document-qa'''     ,      revision='''3dc6de3'''     ,      add_prefix_space=UpperCAmelCase		)
       lowercase					:   Tuple				  =pipeline(
           '''document-question-answering'''     ,      model='''impira/layoutlm-document-qa'''     ,      tokenizer=UpperCAmelCase     ,      revision='''3dc6de3'''     ,      )
       lowercase					:   Tuple				  =INVOICE_URL
       lowercase					:   Any				  ='''What is the invoice number?'''

       lowercase					:   Dict				  =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},
           ]     ,      )

       lowercase					:   Any				  =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},
           ]     ,      )

       lowercase					:   str				  =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     ,      )

       lowercase					:   Tuple				  =list(zip(*apply_tesseract(load_image(UpperCAmelCase		)     ,      UpperCAmelCase     ,      ''''''		)		)		)

       # This model should also work if `image` is set to None
       lowercase					:   Dict				  =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 				A__      ( self			:		Dict		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Dict				  =AutoTokenizer.from_pretrained(
           '''impira/layoutlm-document-qa'''     ,      revision='''3dc6de3'''     ,      add_prefix_space=UpperCAmelCase		)
       lowercase					:   List[Any]				  =pipeline(
           '''document-question-answering'''     ,      model='''impira/layoutlm-document-qa'''     ,      tokenizer=UpperCAmelCase     ,      revision='''3dc6de3'''     ,      max_seq_len=50     ,      )
       lowercase					:   str				  =INVOICE_URL
       lowercase					:   int				  ='''What is the invoice number?'''

       lowercase					:   Tuple				  =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},
           ]     ,      )

       lowercase					:   Union[str, Any]				  =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     ,      )

       lowercase					:   List[str]				  =list(zip(*apply_tesseract(load_image(UpperCAmelCase		)     ,      UpperCAmelCase     ,      ''''''		)		)		)

       # This model should also work if `image` is set to None
       lowercase					:   Union[str, Any]				  =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 				A__      ( self			:		List[Any]		)  ->  Optional[int]:


       '''simple docstring'''

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


       '''simple docstring'''

       pass



