



'''simple docstring'''






import argparse
import os
from pathlib import Path

import torch
from bark.generation import _load_model as _bark_load_model
from huggingface_hub import hf_hub_download

from transformers import EncodecConfig, EncodecModel, set_seed
from transformers.models.bark.configuration_bark import (
    BarkCoarseConfig,
    BarkConfig,
    BarkFineConfig,
    BarkSemanticConfig,
)
from transformers.models.bark.generation_configuration_bark import (
    BarkCoarseGenerationConfig,
    BarkFineGenerationConfig,
    BarkGenerationConfig,
    BarkSemanticGenerationConfig,
)
from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel
from transformers.utils import logging


logging.set_verbosity_info()
UpperCamelCase__:  str				      =     logging.get_logger(__name__)

set_seed(770)


UpperCamelCase__:  Union[str, Any]				      =     {
    "c_attn": "att_proj",
    "c_proj": "out_proj",
    "c_fc": "in_proj",
    "transformer.": "",
    "h.": "layers.",
    "ln_1": "layernorm_1",
    "ln_2": "layernorm_2",
    "ln_f": "layernorm_final",
    "wpe": "position_embeds_layer",
    "wte": "input_embeds_layer",
}


UpperCamelCase__:  Dict				      =     {
    "text_small": {
        "repo_id": "suno/bark",
        "file_name": "text.pt",
    },
    "coarse_small": {
        "repo_id": "suno/bark",
        "file_name": "coarse.pt",
    },
    "fine_small": {
        "repo_id": "suno/bark",
        "file_name": "fine.pt",
    },
    "text": {
        "repo_id": "suno/bark",
        "file_name": "text_2.pt",
    },
    "coarse": {
        "repo_id": "suno/bark",
        "file_name": "coarse_2.pt",
    },
    "fine": {
        "repo_id": "suno/bark",
        "file_name": "fine_2.pt",
    },
}

UpperCamelCase__:  List[Any]				      =     os.path.dirname(os.path.abspath(__file__))
UpperCamelCase__:  Optional[int]				      =     os.path.join(os.path.expanduser("~"), ".cache")
UpperCamelCase__:  Optional[int]				      =     os.path.join(os.getenv("XDG_CACHE_HOME", default_cache_dir), "suno", "bark_v0")


def 					snake_case_	(   _lowerCAmelCase    :		List[str]  , _lowerCAmelCase    :		Optional[int]=False    ) ->  str:
							UpperCAmelCase					:			Optional[Any]									=						model_type
							if use_small:
														key += "_small"
							return os.path.join(A__  , REMOTE_MODEL_PATHS[key]['''file_name''']    )


def 					snake_case_	(   _lowerCAmelCase    :		Optional[int]  , _lowerCAmelCase    :		str    ) ->  Union[str, Any]:
							os.makedirs(A__  , exist_ok=A__    )
							hf_hub_download(repo_id=A__  , filename=A__  , local_dir=A__    )


