

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				:
						'''simple docstring'''







						@staticmethod
						def 				__UpperCamelCase			(      *a				:			Optional[Any]   ,   **a				:			List[str]						)				->							str:

							"""simple docstring"""

							pass

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






@is_pipeline_test
@require_torch
@require_vision
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			lowerCamelCase__							     =MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
			@require_pytesseract
			@require_vision
			def 				__UpperCamelCase			(      self				:			Optional[Any]   ,   a				:			Union[str, Any]   ,   a				:			Any   ,   a				:			Optional[int]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							pipeline(
				    "document-question-answering"   ,   model=__snake_case   ,   tokenizer=__snake_case   ,   image_processor=__snake_case						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							INVOICE_URL
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							list(zip(*apply_tesseract(load_image(__snake_case						)   ,   __snake_case   ,   ""						)						)						)
				SCREAMING_SNAKE_CASE :   Any					  =							"What is the placebo?"
				SCREAMING_SNAKE_CASE :   List[Any]					  =							[
				    {
				        "image": load_image(__snake_case						),
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				        "word_boxes": word_boxes,
				    },
				]
				return dqa_pipeline, examples
			def 				__UpperCamelCase			(      self				:			Optional[int]   ,   a				:			Dict   ,   a				:			Optional[int]						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							dqa_pipeline(__snake_case   ,   top_k=2						)
				self.assertEqual(
				    __snake_case   ,   [
				        [
				            {"score": ANY(__snake_case						), "answer": ANY(__snake_case						), "start": ANY(__snake_case						), "end": ANY(__snake_case						)},
				            {"score": ANY(__snake_case						), "answer": ANY(__snake_case						), "start": ANY(__snake_case						), "end": ANY(__snake_case						)},
				        ]
				    ]
				    * 3   ,   )
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 				__UpperCamelCase			(      self				:			Optional[int]						)				->							Optional[Any]:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							[
				    {"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 :   List[Any]					  =							dqa_pipeline(image=__snake_case   ,   question=__snake_case   ,   top_k=2						)
				self.assertEqual(nested_simplify(__snake_case   ,   decimals=4						)   ,   __snake_case						)

				SCREAMING_SNAKE_CASE :   List[Any]					  =							dqa_pipeline({"image": image, "question": question}   ,   top_k=2						)
				self.assertEqual(nested_simplify(__snake_case   ,   decimals=4						)   ,   __snake_case						)

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

				# We can optionnally pass directly the words and bounding boxes
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							"./tests/fixtures/tests_samples/COCO/000000039769.png"
				SCREAMING_SNAKE_CASE :   str					  =							[]
				SCREAMING_SNAKE_CASE :   str					  =							[]
				SCREAMING_SNAKE_CASE :   Any					  =							dqa_pipeline(image=__snake_case   ,   question=__snake_case   ,   words=__snake_case   ,   boxes=__snake_case   ,   top_k=2						)
				self.assertEqual(__snake_case   ,   []						)
			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Dict:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE :   Dict					  =							dqa_pipeline(image=__snake_case   ,   question=__snake_case   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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}   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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}, {"image": image, "question": question}]   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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 				__UpperCamelCase			(      self				:			Dict						)				->							List[Any]:

				"""simple docstring"""

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

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

				SCREAMING_SNAKE_CASE :   List[Any]					  =							dqa_pipeline({"image": image, "question": question}   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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(__snake_case   ,   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 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							Tuple:

				"""simple docstring"""

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

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

				# This model should also work if `image` is set to None
				SCREAMING_SNAKE_CASE :   Tuple					  =							dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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 				__UpperCamelCase			(      self				:			List[Any]						)				->							Union[str, Any]:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							dqa_pipeline(image=__snake_case   ,   question=__snake_case   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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(__snake_case   ,   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 :   Optional[Any]					  =							list(zip(*apply_tesseract(load_image(__snake_case						)   ,   __snake_case   ,   ""						)						)						)

				# This model should also work if `image` is set to None
				SCREAMING_SNAKE_CASE :   Any					  =							dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}   ,   top_k=2						)
				self.assertEqual(
				    nested_simplify(__snake_case   ,   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 				__UpperCamelCase			(      self				:			int						)				->							Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[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"   ,   )

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






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

				"""simple docstring"""

				pass






'''simple docstring'''




from __future__ import annotations

_SCREAMING_SNAKE_CASE							     =			1.6021E-19  # units = C





def 	__lowerCamelCase			(      __lowerCAmelCase       :							float      ,    __lowerCAmelCase       :							float      ,    __lowerCAmelCase       :							float      ,    )		->  tuple[str, float]:
   if (conductivity, electron_conc, mobility).count(0		) != 1:
      raise ValueError("""You cannot supply more or less than 2 values"""		)
   elif conductivity < 0:
      raise ValueError("""Conductivity cannot be negative"""		)
   elif electron_conc < 0:
      raise ValueError("""Electron concentration cannot be negative"""		)
   elif mobility < 0:
      raise ValueError("""mobility cannot be negative"""		)
   elif conductivity == 0:
      return (
          "conductivity",
          mobility * electron_conc * ELECTRON_CHARGE,
      )
   elif electron_conc == 0:
      return (
          "electron_conc",
          conductivity / (mobility * ELECTRON_CHARGE),
      )
   else:
      return (
          "mobility",
          conductivity / (electron_conc * ELECTRON_CHARGE),
      )


if __name__ == "__main__":
 import doctest

 doctest.testmod()

