


import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

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


def   a						(				SCREAMING_SNAKE_CASE_   :		List[Any]				,      SCREAMING_SNAKE_CASE_   :		Tuple							):






       """simple docstring"""





       UpperCamelCase     :						Union[str, Any] 						=		F"""{sampling_rate}"""
       UpperCamelCase     :						Optional[int] 						=		'''1'''
       UpperCamelCase     :						List[str] 						=		'''f32le'''
       UpperCamelCase     :						Union[str, Any] 						=		[
           '''ffmpeg''',
           '''-i''',
           '''pipe:0''',
           '''-ac''',
           ac,
           '''-ar''',
           ar,
           '''-f''',
           format_for_conversion,
           '''-hide_banner''',
           '''-loglevel''',
           '''quiet''',
           '''pipe:1''',
       ]

       try:
              UpperCamelCase     :						List[Any] 						=		subprocess.Popen(snake_case__				,      stdin=subprocess.PIPE				,      stdout=subprocess.PIPE							)
       except FileNotFoundError:
              raise ValueError('''ffmpeg was not found but is required to load audio files from filename'''							)
       UpperCamelCase     :						List[str] 						=		ffmpeg_process.communicate(snake_case__							)
       UpperCamelCase     :						Tuple 						=		output_stream[0]

       UpperCamelCase     :						int 						=		np.frombuffer(snake_case__				,      np.floataa							)
       if audio.shape[0] == 0:
              raise ValueError('''Malformed soundfile'''							)
       return audio