def 					snake_case_	(   _lowerCAmelCase    :		List[Any]  , _lowerCAmelCase    :		Optional[Any]  , _lowerCAmelCase    :		Any=False  , _lowerCAmelCase    :		Union[str, Any]="text"    ) ->  int:
							if model_type == "text":
														UpperCAmelCase					:			Any									=						BarkSemanticModel
														UpperCAmelCase					:			Optional[Any]									=						BarkSemanticConfig
														UpperCAmelCase					:			List[Any]									=						BarkSemanticGenerationConfig
							elif model_type == "coarse":
														UpperCAmelCase					:			List[str]									=						BarkCoarseModel
														UpperCAmelCase					:			List[Any]									=						BarkCoarseConfig
														UpperCAmelCase					:			str									=						BarkCoarseGenerationConfig
							elif model_type == "fine":
														UpperCAmelCase					:			int									=						BarkFineModel
														UpperCAmelCase					:			Optional[Any]									=						BarkFineConfig
														UpperCAmelCase					:			Dict									=						BarkFineGenerationConfig
							else:
														raise NotImplementedError()
							UpperCAmelCase					:			Tuple									=						f"""{model_type}_small""" if use_small else model_type
							UpperCAmelCase					:			int									=						REMOTE_MODEL_PATHS[model_key]
							if not os.path.exists(A__    ):
														logger.info(f"""{model_type} model not found, downloading into `{CACHE_DIR}`."""    )
														_download(model_info['''repo_id''']  , model_info['''file_name''']    )
							UpperCAmelCase					:			Dict									=						torch.load(A__  , map_location=A__    )
							# this is a hack
							UpperCAmelCase					:			Optional[int]									=						checkpoint["model_args"]
							if "input_vocab_size" not in model_args:
														UpperCAmelCase					:			Optional[int]									=						model_args["vocab_size"]
														UpperCAmelCase					:			str									=						model_args["vocab_size"]
														del model_args["vocab_size"]

							# convert Bark model arguments to HF Bark model arguments
							UpperCAmelCase					:			List[str]									=						model_args.pop('''n_head'''    )
							UpperCAmelCase					:			Optional[Any]									=						model_args.pop('''n_embd'''    )
							UpperCAmelCase					:			Union[str, Any]									=						model_args.pop('''n_layer'''    )

							UpperCAmelCase					:			Tuple									=						ConfigClass(**checkpoint['''model_args''']    )
							UpperCAmelCase					:			Optional[Any]									=						ModelClass(config=A__    )
							UpperCAmelCase					:			List[Any]									=						GenerationConfigClass()

							UpperCAmelCase					:			Optional[Any]									=						model_generation_config
							UpperCAmelCase					:			int									=						checkpoint["model"]
							# fixup checkpoint
							UpperCAmelCase					:			List[str]									=						"_orig_mod."
							for k, v in list(state_dict.items()    ):
														if k.startswith(A__    ):
																					# replace part of the key with corresponding layer name in HF implementation
																					UpperCAmelCase					:			Union[str, Any]									=						k[len(A__    ) :]
																					for old_layer_name in new_layer_name_dict:
																												UpperCAmelCase					:			Dict									=						new_k.replace(A__  , new_layer_name_dict[old_layer_name]    )

																					UpperCAmelCase					:			str									=						state_dict.pop(A__    )

							UpperCAmelCase					:			List[str]									=						set(state_dict.keys()    ) - set(model.state_dict().keys()    )
							UpperCAmelCase					:			List[str]									=						{k for k in extra_keys if not k.endswith('''.attn.bias'''    )}
							UpperCAmelCase					:			List[Any]									=						set(model.state_dict().keys()    ) - set(state_dict.keys()    )
							UpperCAmelCase					:			Optional[Any]									=						{k for k in missing_keys if not k.endswith('''.attn.bias'''    )}
							if len(A__    ) != 0:
														raise ValueError(f"""extra keys found: {extra_keys}"""    )
							if len(A__    ) != 0:
														raise ValueError(f"""missing keys: {missing_keys}"""    )
							model.load_state_dict(A__  , strict=A__    )
							UpperCAmelCase					:			Optional[int]									=						model.num_parameters(exclude_embeddings=A__    )
							UpperCAmelCase					:			Optional[Any]									=						checkpoint["best_val_loss"].item()
							logger.info(f"""model loaded: {round(n_params/1e6  , 1    )}M params, {round(A__  , 3    )} loss"""    )
							model.eval()
							model.to(A__    )
							del checkpoint, state_dict

							return model


