
'''simple docstring'''
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin







class  __A				( UpperCamelCase__			):
 lowerCamelCase				       ='''Speech2TextFeatureExtractor'''
 lowerCamelCase				       ='''Speech2TextTokenizer'''
 def __init__(   self			:    Tuple				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Optional[Any]       ):



   """simple docstring"""





   super().__init__(__A				,   __A       )
   __A		:     str												=		self.feature_extractor
   __A		:     Any												=		False
 def __call__(   self			:    Optional[Any]				,   *lowerCamelCase			:    Optional[int]				,   **lowerCamelCase			:    Tuple       ):



   """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		:     Optional[Any]												=		kwargs.pop("""raw_speech"""       )
   else:
     __A		:     int												=		kwargs.pop("""audio"""				,   __A       )
   __A		:     str												=		kwargs.pop("""sampling_rate"""				,   __A       )
   __A		:     Any												=		kwargs.pop("""text"""				,   __A       )
   if len(__A       ) > 0:
     __A		:     Union[str, Any]												=		args[0]
     __A		:     List[str]												=		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		:     Tuple												=		self.feature_extractor(__A				,   *__A				,   sampling_rate=__A				,   **__A       )
   if text is not None:
     __A		:     int												=		self.tokenizer(__A				,   **__A       )

   if text is None:
     return inputs
   elif audio is None:
     return encodings
   else:
     __A		:     Optional[Any]												=		encodings["""input_ids"""]
     return inputs
 def 		lowercase_(   self			:    Any				,   *lowerCamelCase			:    Tuple				,   **lowerCamelCase			:    Any       ):



   """simple docstring"""





   return self.tokenizer.batch_decode(*__A				,   **__A       )
 def 		lowercase_(   self			:    Tuple				,   *lowerCamelCase			:    Optional[Any]				,   **lowerCamelCase			:    List[Any]       ):



   """simple docstring"""





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






 @contextmanager
 def 		lowercase_(   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		:     int												=		True
   __A		:     Optional[Any]												=		self.tokenizer
   yield
   __A		:     int												=		self.feature_extractor
   __A		:     Tuple												=		False





'''simple docstring'''
from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
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_VISUAL_QUESTION_ANSWERING_MAPPING

A__   :		Any 		=logging.get_logger(__name__)







@add_end_docstrings(_SCREAMING_SNAKE_CASE			)
class  __A				( _SCREAMING_SNAKE_CASE			):
 def __init__(   self			:    Union[str, Any]				,   *lowerCamelCase			:    int				,   **lowerCamelCase			:    Optional[int]       ):



   """simple docstring"""





   super().__init__(*lowerCamelCase				,   **lowerCamelCase       )
   self.check_model_type(lowerCamelCase       )
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Dict=None				,   lowerCamelCase			:    Union[str, Any]=None				,   lowerCamelCase			:    int=None				,   **lowerCamelCase			:    int       ):



   """simple docstring"""





   __A							,					__A		:     Tuple												=		{}, {}
   if padding is not None:
     __A		:     Any												=		padding
   if truncation is not None:
     __A		:     Optional[Any]												=		truncation
   if top_k is not None:
     __A		:     List[str]												=		top_k
   return preprocess_params, {}, postprocess_params
 def __call__(   self			:    Any				,   lowerCamelCase			:    Union["Image.Image", str]				,   lowerCamelCase			:    str = None				,   **lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   if isinstance(lowerCamelCase				,   (Image.Image, str)       ) and isinstance(lowerCamelCase				,   lowerCamelCase       ):
     __A		:     Tuple												=		{"""image""": image, """question""": question}
   else:
     __A		:     List[Any]												=		image
   __A		:     Any												=		super().__call__(lowerCamelCase				,   **lowerCamelCase       )
   return results
 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Dict=False				,   lowerCamelCase			:    str=False       ):



   """simple docstring"""





   __A		:     List[str]												=		load_image(inputs["""image"""]       )
   __A		:     Optional[Any]												=		self.tokenizer(
       inputs["""question"""]				,   return_tensors=self.framework				,   padding=lowerCamelCase				,   truncation=lowerCamelCase       )
   __A		:     Union[str, Any]												=		self.image_processor(images=lowerCamelCase				,   return_tensors=self.framework       )
   model_inputs.update(lowerCamelCase       )
   return model_inputs
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   __A		:     List[str]												=		self.model(**lowerCamelCase       )
   return model_outputs






 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    List[Any]=5       ):



   """simple docstring"""





   if top_k > self.model.config.num_labels:
     __A		:     str												=		self.model.config.num_labels

   if self.framework == "pt":
     __A		:     Optional[Any]												=		model_outputs.logits.sigmoid()[0]
     __A							,					__A		:     int												=		probs.topk(lowerCamelCase       )
   else:
     raise ValueError(f"Unsupported framework: {self.framework}"       )

   __A		:     Optional[Any]												=		scores.tolist()
   __A		:     Dict												=		ids.tolist()
   return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase				,   lowerCamelCase       )]



