
# 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       _snake_case   (	__lowercase							):
 __lowerCAmelCase  :     Optional[int]        =			'naver-clova-ix/donut-base-finetuned-docvqa'
 __lowerCAmelCase  :     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.'
 )
 __lowerCAmelCase  :     int        =			'document_qa'
 __lowerCAmelCase  :     str        =			AutoProcessor
 __lowerCAmelCase  :     List[str]        =			VisionEncoderDecoderModel

 __lowerCAmelCase  :     int        =			['image', 'text']
 __lowerCAmelCase  :     List[str]        =			['text']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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

   super().__init__(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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

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


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


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



 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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

   return sequence["answer"]



# 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.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
     from PIL import Image



class       _snake_case   (	UpperCAmelCase_							):
 __lowerCAmelCase  :     Any        =			'dandelin/vilt-b32-finetuned-vqa'
 __lowerCAmelCase  :     Union[str, Any]        =			(
     'This is a tool that answers a question about an image. It takes an input named `image` which should be the '
     'image containing the information, as well as a `question` which should be the question in English. It '
     'returns a text that is the answer to the question.'
 )
 __lowerCAmelCase  :     str        =			'image_qa'
 __lowerCAmelCase  :     int        =			AutoProcessor
 __lowerCAmelCase  :     Optional[Any]        =			AutoModelForVisualQuestionAnswering

 __lowerCAmelCase  :     str        =			['image', 'text']
 __lowerCAmelCase  :     Optional[int]        =			['text']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""vision"""])
   super().__init__(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return self.pre_processor(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  return_tensors="""pt""")


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   with torch.no_grad():
     return self.model(**SCREAMING_SNAKE_CASE_).logits



 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Dict    		=      outputs.argmax(-1).item()
   return self.model.config.idalabel[idx]






