






#
# Sort large text files in a minimum amount of memory
#
import argparse
import os


class 		lowerCAmelCase_							:
     __lowerCamelCase					:						Any     =    '''block_{0}.dat'''





     def __init__(  self       ,   _UpperCAmelCase     )		->							List[str]:
        lowerCamelCase											=filename
        lowerCamelCase											=[]





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )		->							Tuple:
        lowerCamelCase											=self.BLOCK_FILENAME_FORMAT.format(_UpperCAmelCase     )
        with open(_UpperCAmelCase       ,   """w"""     ) as file:
           file.write(_UpperCAmelCase     )
        self.block_filenames.append(_UpperCAmelCase     )





     def 	lowerCamelCase       (  self     )		->							str:
        return self.block_filenames





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase=None     )		->							Optional[int]:
        lowerCamelCase											=0
        with open(self.filename     ) as file:
           while True:
              lowerCamelCase											=file.readlines(_UpperCAmelCase     )

              if lines == []:
                 break

              if sort_key is None:
                 lines.sort()
              else:
                 lines.sort(key=_UpperCAmelCase     )

              self.write_block("""""".join(_UpperCAmelCase     )       ,   _UpperCAmelCase     )
              i += 1





     def 	lowerCamelCase       (  self     )		->							Any:
        map(os.remove       ,   self.block_filenames     )




class 		lowerCAmelCase_							:





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							Union[str, Any]:
        lowerCamelCase											=-1
        lowerCamelCase											=None

        for i in range(len(_UpperCAmelCase     )     ):
           if min_str is None or choices[i] < min_str:
              lowerCamelCase											=i

        return min_index




class 		lowerCAmelCase_							:





     def __init__(  self       ,   _UpperCAmelCase     )		->							int:
        lowerCamelCase											=files
        lowerCamelCase											=set()
        lowerCamelCase											=len(_UpperCAmelCase     )
        lowerCamelCase											={i: None for i in range(self.num_buffers     )}





     def 	lowerCamelCase       (  self     )		->							str:
        return {
            i: self.buffers[i] for i in range(self.num_buffers     ) if i not in self.empty
        }





     def 	lowerCamelCase       (  self     )		->							List[Any]:
        for i in range(self.num_buffers     ):
           if self.buffers[i] is None and i not in self.empty:
              lowerCamelCase											=self.files[i].readline()

              if self.buffers[i] == "":
                 self.empty.add(_UpperCAmelCase     )
                 self.files[i].close()

        if len(self.empty     ) == self.num_buffers:
           return False

        return True





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							str:
        lowerCamelCase											=self.buffers[index]
        lowerCamelCase											=None

        return value




class 		lowerCAmelCase_							:





     def __init__(  self       ,   _UpperCAmelCase     )		->							Optional[int]:
        lowerCamelCase											=merge_strategy





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )		->							List[str]:
        lowerCamelCase											=FilesArray(self.get_file_handles(_UpperCAmelCase       ,   _UpperCAmelCase     )     )
        with open(_UpperCAmelCase       ,   """w"""       ,   _UpperCAmelCase     ) as outfile:
           while buffers.refresh():
              lowerCamelCase											=self.merge_strategy.select(buffers.get_dict()     )
              outfile.write(buffers.unshift(_UpperCAmelCase     )     )





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )		->							List[Any]:
        lowerCamelCase											={}

        for i in range(len(_UpperCAmelCase     )     ):
           lowerCamelCase											=open(filenames[i]       ,   """r"""       ,   _UpperCAmelCase     )  # noqa: UP015

        return files




class 		lowerCAmelCase_							:





     def __init__(  self       ,   _UpperCAmelCase     )		->							Union[str, Any]:
        lowerCamelCase											=block_size





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase=None     )		->							Optional[int]:
        lowerCamelCase											=self.get_number_blocks(_UpperCAmelCase       ,   self.block_size     )
        lowerCamelCase											=FileSplitter(_UpperCAmelCase     )
        splitter.split(self.block_size       ,   _UpperCAmelCase     )

        lowerCamelCase											=FileMerger(NWayMerge()     )
        lowerCamelCase											=self.block_size / (num_blocks + 1)
        merger.merge(splitter.get_block_filenames()       ,   filename + """.out"""       ,   _UpperCAmelCase     )

        splitter.cleanup()





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )		->							List[str]:
        return (os.stat(_UpperCAmelCase     ).st_size / block_size) + 1






