






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


lowercase_				     =     logging.get_logger(__name__)


@add_end_docstrings(__UpperCAmelCase						)
class 					snake_case		(							__UpperCAmelCase						):


   '''simple docstring'''

   def __init__(	self :     int,       **_lowerCamelCase :     Optional[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 :     List[Any],       _lowerCamelCase :     List[str],       **_lowerCamelCase :     Tuple  ):

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


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any,       **_lowerCamelCase :     Optional[Any]  ):

     '''simple docstring'''
     __A									= {}
     if "candidate_labels" in kwargs:
       __A									= kwargs["""candidate_labels"""]
     if "hypothesis_template" in kwargs:
       __A									= kwargs["""hypothesis_template"""]

     return preprocess_params, {}, {}


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     List[Any]=None,       _lowerCamelCase :     Tuple="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
         __A									= requests.get(_lowerCamelCase  ).content
       else:
         with open(_lowerCamelCase,       '''rb'''  ) as f:
           __A									= f.read()

     if isinstance(_lowerCamelCase,       _lowerCamelCase  ):
       __A									= 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'''  )

     __A									= self.feature_extractor(
         [audio],       sampling_rate=self.feature_extractor.sampling_rate,       return_tensors='''pt'''  )
     __A									= candidate_labels
     __A									= [hypothesis_template.format(_lowerCamelCase  ) for x in candidate_labels]
     __A									= self.tokenizer(_lowerCamelCase,       return_tensors=self.framework,       padding=_lowerCamelCase  )
     __A									= [text_inputs]
     return inputs


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     int  ):

     '''simple docstring'''
     __A									= model_inputs.pop('''candidate_labels'''  )
     __A									= model_inputs.pop('''text_inputs'''  )
     if isinstance(text_inputs[0],       _lowerCamelCase  ):
       __A									= text_inputs[0]
     else:
       # Batching case.
       __A									= text_inputs[0][0]

     __A									= self.model(**_lowerCamelCase,       **_lowerCamelCase  )

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






   def 				_SCREAMING_SNAKE_CASE				(	self :     int,       _lowerCamelCase :     str  ):

     '''simple docstring'''
     __A									= model_outputs.pop('''candidate_labels'''  )
     __A									= model_outputs["""logits"""][0]

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

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









'''simple docstring'''




import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader







class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def __init__(self       ,      lowercase       ,      lowercase = None       ,      lowercase = None       ,      lowercase = None       ,      lowercase = False       ,      lowercase = False       ,      lowercase = None       ,      lowercase = None       ,      **lowercase       ,      ):
				super().__init__(
				    lowercase       ,      split=lowercase       ,      features=lowercase       ,      cache_dir=lowercase       ,      keep_in_memory=lowercase       ,      streaming=lowercase       ,      num_proc=lowercase       ,      **lowercase       ,      )
				A_	:						Any	=     field
				A_	:						Optional[int]	=     path_or_paths if isinstance(lowercase       ,      lowercase							) else {self.split: path_or_paths}
				A_	:						str	=     Json(
				    cache_dir=lowercase       ,      data_files=lowercase       ,      features=lowercase       ,      field=lowercase       ,      **lowercase       ,      )


	def     _a	(self							):
				# Build iterable dataset
				if self.streaming:
							A_	:						Optional[int]	=     self.builder.as_streaming_dataset(split=self.split							)
				# Build regular (map-style) dataset
				else:
							A_	:						Optional[Any]	=     None
							A_	:						Optional[Any]	=     None
							A_	:						Tuple	=     None
							A_	:						Optional[int]	=     None

							self.builder.download_and_prepare(
							    download_config=lowercase       ,      download_mode=lowercase       ,      verification_mode=lowercase       ,      base_path=lowercase       ,      num_proc=self.num_proc       ,      )
							A_	:						Union[str, Any]	=     self.builder.as_dataset(
							    split=self.split       ,      verification_mode=lowercase       ,      in_memory=self.keep_in_memory							)
				return dataset







class 	_lowerCAmelCase							:


	def __init__(self       ,      lowercase       ,      lowercase       ,      lowercase = None       ,      lowercase = None       ,      **lowercase       ,      ):
				if num_proc is not None and num_proc <= 0:
							raise ValueError(F'num_proc {num_proc} must be an integer > 0.'							)

				A_	:						Union[str, Any]	=     dataset
				A_	:						Optional[int]	=     path_or_buf
				A_	:						str	=     batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
				A_	:						List[str]	=     num_proc
				A_	:						Union[str, Any]	=     """utf-8"""
				A_	:						Dict	=     to_json_kwargs


	def     _a	(self							):
				A_	:						Optional[Any]	=     self.to_json_kwargs.pop("""path_or_buf"""       ,      lowercase							)
				A_	:						Tuple	=     self.to_json_kwargs.pop("""orient"""       ,      """records"""							)
				A_	:						Union[str, Any]	=     self.to_json_kwargs.pop("""lines"""       ,      True if orient == """records""" else False							)
				A_	:						str	=     self.to_json_kwargs.pop("""index"""       ,      False if orient in ["""split""", """table"""] else True							)
				A_	:						Dict	=     self.to_json_kwargs.pop("""compression"""       ,      lowercase							)

				if compression not in [None, "infer", "gzip", "bz2", "xz"]:
							raise NotImplementedError(F'`datasets` currently does not support {compression} compression'							)

				if isinstance(self.path_or_buf       ,      (str, bytes, os.PathLike)							):
							with fsspec.open(self.path_or_buf       ,      """wb"""       ,      compression=lowercase							) as buffer:
										A_	:						List[str]	=     self._write(file_obj=lowercase       ,      orient=lowercase       ,      lines=lowercase       ,      index=lowercase       ,      **self.to_json_kwargs							)
				else:
							if compression:
										raise NotImplementedError(
										    F'The compression parameter is not supported when writing to a buffer, but compression={compression}'
										    """ was passed. Please provide a local path instead."""							)
							A_	:						List[Any]	=     self._write(
							    file_obj=self.path_or_buf       ,      orient=lowercase       ,      lines=lowercase       ,      index=lowercase       ,      **self.to_json_kwargs							)
				return written


	def     _a	(self       ,      lowercase							):
				A_,    A_,    A_,    A_,    A_	:						Dict	=     args

				A_	:						Any	=     query_table(
				    table=self.dataset.data       ,      key=slice(lowercase       ,      offset + self.batch_size							)       ,      indices=self.dataset._indices       ,      )
				A_	:						Union[str, Any]	=     batch.to_pandas().to_json(
				    path_or_buf=lowercase       ,      orient=lowercase       ,      lines=lowercase       ,      index=lowercase       ,      **lowercase							)
				if not json_str.endswith("""\n"""							):
							json_str += "\n"
				return json_str.encode(self.encoding							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      **lowercase       ,      ):
				A_	:						Optional[Any]	=     0

				if self.num_proc is None or self.num_proc == 1:
							for offset in logging.tqdm(
							    range(0       ,      len(self.dataset							)       ,      self.batch_size							)       ,      unit="""ba"""       ,      disable=not logging.is_progress_bar_enabled()       ,      desc="""Creating json from Arrow format"""       ,      ):
										A_	:						Any	=     self._batch_json((offset, orient, lines, index, to_json_kwargs)							)
										written += file_obj.write(lowercase							)
				else:
							A_,    A_	:						List[Any]	=     len(self.dataset							), self.batch_size
							with multiprocessing.Pool(self.num_proc							) as pool:
										for json_str in logging.tqdm(
										    pool.imap(
										        self._batch_json       ,      [(offset, orient, lines, index, to_json_kwargs) for offset in range(0       ,      lowercase       ,      lowercase							)]       ,      )       ,      total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size       ,      unit="""ba"""       ,      disable=not logging.is_progress_bar_enabled()       ,      desc="""Creating json from Arrow format"""       ,      ):
													written += file_obj.write(lowercase							)

				return written