


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






# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

__A				='''2.13.1'''

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse('''3.7'''):
			raise ImportWarning(
			    '''To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.'''
			)

if version.parse(pyarrow.__version__).major < 8:
			raise ImportWarning(
			    '''To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n'''
			    '''If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.'''
			)

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

__A				=concatenate_datasets
__A				=DownloadConfig
__A				=DownloadManager
__A				=DownloadMode
__A				=DownloadConfig
__A				=DownloadMode
__A				=DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager


