






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




   @staticmethod
   def        __magic_name__		( *__lowercase		:							List[str]   , **__lowercase		:							Any )		->  List[Any]:
         pass





  def 	_a(					UpperCamelCase__			:  List[str]							):




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






@is_pipeline_test
@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):
 snake_case_												=							MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




 @require_pytesseract
 @require_vision
 def        __magic_name__		( self		:							Any   , __lowercase		:							str   , __lowercase		:							int   , __lowercase		:							Tuple )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			str													=pipeline(
           '''document-question-answering'''   , model=__lowercase   , tokenizer=__lowercase   , image_processor=__lowercase )

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




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

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

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

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




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

       SCREAMING_SNAKE_CASE__       :			Tuple													=dqa_pipeline(image=__lowercase   , question=__lowercase   , top_k=2 )
       self.assertEqual(
           nested_simplify(__lowercase   , decimals=4 )   , [
               {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
           ]   , )

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=dqa_pipeline({'''image''': image, '''question''': question}   , top_k=2 )
       self.assertEqual(
           nested_simplify(__lowercase   , decimals=4 )   , [
               {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
           ]   , )

       SCREAMING_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.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        __magic_name__		( self		:							Optional[int] )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=pipeline(
           '''document-question-answering'''   , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''   , revision='''9977165'''   , max_seq_len=50   , )
       SCREAMING_SNAKE_CASE__       :			str													=INVOICE_URL
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													='''What is the invoice number?'''

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

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

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

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

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

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

       SCREAMING_SNAKE_CASE__       :			List[Any]													=list(zip(*apply_tesseract(load_image(__lowercase )   , __lowercase   , '''''' ) ) )

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

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

       SCREAMING_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.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                   {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               ]
           ]
           * 2   , )

       SCREAMING_SNAKE_CASE__       :			Dict													=list(zip(*apply_tesseract(load_image(__lowercase )   , __lowercase   , '''''' ) ) )

       # This model should also work if `image` is set to None
       SCREAMING_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.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
               {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
           ]   , )




 @slow
 @require_torch
 def        __magic_name__		( self		:							Dict )		->  Dict:
       SCREAMING_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'''   , )

       SCREAMING_SNAKE_CASE__       :			List[str]													=INVOICE_URL
       SCREAMING_SNAKE_CASE__       :			int													='''What is the invoice number?'''
       SCREAMING_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        __magic_name__		( self		:							int )		->  Tuple:
       pass







'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def        __magic_name__		( self		:							List[str] )		->  Optional[Any]:
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def        __magic_name__		( self		:							Union[str, Any] )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' )
       SCREAMING_SNAKE_CASE__       :			str													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			List[Any]													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			List[str]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=output.images

       SCREAMING_SNAKE_CASE__       :			int													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Any													=np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			int													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			Any													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Dict													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Dict													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			List[str]													=np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-1




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			Any													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_dpmpp_2m''' )

       SCREAMING_SNAKE_CASE__       :			Tuple													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=sd_pipe(
           [prompt]   , generator=__lowercase   , guidance_scale=7.5   , num_inference_steps=15   , output_type='''np'''   , use_karras_sigmas=__lowercase   , )

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=np.array(
           [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2