


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 		_snake_case  :




      @staticmethod
      def 	SCREAMING_SNAKE_CASE__    (		*_lowerCamelCase ,  **_lowerCamelCase   ):
            pass







     def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple						):




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







@is_pipeline_test
@require_torch
@require_vision
class 		_snake_case  (				unittest.TestCase							):
 SCREAMING_SNAKE_CASE__				=	MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




 @require_pytesseract
 @require_vision
 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:Union[str, Any]										=	pipeline(
           '''document-question-answering''' ,  model=a_ ,  tokenizer=a_ ,  image_processor=a_   )

       a						:Any										=	INVOICE_URL
       a						:List[str]										=	list(zip(*apply_tesseract(load_image(a_   ) ,  a_ ,  ''''''   )   )   )
       a						:Any										=	'''What is the placebo?'''
       a						:Any										=	[
           {
               '''image''': load_image(a_   ),
               '''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   ):
       a						:Optional[Any]										=	dqa_pipeline(a_ ,  top_k=2   )
       self.assertEqual(
           a_ ,  [
               [
                   {'''score''': ANY(a_   ), '''answer''': ANY(a_   ), '''start''': ANY(a_   ), '''end''': ANY(a_   )},
                   {'''score''': ANY(a_   ), '''answer''': ANY(a_   ), '''start''': ANY(a_   ), '''end''': ANY(a_   )},
               ]
           ]
           * 3 ,  )




 @require_torch
 @require_detectrona
 @require_pytesseract
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:str										=	pipeline('''document-question-answering''' ,  model='''hf-internal-testing/tiny-random-layoutlmv2'''   )
       a						:Union[str, Any]										=	INVOICE_URL
       a						:Tuple										=	'''How many cats are there?'''

       a						:Tuple										=	[
           {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
           {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
       ]
       a						:Union[str, Any]										=	dqa_pipeline(image=a_ ,  question=a_ ,  top_k=2   )
       self.assertEqual(nested_simplify(a_ ,  decimals=4   ) ,  a_   )

       a						:int										=	dqa_pipeline({'''image''': image, '''question''': question} ,  top_k=2   )
       self.assertEqual(nested_simplify(a_ ,  decimals=4   ) ,  a_   )

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

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




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

       a						:Union[str, Any]										=	dqa_pipeline(image=a_ ,  question=a_ ,  top_k=2   )
       self.assertEqual(
           nested_simplify(a_ ,  decimals=4   ) ,  [
               {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
           ] ,  )

       a						:Union[str, Any]										=	dqa_pipeline({'''image''': image, '''question''': question} ,  top_k=2   )
       self.assertEqual(
           nested_simplify(a_ ,  decimals=4   ) ,  [
               {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
           ] ,  )

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

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

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

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

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

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

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

       a						:Union[str, Any]										=	list(zip(*apply_tesseract(load_image(a_   ) ,  a_ ,  ''''''   )   )   )

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

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

       a						:List[Any]										=	dqa_pipeline(
           [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] ,  top_k=2   )
       self.assertEqual(
           nested_simplify(a_ ,  decimals=4   ) ,  [
               [
                   {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                   {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               ]
           ]
           * 2 ,  )

       a						:Union[str, Any]										=	list(zip(*apply_tesseract(load_image(a_   ) ,  a_ ,  ''''''   )   )   )

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

       a						:int										=	INVOICE_URL
       a						:Optional[Any]										=	'''What is the invoice number?'''
       a						:Optional[int]										=	dqa_pipeline(image=a_ ,  question=a_ ,  top_k=2   )
       self.assertEqual(nested_simplify(a_ ,  decimals=4   ) ,  [{'''answer''': '''us-001'''}]   )




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









import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


snake_case    :   Any	    =					pytest.mark.integration
@pytest.mark.parametrize('''path'''			,  ['''paws''', '''csv''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      inspect_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
      a						:List[Any]										=	path + '''.py'''
      assert script_name in os.listdir(UpperCAmelCase_						)
      assert "__pycache__" not in os.listdir(UpperCAmelCase_						)
@pytest.mark.filterwarnings('''ignore:inspect_metric is deprecated:FutureWarning'''						)
@pytest.mark.filterwarnings('''ignore:metric_module_factory is deprecated:FutureWarning'''						)
@pytest.mark.parametrize('''path'''			,  ['''accuracy''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		Any						):




      """simple docstring"""





      inspect_metric(UpperCAmelCase_			,  UpperCAmelCase_						)
      a						:Dict										=	path + '''.py'''
      assert script_name in os.listdir(UpperCAmelCase_						)
      assert "__pycache__" not in os.listdir(UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''path, config_name, expected_splits'''			,  [
        ('''squad''', '''plain_text''', ['''train''', '''validation''']),
        ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']),
        ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		str						):




      """simple docstring"""





      a						:List[str]										=	get_dataset_config_info(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)
      assert info.config_name == config_name
      assert list(info.splits.keys()						) == expected_splits
@pytest.mark.parametrize(
    '''path, config_name, expected_exception'''			,  [
        ('''paws''', None, ValueError),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Tuple						):




      """simple docstring"""





      with pytest.raises(UpperCAmelCase_						):
            get_dataset_config_info(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''path, expected'''			,  [
        ('''squad''', '''plain_text'''),
        ('''acronym_identification''', '''default'''),
        ('''lhoestq/squad''', '''plain_text'''),
        ('''lhoestq/test''', '''default'''),
        ('''lhoestq/demo1''', '''lhoestq--demo1'''),
        ('''dalle-mini/wit''', '''dalle-mini--wit'''),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		int						):




      """simple docstring"""





      a						:List[str]										=	get_dataset_config_names(UpperCAmelCase_						)
      assert expected in config_names
@pytest.mark.parametrize(
    '''path, expected_configs, expected_splits_in_first_config'''			,  [
        ('''squad''', ['''plain_text'''], ['''train''', '''validation''']),
        ('''dalle-mini/wit''', ['''dalle-mini--wit'''], ['''train''']),
        ('''paws''', ['''labeled_final''', '''labeled_swap''', '''unlabeled_final'''], ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		Optional[int]						):




      """simple docstring"""





      a						:Optional[int]										=	get_dataset_infos(UpperCAmelCase_						)
      assert list(infos.keys()						) == expected_configs
      a						:Union[str, Any]										=	expected_configs[0]
      assert expected_config in infos
      a						:List[Any]										=	infos[expected_config]
      assert info.config_name == expected_config
      assert list(info.splits.keys()						) == expected_splits_in_first_config
@pytest.mark.parametrize(
    '''path, expected_config, expected_splits'''			,  [
        ('''squad''', '''plain_text''', ['''train''', '''validation''']),
        ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']),
        ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		Any			,  UpperCAmelCase_						:		Optional[int]						):




      """simple docstring"""





      a						:Union[str, Any]										=	get_dataset_infos(UpperCAmelCase_						)
      assert expected_config in infos
      a						:int										=	infos[expected_config]
      assert info.config_name == expected_config
      assert list(info.splits.keys()						) == expected_splits






@pytest.mark.parametrize(
    '''path, config_name, expected_exception'''			,  [
        ('''paws''', None, ValueError),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Union[str, Any]			,  UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		str						):




      """simple docstring"""





      with pytest.raises(UpperCAmelCase_						):
            get_dataset_split_names(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)





