






'''simple docstring'''




import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						'''Speech2TextFeatureExtractor'''
   snake_case    				=						'''Speech2TextTokenizer'''

   def __init__(		self :	str      ,      __UpperCAmelCase :	Tuple      ,      __UpperCAmelCase :	Tuple		):





         '''simple docstring'''





         super().__init__(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	self.feature_extractor
         _A			     =	False

   def __call__(		self :	Tuple      ,      *__UpperCAmelCase :	Union[str, Any]      ,      **__UpperCAmelCase :	Tuple		):





         '''simple docstring'''





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

         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"      ,      __UpperCAmelCase		)
         _A			     =	kwargs.pop("sampling_rate"      ,      __UpperCAmelCase		)
         _A			     =	kwargs.pop("text"      ,      __UpperCAmelCase		)
         if len(__UpperCAmelCase		) > 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(__UpperCAmelCase      ,      *__UpperCAmelCase      ,      sampling_rate=__UpperCAmelCase      ,      **__UpperCAmelCase		)
         if text is not None:
               _A			     =	self.tokenizer(__UpperCAmelCase      ,      **__UpperCAmelCase		)

         if text is None:
               return inputs
         elif audio is None:
               return encodings
         else:
               _A			     =	encodings["input_ids"]
               return inputs

   def 						lowerCAmelCase (		self :	List[Any]      ,      *__UpperCAmelCase :	Tuple      ,      **__UpperCAmelCase :	Optional[int]		):





         '''simple docstring'''





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

   def 						lowerCAmelCase (		self :	Any      ,      *__UpperCAmelCase :	Any      ,      **__UpperCAmelCase :	Optional[int]		):





         '''simple docstring'''





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

   @contextmanager
   def 						lowerCAmelCase (		self :	str		):





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







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_tf_available():
  import tensorflow as tf

  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
  from ..tf_utils import stable_softmax

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

lowercase  :					Any						    =							logging.get_logger(__name__)

@add_end_docstrings(lowerCAmelCase     )
class __snake_case		(      lowerCAmelCase     ):
    def __init__(  self		,*snake_case		,**snake_case      ):



           '''simple docstring'''







           super().__init__(*snake_case		,**snake_case      )
           requires_backends(self		,"""vision"""      )
           self.check_model_type(
               TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
               if self.framework == """tf"""
               else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case=None      ):



           '''simple docstring'''







           lowercase							:     List[Any]       	=      {}
           if top_k is not None:
                  lowercase							:     int       	=      top_k
           return {}, {}, postprocess_params
    def __call__(  self		,snake_case		,**snake_case      ):



           '''simple docstring'''







           return super().__call__(snake_case		,**snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Any       	=      load_image(snake_case      )
           lowercase							:     List[Any]       	=      self.image_processor(images=snake_case		,return_tensors=self.framework      )
           return model_inputs
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case      ):



           '''simple docstring'''







           lowercase							:     int       	=      self.model(**snake_case      )
           return model_outputs
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case=5      ):



           '''simple docstring'''







           if top_k > self.model.config.num_labels:
                  lowercase							:     Tuple       	=      self.model.config.num_labels

           if self.framework == "pt":
                  lowercase							:     str       	=      model_outputs.logits.softmax(-1      )[0]
                  lowercase	,			lowercase							:     Dict       	=      probs.topk(snake_case      )
           elif self.framework == "tf":
                  lowercase							:     Optional[int]       	=      stable_softmax(model_outputs.logits		,axis=-1      )[0]
                  lowercase							:     Union[str, Any]       	=      tf.math.top_k(snake_case		,k=snake_case      )
                  lowercase	,			lowercase							:     List[str]       	=      topk.values.numpy(), topk.indices.numpy()
           else:
                  raise ValueError(f"Unsupported framework: {self.framework}"      )

           lowercase							:     Tuple       	=      scores.tolist()
           lowercase							:     Dict       	=      ids.tolist()
           return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case		,snake_case      )]
