



'''simple docstring'''






import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
						from PIL import Image

class 				SCREAMING_SNAKE_CASE(							UpperCAmelCase__			):







	"""simple docstring"""






	lowerCamelCase__			 =       "naver-clova-ix/donut-base-finetuned-docvqa"
	lowerCamelCase__			 =       (
	    "This is a tool that answers a question about an document (pdf). It takes an input named `document` which "
	    "should be the document containing the information, as well as a `question` that is the question about the "
	    "document. It returns a text that contains the answer to the question."
	)
	lowerCamelCase__			 =       "document_qa"
	lowerCamelCase__			 =       AutoProcessor
	lowerCamelCase__			 =       VisionEncoderDecoderModel

	lowerCamelCase__			 =       ["image", "text"]
	lowerCamelCase__			 =       ["text"]







	def __init__(						self			:     Union[str, Any]		,			*__snake_case			:     List[Any]		,			**__snake_case			:     Optional[Any]		)			->		List[Any]:







								if not is_vision_available():
															raise ValueError('''Pillow must be installed to use the DocumentQuestionAnsweringTool.'''		)

								super().__init__(*lowerCamelCase__		,			**lowerCamelCase__		)







	def      A	(						self			:     Any		,			__snake_case			:     "Image"		,			__snake_case			:     str		)			->		str:
								UpperCAmelCase :       List[Any] 							=		"<s_docvqa><s_question>{user_input}</s_question><s_answer>"
								UpperCAmelCase :       int 							=		task_prompt.replace('''{user_input}'''		,			lowerCamelCase__		)
								UpperCAmelCase :       List[Any] 							=		self.pre_processor.tokenizer(
								    lowerCamelCase__		,			add_special_tokens=lowerCamelCase__		,			return_tensors='''pt'''		).input_ids
								UpperCAmelCase :       int 							=		self.pre_processor(lowerCamelCase__		,			return_tensors='''pt'''		).pixel_values

								return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}







	def      A	(						self			:     Dict		,			__snake_case			:     Tuple		)			->		Union[str, Any]:
								return self.model.generate(
								    inputs['''pixel_values'''].to(self.device		)		,			decoder_input_ids=inputs['''decoder_input_ids'''].to(self.device		)		,			max_length=self.model.decoder.config.max_position_embeddings		,			early_stopping=lowerCamelCase__		,			pad_token_id=self.pre_processor.tokenizer.pad_token_id		,			eos_token_id=self.pre_processor.tokenizer.eos_token_id		,			use_cache=lowerCamelCase__		,			num_beams=1		,			bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]		,			return_dict_in_generate=lowerCamelCase__		,			).sequences







	def      A	(						self			:     Tuple		,			__snake_case			:     Optional[int]		)			->		int:
								UpperCAmelCase :       Optional[int] 							=		self.pre_processor.batch_decode(lowerCamelCase__		)[0]
								UpperCAmelCase :       List[Any] 							=		sequence.replace(self.pre_processor.tokenizer.eos_token		,			''''''		)
								UpperCAmelCase :       Dict 							=		sequence.replace(self.pre_processor.tokenizer.pad_token		,			''''''		)
								UpperCAmelCase :       Any 							=		re.sub(r'''<.*?>'''		,			''''''		,			lowerCamelCase__		,			count=1		).strip()  # remove first task start token
								UpperCAmelCase :       List[str] 							=		self.pre_processor.tokenajson(lowerCamelCase__		)

								return sequence["answer"]









'''simple docstring'''


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
						from .tokenization_fnet import FNetTokenizer
else:
						lowerCamelCase__					=   None

