import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
      import torch


UpperCamelCase__											=					logging.get_logger(__name__)





@dataclass
class      A							:


   def __init__(self     :						Dict       ,  __UpperCAmelCase     :						str=False       ,  __UpperCAmelCase     :						List[str]=False       ,  __UpperCAmelCase     :						List[Any]=6.0       ,  __UpperCAmelCase     :						str=None       ,  __UpperCAmelCase     :						str=False       ,  __UpperCAmelCase     :						Dict=False       ,  __UpperCAmelCase     :						Optional[int]=None       ,  __UpperCAmelCase     :						str="fp4"       ,  __UpperCAmelCase     :						Dict=False       ,  **__UpperCAmelCase     :						Dict       ,  )						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  load_in_abit
         UpperCAmelCase__								=  load_in_abit
         UpperCAmelCase__								=  llm_inta_threshold
         UpperCAmelCase__								=  llm_inta_skip_modules
         UpperCAmelCase__								=  llm_inta_enable_fpaa_cpu_offload
         UpperCAmelCase__								=  llm_inta_has_fpaa_weight
         UpperCAmelCase__								=  bnb_abit_quant_type
         UpperCAmelCase__								=  bnb_abit_use_double_quant

         if bnb_abit_compute_dtype is None:
               UpperCAmelCase__								=  torch.floataa
         elif isinstance(lowerCAmelCase__       ,  lowerCAmelCase__		):
               UpperCAmelCase__								=  getattr(lowerCAmelCase__       ,  lowerCAmelCase__		)
         elif isinstance(lowerCAmelCase__       ,  torch.dtype		):
               UpperCAmelCase__								=  bnb_abit_compute_dtype
         else:
               raise ValueError("bnb_4bit_compute_dtype must be a string or a torch.dtype"		)

         self.post_init()


   def 			lowercase_      (self     :						List[str]		)						->				int:
         """simple docstring"""



         if not isinstance(self.llm_inta_threshold       ,  lowerCAmelCase__		):
               raise ValueError("llm_int8_threshold must be a float"		)

         if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules       ,  lowerCAmelCase__		):
               raise ValueError("llm_int8_skip_modules must be a list of strings"		)
         if not isinstance(self.llm_inta_enable_fpaa_cpu_offload       ,  lowerCAmelCase__		):
               raise ValueError("llm_int8_enable_fp32_cpu_offload must be a boolean"		)

         if not isinstance(self.llm_inta_has_fpaa_weight       ,  lowerCAmelCase__		):
               raise ValueError("llm_int8_has_fp16_weight must be a boolean"		)

         if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype       ,  torch.dtype		):
               raise ValueError("bnb_4bit_compute_dtype must be torch.dtype"		)

         if not isinstance(self.bnb_abit_quant_type       ,  lowerCAmelCase__		):
               raise ValueError("bnb_4bit_quant_type must be a string"		)

         if not isinstance(self.bnb_abit_use_double_quant       ,  lowerCAmelCase__		):
               raise ValueError("bnb_4bit_use_double_quant must be a boolean"		)

         if self.load_in_abit and not version.parse(importlib.metadata.version("bitsandbytes"		)		) >= version.parse(
             "0.39.0"		):
               raise ValueError(
                   "4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version"		)


   def 			lowercase_      (self     :						List[str]		)						->				Tuple:
         """simple docstring"""



         return self.load_in_abit or self.load_in_abit


   def 			lowercase_      (self     :						int		)						->				Any:
         """simple docstring"""



         if self.load_in_abit:
               return "llm_int8"
         elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
               return "fp4"
         elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
               return "nf4"
         else:
               return None


   @classmethod
   def 			lowercase_      (cls     :						List[str]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						List[Any]       ,  **__UpperCAmelCase     :						List[Any]		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  cls(**lowerCAmelCase__		)

         UpperCAmelCase__								=  []
         for key, value in kwargs.items():
               if hasattr(lowerCAmelCase__       ,  lowerCAmelCase__		):
                     setattr(lowerCAmelCase__       ,  lowerCAmelCase__       ,  lowerCAmelCase__		)
                     to_remove.append(lowerCAmelCase__		)
         for key in to_remove:
               kwargs.pop(lowerCAmelCase__       ,  lowerCAmelCase__		)

         if return_unused_kwargs:
               return config, kwargs
         else:
               return config


   def 			lowercase_      (self     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[int]		)						->				int:
         """simple docstring"""



         with open(lowerCAmelCase__       ,  "w"       ,  encoding="utf-8"		) as writer:
               UpperCAmelCase__								=  self.to_dict()
               UpperCAmelCase__								=  json.dumps(lowerCAmelCase__       ,  indent=2       ,  sort_keys=lowerCAmelCase__		) + "\n"

               writer.write(lowerCAmelCase__		)


   def 			lowercase_      (self     :						Optional[int]		)						->				Dict[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  copy.deepcopy(self.__dict__		)
         UpperCAmelCase__								=  str(output["bnb_4bit_compute_dtype"]		).split("."		)[1]

         return output


   def __repr__(self     :						Dict		)						->				List[str]:
         """simple docstring"""



         return f"""{self.__class__.__name__} {self.to_json_string()}"""


   def 			lowercase_      (self     :						Tuple       ,  __UpperCAmelCase     :						List[str] = True		)						->				str:
         """simple docstring"""



         if use_diff is True:
               UpperCAmelCase__								=  self.to_diff_dict()
         else:
               UpperCAmelCase__								=  self.to_dict()
         return json.dumps(lowerCAmelCase__       ,  indent=2       ,  sort_keys=lowerCAmelCase__		) + "\n"





   def 			lowercase_      (self     :						List[str]		)						->				Dict[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.to_dict()

         # get the default config dict
         UpperCAmelCase__								=  BitsAndBytesConfig().to_dict()

         UpperCAmelCase__								=  {}

         # only serialize values that differ from the default config
         for key, value in config_dict.items():
               if value != default_config_dict[key]:
                     UpperCAmelCase__								=  value

         return serializable_config_dict










"""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     __a   (   *lowerCAmelCase__      ,					**lowerCAmelCase__      )	-> str:
										pass






		def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Tuple					) ->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.
a						: Optional[Any]     	=			(
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)


@is_pipeline_test
@require_torch
@require_vision
class        __UpperCamelCase				(						unittest.TestCase						):
							lowerCamelCase   :					Union[str, Any]      					=MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






							@require_pytesseract
							@require_vision
							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Tuple:
								a      :		Tuple	=				pipeline(
								    "document-question-answering"      ,					model=lowerCAmelCase__      ,					tokenizer=lowerCAmelCase__      ,					image_processor=lowerCAmelCase__      )

								a      :		Optional[int]	=				INVOICE_URL
								a      :		str	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )
								a      :		Union[str, Any]	=				"What is the placebo?"
								a      :		Dict	=				[
								    {
								        "image": load_image(lowerCAmelCase__      ),
								        "question": question,
								    },
								    {
								        "image": image,
								        "question": question,
								    },
								    {
								        "image": image,
								        "question": question,
								        "word_boxes": word_boxes,
								    },
								]
								return dqa_pipeline, examples






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Dict:
								a      :		Tuple	=				dqa_pipeline(lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    lowerCAmelCase__      ,					[
								        [
								            {"score": ANY(lowerCAmelCase__      ), "answer": ANY(lowerCAmelCase__      ), "start": ANY(lowerCAmelCase__      ), "end": ANY(lowerCAmelCase__      )},
								            {"score": ANY(lowerCAmelCase__      ), "answer": ANY(lowerCAmelCase__      ), "start": ANY(lowerCAmelCase__      ), "end": ANY(lowerCAmelCase__      )},
								        ]
								    ]
								    * 3      ,					)






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

								a      :		Tuple	=				[
								    {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39},
								    {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
								]
								a      :		Optional[int]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					lowerCAmelCase__      )

								a      :		Optional[int]	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					lowerCAmelCase__      )

								# 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      :		Any	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(lowerCAmelCase__      ,					[]      )

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






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

								a      :		int	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		str	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		Any	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
								        ],
								    ]
								    * 2      ,					)






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

								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								        {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		str	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								        {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		Tuple	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
								            {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
								        ]
								    ]
								    * 2      ,					)






							@slow
							@require_torch
							@require_pytesseract
							@require_vision
							def     __a   (   self      )	-> str:
								a      :		Optional[int]	=				AutoTokenizer.from_pretrained(
								    "impira/layoutlm-document-qa"      ,					revision="3dc6de3"      ,					add_prefix_space=lowerCAmelCase__      )
								a      :		int	=				pipeline(
								    "document-question-answering"      ,					model="impira/layoutlm-document-qa"      ,					tokenizer=lowerCAmelCase__      ,					revision="3dc6de3"      ,					)
								a      :		List[Any]	=				INVOICE_URL
								a      :		Tuple	=				"What is the invoice number?"

								a      :		List[str]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								    ]      ,					)

								a      :		Dict	=				dqa_pipeline({"image": image, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								    ]      ,					)

								a      :		List[Any]	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								        ]
								    ]
								    * 2      ,					)

								a      :		Dict	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )

								# This model should also work if `image` is set to None
								a      :		Optional[Any]	=				dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
								    ]      ,					)






							@slow
							@require_torch
							@require_pytesseract
							@require_vision
							def     __a   (   self      )	-> Tuple:
								a      :		int	=				AutoTokenizer.from_pretrained(
								    "impira/layoutlm-document-qa"      ,					revision="3dc6de3"      ,					add_prefix_space=lowerCAmelCase__      )
								a      :		Tuple	=				pipeline(
								    "document-question-answering"      ,					model="impira/layoutlm-document-qa"      ,					tokenizer=lowerCAmelCase__      ,					revision="3dc6de3"      ,					max_seq_len=50      ,					)
								a      :		List[str]	=				INVOICE_URL
								a      :		Union[str, Any]	=				"What is the invoice number?"

								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)

								a      :		List[str]	=				dqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,					top_k=2      )
								self.assertEqual(
								    nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        [
								            {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								            {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								        ]
								    ]
								    * 2      ,					)

								a      :		List[Any]	=				list(zip(*apply_tesseract(load_image(lowerCAmelCase__      )      ,					lowerCAmelCase__      ,					""      )      )      )

								# 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(lowerCAmelCase__      ,					decimals=4      )      ,					[
								        {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
								        {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
								    ]      ,					)






							@slow
							@require_torch
							def     __a   (   self      )	-> int:
								a      :		Tuple	=				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      :		Optional[Any]	=				INVOICE_URL
								a      :		Tuple	=				"What is the invoice number?"
								a      :		List[Any]	=				dqa_pipeline(image=lowerCAmelCase__      ,					question=lowerCAmelCase__      ,					top_k=2      )
								self.assertEqual(nested_simplify(lowerCAmelCase__      ,					decimals=4      )      ,					[{"answer": "us-001"}]      )






							@require_tf
							@unittest.skip("Document question answering not implemented in TF"      )
							def     __a   (   self      )	-> int:
								pass


