

'''simple docstring'''

import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase       =     logging.get_logger(__name__)

UpperCAmelCase       =     {
    '''salesforce/blip2-opt-2.7b''': '''https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json''',
}


class      lowerCAmelCase   (					__a      ):
       lowerCAmelCase_				=       '''blip_2_vision_model'''







       def __init__(							self	:       str  ,					__lowercase	:       List[Any]=1408  ,					__lowercase	:       Any=6144  ,					__lowercase	:       Optional[Any]=39  ,					__lowercase	:       str=16  ,					__lowercase	:       Any=224  ,					__lowercase	:       Dict=14  ,					__lowercase	:       Optional[int]="gelu"  ,					__lowercase	:       int=0.0_0_0_0_1  ,					__lowercase	:       Any=0.0  ,					__lowercase	:       List[Any]=1E-10  ,					__lowercase	:       Any=True  ,					**__lowercase	:       Optional[Any]  ,					):







            """simple docstring"""







            super().__init__(**lowerCAmelCase__					)

            __lowercase							=hidden_size
            __lowercase							=intermediate_size
            __lowercase							=num_hidden_layers
            __lowercase							=num_attention_heads
            __lowercase							=patch_size
            __lowercase							=image_size
            __lowercase							=initializer_range
            __lowercase							=attention_dropout
            __lowercase							=layer_norm_eps
            __lowercase							=hidden_act
            __lowercase							=qkv_bias







       @classmethod
       def 	snake_case      (							cls	:       List[Any]  ,					__lowercase	:       Optional[Any]  ,					**__lowercase	:       Dict					):







            """simple docstring"""







            cls._set_token_in_kwargs(lowerCAmelCase__					)

            __lowercase						,   __lowercase							=cls.get_config_dict(lowerCAmelCase__  ,					**lowerCAmelCase__					)

            # get the vision config dict if we are loading from Blip2Config
            if config_dict.get('model_type'					) == "blip-2":
                 __lowercase							=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(lowerCAmelCase__  ,					**lowerCAmelCase__					)






class      lowerCAmelCase   (					__a      ):
       lowerCAmelCase_				=       '''blip_2_qformer'''







       def __init__(							self	:       List[str]  ,					__lowercase	:       Union[str, Any]=30522  ,					__lowercase	:       Tuple=768  ,					__lowercase	:       List[Any]=12  ,					__lowercase	:       Union[str, Any]=12  ,					__lowercase	:       Tuple=3072  ,					__lowercase	:       List[Any]="gelu"  ,					__lowercase	:       Optional[int]=0.1  ,					__lowercase	:       Dict=0.1  ,					__lowercase	:       Tuple=512  ,					__lowercase	:       Optional[int]=0.0_2  ,					__lowercase	:       Union[str, Any]=1E-12  ,					__lowercase	:       List[str]=0  ,					__lowercase	:       List[Any]="absolute"  ,					__lowercase	:       int=2  ,					__lowercase	:       Tuple=1408  ,					**__lowercase	:       Optional[int]  ,					):







            """simple docstring"""







            super().__init__(pad_token_id=lowerCAmelCase__  ,					**lowerCAmelCase__					)

            __lowercase							=vocab_size
            __lowercase							=hidden_size
            __lowercase							=num_hidden_layers
            __lowercase							=num_attention_heads
            __lowercase							=hidden_act
            __lowercase							=intermediate_size
            __lowercase							=hidden_dropout_prob
            __lowercase							=attention_probs_dropout_prob
            __lowercase							=max_position_embeddings
            __lowercase							=initializer_range
            __lowercase							=layer_norm_eps
            __lowercase							=position_embedding_type
            __lowercase							=cross_attention_frequency
            __lowercase							=encoder_hidden_size







       @classmethod
       def 	snake_case      (							cls	:       Any  ,					__lowercase	:       List[str]  ,					**__lowercase	:       List[str]					):







            """simple docstring"""







            cls._set_token_in_kwargs(lowerCAmelCase__					)

            __lowercase						,   __lowercase							=cls.get_config_dict(lowerCAmelCase__  ,					**lowerCAmelCase__					)

            # get the qformer config dict if we are loading from Blip2Config
            if config_dict.get('model_type'					) == "blip-2":
                 __lowercase							=config_dict['qformer_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(lowerCAmelCase__  ,					**lowerCAmelCase__					)






class      lowerCAmelCase   (					__a      ):
       lowerCAmelCase_				=       '''blip-2'''
       lowerCAmelCase_				=       True







       def __init__(							self	:       Union[str, Any]  ,					__lowercase	:       Optional[int]=None  ,					__lowercase	:       List[Any]=None  ,					__lowercase	:       List[str]=None  ,					__lowercase	:       str=32  ,					**__lowercase	:       List[Any]					):







            """simple docstring"""







            super().__init__(**lowerCAmelCase__					)

            if vision_config is None:
                 __lowercase							={}
                 logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'					)

            if qformer_config is None:
                 __lowercase							={}
                 logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.'					)

            if text_config is None:
                 __lowercase							={}
                 logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'					)

            __lowercase							=BlipaVisionConfig(**lowerCAmelCase__					)
            __lowercase							=BlipaQFormerConfig(**lowerCAmelCase__					)
            __lowercase							=text_config['model_type'] if 'model_type' in text_config else 'opt'
            __lowercase							=CONFIG_MAPPING[text_model_type](**lowerCAmelCase__					)

            __lowercase							=self.text_config.tie_word_embeddings
            __lowercase							=self.text_config.is_encoder_decoder

            __lowercase							=num_query_tokens
            __lowercase							=self.vision_config.hidden_size
            __lowercase							=self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
            __lowercase							=1.0
            __lowercase							=0.0_2







       @classmethod
       def 	snake_case      (							cls	:       Optional[Any]  ,					__lowercase	:       str  ,					__lowercase	:       Dict  ,					__lowercase	:       Optional[Any]  ,					**__lowercase	:       Any  ,					):







            """simple docstring"""







            return cls(
                vision_config=vision_config.to_dict()  ,					qformer_config=qformer_config.to_dict()  ,					text_config=text_config.to_dict()  ,					**lowerCAmelCase__  ,					)







       def 	snake_case      (							self	:       Dict					):







            """simple docstring"""







            __lowercase							=copy.deepcopy(self.__dict__					)
            __lowercase							=self.vision_config.to_dict()
            __lowercase							=self.qformer_config.to_dict()
            __lowercase							=self.text_config.to_dict()
            __lowercase							=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.
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 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		'''dandelin/vilt-b32-finetuned-vqa'''
		__lowercase							:  str						   =		(
		    '''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.'''
		)
		__lowercase							:  Optional[int]						   =		'''image_qa'''
		__lowercase							:  Union[str, Any]						   =		AutoProcessor
		__lowercase							:  Optional[Any]						   =		AutoModelForVisualQuestionAnswering

		__lowercase							:  List[str]						   =		['''image''', '''text''']
		__lowercase							:  Optional[Any]						   =		['''text''']




		def __init__(     self							,  *lowerCAmelCase__							,  **lowerCAmelCase__):
				requires_backends(self							,  ["""vision"""])
				super().__init__(*lowerCAmelCase__							,  **lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				return self.pre_processor(lowerCAmelCase__							,  lowerCAmelCase__							,  return_tensors="""pt""")




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				with torch.no_grad():
						return self.model(**lowerCAmelCase__).logits




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = outputs.argmax(-1).item()
				return self.model.config.idalabel[idx]




