




"""simple docstring"""






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


UpperCamelCase__						=			logging.get_logger(__name__)




@add_end_docstrings(_a  )
class   a__			(       _a  ):





						def __init__(							self      :	Optional[int]     ,**a__      :	List[str])						->       List[Any]:




											"""simple docstring"""







											super().__init__(**_A)

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





						def __call__(							self      :	Union[str, Any]     ,a__      :	Union[str, Any]     ,**a__      :	Tuple)						->       List[Any]:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	str     ,**a__      :	Any)						->       Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  {}
											if "candidate_labels" in kwargs:
																_lowerCAmelCase:Optional[int]									=  kwargs['''candidate_labels''']
											if "hypothesis_template" in kwargs:
																_lowerCAmelCase:Tuple									=  kwargs['''hypothesis_template''']

											return preprocess_params, {}, {}





						def       __UpperCamelCase     (							self      :	Optional[int]     ,a__      :	Optional[Any]     ,a__      :	int=None     ,a__      :	List[str]="This is a sound of {}.")						->       Tuple:




											"""simple docstring"""







											if isinstance(_A     ,_A):
																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
																					_lowerCAmelCase:Dict									=  requests.get(_A).content
																else:
																					with open(_A     ,'''rb''') as f:
																										_lowerCAmelCase:List[Any]									=  f.read()

											if isinstance(_A     ,_A):
																_lowerCAmelCase:Any									=  ffmpeg_read(_A     ,self.feature_extractor.sampling_rate)

											if not isinstance(_A     ,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''')

											_lowerCAmelCase:Dict									=  self.feature_extractor(
											    [audio]     ,sampling_rate=self.feature_extractor.sampling_rate     ,return_tensors='''pt''')
											_lowerCAmelCase:Optional[Any]									=  candidate_labels
											_lowerCAmelCase:str									=  [hypothesis_template.format(_A) for x in candidate_labels]
											_lowerCAmelCase:Optional[Any]									=  self.tokenizer(_A     ,return_tensors=self.framework     ,padding=_A)
											_lowerCAmelCase:Optional[int]									=  [text_inputs]
											return inputs





						def       __UpperCamelCase     (							self      :	Any     ,a__      :	Dict)						->       Tuple:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  model_inputs.pop('''candidate_labels''')
											_lowerCAmelCase:Dict									=  model_inputs.pop('''text_inputs''')
											if isinstance(text_inputs[0]     ,_A):
																_lowerCAmelCase:List[Any]									=  text_inputs[0]
											else:
																# Batching case.
																_lowerCAmelCase:List[Any]									=  text_inputs[0][0]

											_lowerCAmelCase:Any									=  self.model(**_A     ,**_A)

											_lowerCAmelCase:Union[str, Any]									=  {
											    '''candidate_labels''': candidate_labels,
											    '''logits''': outputs.logits_per_audio,
											}
											return model_outputs







						def       __UpperCamelCase     (							self      :	Tuple     ,a__      :	List[Any])						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:int									=  model_outputs.pop('''candidate_labels''')
											_lowerCAmelCase:Union[str, Any]									=  model_outputs['''logits'''][0]

											if self.framework == "pt":
																_lowerCAmelCase:int									=  logits.softmax(dim=0)
																_lowerCAmelCase:Union[str, Any]									=  probs.tolist()
											else:
																raise ValueError('''`tf` framework not supported.''')

											_lowerCAmelCase:Any									=  [
											    {'''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 ...utils import logging
from .image_processing_poolformer import PoolFormerImageProcessor


UpperCamelCase__						=			logging.get_logger(__name__)




class   a__			(       UpperCamelCase_  ):





						def __init__(							self      :	Dict     ,*a__      :	List[str]     ,**a__      :	str)						->       None:




											"""simple docstring"""







											warnings.warn(
											    '''The class PoolFormerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
											    ''' Please use PoolFormerImageProcessor instead.'''     ,a__     ,)
											super().__init__(*a__     ,**a__)






