


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


A				:       Any      =  logging.get_logger(__name__)






@add_end_docstrings(_a )
class      UpperCamelCase(	_a ):


					def __init__(    self						:		Union[str, Any]  ,     **SCREAMING_SNAKE_CASE						:		Any    )      ->						List[str]:




											'''simple docstring'''

											super().__init__(**SCREAMING_SNAKE_CASE    )

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


					def __call__(    self						:		Any  ,     SCREAMING_SNAKE_CASE						:		Union[np.ndarray, bytes, str]  ,     **SCREAMING_SNAKE_CASE						:		Optional[int]    )      ->						Any:




											'''simple docstring'''

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


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[Any]  ,     **SCREAMING_SNAKE_CASE						:		Union[str, Any]    )      ->						int:




											'''simple docstring'''

											__snake_case				=		{}
											if "candidate_labels" in kwargs:
																	__snake_case				=		kwargs["candidate_labels"]
											if "hypothesis_template" in kwargs:
																	__snake_case				=		kwargs["hypothesis_template"]

											return preprocess_params, {}, {}


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Dict=None  ,     SCREAMING_SNAKE_CASE						:		Dict="This is a sound of {}."    )      ->						Union[str, Any]:




											'''simple docstring'''

											if isinstance(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE    ):
																	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				=		requests.get(SCREAMING_SNAKE_CASE    ).content
																	else:
																							with open(SCREAMING_SNAKE_CASE  ,     "rb"    ) as f:
																													__snake_case				=		f.read()

											if isinstance(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE    ):
																	__snake_case				=		ffmpeg_read(SCREAMING_SNAKE_CASE  ,     self.feature_extractor.sampling_rate    )

											if not isinstance(SCREAMING_SNAKE_CASE  ,     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				=		self.feature_extractor(
											    [audio]  ,     sampling_rate=self.feature_extractor.sampling_rate  ,     return_tensors="pt"    )
											__snake_case				=		candidate_labels
											__snake_case				=		[hypothesis_template.format(SCREAMING_SNAKE_CASE    ) for x in candidate_labels]
											__snake_case				=		self.tokenizer(SCREAMING_SNAKE_CASE  ,     return_tensors=self.framework  ,     padding=SCREAMING_SNAKE_CASE    )
											__snake_case				=		[text_inputs]
											return inputs


					def 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]    )      ->						List[Any]:




											'''simple docstring'''

											__snake_case				=		model_inputs.pop("candidate_labels"    )
											__snake_case				=		model_inputs.pop("text_inputs"    )
											if isinstance(text_inputs[0]  ,     SCREAMING_SNAKE_CASE    ):
																	__snake_case				=		text_inputs[0]
											else:
																	# Batching case.
																	__snake_case				=		text_inputs[0][0]

											__snake_case				=		self.model(**SCREAMING_SNAKE_CASE  ,     **SCREAMING_SNAKE_CASE    )

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






					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]  ,     SCREAMING_SNAKE_CASE						:		Dict    )      ->						Tuple:




											'''simple docstring'''

											__snake_case				=		model_outputs.pop("candidate_labels"    )
											__snake_case				=		model_outputs["logits"][0]

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

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









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A				:       List[str]      =  {
    'configuration_time_series_transformer': [
        'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'TimeSeriesTransformerConfig',
    ],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A				:       str      =  [
	    'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TimeSeriesTransformerForPrediction',
	    'TimeSeriesTransformerModel',
	    'TimeSeriesTransformerPreTrainedModel',
	]


if TYPE_CHECKING:
	from .configuration_time_series_transformer import (
	    TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    TimeSeriesTransformerConfig,
	)

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_time_series_transformer import (
		    TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TimeSeriesTransformerForPrediction,
		    TimeSeriesTransformerModel,
		    TimeSeriesTransformerPreTrainedModel,
		)

else:
	import sys

	A				:       str      =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





