



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

      @staticmethod
      def 						_snake_case					(     *lowercase_		:       str  ,     **lowercase_		:       Optional[int]				):
          pass





     def __lowercase			(							_a    ):
         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__    :  Tuple							    =	(
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)




@is_pipeline_test
@require_torch
@require_vision
class        _UpperCAmelCase       (			unittest.TestCase):
 _lowerCAmelCase   :			List[Any]							=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

 @require_pytesseract
 @require_vision
 def 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       Tuple  ,     lowercase_		:       int  ,     lowercase_		:       Any				):
     snake_case_ :     Dict				=	pipeline(
         '''document-question-answering'''  ,     model=lowercase_  ,     tokenizer=lowercase_  ,     image_processor=lowercase_				)

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

 def 						_snake_case					(     self		:       int  ,     lowercase_		:       List[Any]  ,     lowercase_		:       Any				):
     snake_case_ :     int				=	dqa_pipeline(lowercase_  ,     top_k=2				)
     self.assertEqual(
         lowercase_  ,     [
             [
                 {'''score''': ANY(lowercase_				), '''answer''': ANY(lowercase_				), '''start''': ANY(lowercase_				), '''end''': ANY(lowercase_				)},
                 {'''score''': ANY(lowercase_				), '''answer''': ANY(lowercase_				), '''start''': ANY(lowercase_				), '''end''': ANY(lowercase_				)},
             ]
         ]
         * 3  ,     )

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

     snake_case_ :     Optional[Any]				=	[
         {'''score''': 0.00_01, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
         {'''score''': 0.00_01, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
     ]
     snake_case_ :     str				=	dqa_pipeline(image=lowercase_  ,     question=lowercase_  ,     top_k=2				)
     self.assertEqual(nested_simplify(lowercase_  ,     decimals=4				)  ,     lowercase_				)

     snake_case_ :     Dict				=	dqa_pipeline({'''image''': image, '''question''': question}  ,     top_k=2				)
     self.assertEqual(nested_simplify(lowercase_  ,     decimals=4				)  ,     lowercase_				)

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

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

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

     snake_case_ :     int				=	dqa_pipeline(image=lowercase_  ,     question=lowercase_  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

     snake_case_ :     str				=	dqa_pipeline({'''image''': image, '''question''': question}  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

     snake_case_ :     str				=	dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             [
                 {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ],
         ]
         * 2  ,     )

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

     snake_case_ :     str				=	dqa_pipeline(image=lowercase_  ,     question=lowercase_  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

     snake_case_ :     Dict				=	dqa_pipeline({'''image''': image, '''question''': question}  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

     snake_case_ :     Tuple				=	dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             [
                 {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                 {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]
         ]
         * 2  ,     )

 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 						_snake_case					(     self		:       Dict				):
     snake_case_ :     List[str]				=	AutoTokenizer.from_pretrained(
         '''impira/layoutlm-document-qa'''  ,     revision='''3dc6de3'''  ,     add_prefix_space=lowercase_				)
     snake_case_ :     str				=	pipeline(
         '''document-question-answering'''  ,     model='''impira/layoutlm-document-qa'''  ,     tokenizer=lowercase_  ,     revision='''3dc6de3'''  ,     )
     snake_case_ :     Dict				=	INVOICE_URL
     snake_case_ :     Tuple				=	'''What is the invoice number?'''

     snake_case_ :     Any				=	dqa_pipeline(image=lowercase_  ,     question=lowercase_  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
         ]  ,     )

     snake_case_ :     Tuple				=	dqa_pipeline({'''image''': image, '''question''': question}  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
         ]  ,     )

     snake_case_ :     int				=	dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             [
                 {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             ]
         ]
         * 2  ,     )

     snake_case_ :     Optional[Any]				=	list(zip(*apply_tesseract(load_image(lowercase_				)  ,     lowercase_  ,     ''''''				)				)				)

     # This model should also work if `image` is set to None
     snake_case_ :     Optional[int]				=	dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
         ]  ,     )

 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 						_snake_case					(     self		:       int				):
     snake_case_ :     str				=	AutoTokenizer.from_pretrained(
         '''impira/layoutlm-document-qa'''  ,     revision='''3dc6de3'''  ,     add_prefix_space=lowercase_				)
     snake_case_ :     str				=	pipeline(
         '''document-question-answering'''  ,     model='''impira/layoutlm-document-qa'''  ,     tokenizer=lowercase_  ,     revision='''3dc6de3'''  ,     max_seq_len=50  ,     )
     snake_case_ :     str				=	INVOICE_URL
     snake_case_ :     str				=	'''What is the invoice number?'''

     snake_case_ :     str				=	dqa_pipeline(image=lowercase_  ,     question=lowercase_  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

     snake_case_ :     Dict				=	dqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             [
                 {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]
         ]
         * 2  ,     )

     snake_case_ :     Dict				=	list(zip(*apply_tesseract(load_image(lowercase_				)  ,     lowercase_  ,     ''''''				)				)				)

     # This model should also work if `image` is set to None
     snake_case_ :     Union[str, Any]				=	dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}  ,     top_k=2				)
     self.assertEqual(
         nested_simplify(lowercase_  ,     decimals=4				)  ,     [
             {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]  ,     )

 @slow
 @require_torch
 def 						_snake_case					(     self		:       List[str]				):
     snake_case_ :     Dict				=	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'''  ,     )

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






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










"""simple docstring"""

import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
     import torch

if is_soundfile_availble():
     import soundfile as sf

if is_vision_available():
     from PIL import Image




def __lowercase			(							_a=""    ):
    snake_case_ :     List[str]				=	tempfile.mkdtemp()
    return os.path.join(_a		,			str(uuid.uuida()    ) + suffix    )




@require_soundfile
@require_torch
class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       str				):
     snake_case_ :     int				=	torch.rand(12  ,     dtype=torch.floataa				) - 0.5
     snake_case_ :     Optional[int]				=	AgentAudio(lowercase_				)
     snake_case_ :     List[str]				=	str(agent_type.to_string()				)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(lowercase_  ,     agent_type.to_raw()  ,     atol=1E-4				)				)

     del agent_type

     # Ensure the path remains even after the object deletion
     self.assertTrue(os.path.exists(lowercase_				)				)

     # Ensure that the file contains the same value as the original tensor
     snake_case_,	snake_case_ :     int				=	sf.read(lowercase_				)
     self.assertTrue(torch.allclose(lowercase_  ,     torch.tensor(lowercase_				)  ,     atol=1E-4				)				)






 def 						_snake_case					(     self		:       Optional[int]				):
     snake_case_ :     Any				=	torch.rand(12  ,     dtype=torch.floataa				) - 0.5
     snake_case_ :     List[str]				=	get_new_path(suffix='''.wav'''				)
     sf.write(lowercase_  ,     lowercase_  ,     16000				)

     snake_case_ :     Tuple				=	AgentAudio(lowercase_				)

     self.assertTrue(torch.allclose(lowercase_  ,     agent_type.to_raw()  ,     atol=1E-4				)				)
     self.assertEqual(agent_type.to_string()  ,     lowercase_				)





@require_vision
@require_torch
class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       Tuple				):
     snake_case_ :     List[Any]				=	torch.randint(0  ,     256  ,     (64, 64, 3)				)
     snake_case_ :     str				=	AgentImage(lowercase_				)
     snake_case_ :     Union[str, Any]				=	str(agent_type.to_string()				)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(lowercase_  ,     agent_type._tensor  ,     atol=1E-4				)				)

     self.assertIsInstance(agent_type.to_raw()  ,     Image.Image				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)

 def 						_snake_case					(     self		:       str				):
     snake_case_ :     Any				=	Path(get_tests_dir('''fixtures/tests_samples/COCO'''				)				) / '''000000039769.png'''
     snake_case_ :     Optional[int]				=	Image.open(lowercase_				)
     snake_case_ :     Tuple				=	AgentImage(lowercase_				)

     self.assertTrue(path.samefile(agent_type.to_string()				)				)
     self.assertTrue(image == agent_type.to_raw()				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)






 def 						_snake_case					(     self		:       str				):
     snake_case_ :     int				=	Path(get_tests_dir('''fixtures/tests_samples/COCO'''				)				) / '''000000039769.png'''
     snake_case_ :     Dict				=	Image.open(lowercase_				)
     snake_case_ :     List[str]				=	AgentImage(lowercase_				)

     self.assertFalse(path.samefile(agent_type.to_string()				)				)
     self.assertTrue(image == agent_type.to_raw()				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)





class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       Any				):
     snake_case_ :     Tuple				=	'''Hey!'''
     snake_case_ :     Optional[Any]				=	AgentText(lowercase_				)

     self.assertEqual(lowercase_  ,     agent_type.to_string()				)
     self.assertEqual(lowercase_  ,     agent_type.to_raw()				)
     self.assertEqual(lowercase_  ,     lowercase_				)





