
"""simple docstring"""






from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)





class 				a__					(						A__       ):



     def  lowerCamelCase_						(							self :Tuple      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      if isinstance(_lowerCamelCase      ,  _lowerCamelCase   ):
       UpperCamelCase_     :					Tuple   	=[label.strip() for label in labels.split(','   ) if label.strip()]
      return labels




     def __call__(							self :str      ,  _lowerCamelCase :int      ,  _lowerCamelCase :List[Any]      ,  _lowerCamelCase :Optional[int]   ):







      '''simple docstring'''



      if len(_lowerCamelCase   ) == 0 or len(_lowerCamelCase   ) == 0:
       raise ValueError('You must include at least one label and at least one sequence.'   )
      if hypothesis_template.format(labels[0]   ) == hypothesis_template:
       raise ValueError(
           (
               'The provided hypothesis_template "{}" was not able to be formatted with the target labels. '
               'Make sure the passed template includes formatting syntax such as {{}} where the label should go.'
           ).format(_lowerCamelCase   )   )

      if isinstance(_lowerCamelCase      ,  _lowerCamelCase   ):
       UpperCamelCase_     :					int   	=[sequences]

      UpperCamelCase_     :					Union[str, Any]   	=[]
      for sequence in sequences:
       sequence_pairs.extend([[sequence, hypothesis_template.format(_lowerCamelCase   )] for label in labels]   )

      return sequence_pairs, sequences





@add_end_docstrings(A__       )
class 				a__					(						A__       ):



     def __init__(							self :List[str]      ,  _lowerCamelCase :str=ZeroShotClassificationArgumentHandler()      ,  *_lowerCamelCase :List[Any]      ,  **_lowerCamelCase :List[str]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=args_parser
      super().__init__(*_lowerCamelCase      ,  **_lowerCamelCase   )
      if self.entailment_id == -1:
       logger.warning(
           'Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '
           '-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.'   )



     @property
     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      for label, ind in self.model.config.labelaid.items():
       if label.lower().startswith('entail'   ):
        return ind
      return -1



     def  lowerCamelCase_						(							self :int      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :List[str]=True      ,  _lowerCamelCase :str=True      ,  _lowerCamelCase :Union[str, Any]=TruncationStrategy.ONLY_FIRST      ,  **_lowerCamelCase :Tuple   ):







      '''simple docstring'''



      UpperCamelCase_     :					Dict   	=self.framework
      if self.tokenizer.pad_token is None:
       # Override for tokenizers not supporting padding
       logger.error(
           'Tokenizer was not supporting padding necessary for zero-shot, attempting to use '
           ' `pad_token=eos_token`'   )
       UpperCamelCase_     :					int   	=self.tokenizer.eos_token
      try:
       UpperCamelCase_     :					List[str]   	=self.tokenizer(
           _lowerCamelCase      ,  add_special_tokens=_lowerCamelCase      ,  return_tensors=_lowerCamelCase      ,  padding=_lowerCamelCase      ,  truncation=_lowerCamelCase      ,  )
      except Exception as e:
       if "too short" in str(_lowerCamelCase   ):
        # tokenizers might yell that we want to truncate
        # to a value that is not even reached by the input.
        # In that case we don't want to truncate.
        # It seems there's not a really better way to catch that
        # exception.

        UpperCamelCase_     :					Union[str, Any]   	=self.tokenizer(
            _lowerCamelCase      ,  add_special_tokens=_lowerCamelCase      ,  return_tensors=_lowerCamelCase      ,  padding=_lowerCamelCase      ,  truncation=TruncationStrategy.DO_NOT_TRUNCATE      ,  )
       else:
        raise e

      return inputs



     def  lowerCamelCase_						(							self :str      ,  **_lowerCamelCase :List[Any]   ):







      '''simple docstring'''



      if kwargs.get('multi_class'      ,  _lowerCamelCase   ) is not None:
       UpperCamelCase_     :					Union[str, Any]   	=kwargs['multi_class']
       logger.warning(
           'The `multi_class` argument has been deprecated and renamed to `multi_label`. '
           '`multi_class` will be removed in a future version of Transformers.'   )
      UpperCamelCase_     :					Optional[Any]   	={}
      if "candidate_labels" in kwargs:
       UpperCamelCase_     :					Optional[Any]   	=self._args_parser._parse_labels(kwargs['candidate_labels']   )
      if "hypothesis_template" in kwargs:
       UpperCamelCase_     :					Optional[int]   	=kwargs['hypothesis_template']

      UpperCamelCase_     :					List[Any]   	={}
      if "multi_label" in kwargs:
       UpperCamelCase_     :					Optional[Any]   	=kwargs['multi_label']
      return preprocess_params, {}, postprocess_params



     def __call__(							self :Union[str, Any]      ,  _lowerCamelCase :Union[str, List[str]]      ,  *_lowerCamelCase :List[str]      ,  **_lowerCamelCase :str      ,  ):







      '''simple docstring'''



      if len(_lowerCamelCase   ) == 0:
       pass
      elif len(_lowerCamelCase   ) == 1 and "candidate_labels" not in kwargs:
       UpperCamelCase_     :					Optional[Any]   	=args[0]
      else:
       raise ValueError(f'''Unable to understand extra arguments {args}'''   )

      return super().__call__(_lowerCamelCase      ,  **_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :Optional[int]      ,  _lowerCamelCase :Tuple=None      ,  _lowerCamelCase :Optional[Any]="This example is {}."   ):







      '''simple docstring'''



      UpperCamelCase_	,     UpperCamelCase_     :					Union[str, Any]   	=self._args_parser(_lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase   )

      for i, (candidate_label, sequence_pair) in enumerate(zip(_lowerCamelCase      ,  _lowerCamelCase   )   ):
       UpperCamelCase_     :					Optional[int]   	=self._parse_and_tokenize([sequence_pair]   )

       yield {
           "candidate_label": candidate_label,
           "sequence": sequences[0],
           "is_last": i == len(_lowerCamelCase   ) - 1,
           **model_input,
       }



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :str   ):







      '''simple docstring'''



      UpperCamelCase_     :					Union[str, Any]   	=inputs['candidate_label']
      UpperCamelCase_     :					List[str]   	=inputs['sequence']
      UpperCamelCase_     :					Dict   	={k: inputs[k] for k in self.tokenizer.model_input_names}
      UpperCamelCase_     :					Any   	=self.model(**_lowerCamelCase   )

      UpperCamelCase_     :					List[str]   	={
          'candidate_label': candidate_label,
          'sequence': sequence,
          'is_last': inputs['is_last'],
          **outputs,
      }
      return model_outputs




     def  lowerCamelCase_						(							self :Any      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :str=False   ):







      '''simple docstring'''



      UpperCamelCase_     :					Any   	=[outputs['candidate_label'] for outputs in model_outputs]
      UpperCamelCase_     :					List[str]   	=[outputs['sequence'] for outputs in model_outputs]
      UpperCamelCase_     :					Any   	=np.concatenate([output['logits'].numpy() for output in model_outputs]   )
      UpperCamelCase_     :					List[str]   	=logits.shape[0]
      UpperCamelCase_     :					Any   	=len(_lowerCamelCase   )
      UpperCamelCase_     :					List[Any]   	=N // n
      UpperCamelCase_     :					Optional[int]   	=logits.reshape((num_sequences, n, -1)   )

      if multi_label or len(_lowerCamelCase   ) == 1:
       # softmax over the entailment vs. contradiction dim for each label independently
       UpperCamelCase_     :					List[str]   	=self.entailment_id
       UpperCamelCase_     :					Dict   	=-1 if entailment_id == 0 else 0
       UpperCamelCase_     :					Dict   	=reshaped_outputs[..., [contradiction_id, entailment_id]]
       UpperCamelCase_     :					Tuple   	=np.exp(_lowerCamelCase   ) / np.exp(_lowerCamelCase   ).sum(-1      ,  keepdims=_lowerCamelCase   )
       UpperCamelCase_     :					Optional[Any]   	=scores[..., 1]
      else:
       # softmax the "entailment" logits over all candidate labels
       UpperCamelCase_     :					Optional[Any]   	=reshaped_outputs[..., self.entailment_id]
       UpperCamelCase_     :					Dict   	=np.exp(_lowerCamelCase   ) / np.exp(_lowerCamelCase   ).sum(-1      ,  keepdims=_lowerCamelCase   )

      UpperCamelCase_     :					Optional[int]   	=list(reversed(scores[0].argsort()   )   )
      return {
          "sequence": sequences[0],
          "labels": [candidate_labels[i] for i in top_inds],
          "scores": scores[0, top_inds].tolist(),
      }


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


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     {
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}





class 				a__					(						A__       ):
     UpperCAmelCase__         =      '''instructblip_vision_model'''



     def __init__(							self :int      ,  _lowerCamelCase :Optional[int]=1_408      ,  _lowerCamelCase :int=6_144      ,  _lowerCamelCase :str=39      ,  _lowerCamelCase :Tuple=16      ,  _lowerCamelCase :List[Any]=224      ,  _lowerCamelCase :int=14      ,  _lowerCamelCase :int="gelu"      ,  _lowerCamelCase :Optional[Any]=1E-6      ,  _lowerCamelCase :str=0.0      ,  _lowerCamelCase :Optional[Any]=1E-1_0      ,  _lowerCamelCase :str=True      ,  **_lowerCamelCase :Optional[Any]      ,  ):







      '''simple docstring'''



      super().__init__(**_lowerCamelCase   )

      UpperCamelCase_     :					Dict   	=hidden_size
      UpperCamelCase_     :					Tuple   	=intermediate_size
      UpperCamelCase_     :					Optional[int]   	=num_hidden_layers
      UpperCamelCase_     :					Union[str, Any]   	=num_attention_heads
      UpperCamelCase_     :					Optional[Any]   	=patch_size
      UpperCamelCase_     :					List[Any]   	=image_size
      UpperCamelCase_     :					Dict   	=initializer_range
      UpperCamelCase_     :					List[Any]   	=attention_dropout
      UpperCamelCase_     :					List[str]   	=layer_norm_eps
      UpperCamelCase_     :					List[str]   	=hidden_act
      UpperCamelCase_     :					Tuple   	=qkv_bias




     @classmethod
     def  lowerCamelCase_						(							cls :Optional[Any]      ,  _lowerCamelCase :Union[str, os.PathLike]      ,  **_lowerCamelCase :Any   ):







      '''simple docstring'''



      cls._set_token_in_kwargs(_lowerCamelCase   )

      UpperCamelCase_	,     UpperCamelCase_     :					List[str]   	=cls.get_config_dict(_lowerCamelCase      ,  **_lowerCamelCase   )

      # get the vision config dict if we are loading from InstructBlipConfig
      if config_dict.get('model_type'   ) == "instructblip":
       UpperCamelCase_     :					Tuple   	=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 				a__					(						A__       ):
     UpperCAmelCase__         =      '''instructblip_qformer'''



     def __init__(							self :Dict      ,  _lowerCamelCase :List[Any]=30_522      ,  _lowerCamelCase :Optional[int]=768      ,  _lowerCamelCase :Tuple=12      ,  _lowerCamelCase :List[str]=12      ,  _lowerCamelCase :int=3_072      ,  _lowerCamelCase :Tuple="gelu"      ,  _lowerCamelCase :Dict=0.1      ,  _lowerCamelCase :List[Any]=0.1      ,  _lowerCamelCase :Optional[int]=512      ,  _lowerCamelCase :List[Any]=0.02      ,  _lowerCamelCase :int=1E-1_2      ,  _lowerCamelCase :str=0      ,  _lowerCamelCase :Dict="absolute"      ,  _lowerCamelCase :Optional[int]=2      ,  _lowerCamelCase :Union[str, Any]=1_408      ,  **_lowerCamelCase :Tuple      ,  ):







      '''simple docstring'''



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

      UpperCamelCase_     :					Optional[Any]   	=vocab_size
      UpperCamelCase_     :					Tuple   	=hidden_size
      UpperCamelCase_     :					Union[str, Any]   	=num_hidden_layers
      UpperCamelCase_     :					List[str]   	=num_attention_heads
      UpperCamelCase_     :					List[str]   	=hidden_act
      UpperCamelCase_     :					Tuple   	=intermediate_size
      UpperCamelCase_     :					Optional[Any]   	=hidden_dropout_prob
      UpperCamelCase_     :					List[str]   	=attention_probs_dropout_prob
      UpperCamelCase_     :					List[str]   	=max_position_embeddings
      UpperCamelCase_     :					List[str]   	=initializer_range
      UpperCamelCase_     :					str   	=layer_norm_eps
      UpperCamelCase_     :					int   	=position_embedding_type
      UpperCamelCase_     :					Tuple   	=cross_attention_frequency
      UpperCamelCase_     :					List[Any]   	=encoder_hidden_size




     @classmethod
     def  lowerCamelCase_						(							cls :Optional[Any]      ,  _lowerCamelCase :Union[str, os.PathLike]      ,  **_lowerCamelCase :List[Any]   ):







      '''simple docstring'''



      cls._set_token_in_kwargs(_lowerCamelCase   )

      UpperCamelCase_	,     UpperCamelCase_     :					Optional[Any]   	=cls.get_config_dict(_lowerCamelCase      ,  **_lowerCamelCase   )

      # get the qformer config dict if we are loading from InstructBlipConfig
      if config_dict.get('model_type'   ) == "instructblip":
       UpperCamelCase_     :					List[Any]   	=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 				a__					(						A__       ):
     UpperCAmelCase__         =      '''instructblip'''
     UpperCAmelCase__         =      True



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







      '''simple docstring'''



      super().__init__(**_lowerCamelCase   )

      if vision_config is None:
       UpperCamelCase_     :					Optional[int]   	={}
       logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'   )

      if qformer_config is None:
       UpperCamelCase_     :					Union[str, Any]   	={}
       logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'   )

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

      UpperCamelCase_     :					Any   	=InstructBlipVisionConfig(**_lowerCamelCase   )
      UpperCamelCase_     :					Optional[Any]   	=InstructBlipQFormerConfig(**_lowerCamelCase   )
      UpperCamelCase_     :					str   	=text_config['model_type'] if 'model_type' in text_config else 'opt'
      UpperCamelCase_     :					int   	=CONFIG_MAPPING[text_model_type](**_lowerCamelCase   )

      UpperCamelCase_     :					Tuple   	=self.text_config.tie_word_embeddings
      UpperCamelCase_     :					Any   	=self.text_config.is_encoder_decoder

      UpperCamelCase_     :					List[Any]   	=num_query_tokens
      UpperCamelCase_     :					Tuple   	=self.vision_config.hidden_size
      UpperCamelCase_     :					Tuple   	=self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      UpperCamelCase_     :					int   	=1.0
      UpperCamelCase_     :					List[str]   	=0.02



     @classmethod
     def  lowerCamelCase_						(							cls :Optional[Any]      ,  _lowerCamelCase :InstructBlipVisionConfig      ,  _lowerCamelCase :InstructBlipQFormerConfig      ,  _lowerCamelCase :PretrainedConfig      ,  **_lowerCamelCase :Dict      ,  ):







      '''simple docstring'''



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




     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=copy.deepcopy(self.__dict__   )
      UpperCamelCase_     :					Dict   	=self.vision_config.to_dict()
      UpperCamelCase_     :					Tuple   	=self.qformer_config.to_dict()
      UpperCamelCase_     :					List[str]   	=self.text_config.to_dict()
      UpperCamelCase_     :					List[str]   	=self.__class__.model_type
      return output