@add_end_docstrings(_UpperCAmelCase)
class  UpperCAmelCase_						(				_UpperCAmelCase):
       '''simple docstring'''






       def __init__(  self			,       *__SCREAMING_SNAKE_CASE			,       **__SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						Tuple 						=		5
              super().__init__(*UpperCAmelCase_			,       **UpperCAmelCase_    )

              if self.framework != "pt":
                     raise ValueError(f"""The {self.__class__} is only available in PyTorch."""    )

              self.check_model_type(UpperCAmelCase_    )






       def __call__(  self			,       __SCREAMING_SNAKE_CASE			,       **__SCREAMING_SNAKE_CASE			,       ):




              """simple docstring"""




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






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE=None			,       **__SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						List[Any] 						=		{}
              if top_k is not None:
                     if top_k > self.model.config.num_labels:
                            UpperCamelCase     :						Any 						=		self.model.config.num_labels
                     UpperCamelCase     :						Optional[int] 						=		top_k
              return {}, {}, postprocess_params






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              if isinstance(UpperCAmelCase_			,       UpperCAmelCase_    ):
                     if inputs.startswith('''http://'''    ) or inputs.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
                            UpperCamelCase     :						int 						=		requests.get(UpperCAmelCase_    ).content
                     else:
                            with open(UpperCAmelCase_			,       '''rb'''    ) as f:
                                   UpperCamelCase     :						Dict 						=		f.read()

              if isinstance(UpperCAmelCase_			,       UpperCAmelCase_    ):
                     UpperCamelCase     :						int 						=		ffmpeg_read(UpperCAmelCase_			,       self.feature_extractor.sampling_rate    )

              if isinstance(UpperCAmelCase_			,       UpperCAmelCase_    ):
                     # Accepting `"array"` which is the key defined in `datasets` for
                     # better integration
                     if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
                            raise ValueError(
                                '''When passing a dictionary to AudioClassificationPipeline, the dict needs to contain a '''
                                '''\"raw\" key containing the numpy array representing the audio and a \"sampling_rate\" key, '''
                                '''containing the sampling_rate associated with that array'''    )

                     UpperCamelCase     :						Tuple 						=		inputs.pop('''raw'''			,       UpperCAmelCase_    )
                     if _inputs is None:
                            # Remove path which will not be used from `datasets`.
                            inputs.pop('''path'''			,       UpperCAmelCase_    )
                            UpperCamelCase     :						Dict 						=		inputs.pop('''array'''			,       UpperCAmelCase_    )
                     UpperCamelCase     :						Union[str, Any] 						=		inputs.pop('''sampling_rate'''    )
                     UpperCamelCase     :						int 						=		_inputs
                     if in_sampling_rate != self.feature_extractor.sampling_rate:
                            import torch

                            if is_torchaudio_available():
                                   from torchaudio import functional as F
                            else:
                                   raise ImportError(
                                       '''torchaudio is required to resample audio samples in AudioClassificationPipeline. '''
                                       '''The torchaudio package can be installed through: `pip install torchaudio`.'''    )

                            UpperCamelCase     :						Dict 						=		F.resample(
                                torch.from_numpy(UpperCAmelCase_    )			,       UpperCAmelCase_			,       self.feature_extractor.sampling_rate    ).numpy()

              if not isinstance(UpperCAmelCase_			,       np.ndarray    ):
                     raise ValueError('''We expect a numpy ndarray as input'''    )
              if len(inputs.shape    ) != 1:
                     raise ValueError('''We expect a single channel audio input for AudioClassificationPipeline'''    )

              UpperCamelCase     :						Optional[Any] 						=		self.feature_extractor(
                  UpperCAmelCase_			,       sampling_rate=self.feature_extractor.sampling_rate			,       return_tensors='''pt'''    )
              return processed






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						Dict 						=		self.model(**UpperCAmelCase_    )
              return model_outputs






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE=5    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		model_outputs.logits[0].softmax(-1    )
              UpperCamelCase						,			UpperCamelCase     :						str 						=		probs.topk(UpperCAmelCase_    )

              UpperCamelCase     :						Union[str, Any] 						=		scores.tolist()
              UpperCamelCase     :						Union[str, Any] 						=		ids.tolist()

              UpperCamelCase     :						str 						=		[{'''score''': score, '''label''': self.model.config.idalabel[_id]} for score, _id in zip(UpperCAmelCase_			,       UpperCAmelCase_    )]

              return labels









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

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



class  __UpperCAmelCase						(_UpperCAmelCase       ):







			def __init__(      self:    Optional[int] ,      UpperCAmelCase_:    NestedDataStructureLike[PathLike] ,      UpperCAmelCase_:    Optional[NamedSplit] = None ,      UpperCAmelCase_:    Optional[Features] = None ,      UpperCAmelCase_:    str = None ,      UpperCAmelCase_:    bool = False ,      UpperCAmelCase_:    bool = False ,      UpperCAmelCase_:    Optional[int] = None ,      **UpperCAmelCase_:    Union[str, Any] ,      ):







									'''simple docstring'''




									super().__init__(
									    UpperCAmelCase_ ,      split=UpperCAmelCase_ ,      features=UpperCAmelCase_ ,      cache_dir=UpperCAmelCase_ ,      keep_in_memory=UpperCAmelCase_ ,      streaming=UpperCAmelCase_ ,      num_proc=UpperCAmelCase_ ,      **UpperCAmelCase_ ,      )
									_SCREAMING_SNAKE_CASE       =    path_or_paths if isinstance(UpperCAmelCase_ ,      UpperCAmelCase_	) else {self.split: path_or_paths}
									_SCREAMING_SNAKE_CASE       =    Csv(
									    cache_dir=UpperCAmelCase_ ,      data_files=UpperCAmelCase_ ,      features=UpperCAmelCase_ ,      **UpperCAmelCase_ ,      )







			def 				UpperCamelCase (      self:    int	):







									'''simple docstring'''




									if self.streaming:
															_SCREAMING_SNAKE_CASE       =    self.builder.as_streaming_dataset(split=self.split	)
									# Build regular (map-style) dataset
									else:
															_SCREAMING_SNAKE_CASE       =    None
															_SCREAMING_SNAKE_CASE       =    None
															_SCREAMING_SNAKE_CASE       =    None
															_SCREAMING_SNAKE_CASE       =    None

															self.builder.download_and_prepare(
															    download_config=UpperCAmelCase_ ,      download_mode=UpperCAmelCase_ ,      verification_mode=UpperCAmelCase_ ,      base_path=UpperCAmelCase_ ,      num_proc=self.num_proc ,      )
															_SCREAMING_SNAKE_CASE       =    self.builder.as_dataset(
															    split=self.split ,      verification_mode=UpperCAmelCase_ ,      in_memory=self.keep_in_memory	)
									return dataset



class  __UpperCAmelCase						:







			def __init__(      self:    List[str] ,      UpperCAmelCase_:    Dataset ,      UpperCAmelCase_:    Union[PathLike, BinaryIO] ,      UpperCAmelCase_:    Optional[int] = None ,      UpperCAmelCase_:    Optional[int] = None ,      **UpperCAmelCase_:    Any ,      ):







									'''simple docstring'''




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

									_SCREAMING_SNAKE_CASE       =    dataset
									_SCREAMING_SNAKE_CASE       =    path_or_buf
									_SCREAMING_SNAKE_CASE       =    batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
									_SCREAMING_SNAKE_CASE       =    num_proc
									_SCREAMING_SNAKE_CASE       =    """utf-8"""
									_SCREAMING_SNAKE_CASE       =    to_csv_kwargs







			def 				UpperCamelCase (      self:    Optional[int]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    self.to_csv_kwargs.pop("""path_or_buf""" ,      UpperCAmelCase_	)
									_SCREAMING_SNAKE_CASE       =    self.to_csv_kwargs.pop("""header""" ,      UpperCAmelCase_	)
									_SCREAMING_SNAKE_CASE       =    self.to_csv_kwargs.pop("""index""" ,      UpperCAmelCase_	)

									if isinstance(self.path_or_buf ,      (str, bytes, os.PathLike)	):
															with open(self.path_or_buf ,      """wb+"""	) as buffer:
																					_SCREAMING_SNAKE_CASE       =    self._write(file_obj=UpperCAmelCase_ ,      header=UpperCAmelCase_ ,      index=UpperCAmelCase_ ,      **self.to_csv_kwargs	)
									else:
															_SCREAMING_SNAKE_CASE       =    self._write(file_obj=self.path_or_buf ,      header=UpperCAmelCase_ ,      index=UpperCAmelCase_ ,      **self.to_csv_kwargs	)
									return written







			def 				UpperCamelCase (      self:    str ,      UpperCAmelCase_:    Tuple	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    args

									_SCREAMING_SNAKE_CASE       =    query_table(
									    table=self.dataset.data ,      key=slice(UpperCAmelCase_ ,      offset + self.batch_size	) ,      indices=self.dataset._indices ,      )
									_SCREAMING_SNAKE_CASE       =    batch.to_pandas().to_csv(
									    path_or_buf=UpperCAmelCase_ ,      header=header if (offset == 0) else False ,      index=UpperCAmelCase_ ,      **UpperCAmelCase_	)
									return csv_str.encode(self.encoding	)







			def 				UpperCamelCase (      self:    Optional[Any] ,      UpperCAmelCase_:    BinaryIO ,      UpperCAmelCase_:    Tuple ,      UpperCAmelCase_:    Any ,      **UpperCAmelCase_:    Union[str, Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    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 CSV from Arrow format""" ,      ):
																					_SCREAMING_SNAKE_CASE       =    self._batch_csv((offset, header, index, to_csv_kwargs)	)
																					written += file_obj.write(UpperCAmelCase_	)

									else:
															_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    len(self.dataset	), self.batch_size
															with multiprocessing.Pool(self.num_proc	) as pool:
																					for csv_str in logging.tqdm(
																					    pool.imap(
																					        self._batch_csv ,      [(offset, header, index, to_csv_kwargs) for offset in range(0 ,      UpperCAmelCase_ ,      UpperCAmelCase_	)] ,      ) ,      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 CSV from Arrow format""" ,      ):
																											written += file_obj.write(UpperCAmelCase_	)

									return written

