
"""simple docstring"""
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase_       :						Union[str, Any]     					=	logging.get_logger(__name__)

lowercase_       :						Tuple     					=	{
    '''microsoft/git-base''': '''https://huggingface.co/microsoft/git-base/resolve/main/config.json''',
}
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE  ):
     A__      =			"""git_vision_model"""
     def __init__(     self  ,     snake_case__=768  ,     snake_case__=3072  ,     snake_case__=12  ,     snake_case__=12  ,     snake_case__=3  ,     snake_case__=224  ,     snake_case__=16  ,     snake_case__="quick_gelu"  ,     snake_case__=1E-5  ,     snake_case__=0.0  ,     snake_case__=0.02  ,     **snake_case__  ,     ):




       """simple docstring"""


       super().__init__(**snake_case__				)

       _SCREAMING_SNAKE_CASE   :				str							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				Any							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				Any							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				Tuple							    = num_channels
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = patch_size
       _SCREAMING_SNAKE_CASE   :				int							    = image_size
       _SCREAMING_SNAKE_CASE   :				str							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				int							    = attention_dropout
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = layer_norm_eps
       _SCREAMING_SNAKE_CASE   :				List[str]							    = hidden_act




     @classmethod
     def 				__SCREAMING_SNAKE_CASE							(     cls  ,     snake_case__  ,     **snake_case__				):




       """simple docstring"""


       cls._set_token_in_kwargs(snake_case__				)

       _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE   :				str							    = cls.get_config_dict(snake_case__  ,     **snake_case__				)

       # get the vision config dict if we are loading from GITConfig
       if config_dict.get("model_type"				) == "git":
         _SCREAMING_SNAKE_CASE   :				Any							    = config_dict["vision_config"]

       if "model_type" in config_dict and hasattr(cls  ,     "model_type"				) and config_dict["model_type"] != cls.model_type:
         logger.warning(
             F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
             F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''				)

       return cls.from_dict(snake_case__  ,     **snake_case__				)
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE  ):
     A__      =			"""git"""
     def __init__(     self  ,     snake_case__=None  ,     snake_case__=30522  ,     snake_case__=768  ,     snake_case__=6  ,     snake_case__=12  ,     snake_case__=3072  ,     snake_case__="gelu"  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=1024  ,     snake_case__=0.02  ,     snake_case__=1E-12  ,     snake_case__=0  ,     snake_case__="absolute"  ,     snake_case__=True  ,     snake_case__=False  ,     snake_case__=101  ,     snake_case__=102  ,     snake_case__=None  ,     **snake_case__  ,     ):




       """simple docstring"""


       super().__init__(bos_token_id=snake_case__  ,     eos_token_id=snake_case__  ,     pad_token_id=snake_case__  ,     **snake_case__				)

       if vision_config is None:
         _SCREAMING_SNAKE_CASE   :				List[str]							    = {}
         logger.info("vision_config is None. initializing the GitVisionConfig with default values."				)

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = GitVisionConfig(**snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				List[str]							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				Any							    = hidden_act
       _SCREAMING_SNAKE_CASE   :				Tuple							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = hidden_dropout_prob
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = max_position_embeddings
       _SCREAMING_SNAKE_CASE   :				Any							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = layer_norm_eps
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = position_embedding_type
       _SCREAMING_SNAKE_CASE   :				int							    = use_cache
       _SCREAMING_SNAKE_CASE   :				str							    = tie_word_embeddings
       _SCREAMING_SNAKE_CASE   :				List[str]							    = num_image_with_embedding

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = bos_token_id
       _SCREAMING_SNAKE_CASE   :				int							    = eos_token_id




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = copy.deepcopy(self.__dict__				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.vision_config.to_dict()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = self.__class__.model_type
       return output





"""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 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE  ):
     A__      =			"""naver-clova-ix/donut-base-finetuned-docvqa"""
     A__      =			(
         """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."""
     )
     A__      =			"""document_qa"""
     A__      =			AutoProcessor
     A__      =			VisionEncoderDecoderModel

     A__      =			["""image""", """text"""]
     A__      =			["""text"""]
     def __init__(     self  ,     *snake_case__  ,     **snake_case__				):




       """simple docstring"""


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

       super().__init__(*snake_case__  ,     **snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


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

       return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     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=snake_case__  ,     pad_token_id=self.pre_processor.tokenizer.pad_token_id  ,     eos_token_id=self.pre_processor.tokenizer.eos_token_id  ,     use_cache=snake_case__  ,     num_beams=1  ,     bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]  ,     return_dict_in_generate=snake_case__  ,     ).sequences




     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__				):




       """simple docstring"""


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

       return sequence["answer"]



