



'''simple docstring'''
import numpy as np

snake_case_					:      Dict    			=    [
    ['a', 'b', 'c', 'd', 'e'],
    ['f', 'g', 'h', 'i', 'k'],
    ['l', 'm', 'n', 'o', 'p'],
    ['q', 'r', 's', 't', 'u'],
    ['v', 'w', 'x', 'y', 'z'],
]






class 				lowercase__      :





	def __init__(					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  np.array(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							str					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						int												=  np.where(letter == self.SQUARE					)
							_UpperCamelCase   :						Optional[int]												=  np.concatenate([indexa + 1, indexa + 1]					)
							return indexes





	def       UpperCamelCase_    (					self   :							Optional[int]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  self.SQUARE[indexa - 1, indexa - 1]
							return letter





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  message.lower()
							_UpperCamelCase   :						Dict												=  message.replace(' '					,''					)
							_UpperCamelCase   :						str												=  message.replace('j'					,'i'					)

							_UpperCamelCase   :						Tuple												=  np.empty((2, len(lowerCamelCase__					))					)
							for letter_index in range(len(lowerCamelCase__					)					):
													_UpperCamelCase   :						Dict												=  self.letter_to_numbers(message[letter_index]					)

													_UpperCamelCase   :						List[Any]												=  numbers[0]
													_UpperCamelCase   :						List[Any]												=  numbers[1]

							_UpperCamelCase   :						List[str]												=  first_step.reshape(2 * len(lowerCamelCase__					)					)
							_UpperCamelCase   :						Tuple												=  ''
							for numbers_index in range(len(lowerCamelCase__					)					):
													_UpperCamelCase   :						Union[str, Any]												=  int(second_step[numbers_index * 2]					)
													_UpperCamelCase   :						Any												=  int(second_step[(numbers_index * 2) + 1]					)
													_UpperCamelCase   :						List[str]												=  self.numbers_to_letter(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						Dict												=  encoded_message + letter

							return encoded_message





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  message.lower()
							message.replace(' '					,''					)
							_UpperCamelCase   :						Any												=  np.empty(2 * len(lowerCamelCase__					)					)
							for letter_index in range(len(lowerCamelCase__					)					):
													_UpperCamelCase   :						int												=  self.letter_to_numbers(message[letter_index]					)
													_UpperCamelCase   :						Optional[Any]												=  numbers[0]
													_UpperCamelCase   :						Union[str, Any]												=  numbers[1]

							_UpperCamelCase   :						Optional[Any]												=  first_step.reshape((2, len(lowerCamelCase__					))					)
							_UpperCamelCase   :						str												=  ''
							for numbers_index in range(len(lowerCamelCase__					)					):
													_UpperCamelCase   :						str												=  int(second_step[0, numbers_index]					)
													_UpperCamelCase   :						Union[str, Any]												=  int(second_step[1, numbers_index]					)
													_UpperCamelCase   :						List[str]												=  self.numbers_to_letter(lowerCamelCase__					,lowerCamelCase__					)
													_UpperCamelCase   :						List[str]												=  decoded_message + letter

							return decoded_message







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


snake_case_					:      List[Any]    			=    logging.get_logger(__name__)






@add_end_docstrings(lowercase  )
class 				lowercase__      (				lowercase  ):





	def __init__(					self   :							Tuple					,**lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							super().__init__(**lowerCamelCase__					)

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





	def __call__(					self   :							Dict					,lowerCamelCase__   :							Union[np.ndarray, bytes, str]					,**lowerCamelCase__   :							Any					):







							'''simple docstring'''
							return super().__call__(lowerCamelCase__					,**lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					,**lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  {}
							if "candidate_labels" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['candidate_labels']
							if "hypothesis_template" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['hypothesis_template']

							return preprocess_params, {}, {}





	def       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							str=None					,lowerCamelCase__   :							List[Any]="This is a sound of {}."					):







							'''simple docstring'''
							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													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
																			_UpperCamelCase   :						Optional[Any]												=  requests.get(lowerCamelCase__					).content
													else:
																			with open(lowerCamelCase__					,'rb'					) as f:
																									_UpperCamelCase   :						List[str]												=  f.read()

							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													_UpperCamelCase   :						int												=  ffmpeg_read(lowerCamelCase__					,self.feature_extractor.sampling_rate					)

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

							_UpperCamelCase   :						Dict												=  self.feature_extractor(
							    [audio]					,sampling_rate=self.feature_extractor.sampling_rate					,return_tensors='pt'					)
							_UpperCamelCase   :						List[Any]												=  candidate_labels
							_UpperCamelCase   :						Tuple												=  [hypothesis_template.format(lowerCamelCase__					) for x in candidate_labels]
							_UpperCamelCase   :						Dict												=  self.tokenizer(lowerCamelCase__					,return_tensors=self.framework					,padding=lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  [text_inputs]
							return inputs





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  model_inputs.pop('candidate_labels'					)
							_UpperCamelCase   :						str												=  model_inputs.pop('text_inputs'					)
							if isinstance(text_inputs[0]					,lowerCamelCase__					):
													_UpperCamelCase   :						Union[str, Any]												=  text_inputs[0]
							else:
													# Batching case.
													_UpperCamelCase   :						int												=  text_inputs[0][0]

							_UpperCamelCase   :						List[str]												=  self.model(**lowerCamelCase__					,**lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  {
							    'candidate_labels': candidate_labels,
							    'logits': outputs.logits_per_audio,
							}
							return model_outputs





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  model_outputs.pop('candidate_labels'					)
							_UpperCamelCase   :						int												=  model_outputs['logits'][0]

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

							_UpperCamelCase   :						Union[str, Any]												=  [
							    {'score': score, 'label': candidate_label}
							    for score, candidate_label in sorted(zip(lowerCamelCase__					,lowerCamelCase__					)					,key=lambda lowerCamelCase__					: -x[0]					)
							]
							return result


