



'''simple docstring'''





from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
  from PIL import Image

  from ..image_utils import load_image

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
  from ..tf_utils import stable_softmax

lowercase_		       =  logging.get_logger(__name__)






@add_end_docstrings(A						)
class __A			(  A						):



 '''simple docstring'''







 def __init__(self ,   **A       )    ->     List[str]:


      """simple docstring"""



      super().__init__(**A       )

      requires_backends(self ,   '''vision'''       )
      self.check_model_type(
          TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
          if self.framework == '''tf'''
          else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING       )



 def __call__(self ,   A ,   **A       )    ->     Union[str, Any]:


      """simple docstring"""



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



 def 					a__  (self ,   **A       )    ->     Union[str, Any]:


      """simple docstring"""



      _a      =  {}
      if "candidate_labels" in kwargs:
           _a      =  kwargs['''candidate_labels''']
      if "hypothesis_template" in kwargs:
           _a      =  kwargs['''hypothesis_template''']

      return preprocess_params, {}, {}



 def 					a__  (self ,   A ,   A=None ,   A="This is a photo of {}."       )    ->     Optional[Any]:


      """simple docstring"""



      _a      =  load_image(A       )
      _a      =  self.image_processor(images=[image] ,   return_tensors=self.framework       )
      _a      =  candidate_labels
      _a      =  [hypothesis_template.format(A       ) for x in candidate_labels]
      _a      =  self.tokenizer(A ,   return_tensors=self.framework ,   padding=A       )
      _a      =  [text_inputs]
      return inputs



 def 					a__  (self ,   A       )    ->     List[str]:


      """simple docstring"""



      _a      =  model_inputs.pop('''candidate_labels'''       )
      _a      =  model_inputs.pop('''text_inputs'''       )
      if isinstance(text_inputs[0] ,   A       ):
           _a      =  text_inputs[0]
      else:
           # Batching case.
           _a      =  text_inputs[0][0]

      _a      =  self.model(**A ,   **A       )

      _a      =  {
          '''candidate_labels''': candidate_labels,
          '''logits''': outputs.logits_per_image,
      }
      return model_outputs



 def 					a__  (self ,   A       )    ->     str:


      """simple docstring"""



      _a      =  model_outputs.pop('''candidate_labels'''       )
      _a      =  model_outputs['''logits'''][0]
      if self.framework == "pt":
           _a      =  logits.softmax(dim=-1       ).squeeze(-1       )
           _a      =  probs.tolist()
           if not isinstance(A ,   A       ):
                _a      =  [scores]
      elif self.framework == "tf":
           _a      =  stable_softmax(A ,   axis=-1       )
           _a      =  probs.numpy().tolist()
      else:
           raise ValueError(f'''Unsupported framework: {self.framework}'''       )

      _a      =  [
          {'''score''': score, '''label''': candidate_label}
          for score, candidate_label in sorted(zip(A ,   A       ) ,   key=lambda A       : -x[0]       )
      ]
      return result








'''simple docstring'''





import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin






class __A			(  A						):



 '''simple docstring'''







 __lowerCamelCase    :       Optional[Any]          =       'MCTCTFeatureExtractor'
 __lowerCamelCase    :       Optional[Any]          =       'AutoTokenizer'



 def __init__(self ,   A ,   A       )    ->     Dict:


      """simple docstring"""



      super().__init__(A ,   A       )
      _a      =  self.feature_extractor
      _a      =  False



 def __call__(self ,   *A ,   **A       )    ->     Optional[int]:


      """simple docstring"""



      if self._in_target_context_manager:
           return self.current_processor(*A ,   **A       )

      if "raw_speech" in kwargs:
           warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''       )
           _a      =  kwargs.pop('''raw_speech'''       )
      else:
           _a      =  kwargs.pop('''audio''' ,   A       )
      _a      =  kwargs.pop('''sampling_rate''' ,   A       )
      _a      =  kwargs.pop('''text''' ,   A       )
      if len(A       ) > 0:
           _a      =  args[0]
           _a      =  args[1:]

      if audio is None and text is None:
           raise ValueError('''You need to specify either an `audio` or `text` input to process.'''       )

      if audio is not None:
           _a      =  self.feature_extractor(A ,   *A ,   sampling_rate=A ,   **A       )
      if text is not None:
           _a      =  self.tokenizer(A ,   **A       )

      if text is None:
           return inputs
      elif audio is None:
           return encodings
      else:
           _a      =  encodings['''input_ids''']
           return inputs



 def 					a__  (self ,   *A ,   **A       )    ->     int:


      """simple docstring"""



      return self.tokenizer.batch_decode(*A ,   **A       )



 def 					a__  (self ,   *A ,   **A       )    ->     Tuple:


      """simple docstring"""



      if self._in_target_context_manager:
           return self.current_processor.pad(*A ,   **A       )

      _a      =  kwargs.pop('''input_features''' ,   A       )
      _a      =  kwargs.pop('''labels''' ,   A       )
      if len(A       ) > 0:
           _a      =  args[0]
           _a      =  args[1:]

      if input_features is not None:
           _a      =  self.feature_extractor.pad(A ,   *A ,   **A       )
      if labels is not None:
           _a      =  self.tokenizer.pad(A ,   **A       )

      if labels is None:
           return input_features
      elif input_features is None:
           return labels
      else:
           _a      =  labels['''input_ids''']
           return input_features



 def 					a__  (self ,   *A ,   **A       )    ->     List[str]:


      """simple docstring"""



      return self.tokenizer.decode(*A ,   **A       )



 @contextmanager
 def 					a__  (self       )    ->     Dict:


      """simple docstring"""



      warnings.warn(
          '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
          '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
          '''your audio inputs, or in a separate call.'''       )
      _a      =  True
      _a      =  self.tokenizer
      yield
      _a      =  self.feature_extractor
      _a      =  False



