


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


A     :   List[Any]			    =			logging.get_logger(__name__)
@add_end_docstrings(lowerCAmelCase__		)
class   _UpperCamelCase  (   lowerCAmelCase__		):
							'''simple docstring'''







							def __init__(     self    ,   **__a	):
										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    ,   __a    ,   **__a	):
										return super().__call__(__a    ,   **__a	)

							def 			snake_case				(     self    ,   **__a	):
										__lowerCAmelCase				    =			{}
										if "candidate_labels" in kwargs:
													__lowerCAmelCase				    =			kwargs["candidate_labels"]
										if "hypothesis_template" in kwargs:
													__lowerCAmelCase				    =			kwargs["hypothesis_template"]

										return preprocess_params, {}, {}

							def 			snake_case				(     self    ,   __a    ,   __a=None    ,   __a="This is a sound of {}."	):
										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				    =			requests.get(__a	).content
													else:
																with open(__a    ,   "rb"	) as f:
																			__lowerCAmelCase				    =			f.read()

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

							def 			snake_case				(     self    ,   __a	):
										__lowerCAmelCase				    =			model_inputs.pop("candidate_labels"	)
										__lowerCAmelCase				    =			model_inputs.pop("text_inputs"	)
										if isinstance(text_inputs[0]    ,   __a	):
													__lowerCAmelCase				    =			text_inputs[0]
										else:
													# Batching case.
													__lowerCAmelCase				    =			text_inputs[0][0]

										__lowerCAmelCase				    =			self.model(**__a    ,   **__a	)

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



							def 			snake_case				(     self    ,   __a	):
										__lowerCAmelCase				    =			model_outputs.pop("candidate_labels"	)
										__lowerCAmelCase				    =			model_outputs["logits"][0]

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

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








"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A     :   Tuple			    =			{
    "configuration_blenderbot": [
        "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "BlenderbotConfig",
        "BlenderbotOnnxConfig",
    ],
    "tokenization_blenderbot": ["BlenderbotTokenizer"],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A     :   Dict			    =			["BlenderbotTokenizerFast"]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A     :   str			    =			[
	    "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST",
	    "BlenderbotForCausalLM",
	    "BlenderbotForConditionalGeneration",
	    "BlenderbotModel",
	    "BlenderbotPreTrainedModel",
	]


try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A     :   List[str]			    =			[
	    "TFBlenderbotForConditionalGeneration",
	    "TFBlenderbotModel",
	    "TFBlenderbotPreTrainedModel",
	]


try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A     :   List[str]			    =			[
	    "FlaxBlenderbotForConditionalGeneration",
	    "FlaxBlenderbotModel",
	    "FlaxBlenderbotPreTrainedModel",
	]


if TYPE_CHECKING:
	from .configuration_blenderbot import (
	    BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    BlenderbotConfig,
	    BlenderbotOnnxConfig,
	)
	from .tokenization_blenderbot import BlenderbotTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_blenderbot_fast import BlenderbotTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_blenderbot import (
		    BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    BlenderbotForCausalLM,
		    BlenderbotForConditionalGeneration,
		    BlenderbotModel,
		    BlenderbotPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_blenderbot import (
		    TFBlenderbotForConditionalGeneration,
		    TFBlenderbotModel,
		    TFBlenderbotPreTrainedModel,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_blenderbot import (
		    FlaxBlenderbotForConditionalGeneration,
		    FlaxBlenderbotModel,
		    FlaxBlenderbotPreTrainedModel,
		)

else:
	import sys

	A     :   Optional[Any]			    =			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




