






# 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 							__UpperCamelCase     (  _lowerCAmelCase ):
	__snake_case						:Dict =							'naver-clova-ix/donut-base-finetuned-docvqa'
	__snake_case						:Dict =							(
	    '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.'
	)
	__snake_case						:List[str] =							'document_qa'
	__snake_case						:List[Any] =							AutoProcessor
	__snake_case						:str =							VisionEncoderDecoderModel

	__snake_case						:List[Any] =							['image', 'text']
	__snake_case						:Optional[Any] =							['text']







	def __init__(	self   :				Any							,					*_lowerCAmelCase   :				Optional[Any]							,					**_lowerCAmelCase   :				Any      )		->			Optional[int]:
		"""simple docstring"""







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

		super().__init__(*_lowerCAmelCase							,					**_lowerCAmelCase      )







	def 			_a (	self   :				Any							,					_lowerCAmelCase   :				"Image"							,					_lowerCAmelCase   :				str      )		->			str:
		"""simple docstring"""







		__lowercase   							=	"""<s_docvqa><s_question>{user_input}</s_question><s_answer>"""
		__lowercase   							=	task_prompt.replace("""{user_input}"""							,					_lowerCAmelCase      )
		__lowercase   							=	self.pre_processor.tokenizer(
		    _lowerCAmelCase							,					add_special_tokens=_lowerCAmelCase							,					return_tensors="""pt"""      ).input_ids
		__lowercase   							=	self.pre_processor(_lowerCAmelCase							,					return_tensors="""pt"""      ).pixel_values

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







	def 			_a (	self   :				List[Any]							,					_lowerCAmelCase   :				Dict      )		->			str:
		"""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=_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   :				Optional[Any]							,					_lowerCAmelCase   :				Optional[Any]      )		->			int:
		"""simple docstring"""







		__lowercase   							=	self.pre_processor.batch_decode(_lowerCAmelCase      )[0]
		__lowercase   							=	sequence.replace(self.pre_processor.tokenizer.eos_token							,					""""""      )
		__lowercase   							=	sequence.replace(self.pre_processor.tokenizer.pad_token							,					""""""      )
		__lowercase   							=	re.sub(r"""<.*?>"""							,					""""""							,					_lowerCAmelCase							,					count=1      ).strip()  # remove first task start token
		__lowercase   							=	self.pre_processor.tokenajson(_lowerCAmelCase      )

		return sequence["answer"]









'''simple docstring'''
import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


UpperCamelCase__			=      False
try:
 UpperCamelCase__			=      _is_package_available('google.colab')
except ModuleNotFoundError:
 pass


@input.register
class 							_UpperCAmelCase    :




    def __init__(      self	:							List[Any]	,  a	:							str = None	,  a	:							list = []      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  0
       lowercase_						:     Dict							     =  choices
       lowercase_						:     List[str]							     =  prompt
       if sys.platform == "win32":
          lowercase_						:     List[Any]							     =  "*"
       else:
          lowercase_						:     Union[str, Any]							     =  "➔ "




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							Optional[int]	,  a	:							str = ""      ):






       '''simple docstring'''






       if sys.platform != "win32":
          writeColor(self.choices[index]	,  3_2	,  a      )
       else:
          forceWrite(self.choices[index]	,  a      )




    def 					lowerCAmelCase__					(      self	:							List[Any]	,  a	:							int      ):






       '''simple docstring'''






       if index == self.position:
          forceWrite(f""" {self.arrow_char} """      )
          self.write_choice(a      )
       else:
          forceWrite(f"""    {self.choices[index]}"""      )
       reset_cursor()




    def 					lowerCAmelCase__					(      self	:							str	,  a	:							Direction	,  a	:							int = 1      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.position
       if direction == Direction.DOWN:
          if self.position + 1 >= len(self.choices      ):
             return
          self.position += num_spaces
       else:
          if self.position - 1 < 0:
             return
          self.position -= num_spaces
       clear_line()
       self.print_choice(a      )
       move_cursor(a	,  direction.name      )
       self.print_choice(self.position      )




    @input.mark(KEYMAP["up"]      )
    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       self.move_direction(Direction.UP      )




    @input.mark(KEYMAP["down"]      )
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       self.move_direction(Direction.DOWN      )




    @input.mark(KEYMAP["newline"]      )
    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       move_cursor(len(self.choices      ) - self.position	,  "DOWN"      )
       return self.position




    @input.mark(KEYMAP["interrupt"]      )
    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       move_cursor(len(self.choices      ) - self.position	,  "DOWN"      )
       raise KeyboardInterrupt




    @input.mark_multiple(*[KEYMAP[str(a      )] for number in range(1_0      )]      )
    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     str							     =  int(chr(self.current_selection      )      )
       lowercase_						:     Tuple							     =  index - self.position
       if index == self.position:
          return
       if index < len(self.choices      ):
          if self.position > index:
             self.move_direction(Direction.UP	,  -movement      )
          elif self.position < index:
             self.move_direction(Direction.DOWN	,  a      )
          else:
             return
       else:
          return





    def 					lowerCAmelCase__					(      self	:							Tuple	,  a	:							int = 0      ):






       '''simple docstring'''






       if self.prompt:
          linebreak()
          forceWrite(self.prompt	,  "\n"      )
          if in_colab:
             forceWrite("Please input a choice index (starting from 0), and press enter"	,  "\n"      )
          else:
             forceWrite("Please select a choice using the arrow or number keys, and selecting with enter"	,  "\n"      )
       lowercase_						:     Dict							     =  default_choice
       for i in range(len(self.choices      )      ):
          self.print_choice(a      )
          forceWrite("\n"      )
       move_cursor(len(self.choices      ) - self.position	,  "UP"      )
       with cursor.hide():
          while True:
             if in_colab:
                try:
                   lowercase_						:     List[str]							     =  int(builtins.input()      )
                except ValueError:
                   lowercase_						:     List[Any]							     =  default_choice
             else:
                lowercase_						:     Dict							     =  self.handle_input()
             if choice is not None:
                reset_cursor()
                for _ in range(len(self.choices      ) + 1      ):
                   move_cursor(1	,  "UP"      )
                   clear_line()
                self.write_choice(a	,  "\n"      )
                return choice