def 					snake_case_	(   _lowerCAmelCase    :		Dict  , _lowerCAmelCase    :		Optional[int]=False  , _lowerCAmelCase    :		Optional[Any]="text"    ) ->  Dict:
							if model_type not in ("text", "coarse", "fine"):
														raise NotImplementedError()

							UpperCAmelCase					:			Dict									=						"cpu"  # do conversion on cpu

							UpperCAmelCase					:			Union[str, Any]									=						_get_ckpt_path(A__  , use_small=A__    )
							UpperCAmelCase					:			int									=						_load_model(A__  , A__  , model_type=A__  , use_small=A__    )

							# load bark initial model
							UpperCAmelCase					:			Optional[Any]									=						_bark_load_model(A__  , '''cpu'''  , model_type=A__  , use_small=A__    )

							if model_type == "text":
														UpperCAmelCase					:			Any									=						bark_model["model"]

							if model.num_parameters(exclude_embeddings=A__    ) != bark_model.get_num_params():
														raise ValueError('''initial and new models don\'t have the same number of parameters'''    )

							# check if same output as the bark model
							UpperCAmelCase					:			Union[str, Any]									=						5
							UpperCAmelCase					:			str									=						10

							if model_type in ["text", "coarse"]:
														UpperCAmelCase					:			Optional[Any]									=						torch.randint(256  , (batch_size, sequence_length)  , dtype=torch.int    )
														UpperCAmelCase					:			int									=						bark_model(A__    )[0]

														UpperCAmelCase					:			List[str]									=						model(A__    )

														# take last logits
														UpperCAmelCase					:			str									=						output_new_model_total.logits[:, [-1], :]

							else:
														UpperCAmelCase					:			int									=						3
														UpperCAmelCase					:			Tuple									=						8
														UpperCAmelCase					:			str									=						torch.randint(256  , (batch_size, sequence_length, n_codes_total)  , dtype=torch.int    )

														UpperCAmelCase					:			Any									=						model(A__  , A__    )
														UpperCAmelCase					:			Any									=						bark_model(A__  , A__    )

														UpperCAmelCase					:			Union[str, Any]									=						output_new_model_total.logits

							# output difference should come from the difference of self-attention implementation design
							if output_new_model.shape != output_old_model.shape:
														raise ValueError('''initial and new outputs don\'t have the same shape'''    )
							if (output_new_model - output_old_model).abs().max().item() > 1e-3:
														raise ValueError('''initial and new outputs are not equal'''    )

							Path(A__    ).mkdir(exist_ok=A__    )
							model.save_pretrained(A__    )


def 					snake_case_	(   _lowerCAmelCase    :		List[str]  , _lowerCAmelCase    :		Optional[int]  , _lowerCAmelCase    :		Any  , _lowerCAmelCase    :		Tuple  , _lowerCAmelCase    :		List[Any]  , _lowerCAmelCase    :		Optional[int]  , ) ->  Optional[int]:
							UpperCAmelCase					:			Union[str, Any]									=						os.path.join(A__  , A__    )

							UpperCAmelCase					:			int									=						BarkSemanticConfig.from_pretrained(os.path.join(A__  , '''config.json'''    )    )
							UpperCAmelCase					:			List[str]									=						BarkCoarseConfig.from_pretrained(os.path.join(A__  , '''config.json'''    )    )
							UpperCAmelCase					:			Optional[Any]									=						BarkFineConfig.from_pretrained(os.path.join(A__  , '''config.json'''    )    )
							UpperCAmelCase					:			Optional[int]									=						EncodecConfig.from_pretrained('''facebook/encodec_24khz'''    )

							UpperCAmelCase					:			List[Any]									=						BarkSemanticModel.from_pretrained(A__    )
							UpperCAmelCase					:			List[Any]									=						BarkCoarseModel.from_pretrained(A__    )
							UpperCAmelCase					:			List[Any]									=						BarkFineModel.from_pretrained(A__    )
							UpperCAmelCase					:			Optional[int]									=						EncodecModel.from_pretrained('''facebook/encodec_24khz'''    )

							UpperCAmelCase					:			List[str]									=						BarkConfig.from_sub_model_configs(
							    A__  , A__  , A__  , A__    )

							UpperCAmelCase					:			int									=						BarkGenerationConfig.from_sub_model_configs(
							    semantic.generation_config  , coarseAcoustic.generation_config  , fineAcoustic.generation_config    )

							UpperCAmelCase					:			str									=						BarkModel(A__    )

							UpperCAmelCase					:			Union[str, Any]									=						semantic
							UpperCAmelCase					:			int									=						coarseAcoustic
							UpperCAmelCase					:			List[Any]									=						fineAcoustic
							UpperCAmelCase					:			Optional[int]									=						codec

							UpperCAmelCase					:			Union[str, Any]									=						bark_generation_config

							Path(A__    ).mkdir(exist_ok=A__    )
							bark.save_pretrained(A__  , repo_id=A__  , push_to_hub=A__    )