lowerCamelCase__					=   logging.get_logger(__name__)
lowerCamelCase__					=   {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

lowerCamelCase__					=   {
    'vocab_file': {
        'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model',
        'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json',
        'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json',
    },
}

lowerCamelCase__					=   {
    'google/fnet-base': 512,
    'google/fnet-large': 512,
}

lowerCamelCase__					=   '▁'






class 		lowerCAmelCase__	(	UpperCAmelCase__		):
			lowerCAmelCase       :		Dict				      =   VOCAB_FILES_NAMES
			lowerCAmelCase       :		Optional[Any]				      =   PRETRAINED_VOCAB_FILES_MAP
			lowerCAmelCase       :		List[str]				      =   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			lowerCAmelCase       :		Optional[int]				      =   ["input_ids", "token_type_ids"]
			lowerCAmelCase       :		Optional[Any]				      =   FNetTokenizer




			def __init__(						self     :   Dict		,     lowerCamelCase__     :   int=None		,     lowerCamelCase__     :   List[Any]=None		,     lowerCamelCase__     :   Any=False		,     lowerCamelCase__     :   List[str]=True		,     lowerCamelCase__     :   Dict=True		,     lowerCamelCase__     :   str="<unk>"		,     lowerCamelCase__     :   List[str]="[SEP]"		,     lowerCamelCase__     :   Union[str, Any]="<pad>"		,     lowerCamelCase__     :   Optional[Any]="[CLS]"		,     lowerCamelCase__     :   Any="[MASK]"		,     **lowerCamelCase__     :   Any		,     )      ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				Dict	    =					(
					    AddedToken(lowerCamelCase__		,     lstrip=lowerCamelCase__		,     rstrip=lowerCamelCase__		,     normalized=lowerCamelCase__       )
					    if isinstance(lowerCamelCase__		,     lowerCamelCase__       )
					    else mask_token
					)

					super().__init__(
					    lowerCamelCase__		,     tokenizer_file=lowerCamelCase__		,     do_lower_case=lowerCamelCase__		,     remove_space=lowerCamelCase__		,     keep_accents=lowerCamelCase__		,     unk_token=lowerCamelCase__		,     sep_token=lowerCamelCase__		,     pad_token=lowerCamelCase__		,     cls_token=lowerCamelCase__		,     mask_token=lowerCamelCase__		,     **lowerCamelCase__		,     )

					_UpperCAmelCase						:				Optional[Any]	    =					do_lower_case
					_UpperCAmelCase						:				Tuple	    =					remove_space
					_UpperCAmelCase						:				List[Any]	    =					keep_accents
					_UpperCAmelCase						:				Tuple	    =					vocab_file
					_UpperCAmelCase						:				str	    =					False if not self.vocab_file else True




			def 							lowerCAmelCase__			(						self     :   str		,     lowerCamelCase__     :   List[int]		,     lowerCamelCase__     :   Optional[List[int]] = None       )      ->List[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					[self.sep_token_id]
					_UpperCAmelCase						:				Tuple	    =					[self.cls_token_id]
					if token_ids_a is None:
							return cls + token_ids_a + sep
					return cls + token_ids_a + sep + token_ids_a + sep




			def 							lowerCAmelCase__			(						self     :   int		,     lowerCamelCase__     :   List[int]		,     lowerCamelCase__     :   Optional[List[int]] = None       )      ->List[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[int]	    =					[self.sep_token_id]
					_UpperCAmelCase						:				Optional[Any]	    =					[self.cls_token_id]

					if token_ids_a is None:
							return len(cls + token_ids_a + sep       ) * [0]
					return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]






			def 							lowerCAmelCase__			(						self     :   Dict		,     lowerCamelCase__     :   str		,     lowerCamelCase__     :   Optional[str] = None       )      ->Tuple[str]:



					'''simple docstring'''







					if not os.path.isdir(lowerCamelCase__       ):
							logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""       )
							return
					_UpperCAmelCase						:				Union[str, Any]	    =					os.path.join(
					    lowerCamelCase__		,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]       )

					if os.path.abspath(self.vocab_file       ) != os.path.abspath(lowerCamelCase__       ):
							copyfile(self.vocab_file		,     lowerCamelCase__       )

					return (out_vocab_file,)



