


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







     @staticmethod
     def 			lowerCamelCase		(     *UpperCamelCase__						,     **UpperCamelCase__						)     ->		Union[str, Any]:

         '''simple docstring'''




         pass







  def   __lowerCAmelCase						(						lowercase  :  str			)     ->						Dict:


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







@is_pipeline_test
@require_torch
@require_vision
class _lowerCAmelCase    (				unittest.TestCase     ):
   __UpperCAmelCase			:	Any							  =  MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







   @require_pytesseract
   @require_vision
   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		int:

       '''simple docstring'''




       snake_case   :				Dict				 =   pipeline(
           "document-question-answering"						,     model=UpperCamelCase__						,     tokenizer=UpperCamelCase__						,     image_processor=UpperCamelCase__						)

       snake_case   :				List[Any]				 =   INVOICE_URL
       snake_case   :				int				 =   list(zip(*apply_tesseract(load_image(UpperCamelCase__						)						,     UpperCamelCase__						,     ""						)						)						)
       snake_case   :				Tuple				 =   "What is the placebo?"
       snake_case   :				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 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   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 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Any				 =   pipeline("document-question-answering"						,     model="hf-internal-testing/tiny-random-layoutlmv2"						)
       snake_case   :				Optional[Any]				 =   INVOICE_URL
       snake_case   :				Optional[int]				 =   "How many cats are there?"

       snake_case   :				Dict				 =   [
           {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
           {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
       ]
       snake_case   :				str				 =   dqa_pipeline(image=UpperCamelCase__						,     question=UpperCamelCase__						,     top_k=2						)
       self.assertEqual(nested_simplify(UpperCamelCase__						,     decimals=4						)						,     UpperCamelCase__						)

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

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







   @slow
   @require_torch
   @require_detectrona
   @require_pytesseract
   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




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

       snake_case   :				Dict				 =   dqa_pipeline(image=UpperCamelCase__						,     question=UpperCamelCase__						,     top_k=2						)
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
           ]						,     )

       snake_case   :				str				 =   dqa_pipeline({"image": image, "question": question}						,     top_k=2						)
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
           ]						,     )

       snake_case   :				int				 =   dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]						,     top_k=2						)
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     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 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   pipeline(
           "document-question-answering"						,     model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"						,     revision="9977165"						,     max_seq_len=50						,     )
       snake_case   :				Any				 =   INVOICE_URL
       snake_case   :				Tuple				 =   "What is the invoice number?"

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

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

       snake_case   :				Optional[Any]				 =   dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}]						,     top_k=2						)
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     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 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




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

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

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

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

       snake_case   :				List[str]				 =   list(zip(*apply_tesseract(load_image(UpperCamelCase__						)						,     UpperCamelCase__						,     ""						)						)						)

       # 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(UpperCamelCase__						,     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 			lowerCamelCase		(     self						)     ->		Union[str, Any]:

       '''simple docstring'''




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

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

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

       snake_case   :				List[Any]				 =   list(zip(*apply_tesseract(load_image(UpperCamelCase__						)						,     UpperCamelCase__						,     ""						)						)						)

       # This model should also work if `image` is set to None
       snake_case   :				Dict				 =   dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}						,     top_k=2						)
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     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 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




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

       snake_case   :				Union[str, Any]				 =   INVOICE_URL
       snake_case   :				Dict				 =   "What is the invoice number?"
       snake_case   :				Union[str, Any]				 =   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 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




       pass









"""simple docstring"""



from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import (
    BaseOutput,
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)







@dataclass
class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	Union[List[PIL.Image.Image], np.ndarray]
   __UpperCAmelCase			:	Optional[List[bool]]


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
  from .pipeline_cycle_diffusion import CycleDiffusionPipeline
  from .pipeline_stable_diffusion import StableDiffusionPipeline
  from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline
  from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline
  from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
  from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
  from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline
  from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline
  from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline
  from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline
  from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline
  from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline
  from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline
  from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
  from .pipeline_stable_unclip import StableUnCLIPPipeline
  from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline
  from .safety_checker import StableDiffusionSafetyChecker
  from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer

try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline
else:
  from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline


try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.26.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      StableDiffusionDepthaImgPipeline,
      StableDiffusionDiffEditPipeline,
      StableDiffusionPixaPixZeroPipeline,
  )
else:
  from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline
  from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline
  from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline


try:
  if not (
      is_torch_available()
      and is_transformers_available()
      and is_k_diffusion_available()
      and is_k_diffusion_version(""">=""", """0.0.12""")
  ):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
  from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline

try:
  if not (is_transformers_available() and is_onnx_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_onnx_objects import *  # noqa F403
else:
  from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline
  from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline
  from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline
  from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy
  from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline

if is_transformers_available() and is_flax_available():
  import flax







  @flax.struct.dataclass
  class _lowerCAmelCase    (				snake_case_     ):
     __UpperCAmelCase			:	np.ndarray
     __UpperCAmelCase			:	List[bool]



  from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState
  from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline
  from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline
  from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline
  from .safety_checker_flax import FlaxStableDiffusionSafetyChecker





