



"""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    (	__UpperCamelCase  ):
   A__		:	Union[str, Any]           =  '''naver-clova-ix/donut-base-finetuned-docvqa'''
   A__		:	Optional[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.'''
   )
   A__		:	Any           =  '''document_qa'''
   A__		:	List[str]           =  AutoProcessor
   A__		:	str           =  VisionEncoderDecoderModel

   A__		:	Union[str, Any]           =  ['''image''', '''text''']
   A__		:	int           =  ['''text''']



   def __init__(       self    :	List[Any]							,       *__lowerCamelCase    :	Optional[int]							,       **__lowerCamelCase    :	Dict				):



     """simple docstring"""



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

     super().__init__(*__UpperCAmelCase							,       **__UpperCAmelCase				)



   def    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	"Image"							,       __lowerCamelCase    :	str				):



     """simple docstring"""
     _snake_case   	=      "<s_docvqa><s_question>{user_input}</s_question><s_answer>"
     _snake_case   	=      task_prompt.replace('''{user_input}'''							,       __UpperCAmelCase				)
     _snake_case   	=      self.pre_processor.tokenizer(
         __UpperCAmelCase							,       add_special_tokens=__UpperCAmelCase							,       return_tensors='''pt'''				).input_ids
     _snake_case   	=      self.pre_processor(__UpperCAmelCase							,       return_tensors='''pt'''				).pixel_values

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



   def    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	Dict				):



     """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    __UpperCAmelCase    (       self    :	int							,       __lowerCamelCase    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      self.pre_processor.batch_decode(__UpperCAmelCase				)[0]
     _snake_case   	=      sequence.replace(self.pre_processor.tokenizer.eos_token							,       ''''''				)
     _snake_case   	=      sequence.replace(self.pre_processor.tokenizer.pad_token							,       ''''''				)
     _snake_case   	=      re.sub(R'''<.*?>'''							,       ''''''							,       __UpperCAmelCase							,       count=1				).strip()  # remove first task start token
     _snake_case   	=      self.pre_processor.tokenajson(__UpperCAmelCase				)

     return sequence["answer"]













import copy
from typing import Dict, List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase__      			=						{
    '''facebook/mask2former-swin-small-coco-instance''': (
        '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json'''
    )
    # See all Mask2Former models at https://huggingface.co/models?filter=mask2former
}

UpperCAmelCase__      			=						logging.get_logger(__name__)




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """mask2former"""
						snake_case__        =      ["""swin"""]
						snake_case__        =      {"""hidden_size""": """hidden_dim"""}



						def __init__(self:					Any ,					__UpperCAmelCase:					Optional[Dict] = None ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 1024 ,					__UpperCAmelCase:					str = "relu" ,					__UpperCAmelCase:					int = 6 ,					__UpperCAmelCase:					int = 10 ,					__UpperCAmelCase:					int = 8 ,					__UpperCAmelCase:					float = 0.0 ,					__UpperCAmelCase:					int = 2048 ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					int = 4 ,					__UpperCAmelCase:					int = 255 ,					__UpperCAmelCase:					int = 100 ,					__UpperCAmelCase:					float = 0.1 ,					__UpperCAmelCase:					float = 2.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					int = 12544 ,					__UpperCAmelCase:					float = 3.0 ,					__UpperCAmelCase:					float = 0.75 ,					__UpperCAmelCase:					float = 0.02 ,					__UpperCAmelCase:					float = 1.0 ,					__UpperCAmelCase:					bool = True ,					__UpperCAmelCase:					List[int] = [4, 8, 16, 32] ,					__UpperCAmelCase:					bool = None ,					**__UpperCAmelCase:					Any ,					)    ->     int:




											'''simple docstring'''



											if backbone_config is None:
																logger.info("`backbone_config` is `None`. Initializing the config with the default `Swin` backbone."   )
																__a					:						str      =					CONFIG_MAPPING["swin"](
																    image_size=224 ,					in_channels=3 ,					patch_size=4 ,					embed_dim=96 ,					depths=[2, 2, 18, 2] ,					num_heads=[3, 6, 12, 24] ,					window_size=7 ,					drop_path_rate=0.3 ,					use_absolute_embeddings=__UpperCAmelCase ,					out_features=["stage1", "stage2", "stage3", "stage4"] ,					)

											if isinstance(__UpperCAmelCase ,					__UpperCAmelCase   ):
																__a					:						List[Any]      =					backbone_config.pop("model_type"   )
																__a					:						List[str]      =					CONFIG_MAPPING[backbone_model_type]
																__a					:						Dict      =					config_class.from_dict(__UpperCAmelCase   )

											# verify that the backbone is supported
											if backbone_config.model_type not in self.backbones_supported:
																logger.warning_once(
																    f'Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. '
																    f'Supported model types: {",".join(self.backbones_supported   )}'   )

											__a					:						Tuple      =					backbone_config
											__a					:						Any      =					feature_size
											__a					:						int      =					mask_feature_size
											__a					:						Optional[int]      =					hidden_dim
											__a					:						int      =					encoder_feedforward_dim
											__a					:						List[str]      =					activation_function
											__a					:						Optional[Any]      =					encoder_layers
											__a					:						List[Any]      =					decoder_layers
											__a					:						Union[str, Any]      =					num_attention_heads
											__a					:						Optional[Any]      =					dropout
											__a					:						Tuple      =					dim_feedforward
											__a					:						List[str]      =					pre_norm
											__a					:						int      =					enforce_input_projection
											__a					:						List[str]      =					common_stride
											__a					:						Any      =					ignore_value
											__a					:						Union[str, Any]      =					num_queries
											__a					:						int      =					no_object_weight
											__a					:						Tuple      =					class_weight
											__a					:						int      =					mask_weight
											__a					:						Union[str, Any]      =					dice_weight
											__a					:						int      =					train_num_points
											__a					:						Any      =					oversample_ratio
											__a					:						Any      =					importance_sample_ratio
											__a					:						List[str]      =					init_std
											__a					:						List[str]      =					init_xavier_std
											__a					:						Dict      =					use_auxiliary_loss
											__a					:						List[Any]      =					feature_strides
											__a					:						Tuple      =					output_auxiliary_logits
											__a					:						List[Any]      =					decoder_layers

											super().__init__(**__UpperCAmelCase   )



						@classmethod
						def        UpperCAmelCase__					(cls:					Tuple ,					__UpperCAmelCase:					PretrainedConfig ,					**__UpperCAmelCase:					List[str]   )    ->     Any:




											'''simple docstring'''



											return cls(
											    backbone_config=__UpperCAmelCase ,					**__UpperCAmelCase ,					)



						def        UpperCAmelCase__					(self:					str   )    ->     Dict[str, any]:




											'''simple docstring'''



											__a					:						Tuple      =					copy.deepcopy(self.__dict__   )
											__a					:						List[str]      =					self.backbone_config.to_dict()
											__a					:						Any      =					self.__class__.model_type
											return output