if __name__ == "__main__":
						UpperCamelCase__:  List[str]				      =     argparse.ArgumentParser()
						# Required parameters

						parser.add_argument("model_type", type=str, help="text, coarse or fine.")
						parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
						parser.add_argument("--is_small", action="store_true", help="convert the small version instead of the large.")

						UpperCamelCase__:  Optional[int]				      =     parser.parse_args()

						load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)













"""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 	_SCREAMING_SNAKE_CASE ( *lowerCAmelCase_   :					Any      ,      **lowerCAmelCase_   :					Dict		)		->     str:
							pass





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







@is_pipeline_test
@require_torch
@require_vision
class      UpperCamelCase_			(unittest.TestCase	):
	__magic_name__       =							MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





	@require_pytesseract
	@require_vision
	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					Optional[int]		)		->     Union[str, Any]:
				UpperCAmelCase_	:					int     	=   pipeline(
				    "document-question-answering"      ,      model=lowerCAmelCase_      ,      tokenizer=lowerCAmelCase_      ,      image_processor=lowerCAmelCase_		)

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





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

				UpperCAmelCase_	:					Any     	=   [
				    {"score": 0.0_0_0_1, "answer": "oy 2312/2019", "start": 38, "end": 39},
				    {"score": 0.0_0_0_1, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
				]
				UpperCAmelCase_	:					List[str]     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      lowerCAmelCase_		)

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

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





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

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

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

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





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

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

				UpperCAmelCase_	:					Optional[int]     	=   dqa_pipeline({"image": image, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23},
				        {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				UpperCAmelCase_	:					str     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        [
				            {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23},
				            {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16},
				        ]
				    ]
				    * 2      ,      )





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

				UpperCAmelCase_	:					str     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
				    ]      ,      )

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

				UpperCAmelCase_	:					Union[str, Any]     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        [
				            {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
				            {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
				        ]
				    ]
				    * 2      ,      )

				UpperCAmelCase_	:					Dict     	=   list(zip(*apply_tesseract(load_image(lowerCAmelCase_		)      ,      lowerCAmelCase_      ,      ""		)		)		)

				# This model should also work if `image` is set to None
				UpperCAmelCase_	:					List[str]     	=   dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
				    ]      ,      )





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

				UpperCAmelCase_	:					int     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				UpperCAmelCase_	:					Tuple     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        [
				            {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
				            {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
				        ]
				    ]
				    * 2      ,      )

				UpperCAmelCase_	:					Optional[int]     	=   list(zip(*apply_tesseract(load_image(lowerCAmelCase_		)      ,      lowerCAmelCase_      ,      ""		)		)		)

				# This model should also work if `image` is set to None
				UpperCAmelCase_	:					Dict     	=   dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      [
				        {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )





	@slow
	@require_torch
	def 	_SCREAMING_SNAKE_CASE ( self   :					Union[str, Any]		)		->     List[str]:
				UpperCAmelCase_	:					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"      ,      )

				UpperCAmelCase_	:					Optional[int]     	=   INVOICE_URL
				UpperCAmelCase_	:					int     	=   "What is the invoice number?"
				UpperCAmelCase_	:					List[str]     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     List[str]:
				pass



