


"""simple docstring"""



import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class _lowerCAmelCase				(      lowerCamelCase__	):
   __UpperCAmelCase :       List[Any]							  =  ['image_processor', 'tokenizer']
   __UpperCAmelCase :       Dict							  =  'ChineseCLIPImageProcessor'
   __UpperCAmelCase :       str							  =  ('BertTokenizer', 'BertTokenizerFast')







   def __init__(	self						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	**UpperCamelCase__						)	->		Optional[Any]:

       '''simple docstring'''




       snake_case    :      List[str]      					=    None
       if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."						,	lowercase_						,	)
           snake_case    :      Any      					=    kwargs.pop("feature_extractor"						)

       snake_case    :      Optional[int]      					=    image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
           raise ValueError("You need to specify an `image_processor`."						)
       if tokenizer is None:
           raise ValueError("You need to specify a `tokenizer`."						)

       super().__init__(lowercase_						,	lowercase_						)
       snake_case    :      Optional[int]      					=    self.image_processor







   def __call__(	self						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	**UpperCamelCase__						)	->		Union[str, Any]:

       '''simple docstring'''




       if text is None and images is None:
           raise ValueError("You have to specify either text or images. Both cannot be none."						)

       if text is not None:
           snake_case    :      str      					=    self.tokenizer(lowercase_						,	return_tensors=lowercase_						,	**lowercase_						)

       if images is not None:
           snake_case    :      List[str]      					=    self.image_processor(lowercase_						,	return_tensors=lowercase_						,	**lowercase_						)

       if text is not None and images is not None:
           snake_case    :      List[Any]      					=    image_features.pixel_values
           return encoding
       elif text is not None:
           return encoding
       else:
           return BatchEncoding(data=dict(**lowercase_						)						,	tensor_type=lowercase_						)







   def  lowerCamelCase		(	self						,	*UpperCamelCase__						,	**UpperCamelCase__						)	->		Union[str, Any]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	*UpperCamelCase__						,	**UpperCamelCase__						)	->		Tuple:

       '''simple docstring'''




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







   @property
   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    self.tokenizer.model_input_names
       snake_case    :      Dict      					=    self.image_processor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names						)						)







   @property
   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       warnings.warn(
           "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."						,	lowercase_						,	)
       return self.image_processor_class








from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


lowerCamelCase           =					logging.get_logger(__name__)







@add_end_docstrings(lowerCamelCase__							)
class 						__magic_name__			(							lowerCamelCase__							):

   '''simple docstring'''




   def __init__(				self,    **lowercase_       )							->    Dict:






     """simple docstring"""






     super().__init__(**lowercase_       )

     if self.framework != "pt":
       raise ValueError(F"""The {self.__class__} is only available in PyTorch."""       )
     # No specific FOR_XXX available yet



   def __call__(				self,    lowercase_,    **lowercase_       )							->    Tuple:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    **lowercase_       )							->    int:






     """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  _UpperCAmelCase (				self,    lowercase_,    lowercase_=None,    lowercase_="This is a sound of {}."       )							->    Union[str, Any]:






     """simple docstring"""






     if isinstance(lowercase_,    lowercase_       ):
       if audio.startswith('''http://'''       ) or audio.startswith('''https://'''       ):
         # We need to actually check for a real protocol, otherwise it's impossible to use a local file
         # like http_huggingface_co.png
         a__  =requests.get(lowercase_       ).content
       else:
         with open(lowercase_,    '''rb'''       ) as f:
           a__  =f.read()

     if isinstance(lowercase_,    lowercase_       ):
       a__  =ffmpeg_read(lowercase_,    self.feature_extractor.sampling_rate       )

     if not isinstance(lowercase_,    np.ndarray       ):
       raise ValueError('''We expect a numpy ndarray as input'''       )
     if len(audio.shape       ) != 1:
       raise ValueError('''We expect a single channel audio input for ZeroShotAudioClassificationPipeline'''       )

     a__  =self.feature_extractor(
         [audio],    sampling_rate=self.feature_extractor.sampling_rate,    return_tensors='''pt'''       )
     a__  =candidate_labels
     a__  =[hypothesis_template.format(lowercase_       ) for x in candidate_labels]
     a__  =self.tokenizer(lowercase_,    return_tensors=self.framework,    padding=lowercase_       )
     a__  =[text_inputs]
     return inputs



   def  _UpperCAmelCase (				self,    lowercase_       )							->    str:






     """simple docstring"""






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

     a__  =self.model(**lowercase_,    **lowercase_       )

     a__  ={
         '''candidate_labels''': candidate_labels,
         '''logits''': outputs.logits_per_audio,
     }
     return model_outputs



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Any:






     """simple docstring"""






     a__  =model_outputs.pop('''candidate_labels'''       )
     a__  =model_outputs['''logits'''][0]

     if self.framework == "pt":
       a__  =logits.softmax(dim=0       )
       a__  =probs.tolist()
     else:
       raise ValueError('''`tf` framework not supported.'''       )

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