


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[str]      =    logging.get_logger(__name__)

@add_end_docstrings(_lowerCAmelCase				)
class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							def __init__(							self	:  Optional[Any]   ,     **lowerCamelCase	:  List[str]    )     ->   Dict:
														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	:  List[Any]   ,     lowerCamelCase	:  Union[np.ndarray, bytes, str]   ,     **lowerCamelCase	:  Union[str, Any]    )     ->   Tuple:
														return super().__call__(lowerCamelCase   ,     **lowerCamelCase    )

							def    __snake_case (							self	:  Optional[Any]   ,     **lowerCamelCase	:  Optional[Any]    )     ->   str:
														__snake_case		:      List[Any]										=       {}
														if "candidate_labels" in kwargs:
																					__snake_case		:      Optional[int]										=       kwargs["candidate_labels"]
														if "hypothesis_template" in kwargs:
																					__snake_case		:      List[str]										=       kwargs["hypothesis_template"]

														return preprocess_params, {}, {}

							def    __snake_case (							self	:  List[str]   ,     lowerCamelCase	:  Union[str, Any]   ,     lowerCamelCase	:  Dict=None   ,     lowerCamelCase	:  Dict="This is a sound of {}."    )     ->   Optional[int]:
														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
																												__snake_case		:      Dict										=       requests.get(lowerCamelCase    ).content
																					else:
																												with open(lowerCamelCase   ,     "rb"    ) as f:
																																			__snake_case		:      List[Any]										=       f.read()

														if isinstance(lowerCamelCase   ,     lowerCamelCase    ):
																					__snake_case		:      List[Any]										=       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"    )

														__snake_case		:      List[Any]										=       self.feature_extractor(
														    [audio]   ,     sampling_rate=self.feature_extractor.sampling_rate   ,     return_tensors="pt"    )
														__snake_case		:      str										=       candidate_labels
														__snake_case		:      List[str]										=       [hypothesis_template.format(lowerCamelCase    ) for x in candidate_labels]
														__snake_case		:      Union[str, Any]										=       self.tokenizer(lowerCamelCase   ,     return_tensors=self.framework   ,     padding=lowerCamelCase    )
														__snake_case		:      int										=       [text_inputs]
														return inputs

							def    __snake_case (							self	:  List[str]   ,     lowerCamelCase	:  Union[str, Any]    )     ->   List[str]:
														__snake_case		:      List[str]										=       model_inputs.pop("candidate_labels"    )
														__snake_case		:      str										=       model_inputs.pop("text_inputs"    )
														if isinstance(text_inputs[0]   ,     lowerCamelCase    ):
																					__snake_case		:      List[Any]										=       text_inputs[0]
														else:
																					# Batching case.
																					__snake_case		:      str										=       text_inputs[0][0]

														__snake_case		:      str										=       self.model(**lowerCamelCase   ,     **lowerCamelCase    )

														__snake_case		:      Tuple										=       {
														    "candidate_labels": candidate_labels,
														    "logits": outputs.logits_per_audio,
														}
														return model_outputs






							def    __snake_case (							self	:  Union[str, Any]   ,     lowerCamelCase	:  Union[str, Any]    )     ->   Optional[Any]:
														__snake_case		:      int										=       model_outputs.pop("candidate_labels"    )
														__snake_case		:      Union[str, Any]										=       model_outputs["logits"][0]

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

														__snake_case		:      List[Any]										=       [
														    {"score": score, "label": candidate_label}
														    for score, candidate_label in sorted(zip(lowerCamelCase   ,     lowerCamelCase    )   ,     key=lambda lowerCamelCase    : -x[0]    )
														]
														return result





import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_snake_case					:		Union[str, Any]      =    logging.get_logger(__name__)

_snake_case					:		List[str]      =    {
    "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json",
    "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json",
}

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Tuple					  =  "encodec"

							def __init__(							self	:  Any   ,     lowerCamelCase	:  Optional[int]=[1.5, 3.0, 6.0, 12.0, 24.0]   ,     lowerCamelCase	:  List[str]=24000   ,     lowerCamelCase	:  int=1   ,     lowerCamelCase	:  Optional[int]=False   ,     lowerCamelCase	:  Dict=None   ,     lowerCamelCase	:  Tuple=None   ,     lowerCamelCase	:  Optional[int]=128   ,     lowerCamelCase	:  Optional[int]=32   ,     lowerCamelCase	:  List[str]=1   ,     lowerCamelCase	:  str=[8, 5, 4, 2]   ,     lowerCamelCase	:  List[str]="weight_norm"   ,     lowerCamelCase	:  Any=7   ,     lowerCamelCase	:  Tuple=7   ,     lowerCamelCase	:  int=3   ,     lowerCamelCase	:  int=2   ,     lowerCamelCase	:  Union[str, Any]=True   ,     lowerCamelCase	:  List[Any]="reflect"   ,     lowerCamelCase	:  Union[str, Any]=2   ,     lowerCamelCase	:  Optional[int]=2   ,     lowerCamelCase	:  int=1.0   ,     lowerCamelCase	:  Optional[Any]=1024   ,     lowerCamelCase	:  Optional[Any]=None   ,     lowerCamelCase	:  str=True   ,     **lowerCamelCase	:  Dict   ,     )     ->   Any:
														__snake_case		:      Tuple										=       target_bandwidths
														__snake_case		:      Union[str, Any]										=       sampling_rate
														__snake_case		:      Union[str, Any]										=       audio_channels
														__snake_case		:      Dict										=       normalize
														__snake_case		:      List[Any]										=       chunk_length_s
														__snake_case		:      Tuple										=       overlap
														__snake_case		:      Optional[int]										=       hidden_size
														__snake_case		:      List[Any]										=       num_filters
														__snake_case		:      Union[str, Any]										=       num_residual_layers
														__snake_case		:      Optional[int]										=       upsampling_ratios
														__snake_case		:      List[str]										=       norm_type
														__snake_case		:      Optional[int]										=       kernel_size
														__snake_case		:      Dict										=       last_kernel_size
														__snake_case		:      Tuple										=       residual_kernel_size
														__snake_case		:      List[Any]										=       dilation_growth_rate
														__snake_case		:      Optional[int]										=       use_causal_conv
														__snake_case		:      Tuple										=       pad_mode
														__snake_case		:      Union[str, Any]										=       compress
														__snake_case		:      Union[str, Any]										=       num_lstm_layers
														__snake_case		:      int										=       trim_right_ratio
														__snake_case		:      Tuple										=       codebook_size
														__snake_case		:      Optional[Any]										=       codebook_dim if codebook_dim is not None else hidden_size
														__snake_case		:      int										=       use_conv_shortcut

														if self.norm_type not in ["weight_norm", "time_group_norm"]:
																					raise ValueError(
																					    F'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'    )

														super().__init__(**lowerCamelCase    )

							@property
							def    __snake_case (							self	:  int    )     ->   Optional[int]:
														if self.chunk_length_s is None:
																					return None
														else:
																					return int(self.chunk_length_s * self.sampling_rate    )

							@property
							def    __snake_case (							self	:  Union[str, Any]    )     ->   Optional[int]:
														if self.chunk_length_s is None or self.overlap is None:
																					return None
														else:
																					return max(1   ,     int((1.0 - self.overlap) * self.chunk_length    )    )

							@property
							def    __snake_case (							self	:  Optional[Any]    )     ->   int:
														__snake_case		:      Union[str, Any]										=       np.prod(self.upsampling_ratios    )
														return math.ceil(self.sampling_rate / hop_length    )






							@property
							def    __snake_case (							self	:  Optional[Any]    )     ->   int:
														return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10)    )

