


"""simple docstring"""



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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 _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: int													=						'''naver-clova-ix/donut-base-finetuned-docvqa'''
   __UpperCAmelCase				: Any													=						(
       '''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.'''
   )
   __UpperCAmelCase				: Union[str, Any]													=						'''document_qa'''
   __UpperCAmelCase				: Any													=						AutoProcessor
   __UpperCAmelCase				: int													=						VisionEncoderDecoderModel

   __UpperCAmelCase				: Optional[int]													=						['''image''', '''text''']
   __UpperCAmelCase				: Optional[int]													=						['''text''']







   def __init__(			self		,			*UpperCamelCase__		,			**UpperCamelCase__		)			->	List[Any]:

       '''simple docstring'''




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

       super().__init__(*UpperCamelCase__		,			**UpperCamelCase__		)







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__		)			->	Optional[Any]:

       '''simple docstring'''




       snake_case					:   Tuple         =					"<s_docvqa><s_question>{user_input}</s_question><s_answer>"
       snake_case					:   str         =					task_prompt.replace("{user_input}"		,			UpperCamelCase__		)
       snake_case					:   Union[str, Any]         =					self.pre_processor.tokenizer(
           UpperCamelCase__		,			add_special_tokens=UpperCamelCase__		,			return_tensors="pt"		).input_ids
       snake_case					:   List[str]         =					self.pre_processor(UpperCamelCase__		,			return_tensors="pt"		).pixel_values

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







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		)			->	Optional[int]:

       '''simple docstring'''




       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=UpperCamelCase__		,			pad_token_id=self.pre_processor.tokenizer.pad_token_id		,			eos_token_id=self.pre_processor.tokenizer.eos_token_id		,			use_cache=UpperCamelCase__		,			num_beams=1		,			bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]		,			return_dict_in_generate=UpperCamelCase__		,			).sequences







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		)			->	List[Any]:

       '''simple docstring'''




       snake_case					:   Dict         =					self.pre_processor.batch_decode(UpperCamelCase__		)[0]
       snake_case					:   Dict         =					sequence.replace(self.pre_processor.tokenizer.eos_token		,			""		)
       snake_case					:   Dict         =					sequence.replace(self.pre_processor.tokenizer.pad_token		,			""		)
       snake_case					:   str         =					re.sub(r"<.*?>"		,			""		,			UpperCamelCase__		,			count=1		).strip()  # remove first task start token
       snake_case					:   Union[str, Any]         =					self.pre_processor.tokenajson(UpperCamelCase__		)

       return sequence["answer"]









"""simple docstring"""



import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class _lowerCAmelCase       (   snake_case_					,							unittest.TestCase			):
   __UpperCAmelCase				: List[Any]													=						LayoutLMTokenizer
   __UpperCAmelCase				: List[Any]													=						LayoutLMTokenizerFast
   __UpperCAmelCase				: List[Any]													=						True
   __UpperCAmelCase				: Dict													=						True







   def 				lowerCamelCase	(			self		)			->	str:

       '''simple docstring'''




       super().setUp()

       snake_case					:   Dict         =					[
           "[UNK]",
           "[CLS]",
           "[SEP]",
           "want",
           "##want",
           "##ed",
           "wa",
           "un",
           "runn",
           "##ing",
           ",",
           "low",
           "lowest",
       ]
       snake_case					:   List[str]         =					os.path.join(self.tmpdirname		,			VOCAB_FILES_NAMES["vocab_file"]		)
       with open(self.vocab_file		,			"w"		,			encoding="utf-8"		) as vocab_writer:
           vocab_writer.write("".join([x + "\n" for x in vocab_tokens]		)		)







   def 				lowerCamelCase	(			self		,			**UpperCamelCase__		)			->	Dict:

       '''simple docstring'''




       return LayoutLMTokenizer.from_pretrained(self.tmpdirname		,			**UpperCamelCase__		)







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   str         =					"UNwant\u00E9d,running"
       snake_case					:   Optional[int]         =					"unwanted, running"
       return input_text, output_text







   def 				lowerCamelCase	(			self		)			->	Any:

       '''simple docstring'''




       snake_case					:   List[str]         =					self.tokenizer_class(self.vocab_file		)

       snake_case					:   Optional[Any]         =					tokenizer.tokenize("UNwant\u00E9d,running"		)
       self.assertListEqual(UpperCamelCase__		,			["un", "##want", "##ed", ",", "runn", "##ing"]		)
       self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__		)		,			[7, 4, 5, 10, 8, 9]		)







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




       pass





