


from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings
@maybe_allow_in_graph
class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Optional[int]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:List[str]=0.0			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:str = "geglu"			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:str = "layer_norm"			,			lowerCAmelCase__			:bool = False			,			)		->			Tuple:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		only_cross_attention

										__SCREAMING_SNAKE_CASE						:     int					=		(num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero'''
										__SCREAMING_SNAKE_CASE						:     List[str]					=		(num_embeds_ada_norm is not None) and norm_type == '''ada_norm'''

										if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
																raise ValueError(
																    f'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to'''
																    f''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.'''	)

										# Define 3 blocks. Each block has its own normalization layer.
										# 1. Self-Attn
										if self.use_ada_layer_norm:
																__SCREAMING_SNAKE_CASE						:     Dict					=		AdaLayerNorm(lowerCAmelCase__			,			lowerCAmelCase__	)
										elif self.use_ada_layer_norm_zero:
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		AdaLayerNormZero(lowerCAmelCase__			,			lowerCAmelCase__	)
										else:
																__SCREAMING_SNAKE_CASE						:     Dict					=		nn.LayerNorm(lowerCAmelCase__			,			elementwise_affine=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		Attention(
										    query_dim=lowerCAmelCase__			,			heads=lowerCAmelCase__			,			dim_head=lowerCAmelCase__			,			dropout=lowerCAmelCase__			,			bias=lowerCAmelCase__			,			cross_attention_dim=cross_attention_dim if only_cross_attention else None			,			upcast_attention=lowerCAmelCase__			,			)

										# 2. Cross-Attn
										if cross_attention_dim is not None or double_self_attention:
																# We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
																# I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
																# the second cross attention block.
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		(
																    AdaLayerNorm(lowerCAmelCase__			,			lowerCAmelCase__	)
																    if self.use_ada_layer_norm
																    else nn.LayerNorm(lowerCAmelCase__			,			elementwise_affine=lowerCAmelCase__	)
																)
																__SCREAMING_SNAKE_CASE						:     Dict					=		Attention(
																    query_dim=lowerCAmelCase__			,			cross_attention_dim=cross_attention_dim if not double_self_attention else None			,			heads=lowerCAmelCase__			,			dim_head=lowerCAmelCase__			,			dropout=lowerCAmelCase__			,			bias=lowerCAmelCase__			,			upcast_attention=lowerCAmelCase__			,			)  # is self-attn if encoder_hidden_states is none
										else:
																__SCREAMING_SNAKE_CASE						:     Dict					=		None
																__SCREAMING_SNAKE_CASE						:     Dict					=		None

										# 3. Feed-forward
										__SCREAMING_SNAKE_CASE						:     Tuple					=		nn.LayerNorm(lowerCAmelCase__			,			elementwise_affine=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		FeedForward(lowerCAmelCase__			,			dropout=lowerCAmelCase__			,			activation_fn=lowerCAmelCase__			,			final_dropout=lowerCAmelCase__	)

										# let chunk size default to None
										__SCREAMING_SNAKE_CASE						:     int					=		None
										__SCREAMING_SNAKE_CASE						:     int					=		0


				def 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:int	)		->			Union[str, Any]:
										# Sets chunk feed-forward
										__SCREAMING_SNAKE_CASE						:     Tuple					=		chunk_size
										__SCREAMING_SNAKE_CASE						:     str					=		dim



				def 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:torch.FloatTensor			,			lowerCAmelCase__			:Optional[torch.FloatTensor] = None			,			lowerCAmelCase__			:Optional[torch.FloatTensor] = None			,			lowerCAmelCase__			:Optional[torch.FloatTensor] = None			,			lowerCAmelCase__			:Optional[torch.LongTensor] = None			,			lowerCAmelCase__			:Dict[str, Any] = None			,			lowerCAmelCase__			:Optional[torch.LongTensor] = None			,			)		->			List[Any]:
										# Notice that normalization is always applied before the real computation in the following blocks.
										# 1. Self-Attention
										if self.use_ada_layer_norm:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.norma(lowerCAmelCase__			,			lowerCAmelCase__	)
										elif self.use_ada_layer_norm_zero:
																__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     List[str]					=		self.norma(
																    lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			hidden_dtype=hidden_states.dtype	)
										else:
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.norma(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     int					=		cross_attention_kwargs if cross_attention_kwargs is not None else {}

										__SCREAMING_SNAKE_CASE						:     Any					=		self.attna(
										    lowerCAmelCase__			,			encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None			,			attention_mask=lowerCAmelCase__			,			**lowerCAmelCase__			,			)
										if self.use_ada_layer_norm_zero:
																__SCREAMING_SNAKE_CASE						:     List[str]					=		gate_msa.unsqueeze(1	) * attn_output
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		attn_output + hidden_states

										# 2. Cross-Attention
										if self.attna is not None:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		(
																    self.norma(lowerCAmelCase__			,			lowerCAmelCase__	) if self.use_ada_layer_norm else self.norma(lowerCAmelCase__	)
																)

																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.attna(
																    lowerCAmelCase__			,			encoder_hidden_states=lowerCAmelCase__			,			attention_mask=lowerCAmelCase__			,			**lowerCAmelCase__			,			)
																__SCREAMING_SNAKE_CASE						:     str					=		attn_output + hidden_states

										# 3. Feed-forward
										__SCREAMING_SNAKE_CASE						:     int					=		self.norma(lowerCAmelCase__	)

										if self.use_ada_layer_norm_zero:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

										if self._chunk_size is not None:
																# "feed_forward_chunk_size" can be used to save memory
																if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
																						raise ValueError(
																						    f'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.'''	)

																__SCREAMING_SNAKE_CASE						:     List[str]					=		norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		torch.cat(
																    [self.ff(lowerCAmelCase__	) for hid_slice in norm_hidden_states.chunk(lowerCAmelCase__			,			dim=self._chunk_dim	)]			,			dim=self._chunk_dim			,			)
										else:
																__SCREAMING_SNAKE_CASE						:     int					=		self.ff(lowerCAmelCase__	)

										if self.use_ada_layer_norm_zero:
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		gate_mlp.unsqueeze(1	) * ff_output

										__SCREAMING_SNAKE_CASE						:     Tuple					=		ff_output + hidden_states

										return hidden_states




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:int = 4			,			lowerCAmelCase__			:float = 0.0			,			lowerCAmelCase__			:str = "geglu"			,			lowerCAmelCase__			:bool = False			,			)		->			Optional[Any]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		int(dim * mult	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		dim_out if dim_out is not None else dim

										if activation_fn == "gelu":
																__SCREAMING_SNAKE_CASE						:     Tuple					=		GELU(lowerCAmelCase__			,			lowerCAmelCase__	)
										if activation_fn == "gelu-approximate":
																__SCREAMING_SNAKE_CASE						:     int					=		GELU(lowerCAmelCase__			,			lowerCAmelCase__			,			approximate='''tanh'''	)
										elif activation_fn == "geglu":
																__SCREAMING_SNAKE_CASE						:     List[str]					=		GEGLU(lowerCAmelCase__			,			lowerCAmelCase__	)
										elif activation_fn == "geglu-approximate":
																__SCREAMING_SNAKE_CASE						:     int					=		ApproximateGELU(lowerCAmelCase__			,			lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		nn.ModuleList([]	)
										# project in
										self.net.append(lowerCAmelCase__	)
										# project dropout
										self.net.append(nn.Dropout(lowerCAmelCase__	)	)
										# project out
										self.net.append(nn.Linear(lowerCAmelCase__			,			lowerCAmelCase__	)	)
										# FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
										if final_dropout:
																self.net.append(nn.Dropout(lowerCAmelCase__	)	)



				def 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:int	)		->			Any:
										for module in self.net:
																__SCREAMING_SNAKE_CASE						:     str					=		module(lowerCAmelCase__	)
										return hidden_states




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:List[Any]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:str = "none"	)		->			Optional[int]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Tuple					=		nn.Linear(lowerCAmelCase__			,			lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		approximate


				def 		__magic_name__(       self			:int			,			lowerCAmelCase__			:Optional[int]	)		->			Tuple:
										if gate.device.type != "mps":
																return F.gelu(lowerCAmelCase__			,			approximate=self.approximate	)
										# mps: gelu is not implemented for float16
										return F.gelu(gate.to(dtype=torch.floataa	)			,			approximate=self.approximate	).to(dtype=gate.dtype	)



				def 		__magic_name__(       self			:int			,			lowerCAmelCase__			:Union[str, Any]	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.proj(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		self.gelu(lowerCAmelCase__	)
										return hidden_states




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Dict			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int	)		->			Optional[Any]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Linear(lowerCAmelCase__			,			dim_out * 2	)


				def 		__magic_name__(       self			:List[Any]			,			lowerCAmelCase__			:List[Any]	)		->			Optional[Any]:
										if gate.device.type != "mps":
																return F.gelu(lowerCAmelCase__	)
										# mps: gelu is not implemented for float16
										return F.gelu(gate.to(dtype=torch.floataa	)	).to(dtype=gate.dtype	)



				def 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:List[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		self.proj(lowerCAmelCase__	).chunk(2			,			dim=-1	)
										return hidden_states * self.gelu(lowerCAmelCase__	)




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:List[str]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int	)		->			Tuple:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Linear(lowerCAmelCase__			,			lowerCAmelCase__	)



				def 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:List[Any]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.proj(lowerCAmelCase__	)
										return x * torch.sigmoid(1.702 * x	)




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Dict	)		->			Any:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Embedding(lowerCAmelCase__			,			lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		nn.SiLU()
										__SCREAMING_SNAKE_CASE						:     Any					=		nn.Linear(lowerCAmelCase__			,			embedding_dim * 2	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		nn.LayerNorm(lowerCAmelCase__			,			elementwise_affine=lowerCAmelCase__	)



				def 		__magic_name__(       self			:Any			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Dict	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		self.linear(self.silu(self.emb(lowerCAmelCase__	)	)	)
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		torch.chunk(lowerCAmelCase__			,			2	)
										__SCREAMING_SNAKE_CASE						:     str					=		self.norm(lowerCAmelCase__	) * (1 + scale) + shift
										return x




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:str	)		->			Dict:
										super().__init__()

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		CombinedTimestepLabelEmbeddings(lowerCAmelCase__			,			lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Any					=		nn.SiLU()
										__SCREAMING_SNAKE_CASE						:     int					=		nn.Linear(lowerCAmelCase__			,			6 * embedding_dim			,			bias=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     int					=		nn.LayerNorm(lowerCAmelCase__			,			elementwise_affine=lowerCAmelCase__			,			eps=1E-6	)



				def 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Optional[Any]=None	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.linear(self.silu(self.emb(lowerCAmelCase__			,			lowerCAmelCase__			,			hidden_dtype=lowerCAmelCase__	)	)	)
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		emb.chunk(6			,			dim=1	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.norm(lowerCAmelCase__	) * (1 + scale_msa[:, None]) + shift_msa[:, None]
										return x, gate_msa, shift_mlp, scale_mlp, gate_mlp




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Optional[str] = None			,			lowerCAmelCase__			:float = 1E-5	)		->			Tuple:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Dict					=		num_groups
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		eps

										if act_fn is None:
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		None
										else:
																__SCREAMING_SNAKE_CASE						:     str					=		get_activation(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		nn.Linear(lowerCAmelCase__			,			out_dim * 2	)



				def 		__magic_name__(       self			:int			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Optional[Any]	)		->			Optional[int]:
										if self.act:
																__SCREAMING_SNAKE_CASE						:     Dict					=		self.act(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		self.linear(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		emb[:, :, None, None]
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		emb.chunk(2			,			dim=1	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		F.group_norm(lowerCAmelCase__			,			self.num_groups			,			eps=self.eps	)
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		x * (1 + scale) + shift
										return x




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        _lowercase      :






										'''simple docstring'''



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




						def        _UpperCamelCase							(      lowercase__      ):
												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	: str  				=(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)
@is_pipeline_test
@require_torch
@require_vision
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Tuple						=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


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

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


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

										__SCREAMING_SNAKE_CASE						:     Optional[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						:     Tuple					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			lowerCAmelCase__	)

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

										# We can optionnally pass directly the words and bounding boxes
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		[]
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		[]
										__SCREAMING_SNAKE_CASE						:     Union[str, 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 		__magic_name__(       self			:int	)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		pipeline(
										    '''document-question-answering'''			,			model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,			revision='''9977165'''			,			)
										__SCREAMING_SNAKE_CASE						:     Dict					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Any					=		'''What is the invoice number?'''

										__SCREAMING_SNAKE_CASE						:     Any					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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						:     Tuple					=		dqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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						:     List[Any]					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     int					=		pipeline(
										    '''document-question-answering'''			,			model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,			revision='''9977165'''			,			max_seq_len=50			,			)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Tuple					=		'''What is the invoice number?'''

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

										__SCREAMING_SNAKE_CASE						:     int					=		dqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        {'''score''': 0.9948, '''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(lowerCAmelCase__			,			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			:int	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''			,			revision='''3dc6de3'''			,			add_prefix_space=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		pipeline(
										    '''document-question-answering'''			,			model='''impira/layoutlm-document-qa'''			,			tokenizer=lowerCAmelCase__			,			revision='''3dc6de3'''			,			)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     str					=		'''What is the invoice number?'''

										__SCREAMING_SNAKE_CASE						:     Dict					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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(lowerCAmelCase__			,			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						:     Optional[int]					=		list(zip(*apply_tesseract(load_image(lowerCAmelCase__	)			,			lowerCAmelCase__			,			''''''	)	)	)

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

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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						:     Optional[int]					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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						:     List[str]					=		list(zip(*apply_tesseract(load_image(lowerCAmelCase__	)			,			lowerCAmelCase__			,			''''''	)	)	)

										# This model should also work if `image` is set to None
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			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			:Union[str, Any]	)		->			Tuple:
										__SCREAMING_SNAKE_CASE						:     str					=		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						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		'''What is the invoice number?'''
										__SCREAMING_SNAKE_CASE						:     Tuple					=		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 		__magic_name__(       self			:Union[str, Any]	)		->			Tuple:
										pass
