
"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase_       :						Any     					=	logging.get_logger(__name__)

lowercase_       :						Any     					=	{
    '''google/switch-base-8''': '''https://huggingface.co/google/switch-base-8/blob/main/config.json''',
}
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE  ):
     A__      =			"""switch_transformers"""
     A__      =			["""past_key_values"""]
     A__      =			{"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""}
     def __init__(     self  ,     snake_case__=32128  ,     snake_case__=768  ,     snake_case__=64  ,     snake_case__=2048  ,     snake_case__=64  ,     snake_case__=12  ,     snake_case__=3  ,     snake_case__=12  ,     snake_case__=3  ,     snake_case__=12  ,     snake_case__=8  ,     snake_case__=False  ,     snake_case__=0.01  ,     snake_case__="float32"  ,     snake_case__=False  ,     snake_case__=32  ,     snake_case__=128  ,     snake_case__=0.1  ,     snake_case__=1E-6  ,     snake_case__=0.001  ,     snake_case__=0.001  ,     snake_case__=1.0  ,     snake_case__="relu"  ,     snake_case__=True  ,     snake_case__=False  ,     snake_case__=True  ,     snake_case__=0  ,     snake_case__=1  ,     **snake_case__  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				Tuple							    = d_model
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = d_kv
       _SCREAMING_SNAKE_CASE   :				Tuple							    = d_ff

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = num_sparse_encoder_layers

       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = num_layers
       _SCREAMING_SNAKE_CASE   :				Tuple							    = (
           num_decoder_layers if num_decoder_layers is not None else self.num_layers
       )  # default = symmetry
       _SCREAMING_SNAKE_CASE   :				Tuple							    = num_sparse_decoder_layers

       # This tells us, each how many encoder layer we'll have to set a sparse layer.
       if self.num_sparse_encoder_layers > 0:
         _SCREAMING_SNAKE_CASE   :				Any							    = self.num_layers // self.num_sparse_encoder_layers
       else:
         _SCREAMING_SNAKE_CASE   :				Dict							    = self.num_layers  # HACK: this will create 0 sparse layers

       # This tells us, each how many encoder layer we'll have to set a sparse layer.
       if self.num_sparse_decoder_layers > 0:
         _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = self.num_decoder_layers // self.num_sparse_decoder_layers
       else:
         _SCREAMING_SNAKE_CASE   :				str							    = self.num_decoder_layers  # HACK: this will create 0 sparse layers

       _SCREAMING_SNAKE_CASE   :				Any							    = num_heads
       _SCREAMING_SNAKE_CASE   :				int							    = num_experts
       _SCREAMING_SNAKE_CASE   :				int							    = expert_capacity
       _SCREAMING_SNAKE_CASE   :				int							    = router_bias
       _SCREAMING_SNAKE_CASE   :				Tuple							    = router_jitter_noise
       if router_dtype not in ["float32", "float16", "bfloat16"]:
         raise ValueError(F'''`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}'''				)
       _SCREAMING_SNAKE_CASE   :				str							    = router_dtype

       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = router_ignore_padding_tokens
       _SCREAMING_SNAKE_CASE   :				str							    = relative_attention_num_buckets
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = relative_attention_max_distance

       _SCREAMING_SNAKE_CASE   :				Any							    = dropout_rate
       _SCREAMING_SNAKE_CASE   :				Dict							    = layer_norm_epsilon
       _SCREAMING_SNAKE_CASE   :				Dict							    = initializer_factor
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = feed_forward_proj
       _SCREAMING_SNAKE_CASE   :				List[str]							    = use_cache
       _SCREAMING_SNAKE_CASE   :				List[str]							    = add_router_probs

       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = router_z_loss_coef
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = router_aux_loss_coef

       _SCREAMING_SNAKE_CASE   :				str							    = self.feed_forward_proj.split("-"				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = act_info[-1]
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = act_info[0] == "gated"

       if len(snake_case__				) > 1 and act_info[0] != "gated" or len(snake_case__				) > 2:
         raise ValueError(
             F'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'''
             "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
             "'gated-gelu' or 'relu'"				)

       # for backwards compatibility
       if feed_forward_proj == "gated-gelu":
         _SCREAMING_SNAKE_CASE   :				Any							    = "gelu_new"

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





"""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"]