def    UpperCAmelCase__	(						lowerCamelCase  : Tuple						):
   if string[-1].lower() == "k":
      return int(string[:-1]						) * 10_24
   elif string[-1].lower() == "m":
      return int(string[:-1]						) * 10_24 * 10_24
   elif string[-1].lower() == "g":
      return int(string[:-1]						) * 10_24 * 10_24 * 10_24
   else:
      return int(lowerCamelCase						)






def    UpperCAmelCase__	(						):
   lowerCamelCase											=argparse.ArgumentParser()
   parser.add_argument(
       """-m""" ,"""--mem""" ,help="""amount of memory to use for sorting""" ,default="""100M"""						)
   parser.add_argument(
       """filename""" ,metavar="""<filename>""" ,nargs=1 ,help="""name of file to sort"""						)
   lowerCamelCase											=parser.parse_args()

   lowerCamelCase											=ExternalSort(parse_memory(args.mem						)						)
   sorter.sort(args.filename[0]						)


if __name__ == "__main__":
   main()







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				:	Optional[int]    						=    logging.get_logger(__name__)






def    UpperCAmelCase__	(						lowerCamelCase  : bytes ,lowerCamelCase  : int						):
   lowerCamelCase											=f'''{sampling_rate}'''
   lowerCamelCase											="""1"""
   lowerCamelCase											="""f32le"""
   lowerCamelCase											=[
       """ffmpeg""",
       """-i""",
       """pipe:0""",
       """-ac""",
       ac,
       """-ar""",
       ar,
       """-f""",
       format_for_conversion,
       """-hide_banner""",
       """-loglevel""",
       """quiet""",
       """pipe:1""",
   ]

   try:
      lowerCamelCase											=subprocess.Popen(lowerCamelCase ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE						)
   except FileNotFoundError:
      raise ValueError("""ffmpeg was not found but is required to load audio files from filename"""						)
   lowerCamelCase											=ffmpeg_process.communicate(lowerCamelCase						)
   lowerCamelCase											=output_stream[0]

   lowerCamelCase											=np.frombuffer(lowerCamelCase ,np.floataa						)
   if audio.shape[0] == 0:
      raise ValueError("""Malformed soundfile"""						)
   return audio







@add_end_docstrings(UpperCamelCase   )
class 		lowerCAmelCase_							(     UpperCamelCase   ):





     def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Dict:
        # Default, might be overriden by the model.config.
        lowerCamelCase											=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       ,   _UpperCAmelCase       ,   **_UpperCAmelCase       ,   )		->							Union[str, Any]:
        return super().__call__(_UpperCAmelCase       ,   **_UpperCAmelCase     )





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase=None       ,   **_UpperCAmelCase     )		->							Optional[int]:
        # No parameters on this pipeline right now
        lowerCamelCase											={}
        if top_k is not None:
           if top_k > self.model.config.num_labels:
              lowerCamelCase											=self.model.config.num_labels
           lowerCamelCase											=top_k
        return {}, {}, postprocess_params





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							Dict:
        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
              lowerCamelCase											=requests.get(_UpperCAmelCase     ).content
           else:
              with open(_UpperCAmelCase       ,   """rb"""     ) as f:
                 lowerCamelCase											=f.read()

        if isinstance(_UpperCAmelCase       ,   _UpperCAmelCase     ):
           lowerCamelCase											=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"""     )

           lowerCamelCase											=inputs.pop("""raw"""       ,   _UpperCAmelCase     )
           if _inputs is None:
              # Remove path which will not be used from `datasets`.
              inputs.pop("""path"""       ,   _UpperCAmelCase     )
              lowerCamelCase											=inputs.pop("""array"""       ,   _UpperCAmelCase     )
           lowerCamelCase											=inputs.pop("""sampling_rate"""     )
           lowerCamelCase											=_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`."""     )

              lowerCamelCase											=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"""     )

        lowerCamelCase											=self.feature_extractor(
            _UpperCAmelCase       ,   sampling_rate=self.feature_extractor.sampling_rate       ,   return_tensors="""pt"""     )
        return processed





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							int:
        lowerCamelCase											=self.model(**_UpperCAmelCase     )
        return model_outputs





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase=5     )		->							Tuple:
        lowerCamelCase											=model_outputs.logits[0].softmax(-1     )
        lowerCamelCase							,					lowerCamelCase											=probs.topk(_UpperCAmelCase     )

        lowerCamelCase											=scores.tolist()
        lowerCamelCase											=ids.tolist()

        lowerCamelCase											=[{"""score""": score, """label""": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCAmelCase       ,   _UpperCAmelCase     )]

        return labels